@reliverse/relico 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +246 -100
- package/bin/mod.d.ts +364 -0
- package/bin/mod.js +1187 -0
- package/bin/utils.d.ts +4 -0
- package/bin/utils.js +10 -0
- package/package.json +16 -15
- package/bin/main.d.ts +0 -124
- package/bin/main.js +0 -369
package/bin/utils.d.ts
ADDED
package/bin/utils.js
ADDED
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"dependencies": {
|
|
3
3
|
"@reliverse/runtime": "^1.0.3",
|
|
4
|
-
"c12": "^3.0.
|
|
4
|
+
"c12": "^3.0.3",
|
|
5
5
|
"pathe": "^2.0.3"
|
|
6
6
|
},
|
|
7
|
-
"description": "@reliverse/relico is
|
|
7
|
+
"description": "@reliverse/relico is a themeable, chainable, typed, truecolor-powered, modern terminal styling toolkit. Designed to make your CLI output colorful, accessible, and human-friendly. It gives you a flexible way to apply colors and styles — with reliability and a smart developer experience baked in. Built for humans, not just terminals.",
|
|
8
8
|
"homepage": "https://docs.reliverse.org",
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"name": "@reliverse/relico",
|
|
11
11
|
"type": "module",
|
|
12
|
-
"version": "1.1.
|
|
12
|
+
"version": "1.1.2",
|
|
13
13
|
"author": "reliverse",
|
|
14
14
|
"bugs": {
|
|
15
15
|
"email": "blefnk@gmail.com",
|
|
@@ -24,22 +24,23 @@
|
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@biomejs/biome": "1.9.4",
|
|
27
|
-
"@eslint/js": "^9.
|
|
27
|
+
"@eslint/js": "^9.26.0",
|
|
28
28
|
"@reliverse/relidler-cfg": "^1.1.3",
|
|
29
29
|
"@stylistic/eslint-plugin": "^4.2.0",
|
|
30
|
-
"@
|
|
31
|
-
"@types/
|
|
32
|
-
"
|
|
30
|
+
"@total-typescript/ts-reset": "^0.6.1",
|
|
31
|
+
"@types/bun": "^1.2.13",
|
|
32
|
+
"@types/node": "^22.15.18",
|
|
33
|
+
"eslint": "^9.26.0",
|
|
33
34
|
"eslint-plugin-no-relative-import-paths": "^1.6.1",
|
|
34
|
-
"eslint-plugin-perfectionist": "^4.
|
|
35
|
+
"eslint-plugin-perfectionist": "^4.13.0",
|
|
35
36
|
"jiti": "^2.4.2",
|
|
36
|
-
"knip": "^5.
|
|
37
|
-
"typescript": "^5.8.
|
|
38
|
-
"typescript-eslint": "^8.
|
|
39
|
-
"vitest": "^3.1.
|
|
37
|
+
"knip": "^5.56.0",
|
|
38
|
+
"typescript": "^5.8.3",
|
|
39
|
+
"typescript-eslint": "^8.32.1",
|
|
40
|
+
"vitest": "^3.1.3"
|
|
40
41
|
},
|
|
41
42
|
"exports": {
|
|
42
|
-
".": "./bin/
|
|
43
|
+
".": "./bin/mod.js"
|
|
43
44
|
},
|
|
44
45
|
"files": [
|
|
45
46
|
"bin",
|
|
@@ -47,8 +48,8 @@
|
|
|
47
48
|
"README.md",
|
|
48
49
|
"LICENSE"
|
|
49
50
|
],
|
|
50
|
-
"main": "./bin/
|
|
51
|
-
"module": "./bin/
|
|
51
|
+
"main": "./bin/mod.js",
|
|
52
|
+
"module": "./bin/mod.js",
|
|
52
53
|
"publishConfig": {
|
|
53
54
|
"access": "public"
|
|
54
55
|
}
|
package/bin/main.d.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/** A color definition: [primary, secondary]. */
|
|
2
|
-
export type ColorDefinition = [string, string];
|
|
3
|
-
/** A list of default color keys. */
|
|
4
|
-
export declare const defaultColorKeys: readonly ["reset", "bold", "dim", "italic", "underline", "inverse", "hidden", "strikethrough", "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "gray", "bgBlack", "bgRed", "bgGreen", "bgYellow", "bgBlue", "bgMagenta", "bgCyan", "bgWhite", "blackBright", "redBright", "greenBright", "yellowBright", "blueBright", "magentaBright", "cyanBright", "whiteBright", "bgBlackBright", "bgRedBright", "bgGreenBright", "bgYellowBright", "bgBlueBright", "bgMagentaBright", "bgCyanBright", "bgWhiteBright"];
|
|
5
|
-
/** Union of all default color keys */
|
|
6
|
-
export type DefaultColorKeys = (typeof defaultColorKeys)[number];
|
|
7
|
-
/**
|
|
8
|
-
* Format keys that must NOT be overridden by the user.
|
|
9
|
-
* We'll exclude them from IntelliSense and also skip them at runtime.
|
|
10
|
-
*/
|
|
11
|
-
type RestrictedKeys = "reset" | "bold" | "dim" | "italic" | "underline" | "inverse" | "hidden" | "strikethrough";
|
|
12
|
-
/** All the keys user is allowed to override */
|
|
13
|
-
type OverridableColorKeys = Exclude<DefaultColorKeys, RestrictedKeys>;
|
|
14
|
-
/** A map of user-overridable color definitions (no format keys) */
|
|
15
|
-
export type OverridableColorMap = Partial<Record<OverridableColorKeys, ColorDefinition>>;
|
|
16
|
-
/**
|
|
17
|
-
* `relico.config.ts` configuration options.
|
|
18
|
-
* Note: `customColors` is restricted to OverridableColorMap.
|
|
19
|
-
*/
|
|
20
|
-
export type RelicoConfig = {
|
|
21
|
-
/**
|
|
22
|
-
* Determines which ANSI mode is used:
|
|
23
|
-
* - 0: no color
|
|
24
|
-
* - 1: basic ANSI (8 colors)
|
|
25
|
-
* - 2: 256 color palette
|
|
26
|
-
* - 3: 24-bit truecolor (default)
|
|
27
|
-
*/
|
|
28
|
-
colorLevel?: 0 | 1 | 2 | 3;
|
|
29
|
-
/**
|
|
30
|
-
* Theme to use for color definitions.
|
|
31
|
-
* - "primary": primary theme (default)
|
|
32
|
-
* - "secondary": secondary theme
|
|
33
|
-
*/
|
|
34
|
-
theme?: "primary" | "secondary";
|
|
35
|
-
/**
|
|
36
|
-
* Custom color definitions.
|
|
37
|
-
* - Theming: ["primary", "secondary"]
|
|
38
|
-
*/
|
|
39
|
-
customColors?: OverridableColorMap;
|
|
40
|
-
};
|
|
41
|
-
export type IRelicoColors = {
|
|
42
|
-
reset(text: string | number): string;
|
|
43
|
-
bold(text: string | number): string;
|
|
44
|
-
dim(text: string | number): string;
|
|
45
|
-
italic(text: string | number): string;
|
|
46
|
-
underline(text: string | number): string;
|
|
47
|
-
inverse(text: string | number): string;
|
|
48
|
-
hidden(text: string | number): string;
|
|
49
|
-
strikethrough(text: string | number): string;
|
|
50
|
-
black(text: string | number): string;
|
|
51
|
-
red(text: string | number): string;
|
|
52
|
-
green(text: string | number): string;
|
|
53
|
-
yellow(text: string | number): string;
|
|
54
|
-
blue(text: string | number): string;
|
|
55
|
-
magenta(text: string | number): string;
|
|
56
|
-
cyan(text: string | number): string;
|
|
57
|
-
white(text: string | number): string;
|
|
58
|
-
gray(text: string | number): string;
|
|
59
|
-
bgBlack(text: string | number): string;
|
|
60
|
-
bgRed(text: string | number): string;
|
|
61
|
-
bgGreen(text: string | number): string;
|
|
62
|
-
bgYellow(text: string | number): string;
|
|
63
|
-
bgBlue(text: string | number): string;
|
|
64
|
-
bgMagenta(text: string | number): string;
|
|
65
|
-
bgCyan(text: string | number): string;
|
|
66
|
-
bgWhite(text: string | number): string;
|
|
67
|
-
blackBright(text: string | number): string;
|
|
68
|
-
redBright(text: string | number): string;
|
|
69
|
-
greenBright(text: string | number): string;
|
|
70
|
-
yellowBright(text: string | number): string;
|
|
71
|
-
blueBright(text: string | number): string;
|
|
72
|
-
magentaBright(text: string | number): string;
|
|
73
|
-
cyanBright(text: string | number): string;
|
|
74
|
-
whiteBright(text: string | number): string;
|
|
75
|
-
bgBlackBright(text: string | number): string;
|
|
76
|
-
bgRedBright(text: string | number): string;
|
|
77
|
-
bgGreenBright(text: string | number): string;
|
|
78
|
-
bgYellowBright(text: string | number): string;
|
|
79
|
-
bgBlueBright(text: string | number): string;
|
|
80
|
-
bgMagentaBright(text: string | number): string;
|
|
81
|
-
bgCyanBright(text: string | number): string;
|
|
82
|
-
bgWhiteBright(text: string | number): string;
|
|
83
|
-
[k: string]: (text: string | number) => string;
|
|
84
|
-
};
|
|
85
|
-
export declare const re: IRelicoColors;
|
|
86
|
-
/**
|
|
87
|
-
* Configures the library with a partial or complete
|
|
88
|
-
* `RelicoConfig`. Invalid fields are just ignored.
|
|
89
|
-
*/
|
|
90
|
-
export declare function configure(userInput: unknown): void;
|
|
91
|
-
/** Returns a color function by name (or `reset` or identity if not found). */
|
|
92
|
-
export declare function getColor(name: string): (text: string | number) => string;
|
|
93
|
-
/** Colorizes text with a color function. */
|
|
94
|
-
export declare function colorize(name: string, text: string | number): string;
|
|
95
|
-
/** Sets the color level (0=none, 1=basic, 2=256, 3=truecolor). */
|
|
96
|
-
export declare function setColorLevel(level: 0 | 1 | 2 | 3): void;
|
|
97
|
-
/** Returns a custom "rgb" color function if level is truecolor, otherwise identity. */
|
|
98
|
-
export declare function rgb(r: number, g: number, b: number): (text: string | number) => string;
|
|
99
|
-
export type ColorSupport = {
|
|
100
|
-
isColorSupported: boolean;
|
|
101
|
-
isForced: boolean;
|
|
102
|
-
isDisabled: boolean;
|
|
103
|
-
terminalName: string;
|
|
104
|
-
};
|
|
105
|
-
export declare const colorSupport: ColorSupport;
|
|
106
|
-
export declare function initUserConfig(): Promise<void>;
|
|
107
|
-
/**
|
|
108
|
-
* Provides type safety and IntelliSense for user configuration.
|
|
109
|
-
* Example usage in `relico.config.ts`:
|
|
110
|
-
* ```ts
|
|
111
|
-
* import { defineConfig } from "@reliverse/relico-cfg";
|
|
112
|
-
* export default defineConfig({
|
|
113
|
-
* colorLevel: 3,
|
|
114
|
-
* theme: "secondary",
|
|
115
|
-
* customColors: {
|
|
116
|
-
* red: ["#f00", "#c00"],
|
|
117
|
-
* blue: ["#0af", "#08f"],
|
|
118
|
-
* green: ["#00ff00", "#00cc00"],
|
|
119
|
-
* },
|
|
120
|
-
* });
|
|
121
|
-
* ```
|
|
122
|
-
*/
|
|
123
|
-
export declare function defineConfig(cfg: RelicoConfig): RelicoConfig;
|
|
124
|
-
export {};
|
package/bin/main.js
DELETED
|
@@ -1,369 +0,0 @@
|
|
|
1
|
-
import { getCurrentTerminalName } from "@reliverse/runtime";
|
|
2
|
-
import { env, isWindows } from "@reliverse/runtime";
|
|
3
|
-
import { loadConfig } from "c12";
|
|
4
|
-
export const defaultColorKeys = [
|
|
5
|
-
"reset",
|
|
6
|
-
"bold",
|
|
7
|
-
"dim",
|
|
8
|
-
"italic",
|
|
9
|
-
"underline",
|
|
10
|
-
"inverse",
|
|
11
|
-
"hidden",
|
|
12
|
-
"strikethrough",
|
|
13
|
-
"black",
|
|
14
|
-
"red",
|
|
15
|
-
"green",
|
|
16
|
-
"yellow",
|
|
17
|
-
"blue",
|
|
18
|
-
"magenta",
|
|
19
|
-
"cyan",
|
|
20
|
-
"white",
|
|
21
|
-
"gray",
|
|
22
|
-
"bgBlack",
|
|
23
|
-
"bgRed",
|
|
24
|
-
"bgGreen",
|
|
25
|
-
"bgYellow",
|
|
26
|
-
"bgBlue",
|
|
27
|
-
"bgMagenta",
|
|
28
|
-
"bgCyan",
|
|
29
|
-
"bgWhite",
|
|
30
|
-
"blackBright",
|
|
31
|
-
"redBright",
|
|
32
|
-
"greenBright",
|
|
33
|
-
"yellowBright",
|
|
34
|
-
"blueBright",
|
|
35
|
-
"magentaBright",
|
|
36
|
-
"cyanBright",
|
|
37
|
-
"whiteBright",
|
|
38
|
-
"bgBlackBright",
|
|
39
|
-
"bgRedBright",
|
|
40
|
-
"bgGreenBright",
|
|
41
|
-
"bgYellowBright",
|
|
42
|
-
"bgBlueBright",
|
|
43
|
-
"bgMagentaBright",
|
|
44
|
-
"bgCyanBright",
|
|
45
|
-
"bgWhiteBright"
|
|
46
|
-
];
|
|
47
|
-
const argv = typeof process === "undefined" ? [] : process.argv;
|
|
48
|
-
const isDisabled = "NO_COLOR" in env || argv.includes("--no-color");
|
|
49
|
-
const isForced = "FORCE_COLOR" in env || argv.includes("--color");
|
|
50
|
-
const isCI = "CI" in env && ("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env);
|
|
51
|
-
const isCompatibleTerminal = typeof process !== "undefined" && Boolean(process.stdout) && Boolean(process.stdout.isTTY) && env.TERM !== "dumb";
|
|
52
|
-
const colorterm = (env.COLORTERM ?? "").toLowerCase();
|
|
53
|
-
const supportsTrueColor = colorterm === "truecolor" || colorterm === "24bit";
|
|
54
|
-
function detectColorLevel() {
|
|
55
|
-
if (isDisabled) return 0;
|
|
56
|
-
if (isForced) return 3;
|
|
57
|
-
if (supportsTrueColor) return 3;
|
|
58
|
-
if (isWindows) return 2;
|
|
59
|
-
if (isCI) return 2;
|
|
60
|
-
if (isCompatibleTerminal) return 2;
|
|
61
|
-
return 0;
|
|
62
|
-
}
|
|
63
|
-
const baseColors = {
|
|
64
|
-
// Text formatting
|
|
65
|
-
reset: ["\x1B[0m", "\x1B[0m"],
|
|
66
|
-
bold: ["\x1B[1m", "\x1B[22m"],
|
|
67
|
-
dim: ["\x1B[2m", "\x1B[22m"],
|
|
68
|
-
italic: ["\x1B[3m", "\x1B[23m"],
|
|
69
|
-
underline: ["\x1B[4m", "\x1B[24m"],
|
|
70
|
-
inverse: ["\x1B[7m", "\x1B[27m"],
|
|
71
|
-
hidden: ["\x1B[8m", "\x1B[28m"],
|
|
72
|
-
strikethrough: ["\x1B[9m", "\x1B[29m"],
|
|
73
|
-
// Foreground colors
|
|
74
|
-
black: ["#000000", "#000000"],
|
|
75
|
-
red: ["#ff5555", "#ff0000"],
|
|
76
|
-
green: ["#00ff00", "#00ff00"],
|
|
77
|
-
yellow: ["#ffff00", "#ffff00"],
|
|
78
|
-
blue: ["#0000ff", "#0000ff"],
|
|
79
|
-
magenta: ["#ff00ff", "#ff00ff"],
|
|
80
|
-
cyan: ["#00ffff", "#00ffff"],
|
|
81
|
-
white: ["#ffffff", "#ffffff"],
|
|
82
|
-
gray: ["#808080", "#808080"],
|
|
83
|
-
// Background colors
|
|
84
|
-
bgBlack: ["#000000", "#000000"],
|
|
85
|
-
bgRed: ["#ff5555", "#ff0000"],
|
|
86
|
-
bgGreen: ["#00ff00", "#00ff00"],
|
|
87
|
-
bgYellow: ["#ffff00", "#ffff00"],
|
|
88
|
-
bgBlue: ["#0000ff", "#0000ff"],
|
|
89
|
-
bgMagenta: ["#ff00ff", "#ff00ff"],
|
|
90
|
-
bgCyan: ["#00ffff", "#00ffff"],
|
|
91
|
-
bgWhite: ["#ffffff", "#ffffff"],
|
|
92
|
-
// Bright colors.
|
|
93
|
-
blackBright: ["#000000", "#000000"],
|
|
94
|
-
redBright: ["#ff5555", "#ff5555"],
|
|
95
|
-
greenBright: ["#50fa7b", "#50fa7b"],
|
|
96
|
-
yellowBright: ["#f1fa8c", "#f1fa8c"],
|
|
97
|
-
blueBright: ["#24bdff", "#24bdff"],
|
|
98
|
-
magentaBright: ["#ff79c6", "#ff79c6"],
|
|
99
|
-
cyanBright: ["#8be9fd", "#8be9fd"],
|
|
100
|
-
whiteBright: ["#ffffff", "#ffffff"],
|
|
101
|
-
// Bright background colors.
|
|
102
|
-
bgBlackBright: ["#000000", "#000000"],
|
|
103
|
-
bgRedBright: ["#ff5555", "#ff5555"],
|
|
104
|
-
bgGreenBright: ["#50fa7b", "#50fa7b"],
|
|
105
|
-
bgYellowBright: ["#f1fa8c", "#f1fa8c"],
|
|
106
|
-
bgBlueBright: ["#24bdff", "#24bdff"],
|
|
107
|
-
bgMagentaBright: ["#ff79c6", "#ff79c6"],
|
|
108
|
-
bgCyanBright: ["#8be9fd", "#8be9fd"],
|
|
109
|
-
bgWhiteBright: ["#ffffff", "#ffffff"]
|
|
110
|
-
};
|
|
111
|
-
const windowsTerminalColors = {
|
|
112
|
-
...baseColors,
|
|
113
|
-
red: ["#ff5555", "#ff5555"],
|
|
114
|
-
green: ["#50fa7b", "#50fa7b"],
|
|
115
|
-
yellow: ["#f1fa8c", "#f1fa8c"],
|
|
116
|
-
blue: ["#6272a4", "#6272a4"],
|
|
117
|
-
magenta: ["#ff79c6", "#ff79c6"],
|
|
118
|
-
cyan: ["#8be9fd", "#8be9fd"]
|
|
119
|
-
};
|
|
120
|
-
const restrictedKeys = /* @__PURE__ */ new Set([
|
|
121
|
-
"reset",
|
|
122
|
-
"bold",
|
|
123
|
-
"dim",
|
|
124
|
-
"italic",
|
|
125
|
-
"underline",
|
|
126
|
-
"inverse",
|
|
127
|
-
"hidden",
|
|
128
|
-
"strikethrough"
|
|
129
|
-
]);
|
|
130
|
-
let config = {
|
|
131
|
-
colorLevel: detectColorLevel(),
|
|
132
|
-
theme: "primary"
|
|
133
|
-
// default theme; can be overridden in user config
|
|
134
|
-
};
|
|
135
|
-
let colorMap = {};
|
|
136
|
-
let colorFunctions = {};
|
|
137
|
-
function hexToRGB(hex) {
|
|
138
|
-
if (hex.startsWith("#")) {
|
|
139
|
-
hex = hex.slice(1);
|
|
140
|
-
}
|
|
141
|
-
if (hex.length === 3) {
|
|
142
|
-
hex = hex.split("").map((c) => c + c).join("");
|
|
143
|
-
}
|
|
144
|
-
if (hex.length !== 6) {
|
|
145
|
-
throw new Error(`Invalid hex color: ${hex}`);
|
|
146
|
-
}
|
|
147
|
-
const r = Number.parseInt(hex.substring(0, 2), 16);
|
|
148
|
-
const g = Number.parseInt(hex.substring(2, 4), 16);
|
|
149
|
-
const b = Number.parseInt(hex.substring(4, 6), 16);
|
|
150
|
-
return { r, g, b };
|
|
151
|
-
}
|
|
152
|
-
function hexToAnsiParts(hex, isBg = false) {
|
|
153
|
-
const { r, g, b } = hexToRGB(hex);
|
|
154
|
-
const open = isBg ? `\x1B[48;2;${r};${g};${b}m` : `\x1B[38;2;${r};${g};${b}m`;
|
|
155
|
-
const close = isBg ? "\x1B[49m" : "\x1B[39m";
|
|
156
|
-
return { open, close };
|
|
157
|
-
}
|
|
158
|
-
function hexToAnsi256(hex, isBg = false) {
|
|
159
|
-
const { r, g, b } = hexToRGB(hex);
|
|
160
|
-
const r5 = Math.round(r / 51);
|
|
161
|
-
const g5 = Math.round(g / 51);
|
|
162
|
-
const b5 = Math.round(b / 51);
|
|
163
|
-
const index = 16 + 36 * r5 + 6 * g5 + b5;
|
|
164
|
-
return isBg ? `\x1B[48;5;${index}m` : `\x1B[38;5;${index}m`;
|
|
165
|
-
}
|
|
166
|
-
const basicColors = [
|
|
167
|
-
{ name: "black", rgb: { r: 0, g: 0, b: 0 }, fg: 30, bg: 40 },
|
|
168
|
-
{ name: "red", rgb: { r: 205, g: 0, b: 0 }, fg: 31, bg: 41 },
|
|
169
|
-
{ name: "green", rgb: { r: 0, g: 205, b: 0 }, fg: 32, bg: 42 },
|
|
170
|
-
{ name: "yellow", rgb: { r: 205, g: 205, b: 0 }, fg: 33, bg: 43 },
|
|
171
|
-
{ name: "blue", rgb: { r: 0, g: 0, b: 238 }, fg: 34, bg: 44 },
|
|
172
|
-
{ name: "magenta", rgb: { r: 205, g: 0, b: 205 }, fg: 35, bg: 45 },
|
|
173
|
-
{ name: "cyan", rgb: { r: 0, g: 205, b: 205 }, fg: 36, bg: 46 },
|
|
174
|
-
{ name: "white", rgb: { r: 229, g: 229, b: 229 }, fg: 37, bg: 47 }
|
|
175
|
-
];
|
|
176
|
-
function hexToAnsiBasic(hex, isBg = false) {
|
|
177
|
-
const { r, g, b } = hexToRGB(hex);
|
|
178
|
-
let bestMatch = basicColors[0];
|
|
179
|
-
let bestDistance = Number.MAX_VALUE;
|
|
180
|
-
for (const color of basicColors) {
|
|
181
|
-
const dr = r - color.rgb.r;
|
|
182
|
-
const dg = g - color.rgb.g;
|
|
183
|
-
const db = b - color.rgb.b;
|
|
184
|
-
const distance = dr * dr + dg * dg + db * db;
|
|
185
|
-
if (distance < bestDistance) {
|
|
186
|
-
bestDistance = distance;
|
|
187
|
-
bestMatch = color;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
const code = isBg ? bestMatch.bg : bestMatch.fg;
|
|
191
|
-
return `\x1B[${code}m`;
|
|
192
|
-
}
|
|
193
|
-
function convertColorDefinition(key, def) {
|
|
194
|
-
const isBg = key.toLowerCase().startsWith("bg");
|
|
195
|
-
const theme = config.theme ?? "primary";
|
|
196
|
-
const chosen = theme === "primary" ? def[0] : def[1];
|
|
197
|
-
function convert(str) {
|
|
198
|
-
if (!str.startsWith("#")) return str;
|
|
199
|
-
if (config.colorLevel === 3) {
|
|
200
|
-
return hexToAnsiParts(str, isBg).open;
|
|
201
|
-
}
|
|
202
|
-
if (config.colorLevel === 2) {
|
|
203
|
-
return hexToAnsi256(str, isBg);
|
|
204
|
-
}
|
|
205
|
-
if (config.colorLevel === 1) {
|
|
206
|
-
return hexToAnsiBasic(str, isBg);
|
|
207
|
-
}
|
|
208
|
-
return "";
|
|
209
|
-
}
|
|
210
|
-
const openConverted = convert(chosen);
|
|
211
|
-
const close = isBg ? "\x1B[49m" : "\x1B[39m";
|
|
212
|
-
return [openConverted, close];
|
|
213
|
-
}
|
|
214
|
-
function buildColorMap(cfg) {
|
|
215
|
-
const terminalName = getCurrentTerminalName();
|
|
216
|
-
const isWinTerm = terminalName === "Windows Terminal";
|
|
217
|
-
if (cfg.colorLevel === 0) {
|
|
218
|
-
const noColorMap = {};
|
|
219
|
-
for (const k of Object.keys(baseColors)) {
|
|
220
|
-
noColorMap[k] = ["", ""];
|
|
221
|
-
}
|
|
222
|
-
return noColorMap;
|
|
223
|
-
}
|
|
224
|
-
let builtIn = { ...baseColors };
|
|
225
|
-
if (cfg.customColors) {
|
|
226
|
-
for (const [k, v] of Object.entries(cfg.customColors)) {
|
|
227
|
-
if (!restrictedKeys.has(k)) {
|
|
228
|
-
builtIn[k] = v;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
if (isWinTerm && cfg.colorLevel === 3) {
|
|
233
|
-
builtIn = { ...windowsTerminalColors, ...builtIn };
|
|
234
|
-
}
|
|
235
|
-
for (const key of Object.keys(builtIn)) {
|
|
236
|
-
builtIn[key] = convertColorDefinition(key, builtIn[key]);
|
|
237
|
-
}
|
|
238
|
-
return builtIn;
|
|
239
|
-
}
|
|
240
|
-
function createFormatter(open, close) {
|
|
241
|
-
return (input) => {
|
|
242
|
-
return open + String(input) + close;
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
function identityColor(text) {
|
|
246
|
-
return String(text);
|
|
247
|
-
}
|
|
248
|
-
const typedRe = {
|
|
249
|
-
reset: identityColor,
|
|
250
|
-
bold: identityColor,
|
|
251
|
-
dim: identityColor,
|
|
252
|
-
italic: identityColor,
|
|
253
|
-
underline: identityColor,
|
|
254
|
-
inverse: identityColor,
|
|
255
|
-
hidden: identityColor,
|
|
256
|
-
strikethrough: identityColor,
|
|
257
|
-
black: identityColor,
|
|
258
|
-
red: identityColor,
|
|
259
|
-
green: identityColor,
|
|
260
|
-
yellow: identityColor,
|
|
261
|
-
blue: identityColor,
|
|
262
|
-
magenta: identityColor,
|
|
263
|
-
cyan: identityColor,
|
|
264
|
-
white: identityColor,
|
|
265
|
-
gray: identityColor,
|
|
266
|
-
bgBlack: identityColor,
|
|
267
|
-
bgRed: identityColor,
|
|
268
|
-
bgGreen: identityColor,
|
|
269
|
-
bgYellow: identityColor,
|
|
270
|
-
bgBlue: identityColor,
|
|
271
|
-
bgMagenta: identityColor,
|
|
272
|
-
bgCyan: identityColor,
|
|
273
|
-
bgWhite: identityColor,
|
|
274
|
-
blackBright: identityColor,
|
|
275
|
-
redBright: identityColor,
|
|
276
|
-
greenBright: identityColor,
|
|
277
|
-
yellowBright: identityColor,
|
|
278
|
-
blueBright: identityColor,
|
|
279
|
-
magentaBright: identityColor,
|
|
280
|
-
cyanBright: identityColor,
|
|
281
|
-
whiteBright: identityColor,
|
|
282
|
-
bgBlackBright: identityColor,
|
|
283
|
-
bgRedBright: identityColor,
|
|
284
|
-
bgGreenBright: identityColor,
|
|
285
|
-
bgYellowBright: identityColor,
|
|
286
|
-
bgBlueBright: identityColor,
|
|
287
|
-
bgMagentaBright: identityColor,
|
|
288
|
-
bgCyanBright: identityColor,
|
|
289
|
-
bgWhiteBright: identityColor
|
|
290
|
-
};
|
|
291
|
-
export const re = typedRe;
|
|
292
|
-
function refreshTypedRe() {
|
|
293
|
-
for (const colorName of Object.keys(typedRe)) {
|
|
294
|
-
typedRe[colorName] = identityColor;
|
|
295
|
-
}
|
|
296
|
-
for (const [k, fn] of Object.entries(colorFunctions)) {
|
|
297
|
-
typedRe[k] = fn;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
function initColorFunctions() {
|
|
301
|
-
colorFunctions = {};
|
|
302
|
-
if (config.colorLevel === 0) {
|
|
303
|
-
for (const k of Object.keys(baseColors)) {
|
|
304
|
-
colorFunctions[k] = identityColor;
|
|
305
|
-
}
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
for (const [key, [open, close]] of Object.entries(colorMap)) {
|
|
309
|
-
colorFunctions[key] = createFormatter(open, close);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
function rebuild() {
|
|
313
|
-
colorMap = buildColorMap(config);
|
|
314
|
-
initColorFunctions();
|
|
315
|
-
refreshTypedRe();
|
|
316
|
-
}
|
|
317
|
-
rebuild();
|
|
318
|
-
export function configure(userInput) {
|
|
319
|
-
let newConfig;
|
|
320
|
-
if (typeof userInput === "object" && userInput !== null) {
|
|
321
|
-
newConfig = { ...config, ...userInput };
|
|
322
|
-
} else {
|
|
323
|
-
newConfig = { ...config };
|
|
324
|
-
}
|
|
325
|
-
config = newConfig;
|
|
326
|
-
rebuild();
|
|
327
|
-
}
|
|
328
|
-
export function getColor(name) {
|
|
329
|
-
const maybeFn = colorFunctions[name];
|
|
330
|
-
if (maybeFn) return maybeFn;
|
|
331
|
-
const resetFn = colorFunctions.reset;
|
|
332
|
-
if (resetFn) return resetFn;
|
|
333
|
-
return identityColor;
|
|
334
|
-
}
|
|
335
|
-
export function colorize(name, text) {
|
|
336
|
-
const fn = getColor(name);
|
|
337
|
-
return fn(text);
|
|
338
|
-
}
|
|
339
|
-
export function setColorLevel(level) {
|
|
340
|
-
configure({ colorLevel: level });
|
|
341
|
-
}
|
|
342
|
-
export function rgb(r, g, b) {
|
|
343
|
-
if (config.colorLevel === 3) {
|
|
344
|
-
const open = `\x1B[38;2;${r};${g};${b}m`;
|
|
345
|
-
const close = "\x1B[39m";
|
|
346
|
-
return createFormatter(open, close);
|
|
347
|
-
}
|
|
348
|
-
return identityColor;
|
|
349
|
-
}
|
|
350
|
-
function getConfig() {
|
|
351
|
-
return { ...config };
|
|
352
|
-
}
|
|
353
|
-
export const colorSupport = {
|
|
354
|
-
isColorSupported: getConfig().colorLevel !== 0,
|
|
355
|
-
isForced,
|
|
356
|
-
isDisabled,
|
|
357
|
-
terminalName: getCurrentTerminalName()
|
|
358
|
-
};
|
|
359
|
-
export async function initUserConfig() {
|
|
360
|
-
try {
|
|
361
|
-
const { config: userConfig } = await loadConfig({ name: "relico" });
|
|
362
|
-
configure(userConfig);
|
|
363
|
-
} catch (err) {
|
|
364
|
-
console.warn("Failed to load user config via c12:", err);
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
export function defineConfig(cfg) {
|
|
368
|
-
return cfg;
|
|
369
|
-
}
|