-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathfancy.ts
More file actions
156 lines (134 loc) Β· 4.34 KB
/
Copy pathfancy.ts
File metadata and controls
156 lines (134 loc) Β· 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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 } = {
info: "cyan",
fail: "red",
success: "green",
ready: "green",
start: "magenta",
};
export const LEVEL_COLOR_MAP: { [k in LogLevel]?: string } = {
0: "red",
1: "yellow",
};
const unicode = isUnicodeSupported();
const s = (c: string, fallback: string) => (unicode ? c : fallback);
const TYPE_ICONS: { [k in LogType]?: string } = {
error: s("β", "Γ"),
fatal: s("β", "Γ"),
ready: s("β", "β"),
warn: s("β ", "βΌ"),
info: s("βΉ", "i"),
success: s("β", "β"),
debug: s("β", "D"),
trace: s("β", "β"),
fail: s("β", "Γ"),
start: s("β", "o"),
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);
return (
`\n${indent}` +
parseStack(stack, message)
.map(
(line) =>
" " +
line
.replace(/^at +/, (m) => colors.gray(m))
.replace(/\((.+)\)/, (_, m) => `(${colors.cyan(m)})`),
)
.join(`\n${indent}`)
);
}
formatType(logObj: LogObject, isBadge: boolean, opts: FormatOptions) {
const typeColor =
(TYPE_COLOR_MAP as any)[logObj.type] ||
(LEVEL_COLOR_MAP as any)[logObj.level] ||
"gray";
if (isBadge) {
return getBgColor(typeColor)(
colors.black(` ${logObj.type.toUpperCase()} `),
);
}
const _type =
typeof (TYPE_ICONS as any)[logObj.type] === "string"
? (TYPE_ICONS as any)[logObj.type]
: (logObj as any).icon || logObj.type;
return _type ? getColor(typeColor)(_type) : "";
}
formatLogObj(logObj: LogObject, opts: FormatOptions) {
const [message, ...additional] = this.formatArgs(logObj.args, opts).split(
"\n",
);
if (logObj.type === "box") {
return box(
characterFormat(
message + (additional.length > 0 ? "\n" + additional.join("\n") : ""),
),
{
title: logObj.title
? characterFormat(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) : "";
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;
line += characterFormat(
additional.length > 0 ? "\n" + additional.join("\n") : "",
);
if (logObj.type === "trace") {
const _err = new Error("Trace: " + logObj.message);
line += this.formatStack(_err.stack || "", _err.message);
}
return isBadge ? "\n" + line + "\n" : line;
}
}
function characterFormat(str: string) {
return (
str
// highlight backticks (preserve them in output)
.replace(/`([^`]+)`/gm, (_, m) => colors.cyan(`\`${m}\``))
// underline underscores
.replace(/\s+_([^_]+)_\s+/gm, (_, m) => ` ${colors.underline(m)} `)
);
}
function getColor(color = "white") {
return (colors as any)[color] || colors.white;
}
function getBgColor(color = "bgWhite") {
return (
(colors as any)[`bg${color[0].toUpperCase()}${color.slice(1)}`] ||
colors.bgWhite
);
}