@peteanderson/ansi 0.0.1 → 0.1.0
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 +25 -30
- package/dist/ansi.d.ts +64 -0
- package/dist/ansi.js +75 -0
- package/package.json +5 -5
- package/src/ansi.js +89 -0
- package/dist/color.d.ts +0 -44
- package/dist/color.js +0 -85
- package/src/color.js +0 -101
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @peteanderson/ansi
|
|
2
2
|
|
|
3
|
-
ANSI escape sequence helpers for Node.js terminal output.
|
|
3
|
+
ANSI escape sequence helpers for Node.js terminal output.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -13,52 +13,47 @@ npm install @peteanderson/ansi
|
|
|
13
13
|
```js
|
|
14
14
|
const ansi = require("@peteanderson/ansi");
|
|
15
15
|
|
|
16
|
-
console.log(ansi.fg.red
|
|
17
|
-
console.log(ansi.bg.blue
|
|
18
|
-
console.log(ansi.
|
|
19
|
-
console.log(ansi.
|
|
20
|
-
console.log("some styled text" + ansi.reset);
|
|
21
|
-
console.log(ansi.stripAnsiSequences("\x1b[31mred\x1b[0m")); // => "red"
|
|
16
|
+
console.log(ansi.fg.red + "error: something went wrong" + ansi.fg.default);
|
|
17
|
+
console.log(ansi.bg.blue + ansi.fg.white + "highlighted" + ansi.fg.default + ansi.bg.default);
|
|
18
|
+
console.log(ansi.erase.screen);
|
|
19
|
+
console.log(ansi.caret.show);
|
|
22
20
|
```
|
|
23
21
|
|
|
24
22
|
## API
|
|
25
23
|
|
|
26
|
-
All color and style functions accept a string and return a string. When color is disabled, the functions are identity — they return the input unchanged.
|
|
27
|
-
|
|
28
24
|
### `fg` — foreground colors
|
|
29
25
|
|
|
30
|
-
`black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `
|
|
31
|
-
|
|
32
|
-
```js
|
|
33
|
-
ansi.fg.red("text")
|
|
34
|
-
ansi.fg.rgb(r, g, b)("text") // r, g, b in 0–5 for the 6×6×6 cube, or grayscale 232–255
|
|
35
|
-
```
|
|
26
|
+
Raw ANSI open sequences: `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `default`
|
|
36
27
|
|
|
37
28
|
### `bg` — background colors
|
|
38
29
|
|
|
39
|
-
`black` `red` `green` `yellow` `blue` `magenta` `cyan` `white`
|
|
40
|
-
|
|
41
|
-
```js
|
|
42
|
-
ansi.bg.green("text")
|
|
43
|
-
ansi.bg.rgb(r, g, b)("text")
|
|
44
|
-
```
|
|
30
|
+
Raw ANSI open sequences: `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `default`
|
|
45
31
|
|
|
46
32
|
### `style`
|
|
47
33
|
|
|
48
|
-
`
|
|
34
|
+
`underline` `reverse` (raw sequences)
|
|
49
35
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
36
|
+
### `caret`
|
|
37
|
+
|
|
38
|
+
- `show` / `hide` - show or hide the cursor
|
|
39
|
+
- `position.get` - get the current cursor position (terminal sends position to stdin)
|
|
40
|
+
- `position.set(row, col)` - set cursor position (1-based indexing)
|
|
41
|
+
- `shape.block` / `shape.underline` / `shape.bar` - change cursor shape
|
|
42
|
+
|
|
43
|
+
### `erase`
|
|
44
|
+
|
|
45
|
+
Raw sequences:
|
|
46
|
+
- `erase.line.toStart` / `erase.line.toEnd` / `erase.line.full`
|
|
47
|
+
- `erase.screen`
|
|
54
48
|
|
|
55
|
-
### `
|
|
49
|
+
### `scroll`
|
|
56
50
|
|
|
57
|
-
|
|
51
|
+
- `scroll.up(lines)` - scroll up (default 1 line)
|
|
52
|
+
- `scroll.down(lines)` - scroll down (default 1 line)
|
|
58
53
|
|
|
59
|
-
### `
|
|
54
|
+
### `strip(text)`
|
|
60
55
|
|
|
61
|
-
Removes ANSI
|
|
56
|
+
Removes ANSI CSI sequences from a string.
|
|
62
57
|
|
|
63
58
|
## License
|
|
64
59
|
|
package/dist/ansi.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export let reset: string;
|
|
2
|
+
export let underline: string;
|
|
3
|
+
export let reverse: string;
|
|
4
|
+
export namespace fg {
|
|
5
|
+
export let black: string;
|
|
6
|
+
export let red: string;
|
|
7
|
+
export let green: string;
|
|
8
|
+
export let yellow: string;
|
|
9
|
+
export let blue: string;
|
|
10
|
+
export let magenta: string;
|
|
11
|
+
export let cyan: string;
|
|
12
|
+
export let white: string;
|
|
13
|
+
let _default: string;
|
|
14
|
+
export { _default as default };
|
|
15
|
+
export let rgb: (r: number, g: number, b: number) => string;
|
|
16
|
+
}
|
|
17
|
+
export namespace bg {
|
|
18
|
+
let black_1: string;
|
|
19
|
+
export { black_1 as black };
|
|
20
|
+
let red_1: string;
|
|
21
|
+
export { red_1 as red };
|
|
22
|
+
let green_1: string;
|
|
23
|
+
export { green_1 as green };
|
|
24
|
+
let yellow_1: string;
|
|
25
|
+
export { yellow_1 as yellow };
|
|
26
|
+
let blue_1: string;
|
|
27
|
+
export { blue_1 as blue };
|
|
28
|
+
let magenta_1: string;
|
|
29
|
+
export { magenta_1 as magenta };
|
|
30
|
+
let cyan_1: string;
|
|
31
|
+
export { cyan_1 as cyan };
|
|
32
|
+
let white_1: string;
|
|
33
|
+
export { white_1 as white };
|
|
34
|
+
let _default_1: string;
|
|
35
|
+
export { _default_1 as default };
|
|
36
|
+
let rgb_1: (r: number, g: number, b: number) => string;
|
|
37
|
+
export { rgb_1 as rgb };
|
|
38
|
+
}
|
|
39
|
+
export namespace erase {
|
|
40
|
+
let toLineStart: string;
|
|
41
|
+
let toLineEnd: string;
|
|
42
|
+
let line: string;
|
|
43
|
+
let screen: string;
|
|
44
|
+
}
|
|
45
|
+
export namespace caret {
|
|
46
|
+
let hide: string;
|
|
47
|
+
let show: string;
|
|
48
|
+
namespace position {
|
|
49
|
+
let get: string;
|
|
50
|
+
let set: (x: number, y: number) => string;
|
|
51
|
+
}
|
|
52
|
+
namespace shape {
|
|
53
|
+
export let steadyBlock: string;
|
|
54
|
+
export let steadyBar: string;
|
|
55
|
+
export let steadyUnderline: string;
|
|
56
|
+
export let blinkingBlock: string;
|
|
57
|
+
export let blinkingBar: string;
|
|
58
|
+
export let blinkingUnderline: string;
|
|
59
|
+
let _default_2: string;
|
|
60
|
+
export { _default_2 as default };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export let scrollUp: (lines: number) => string;
|
|
64
|
+
export let scrollDown: (lines: number) => string;
|
package/dist/ansi.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** @type {(r: number, g: number, b: number, code: number) => string} */
|
|
3
|
+
function rgb(r, g, b, code) {
|
|
4
|
+
if (r === g && g === b) {
|
|
5
|
+
// Grayscale range.
|
|
6
|
+
if (r >= 232 && r <= 255)
|
|
7
|
+
return `\x1b[${code};5;${r}m`;
|
|
8
|
+
}
|
|
9
|
+
else if (r >= 0 && r <= 5 && g >= 0 && g <= 5 && b >= 0 && b <= 5) {
|
|
10
|
+
// 6x6x6 color cube.
|
|
11
|
+
const code = 16 + r * 36 + g * 6 + b;
|
|
12
|
+
return `\x1b[${code};5;${code}m`;
|
|
13
|
+
}
|
|
14
|
+
return `\x1b[${code};2;${r};${g};${b}m`;
|
|
15
|
+
}
|
|
16
|
+
const ansi = {
|
|
17
|
+
reset: "\x1b[0m",
|
|
18
|
+
underline: "\x1b[4m",
|
|
19
|
+
reverse: "\x1b[7m",
|
|
20
|
+
fg: {
|
|
21
|
+
black: "\x1b[30m",
|
|
22
|
+
red: "\x1b[31m",
|
|
23
|
+
green: "\x1b[32m",
|
|
24
|
+
yellow: "\x1b[33m",
|
|
25
|
+
blue: "\x1b[34m",
|
|
26
|
+
magenta: "\x1b[35m",
|
|
27
|
+
cyan: "\x1b[36m",
|
|
28
|
+
white: "\x1b[37m",
|
|
29
|
+
default: "\x1b[39m",
|
|
30
|
+
/** @type {(r: number, g: number, b: number) => string} */
|
|
31
|
+
rgb: (r, g, b) => rgb(r, g, b, 38),
|
|
32
|
+
},
|
|
33
|
+
bg: {
|
|
34
|
+
black: "\x1b[40m",
|
|
35
|
+
red: "\x1b[41m",
|
|
36
|
+
green: "\x1b[42m",
|
|
37
|
+
yellow: "\x1b[43m",
|
|
38
|
+
blue: "\x1b[44m",
|
|
39
|
+
magenta: "\x1b[45m",
|
|
40
|
+
cyan: "\x1b[46m",
|
|
41
|
+
white: "\x1b[47m",
|
|
42
|
+
default: "\x1b[49m",
|
|
43
|
+
/** @type {(r: number, g: number, b: number) => string} */
|
|
44
|
+
rgb: (r, g, b) => rgb(r, g, b, 48),
|
|
45
|
+
},
|
|
46
|
+
erase: {
|
|
47
|
+
toLineStart: "\x1b[1K",
|
|
48
|
+
toLineEnd: "\x1b[0K",
|
|
49
|
+
line: "\x1b[2K",
|
|
50
|
+
screen: "\x1b[2J",
|
|
51
|
+
},
|
|
52
|
+
caret: {
|
|
53
|
+
hide: "\x1b[?25l",
|
|
54
|
+
show: "\x1b[?25h",
|
|
55
|
+
position: {
|
|
56
|
+
get: "\x1b[6n",
|
|
57
|
+
/** @type {(x: number, y: number) => string} */
|
|
58
|
+
set: (x, y) => `\x1b[${y + 1};${x + 1}H`,
|
|
59
|
+
},
|
|
60
|
+
shape: {
|
|
61
|
+
steadyBlock: "\x1b[2 q",
|
|
62
|
+
steadyBar: "\x1b[6 q",
|
|
63
|
+
steadyUnderline: "\x1b[4 q",
|
|
64
|
+
blinkingBlock: "\x1b[1 q",
|
|
65
|
+
blinkingBar: "\x1b[5 q",
|
|
66
|
+
blinkingUnderline: "\x1b[3 q",
|
|
67
|
+
default: "\x1b[0 q",
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
/** @type {(lines: number) => string} */
|
|
71
|
+
scrollUp: lines => `\x1b[${lines}S`,
|
|
72
|
+
/** @type {(lines: number) => string} */
|
|
73
|
+
scrollDown: lines => `\x1b[${lines}T`,
|
|
74
|
+
};
|
|
75
|
+
module.exports = ansi;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name" : "@peteanderson/ansi",
|
|
3
|
-
"version" : "0.0
|
|
3
|
+
"version" : "0.1.0",
|
|
4
4
|
"description" : "ANSI escape sequence generation with automatic feature detection",
|
|
5
5
|
"author" : {
|
|
6
6
|
"name" : "Pete Anderson",
|
|
@@ -13,12 +13,12 @@
|
|
|
13
13
|
"keywords" : ["ansi", "nodejs", "color", "terminal", "cli"],
|
|
14
14
|
"engines" : {"node": ">=18"},
|
|
15
15
|
"files" : ["dist", "src"],
|
|
16
|
-
"main" : "dist/
|
|
17
|
-
"types" : "dist/
|
|
16
|
+
"main" : "dist/ansi.js",
|
|
17
|
+
"types" : "dist/ansi.d.ts",
|
|
18
18
|
"exports" : {
|
|
19
19
|
".": {
|
|
20
|
-
"types" : "./dist/
|
|
21
|
-
"require" : "./dist/
|
|
20
|
+
"types" : "./dist/ansi.d.ts",
|
|
21
|
+
"require" : "./dist/ansi.js"
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"license" : "MIT",
|
package/src/ansi.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/** @type {(r: number, g: number, b: number, code: number) => string} */
|
|
4
|
+
function rgb(r, g, b, code) {
|
|
5
|
+
if (r === g && g === b) {
|
|
6
|
+
// Grayscale range.
|
|
7
|
+
if (r >= 232 && r <= 255)
|
|
8
|
+
return `\x1b[${code};5;${r}m`;
|
|
9
|
+
} else if (r >= 0 && r <= 5 && g >= 0 && g <= 5 && b >= 0 && b <= 5) {
|
|
10
|
+
// 6x6x6 color cube.
|
|
11
|
+
const code = 16 + r * 36 + g * 6 + b;
|
|
12
|
+
return `\x1b[${code};5;${code}m`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return `\x1b[${code};2;${r};${g};${b}m`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const ansi = {
|
|
19
|
+
reset : "\x1b[0m",
|
|
20
|
+
underline : "\x1b[4m",
|
|
21
|
+
reverse : "\x1b[7m",
|
|
22
|
+
|
|
23
|
+
fg: {
|
|
24
|
+
black : "\x1b[30m",
|
|
25
|
+
red : "\x1b[31m",
|
|
26
|
+
green : "\x1b[32m",
|
|
27
|
+
yellow : "\x1b[33m",
|
|
28
|
+
blue : "\x1b[34m",
|
|
29
|
+
magenta : "\x1b[35m",
|
|
30
|
+
cyan : "\x1b[36m",
|
|
31
|
+
white : "\x1b[37m",
|
|
32
|
+
|
|
33
|
+
default : "\x1b[39m",
|
|
34
|
+
|
|
35
|
+
/** @type {(r: number, g: number, b: number) => string} */
|
|
36
|
+
rgb: (r, g, b) => rgb(r, g, b, 38),
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
bg: {
|
|
40
|
+
black : "\x1b[40m",
|
|
41
|
+
red : "\x1b[41m",
|
|
42
|
+
green : "\x1b[42m",
|
|
43
|
+
yellow : "\x1b[43m",
|
|
44
|
+
blue : "\x1b[44m",
|
|
45
|
+
magenta : "\x1b[45m",
|
|
46
|
+
cyan : "\x1b[46m",
|
|
47
|
+
white : "\x1b[47m",
|
|
48
|
+
|
|
49
|
+
default : "\x1b[49m",
|
|
50
|
+
|
|
51
|
+
/** @type {(r: number, g: number, b: number) => string} */
|
|
52
|
+
rgb: (r, g, b) => rgb(r, g, b, 48),
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
erase: {
|
|
56
|
+
toLineStart : "\x1b[1K",
|
|
57
|
+
toLineEnd : "\x1b[0K",
|
|
58
|
+
line : "\x1b[2K",
|
|
59
|
+
screen : "\x1b[2J",
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
caret: {
|
|
63
|
+
hide : "\x1b[?25l",
|
|
64
|
+
show : "\x1b[?25h",
|
|
65
|
+
|
|
66
|
+
position: {
|
|
67
|
+
get: "\x1b[6n",
|
|
68
|
+
/** @type {(x: number, y: number) => string} */
|
|
69
|
+
set: (x, y) => `\x1b[${y + 1};${x + 1}H`,
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
shape: {
|
|
73
|
+
steadyBlock : "\x1b[2 q",
|
|
74
|
+
steadyBar : "\x1b[6 q",
|
|
75
|
+
steadyUnderline : "\x1b[4 q",
|
|
76
|
+
blinkingBlock : "\x1b[1 q",
|
|
77
|
+
blinkingBar : "\x1b[5 q",
|
|
78
|
+
blinkingUnderline : "\x1b[3 q",
|
|
79
|
+
default : "\x1b[0 q",
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
/** @type {(lines: number) => string} */
|
|
84
|
+
scrollUp : lines => `\x1b[${lines}S`,
|
|
85
|
+
/** @type {(lines: number) => string} */
|
|
86
|
+
scrollDown : lines => `\x1b[${lines}T`,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
module.exports = ansi;
|
package/dist/color.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/** @param {string} text */
|
|
2
|
-
export function stripAnsiSequences(text: string): string;
|
|
3
|
-
export namespace style {
|
|
4
|
-
let bold: (text: string) => string;
|
|
5
|
-
let dim: (text: string) => string;
|
|
6
|
-
let italic: (text: string) => string;
|
|
7
|
-
let underline: (text: string) => string;
|
|
8
|
-
let inverse: (text: string) => string;
|
|
9
|
-
let hidden: (text: string) => string;
|
|
10
|
-
let strikethrough: (text: string) => string;
|
|
11
|
-
}
|
|
12
|
-
export namespace fg {
|
|
13
|
-
let black: (text: string) => string;
|
|
14
|
-
let red: (text: string) => string;
|
|
15
|
-
let green: (text: string) => string;
|
|
16
|
-
let yellow: (text: string) => string;
|
|
17
|
-
let blue: (text: string) => string;
|
|
18
|
-
let magenta: (text: string) => string;
|
|
19
|
-
let cyan: (text: string) => string;
|
|
20
|
-
let white: (text: string) => string;
|
|
21
|
-
let gray: (text: string) => string;
|
|
22
|
-
let rgb: (r: number, g: number, b: number) => (text: string) => string;
|
|
23
|
-
}
|
|
24
|
-
export namespace bg {
|
|
25
|
-
let black_1: (text: string) => string;
|
|
26
|
-
export { black_1 as black };
|
|
27
|
-
let red_1: (text: string) => string;
|
|
28
|
-
export { red_1 as red };
|
|
29
|
-
let green_1: (text: string) => string;
|
|
30
|
-
export { green_1 as green };
|
|
31
|
-
let yellow_1: (text: string) => string;
|
|
32
|
-
export { yellow_1 as yellow };
|
|
33
|
-
let blue_1: (text: string) => string;
|
|
34
|
-
export { blue_1 as blue };
|
|
35
|
-
let magenta_1: (text: string) => string;
|
|
36
|
-
export { magenta_1 as magenta };
|
|
37
|
-
let cyan_1: (text: string) => string;
|
|
38
|
-
export { cyan_1 as cyan };
|
|
39
|
-
let white_1: (text: string) => string;
|
|
40
|
-
export { white_1 as white };
|
|
41
|
-
let rgb_1: (r: number, g: number, b: number) => (text: string) => string;
|
|
42
|
-
export { rgb_1 as rgb };
|
|
43
|
-
}
|
|
44
|
-
export const reset: "\u001B[0m";
|
package/dist/color.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @param {string | undefined} value
|
|
4
|
-
* @returns {boolean}
|
|
5
|
-
*/
|
|
6
|
-
const isSet = value => !!value && value !== '0' && value.toLowerCase() !== "false";
|
|
7
|
-
const enabled = isSet(process.env.FORCE_COLOR) ||
|
|
8
|
-
!isSet(process.env.NODE_DISABLE_COLORS) &&
|
|
9
|
-
!isSet(process.env.NO_COLOR) &&
|
|
10
|
-
process.env.TERM !== "dumb" &&
|
|
11
|
-
process.stdout.isTTY;
|
|
12
|
-
/**
|
|
13
|
-
* @param {number | string} open
|
|
14
|
-
* @param {number | string} close
|
|
15
|
-
* @returns {(text: string) => string}
|
|
16
|
-
*/
|
|
17
|
-
function ansi(open, close) {
|
|
18
|
-
if (!enabled)
|
|
19
|
-
return s => s;
|
|
20
|
-
const openSequence = `\x1b[${open}m`;
|
|
21
|
-
const closeSequence = `\x1b[${close}m`;
|
|
22
|
-
return s => openSequence +
|
|
23
|
-
s.replaceAll(openSequence, '').replaceAll(closeSequence, openSequence) +
|
|
24
|
-
closeSequence;
|
|
25
|
-
}
|
|
26
|
-
/** @param {string} text */
|
|
27
|
-
const stripAnsiSequences = text => text.replace(/\x1b\[\d+m/g, '');
|
|
28
|
-
/** @type {(r: number, g: number, b: number, open: number, close: number) => (text: string) => string} */
|
|
29
|
-
function rgb(r, g, b, open, close) {
|
|
30
|
-
if (r === g && g === b) {
|
|
31
|
-
// Grayscale range.
|
|
32
|
-
if (r >= 232 && r <= 255)
|
|
33
|
-
return ansi(`${open};5;${r}`, close);
|
|
34
|
-
}
|
|
35
|
-
else if (r >= 0 && r <= 5 && g >= 0 && g <= 5 && b >= 0 && b <= 5) {
|
|
36
|
-
// 6x6x6 color cube.
|
|
37
|
-
const code = 16 + r * 36 + g * 6 + b;
|
|
38
|
-
return ansi(`${open};5;${code}`, close);
|
|
39
|
-
}
|
|
40
|
-
return ansi(`${open};2;${r};${g};${b}`, close);
|
|
41
|
-
}
|
|
42
|
-
const style = {
|
|
43
|
-
bold: ansi(1, 22),
|
|
44
|
-
dim: ansi(2, 22),
|
|
45
|
-
italic: ansi(3, 23),
|
|
46
|
-
underline: ansi(4, 24),
|
|
47
|
-
inverse: ansi(7, 27),
|
|
48
|
-
hidden: ansi(8, 28),
|
|
49
|
-
strikethrough: ansi(9, 29),
|
|
50
|
-
};
|
|
51
|
-
// export type Style = keyof typeof style;
|
|
52
|
-
const fg = {
|
|
53
|
-
black: ansi(30, 39),
|
|
54
|
-
red: ansi(31, 39),
|
|
55
|
-
green: ansi(32, 39),
|
|
56
|
-
yellow: ansi(33, 39),
|
|
57
|
-
blue: ansi(34, 39),
|
|
58
|
-
magenta: ansi(35, 39),
|
|
59
|
-
cyan: ansi(36, 39),
|
|
60
|
-
white: ansi(37, 39),
|
|
61
|
-
gray: ansi(90, 39),
|
|
62
|
-
/** @type {(r: number, g: number, b: number) => (text: string) => string} */
|
|
63
|
-
rgb: (r, g, b) => rgb(r, g, b, 38, 39),
|
|
64
|
-
};
|
|
65
|
-
// export type Color = keyof typeof fg;
|
|
66
|
-
const bg = {
|
|
67
|
-
black: ansi(40, 49),
|
|
68
|
-
red: ansi(41, 49),
|
|
69
|
-
green: ansi(42, 49),
|
|
70
|
-
yellow: ansi(43, 49),
|
|
71
|
-
blue: ansi(44, 49),
|
|
72
|
-
magenta: ansi(45, 49),
|
|
73
|
-
cyan: ansi(46, 49),
|
|
74
|
-
white: ansi(47, 49),
|
|
75
|
-
/** @type {(r: number, g: number, b: number) => (text: string) => string} */
|
|
76
|
-
rgb: (r, g, b) => rgb(r, g, b, 48, 49),
|
|
77
|
-
};
|
|
78
|
-
const reset = "\x1b[0m";
|
|
79
|
-
module.exports = {
|
|
80
|
-
stripAnsiSequences,
|
|
81
|
-
style,
|
|
82
|
-
fg,
|
|
83
|
-
bg,
|
|
84
|
-
reset,
|
|
85
|
-
};
|
package/src/color.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param {string | undefined} value
|
|
3
|
-
* @returns {boolean}
|
|
4
|
-
*/
|
|
5
|
-
const isSet = value => !!value && value !== '0' && value.toLowerCase() !== "false";
|
|
6
|
-
|
|
7
|
-
const enabled =
|
|
8
|
-
isSet(process.env.FORCE_COLOR) ||
|
|
9
|
-
!isSet(process.env.NODE_DISABLE_COLORS) &&
|
|
10
|
-
!isSet(process.env.NO_COLOR) &&
|
|
11
|
-
process.env.TERM !== "dumb" &&
|
|
12
|
-
process.stdout.isTTY;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @param {number | string} open
|
|
16
|
-
* @param {number | string} close
|
|
17
|
-
* @returns {(text: string) => string}
|
|
18
|
-
*/
|
|
19
|
-
function ansi(open, close) {
|
|
20
|
-
if (!enabled)
|
|
21
|
-
return s => s;
|
|
22
|
-
|
|
23
|
-
const openSequence = `\x1b[${open}m`;
|
|
24
|
-
const closeSequence = `\x1b[${close}m`;
|
|
25
|
-
|
|
26
|
-
return s =>
|
|
27
|
-
openSequence +
|
|
28
|
-
s.replaceAll(openSequence, '').replaceAll(closeSequence, openSequence) +
|
|
29
|
-
closeSequence;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** @param {string} text */
|
|
33
|
-
const stripAnsiSequences = text => text.replace(/\x1b\[\d+m/g, '');
|
|
34
|
-
|
|
35
|
-
/** @type {(r: number, g: number, b: number, open: number, close: number) => (text: string) => string} */
|
|
36
|
-
function rgb(r, g, b, open, close) {
|
|
37
|
-
if (r === g && g === b) {
|
|
38
|
-
// Grayscale range.
|
|
39
|
-
if (r >= 232 && r <= 255)
|
|
40
|
-
return ansi(`${open};5;${r}`, close);
|
|
41
|
-
} else if (r >= 0 && r <= 5 && g >= 0 && g <= 5 && b >= 0 && b <= 5) {
|
|
42
|
-
// 6x6x6 color cube.
|
|
43
|
-
const code = 16 + r * 36 + g * 6 + b;
|
|
44
|
-
return ansi(`${open};5;${code}`, close);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return ansi(`${open};2;${r};${g};${b}`, close);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const style = {
|
|
51
|
-
bold : ansi(1, 22),
|
|
52
|
-
dim : ansi(2, 22),
|
|
53
|
-
italic : ansi(3, 23),
|
|
54
|
-
underline : ansi(4, 24),
|
|
55
|
-
inverse : ansi(7, 27),
|
|
56
|
-
hidden : ansi(8, 28),
|
|
57
|
-
strikethrough : ansi(9, 29),
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
// export type Style = keyof typeof style;
|
|
61
|
-
|
|
62
|
-
const fg = {
|
|
63
|
-
black : ansi(30, 39),
|
|
64
|
-
red : ansi(31, 39),
|
|
65
|
-
green : ansi(32, 39),
|
|
66
|
-
yellow : ansi(33, 39),
|
|
67
|
-
blue : ansi(34, 39),
|
|
68
|
-
magenta : ansi(35, 39),
|
|
69
|
-
cyan : ansi(36, 39),
|
|
70
|
-
white : ansi(37, 39),
|
|
71
|
-
gray : ansi(90, 39),
|
|
72
|
-
|
|
73
|
-
/** @type {(r: number, g: number, b: number) => (text: string) => string} */
|
|
74
|
-
rgb: (r, g, b) => rgb(r, g, b, 38, 39),
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
// export type Color = keyof typeof fg;
|
|
78
|
-
|
|
79
|
-
const bg = {
|
|
80
|
-
black : ansi(40, 49),
|
|
81
|
-
red : ansi(41, 49),
|
|
82
|
-
green : ansi(42, 49),
|
|
83
|
-
yellow : ansi(43, 49),
|
|
84
|
-
blue : ansi(44, 49),
|
|
85
|
-
magenta : ansi(45, 49),
|
|
86
|
-
cyan : ansi(46, 49),
|
|
87
|
-
white : ansi(47, 49),
|
|
88
|
-
|
|
89
|
-
/** @type {(r: number, g: number, b: number) => (text: string) => string} */
|
|
90
|
-
rgb: (r, g, b) => rgb(r, g, b, 48, 49),
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const reset = "\x1b[0m";
|
|
94
|
-
|
|
95
|
-
module.exports = {
|
|
96
|
-
stripAnsiSequences,
|
|
97
|
-
style,
|
|
98
|
-
fg,
|
|
99
|
-
bg,
|
|
100
|
-
reset,
|
|
101
|
-
};
|