@peteanderson/ansi 0.0.1 → 0.1.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @peteanderson/ansi
2
2
 
3
- ANSI escape sequence helpers for Node.js terminal output. Automatically disables color when stdout is not a TTY or when standard environment variables (`NO_COLOR`, `NODE_DISABLE_COLORS`, `FORCE_COLOR`, `TERM=dumb`) indicate it should be.
3
+ ANSI escape sequence helpers for Node.js terminal output.
4
4
 
5
5
  ## Installation
6
6
 
@@ -13,52 +13,53 @@ npm install @peteanderson/ansi
13
13
  ```js
14
14
  const ansi = require("@peteanderson/ansi");
15
15
 
16
- console.log(ansi.fg.red("error: something went wrong"));
17
- console.log(ansi.bg.blue(ansi.fg.white("highlighted")));
18
- console.log(ansi.style.bold("important"));
19
- console.log(ansi.fg.rgb(5, 2, 0)("custom color"));
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.slice(coloredText, 0, 10)); // slice by visible characters
20
+ console.log(ansi.strip(coloredText)); // remove ANSI codes
22
21
  ```
23
22
 
24
23
  ## API
25
24
 
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
25
  ### `fg` — foreground colors
29
26
 
30
- `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `gray`
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
- ```
27
+ Raw ANSI open sequences: `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `default`
36
28
 
37
29
  ### `bg` — background colors
38
30
 
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
- ```
31
+ Raw ANSI open sequences: `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `default`
45
32
 
46
33
  ### `style`
47
34
 
48
- `bold` `dim` `italic` `underline` `inverse` `hidden` `strikethrough`
35
+ `underline` `reverse` (raw sequences)
49
36
 
50
- ```js
51
- ansi.style.bold("text")
52
- ansi.style.italic(ansi.style.bold("text"))
53
- ```
37
+ ### `caret`
38
+
39
+ - `show` / `hide` - show or hide the cursor
40
+ - `position.get` - get the current cursor position (terminal sends position to stdin)
41
+ - `position.set(row, col)` - set cursor position (1-based indexing)
42
+ - `shape.block` / `shape.underline` / `shape.bar` - change cursor shape
43
+
44
+ ### `erase`
45
+
46
+ Raw sequences:
47
+ - `erase.line.toStart` / `erase.line.toEnd` / `erase.line.full`
48
+ - `erase.screen`
49
+
50
+ ### `scroll`
51
+
52
+ - `scroll.up(lines)` - scroll up (default 1 line)
53
+ - `scroll.down(lines)` - scroll down (default 1 line)
54
54
 
55
- ### `reset`
55
+ ### `strip(text)`
56
56
 
57
- The SGR reset sequence (`\x1b[0m`). An empty string when color is disabled.
57
+ Removes ANSI CSI sequences from a string.
58
58
 
59
- ### `stripAnsiSequences(text)`
59
+ ### `slice(text, start, end)`
60
60
 
61
- Removes ANSI SGR escape sequences from a string.
61
+ Slices a string by visible characters, ignoring ANSI sequences. Works like `String.prototype.slice`
62
+ but counts only visible characters.
62
63
 
63
64
  ## License
64
65
 
package/dist/ansi.d.ts ADDED
@@ -0,0 +1,66 @@
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;
65
+ export let strip: (text: string) => string;
66
+ export function slice(text: string, start: number, end: number): string;
package/dist/ansi.js ADDED
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ const rxCSI = /\x1b\[[0-?]*[ -/]*[@-~]/g;
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
+ }
10
+ else if (r >= 0 && r <= 5 && g >= 0 && g <= 5 && b >= 0 && b <= 5) {
11
+ // 6x6x6 color cube.
12
+ const code = 16 + r * 36 + g * 6 + b;
13
+ return `\x1b[${code};5;${code}m`;
14
+ }
15
+ return `\x1b[${code};2;${r};${g};${b}m`;
16
+ }
17
+ const ansi = {
18
+ reset: "\x1b[0m",
19
+ underline: "\x1b[4m",
20
+ reverse: "\x1b[7m",
21
+ fg: {
22
+ black: "\x1b[30m",
23
+ red: "\x1b[31m",
24
+ green: "\x1b[32m",
25
+ yellow: "\x1b[33m",
26
+ blue: "\x1b[34m",
27
+ magenta: "\x1b[35m",
28
+ cyan: "\x1b[36m",
29
+ white: "\x1b[37m",
30
+ default: "\x1b[39m",
31
+ /** @type {(r: number, g: number, b: number) => string} */
32
+ rgb: (r, g, b) => rgb(r, g, b, 38),
33
+ },
34
+ bg: {
35
+ black: "\x1b[40m",
36
+ red: "\x1b[41m",
37
+ green: "\x1b[42m",
38
+ yellow: "\x1b[43m",
39
+ blue: "\x1b[44m",
40
+ magenta: "\x1b[45m",
41
+ cyan: "\x1b[46m",
42
+ white: "\x1b[47m",
43
+ default: "\x1b[49m",
44
+ /** @type {(r: number, g: number, b: number) => string} */
45
+ rgb: (r, g, b) => rgb(r, g, b, 48),
46
+ },
47
+ erase: {
48
+ toLineStart: "\x1b[1K",
49
+ toLineEnd: "\x1b[0K",
50
+ line: "\x1b[2K",
51
+ screen: "\x1b[2J",
52
+ },
53
+ caret: {
54
+ hide: "\x1b[?25l",
55
+ show: "\x1b[?25h",
56
+ position: {
57
+ get: "\x1b[6n",
58
+ /** @type {(x: number, y: number) => string} */
59
+ set: (x, y) => `\x1b[${y + 1};${x + 1}H`,
60
+ },
61
+ shape: {
62
+ steadyBlock: "\x1b[2 q",
63
+ steadyBar: "\x1b[6 q",
64
+ steadyUnderline: "\x1b[4 q",
65
+ blinkingBlock: "\x1b[1 q",
66
+ blinkingBar: "\x1b[5 q",
67
+ blinkingUnderline: "\x1b[3 q",
68
+ default: "\x1b[0 q",
69
+ },
70
+ },
71
+ /** @type {(lines: number) => string} */
72
+ scrollUp: lines => `\x1b[${lines}S`,
73
+ /** @type {(lines: number) => string} */
74
+ scrollDown: lines => `\x1b[${lines}T`,
75
+ /** @type {(text: string) => string} */
76
+ strip: text => text.replace(rxCSI, ''),
77
+ /** @type {(text: string, start: number, end: number) => string} */
78
+ slice(text, start, end) {
79
+ if (start < 0 || end < 0) {
80
+ const visibleLength = ansi.strip(text).length;
81
+ if (start < 0)
82
+ start = Math.max(0, visibleLength + start);
83
+ if (end < 0)
84
+ end = Math.max(0, visibleLength + end);
85
+ }
86
+ if (start > end)
87
+ return "";
88
+ let visibleIndex = 0;
89
+ let startIndex = 0;
90
+ while (startIndex < text.length && visibleIndex < start) {
91
+ if (text[startIndex] === "\x1b") {
92
+ rxCSI.lastIndex = startIndex;
93
+ const match = rxCSI.exec(text);
94
+ if (match?.index === startIndex) {
95
+ startIndex += match[0].length;
96
+ continue;
97
+ }
98
+ }
99
+ startIndex++;
100
+ visibleIndex++;
101
+ }
102
+ let endIndex = startIndex;
103
+ let hasAnsi = false;
104
+ while (endIndex < text.length && visibleIndex < end) {
105
+ if (text[endIndex] === "\x1b") {
106
+ rxCSI.lastIndex = endIndex;
107
+ const match = rxCSI.exec(text);
108
+ if (match?.index === endIndex) {
109
+ endIndex += match[0].length;
110
+ hasAnsi = true;
111
+ continue;
112
+ }
113
+ }
114
+ endIndex++;
115
+ visibleIndex++;
116
+ }
117
+ return text.slice(startIndex, endIndex) + (hasAnsi ? ansi.reset : "");
118
+ },
119
+ };
120
+ module.exports = ansi;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name" : "@peteanderson/ansi",
3
- "version" : "0.0.1",
3
+ "version" : "0.1.1",
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/color.js",
17
- "types" : "dist/color.d.ts",
16
+ "main" : "dist/ansi.js",
17
+ "types" : "dist/ansi.d.ts",
18
18
  "exports" : {
19
19
  ".": {
20
- "types" : "./dist/color.d.ts",
21
- "require" : "./dist/color.js"
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,143 @@
1
+ "use strict";
2
+
3
+ const rxCSI = /\x1b\[[0-?]*[ -/]*[@-~]/g;
4
+
5
+ /** @type {(r: number, g: number, b: number, code: number) => string} */
6
+ function rgb(r, g, b, code) {
7
+ if (r === g && g === b) {
8
+ // Grayscale range.
9
+ if (r >= 232 && r <= 255)
10
+ return `\x1b[${code};5;${r}m`;
11
+ } else if (r >= 0 && r <= 5 && g >= 0 && g <= 5 && b >= 0 && b <= 5) {
12
+ // 6x6x6 color cube.
13
+ const code = 16 + r * 36 + g * 6 + b;
14
+ return `\x1b[${code};5;${code}m`;
15
+ }
16
+
17
+ return `\x1b[${code};2;${r};${g};${b}m`;
18
+ }
19
+
20
+ const ansi = {
21
+ reset : "\x1b[0m",
22
+ underline : "\x1b[4m",
23
+ reverse : "\x1b[7m",
24
+
25
+ fg: {
26
+ black : "\x1b[30m",
27
+ red : "\x1b[31m",
28
+ green : "\x1b[32m",
29
+ yellow : "\x1b[33m",
30
+ blue : "\x1b[34m",
31
+ magenta : "\x1b[35m",
32
+ cyan : "\x1b[36m",
33
+ white : "\x1b[37m",
34
+
35
+ default : "\x1b[39m",
36
+
37
+ /** @type {(r: number, g: number, b: number) => string} */
38
+ rgb: (r, g, b) => rgb(r, g, b, 38),
39
+ },
40
+
41
+ bg: {
42
+ black : "\x1b[40m",
43
+ red : "\x1b[41m",
44
+ green : "\x1b[42m",
45
+ yellow : "\x1b[43m",
46
+ blue : "\x1b[44m",
47
+ magenta : "\x1b[45m",
48
+ cyan : "\x1b[46m",
49
+ white : "\x1b[47m",
50
+
51
+ default : "\x1b[49m",
52
+
53
+ /** @type {(r: number, g: number, b: number) => string} */
54
+ rgb: (r, g, b) => rgb(r, g, b, 48),
55
+ },
56
+
57
+ erase: {
58
+ toLineStart : "\x1b[1K",
59
+ toLineEnd : "\x1b[0K",
60
+ line : "\x1b[2K",
61
+ screen : "\x1b[2J",
62
+ },
63
+
64
+ caret: {
65
+ hide : "\x1b[?25l",
66
+ show : "\x1b[?25h",
67
+
68
+ position: {
69
+ get: "\x1b[6n",
70
+ /** @type {(x: number, y: number) => string} */
71
+ set: (x, y) => `\x1b[${y + 1};${x + 1}H`,
72
+ },
73
+
74
+ shape: {
75
+ steadyBlock : "\x1b[2 q",
76
+ steadyBar : "\x1b[6 q",
77
+ steadyUnderline : "\x1b[4 q",
78
+ blinkingBlock : "\x1b[1 q",
79
+ blinkingBar : "\x1b[5 q",
80
+ blinkingUnderline : "\x1b[3 q",
81
+ default : "\x1b[0 q",
82
+ },
83
+ },
84
+
85
+ /** @type {(lines: number) => string} */
86
+ scrollUp : lines => `\x1b[${lines}S`,
87
+ /** @type {(lines: number) => string} */
88
+ scrollDown : lines => `\x1b[${lines}T`,
89
+
90
+ /** @type {(text: string) => string} */
91
+ strip: text => text.replace(rxCSI, ''),
92
+
93
+ /** @type {(text: string, start: number, end: number) => string} */
94
+ slice(text, start, end) {
95
+ if (start < 0 || end < 0) {
96
+ const visibleLength = ansi.strip(text).length;
97
+ if (start < 0)
98
+ start = Math.max(0, visibleLength + start);
99
+ if (end < 0)
100
+ end = Math.max(0, visibleLength + end);
101
+ }
102
+ if (start > end)
103
+ return "";
104
+
105
+ let visibleIndex = 0;
106
+ let startIndex = 0;
107
+
108
+ while (startIndex < text.length && visibleIndex < start) {
109
+ if (text[startIndex] === "\x1b") {
110
+ rxCSI.lastIndex = startIndex;
111
+ const match = rxCSI.exec(text);
112
+ if (match?.index === startIndex) {
113
+ startIndex += match[0].length;
114
+ continue;
115
+ }
116
+ }
117
+
118
+ startIndex++;
119
+ visibleIndex++;
120
+ }
121
+
122
+ let endIndex = startIndex;
123
+ let hasAnsi = false;
124
+ while (endIndex < text.length && visibleIndex < end) {
125
+ if (text[endIndex] === "\x1b") {
126
+ rxCSI.lastIndex = endIndex;
127
+ const match = rxCSI.exec(text);
128
+ if (match?.index === endIndex) {
129
+ endIndex += match[0].length;
130
+ hasAnsi = true;
131
+ continue;
132
+ }
133
+ }
134
+
135
+ endIndex++;
136
+ visibleIndex++;
137
+ }
138
+
139
+ return text.slice(startIndex, endIndex) + (hasAnsi ? ansi.reset : "");
140
+ },
141
+ };
142
+
143
+ 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
- };