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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fancy): formatting syntax #206

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ consola.box("I am a simple box");
await consola.prompt("Deploy to the production?", {
type: "confirm",
});

// formatting with colorette
consola.log("Text as `monospace` and _underline_ or `(underline,cyan)both`!")
```

Will display in the terminal:
Expand Down
4 changes: 4 additions & 0 deletions examples/special.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ consola.log(
"We can also _underline_ words but not_this or this should_not_be_underlined!"
);

consola.log("We can also do `(bgGreen)special` highlight");

consola.log("How about `(yellow,underline)chaining` them?");

// Nonstandard error
const { message, stack } = new Error("Custom Error!");
consola.error({ message, stack });
Expand Down
4 changes: 4 additions & 0 deletions src/reporters/fancy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ export class FancyReporter extends BasicReporter {
function characterFormat(str: string) {
return (
str
.replace(/`\(([^)]*)\)([^`]+)`/gm, (_, f, m) =>
// eslint-disable-next-line unicorn/no-array-reduce
f.split(',').reduce((p: string, c: string) => getColor(c)(p), m)
)
// highlight backticks
.replace(/`([^`]+)`/gm, (_, m) => colors.cyan(m))
// underline underscores
Expand Down