Skip to content

Commit

Permalink
feat(fancy): support underlining (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
ineshbose committed Jun 26, 2023
1 parent 0d03d70 commit 6ed7bc8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
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

0 comments on commit 6ed7bc8

Please sign in to comment.