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: consola.box #193

Merged
merged 14 commits into from Jun 27, 2023
27 changes: 27 additions & 0 deletions examples/box.ts
@@ -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();
4 changes: 4 additions & 0 deletions src/constants.ts
Expand Up @@ -34,6 +34,7 @@ export type LogType =
| "fail"
| "ready"
| "start"
| "box"
// Verbose
| "debug"
| "trace"
Expand Down Expand Up @@ -79,6 +80,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
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
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
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