Skip to content
Open
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
14 changes: 0 additions & 14 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ export default defineBuildConfig({
}
},
});

// Node.js 14 support
// https://github.com/unjs/consola/issues/204
options.plugins.push({
name: "icu-compat",
transform(code, id) {
if (id.endsWith("string-width/index.js")) {
return code.replace(
"const segmenter = new Intl.Segmenter();",
"const segmenter = globalThis.Intl?.Segmenter ? new Intl.Segmenter() : { segment: (str) => str.split('') };",
);
}
},
});
},
},
});
12 changes: 0 additions & 12 deletions examples/no-width.ts

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
"prettier": "^3.5.3",
"sentencer": "^0.2.1",
"std-env": "^3.8.1",
"string-width": "^7.2.0",
"typescript": "^5.8.2",
"unbuild": "^3.5.0",
"vitest": "^3.0.9"
Expand Down
24 changes: 0 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/consola.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export class Consola {
throttle: 1000,
throttleMin: 5,
formatOptions: {
date: true,
colors: false,
compact: true,
},
Expand Down
9 changes: 1 addition & 8 deletions src/reporters/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
return formatWithOptions(opts, ..._args);
}

formatDate(date: Date, opts: FormatOptions) {
return opts.date ? date.toLocaleTimeString() : "";
}

filterAndJoin(arr: any[]) {
return arr.filter(Boolean).join(" ");
}
Expand Down Expand Up @@ -76,10 +72,7 @@
}

log(logObj: LogObject, ctx: { options: ConsolaOptions }) {
const line = this.formatLogObj(logObj, {
columns: (ctx.options.stdout as any).columns || 0,
...ctx.options.formatOptions,
});
const line = this.formatLogObj(logObj, ctx.options.formatOptions);

Check warning on line 75 in src/reporters/basic.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/basic.ts#L75

Added line #L75 was not covered by tests

return writeStream(
line + "\n",
Expand Down
32 changes: 5 additions & 27 deletions src/reporters/fancy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import _stringWidth from "string-width";
import isUnicodeSupported from "is-unicode-supported";
import { colors } from "../utils/color";
import { parseStack } from "../utils/error";
import { FormatOptions, LogObject } from "../types";
import { LogLevel, LogType } from "../constants";
import { BoxOpts, box } from "../utils/box";
import { stripAnsi } from "../utils";
import { BasicReporter } from "./basic";

export const TYPE_COLOR_MAP: { [k in LogType]?: string } = {
Expand Down Expand Up @@ -37,15 +35,6 @@
log: "",
};

function stringWidth(str: string) {
// https://github.com/unjs/consola/issues/204
const hasICU = typeof Intl === "object";
if (!hasICU || !Intl.Segmenter) {
return stripAnsi(str).length;
}
return _stringWidth(str);
}

export class FancyReporter extends BasicReporter {
formatStack(stack: string, message: string, opts?: FormatOptions) {
const indent = " ".repeat((opts?.errorLevel || 0) + 1);
Expand Down Expand Up @@ -102,24 +91,13 @@
);
}

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) : "";

let line;
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;

line =
space > 0 && (opts.columns || 0) >= 80
? left + " ".repeat(space) + right
: (right ? `${colors.gray(`[${right}]`)} ` : "") + left;
let line = this.filterAndJoin([
logObj.tag ? colors.gray(`[${logObj.tag}]`) : "",
this.formatType(logObj, isBadge, opts),
characterFormat(message),
]);

Check warning on line 100 in src/reporters/fancy.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/fancy.ts#L96-L100

Added lines #L96 - L100 were not covered by tests

line += characterFormat(
additional.length > 0 ? "\n" + additional.join("\n") : "",
Expand Down
12 changes: 0 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ export interface ConsolaOptions {
* @see https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors
*/
export interface FormatOptions {
/**
* The maximum number of columns to output, affects formatting.
* @optional
*/
columns?: number;

/**
* Whether to include timestamp information in log messages.
* @optional
*/
date?: boolean;

/**
* Whether to use colors in the output.
* @optional
Expand Down
9 changes: 4 additions & 5 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

// Predefined rules for replacing format arguments
const FORMAT_ARGS = [
["additional", 5],
["message", 4],
["type", 2],
["date", 1],
["tag", 3],
["additional", 4],
["message", 3],
["type", 1],
["tag", 2],

Check warning on line 8 in src/utils/format.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/format.ts#L5-L8

Added lines #L5 - L8 were not covered by tests
]; // .sort((a, b) => b[0].length - a[0].length)

// Caches compiled format strings for reuse
Expand Down