Skip to content

Commit

Permalink
feat: consola.box (unjs#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreston321 committed Jun 27, 2023
1 parent 2c4003c commit 45ed7f1
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ consola.start("Building project...");
consola.warn("A new version of consola is available: 3.0.1");
consola.success("Project built!");
consola.error(new Error("This is an example error. Everything is fine!"));
consola.box("I am a simple box");
await consola.prompt("Deploy to the production?", {
type: "confirm",
});
Expand Down
27 changes: 27 additions & 0 deletions examples/box.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { consola } from "./utils";

function main() {
consola.box(`I am the default banner`);

consola.box({
title: "Box with options",
message: `I am a banner with different options`,
style: {
padding: 1,
borderColor: "magenta",
borderStyle: "double-single-rounded",
},
});

consola.box({
title: "Update available for `consola`",
message: `\`v1.0.2\` → \`v2.0.0\`\n\nRun \`npm install -g consola\` to update`,
style: {
padding: 2,
borderColor: "yellow",
borderStyle: "rounded",
},
});
}

main();
2 changes: 1 addition & 1 deletion src/consola.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Consola {
level: _normalizeLogLevel(options.level, types),
reporters: [...(options.reporters || [])],
},
<ConsolaOptions>{
<Partial<ConsolaOptions>>{
types: LogTypes,
throttle: 1000,
throttleMin: 5,
Expand Down
12 changes: 12 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ export type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {});

export const LogLevels: Record<LogType, number> = {
silent: Number.NEGATIVE_INFINITY,

fatal: 0,
error: 0,

warn: 1,

log: 2,
info: 3,

success: 3,
fail: 3,
ready: 3,
start: 3,
box: 3,

debug: 4,

trace: 5,

verbose: Number.POSITIVE_INFINITY,
};

Expand All @@ -34,6 +42,7 @@ export type LogType =
| "fail"
| "ready"
| "start"
| "box"
// Verbose
| "debug"
| "trace"
Expand Down Expand Up @@ -79,6 +88,9 @@ export const LogTypes: Record<LogType, Partial<LogObject>> = {
start: {
level: LogLevels.info,
},
box: {
level: LogLevels.info,
},

// Level 4
debug: {
Expand Down
17 changes: 16 additions & 1 deletion src/reporters/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { formatWithOptions } from "node:util";
import type {
LogObject,
ConsolaReporter,
ConsolaOptions,
FormatOptions,
ConsolaOptions,
} from "../types";
import { parseStack } from "../utils/error";
import { writeStream } from "../utils/stream";
Expand Down Expand Up @@ -39,6 +39,21 @@ export class BasicReporter implements ConsolaReporter {
formatLogObj(logObj: LogObject, opts: FormatOptions) {
const message = this.formatArgs(logObj.args, opts);

if (logObj.type === "box") {
return (
"\n" +
[
bracket(logObj.tag),
logObj.title && logObj.title,
...message.split("\n"),
]
.filter(Boolean)
.map((l) => " > " + l)
.join("\n") +
"\n"
);
}

return this.filterAndJoin([
bracket(logObj.type),
bracket(logObj.tag),
Expand Down
16 changes: 15 additions & 1 deletion src/reporters/fancy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as colors from "colorette";
import { parseStack } from "../utils/error";
import { FormatOptions, LogObject } from "../types";
import { LogLevel, LogType } from "../constants";
import { BoxOpts, box } from "../utils/box";
import { BasicReporter } from "./basic";

export const TYPE_COLOR_MAP: { [k in LogType]?: string } = {
Expand Down Expand Up @@ -76,11 +77,24 @@ export class FancyReporter extends BasicReporter {
"\n"
);

const isBadge = (logObj as any).badge ?? logObj.level < 2;
if (logObj.type === "box") {
return box(
highlightBackticks(
message + (additional.length > 0 ? "\n" + additional.join("\n") : "")
),
{
title: logObj.title
? highlightBackticks(logObj.title as string)
: undefined,
style: logObj.style as BoxOpts["style"],
}
);
}

const date = this.formatDate(logObj.date, opts);
const coloredDate = date && colors.gray(date);

const isBadge = (logObj.badge as boolean) ?? logObj.level < 2;
const type = this.formatType(logObj, isBadge, opts);

const tag = logObj.tag ? colors.gray(logObj.tag) : "";
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface FormatOptions {
date?: boolean;
colors?: boolean;
compact?: boolean | number;
[key: string]: any;
[key: string]: unknown;
}

export interface InputLogObject {
Expand All @@ -41,6 +41,7 @@ export interface LogObject extends InputLogObject {
tag: string;
args: any[];
date: Date;
[key: string]: unknown;
}

export interface ConsolaReporter {
Expand Down
Loading

0 comments on commit 45ed7f1

Please sign in to comment.