Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fancy): underlining #191

Merged
merged 5 commits into from Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/special.ts
Expand Up @@ -18,6 +18,8 @@ consola.error(undefined, null, false, true, Number.NaN);

consola.log("We can `monospace` keyword using grave accent charachter!");

consola.log("We can also _underline_ words!");

// Nonstandard error
const { message, stack } = new Error("Custom Error!");
consola.error({ message, stack });
Expand Down
14 changes: 10 additions & 4 deletions src/reporters/fancy.ts
Expand Up @@ -86,7 +86,7 @@ export class FancyReporter extends BasicReporter {
const tag = logObj.tag ? colors.gray(logObj.tag) : "";

let line;
const left = this.filterAndJoin([type, highlightBackticks(message)]);
const left = this.filterAndJoin([type, characterFormat(message)]);
const right = this.filterAndJoin(opts.columns ? [tag, coloredDate] : [tag]);
const space =
(opts.columns || 0) - stringWidth(left) - stringWidth(right) - 2;
Expand All @@ -96,7 +96,7 @@ export class FancyReporter extends BasicReporter {
? left + " ".repeat(space) + right
: (right ? `${colors.gray(`[${right}]`)} ` : "") + left;

line += highlightBackticks(
line += characterFormat(
additional.length > 0 ? "\n" + additional.join("\n") : ""
);

Expand All @@ -109,8 +109,14 @@ export class FancyReporter extends BasicReporter {
}
}

function highlightBackticks(str: string) {
return str.replace(/`([^`]+)`/gm, (_, m) => colors.cyan(m));
function characterFormat(str: string) {
return (
str
// highlight backticks
.replace(/`([^`]+)`/gm, (_, m) => colors.cyan(m))
// underline underscores
.replace(/_([^_]+)_/gm, (_, m) => colors.underline(m))
);
}

function getColor(color = "white") {
Expand Down