@peteanderson/ansi 0.1.0 → 0.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/dist/sgr.d.ts ADDED
@@ -0,0 +1,205 @@
1
+ export const reset: "\u001B[0m";
2
+ export namespace style {
3
+ let bold: ((s: string) => string) & {
4
+ open: string;
5
+ close: string;
6
+ };
7
+ let dim: ((s: string) => string) & {
8
+ open: string;
9
+ close: string;
10
+ };
11
+ let italic: ((s: string) => string) & {
12
+ open: string;
13
+ close: string;
14
+ };
15
+ let underline: ((s: string) => string) & {
16
+ open: string;
17
+ close: string;
18
+ };
19
+ let inverse: ((s: string) => string) & {
20
+ open: string;
21
+ close: string;
22
+ };
23
+ let hidden: ((s: string) => string) & {
24
+ open: string;
25
+ close: string;
26
+ };
27
+ let strikethrough: ((s: string) => string) & {
28
+ open: string;
29
+ close: string;
30
+ };
31
+ let doubleUnderline: ((s: string) => string) & {
32
+ open: string;
33
+ close: string;
34
+ };
35
+ let frame: ((s: string) => string) & {
36
+ open: string;
37
+ close: string;
38
+ };
39
+ let encircle: ((s: string) => string) & {
40
+ open: string;
41
+ close: string;
42
+ };
43
+ let overline: ((s: string) => string) & {
44
+ open: string;
45
+ close: string;
46
+ };
47
+ }
48
+ export namespace fg {
49
+ export let black: ((s: string) => string) & {
50
+ open: string;
51
+ close: string;
52
+ };
53
+ export let red: ((s: string) => string) & {
54
+ open: string;
55
+ close: string;
56
+ };
57
+ export let green: ((s: string) => string) & {
58
+ open: string;
59
+ close: string;
60
+ };
61
+ export let yellow: ((s: string) => string) & {
62
+ open: string;
63
+ close: string;
64
+ };
65
+ export let blue: ((s: string) => string) & {
66
+ open: string;
67
+ close: string;
68
+ };
69
+ export let magenta: ((s: string) => string) & {
70
+ open: string;
71
+ close: string;
72
+ };
73
+ export let cyan: ((s: string) => string) & {
74
+ open: string;
75
+ close: string;
76
+ };
77
+ export let white: ((s: string) => string) & {
78
+ open: string;
79
+ close: string;
80
+ };
81
+ export let brightBlack: ((s: string) => string) & {
82
+ open: string;
83
+ close: string;
84
+ };
85
+ export let brightRed: ((s: string) => string) & {
86
+ open: string;
87
+ close: string;
88
+ };
89
+ export let brightGreen: ((s: string) => string) & {
90
+ open: string;
91
+ close: string;
92
+ };
93
+ export let brightYellow: ((s: string) => string) & {
94
+ open: string;
95
+ close: string;
96
+ };
97
+ export let brightBlue: ((s: string) => string) & {
98
+ open: string;
99
+ close: string;
100
+ };
101
+ export let brightMagenta: ((s: string) => string) & {
102
+ open: string;
103
+ close: string;
104
+ };
105
+ export let brightCyan: ((s: string) => string) & {
106
+ open: string;
107
+ close: string;
108
+ };
109
+ export let brightWhite: ((s: string) => string) & {
110
+ open: string;
111
+ close: string;
112
+ };
113
+ let _default: string;
114
+ export { _default as default };
115
+ export let rgb: (r: number, g: number, b: number) => (text: string) => string;
116
+ export let x256: (r: number, g: number, b: number) => (text: string) => string;
117
+ }
118
+ export namespace bg {
119
+ let black_1: ((s: string) => string) & {
120
+ open: string;
121
+ close: string;
122
+ };
123
+ export { black_1 as black };
124
+ let red_1: ((s: string) => string) & {
125
+ open: string;
126
+ close: string;
127
+ };
128
+ export { red_1 as red };
129
+ let green_1: ((s: string) => string) & {
130
+ open: string;
131
+ close: string;
132
+ };
133
+ export { green_1 as green };
134
+ let yellow_1: ((s: string) => string) & {
135
+ open: string;
136
+ close: string;
137
+ };
138
+ export { yellow_1 as yellow };
139
+ let blue_1: ((s: string) => string) & {
140
+ open: string;
141
+ close: string;
142
+ };
143
+ export { blue_1 as blue };
144
+ let magenta_1: ((s: string) => string) & {
145
+ open: string;
146
+ close: string;
147
+ };
148
+ export { magenta_1 as magenta };
149
+ let cyan_1: ((s: string) => string) & {
150
+ open: string;
151
+ close: string;
152
+ };
153
+ export { cyan_1 as cyan };
154
+ let white_1: ((s: string) => string) & {
155
+ open: string;
156
+ close: string;
157
+ };
158
+ export { white_1 as white };
159
+ let brightBlack_1: ((s: string) => string) & {
160
+ open: string;
161
+ close: string;
162
+ };
163
+ export { brightBlack_1 as brightBlack };
164
+ let brightRed_1: ((s: string) => string) & {
165
+ open: string;
166
+ close: string;
167
+ };
168
+ export { brightRed_1 as brightRed };
169
+ let brightGreen_1: ((s: string) => string) & {
170
+ open: string;
171
+ close: string;
172
+ };
173
+ export { brightGreen_1 as brightGreen };
174
+ let brightYellow_1: ((s: string) => string) & {
175
+ open: string;
176
+ close: string;
177
+ };
178
+ export { brightYellow_1 as brightYellow };
179
+ let brightBlue_1: ((s: string) => string) & {
180
+ open: string;
181
+ close: string;
182
+ };
183
+ export { brightBlue_1 as brightBlue };
184
+ let brightMagenta_1: ((s: string) => string) & {
185
+ open: string;
186
+ close: string;
187
+ };
188
+ export { brightMagenta_1 as brightMagenta };
189
+ let brightCyan_1: ((s: string) => string) & {
190
+ open: string;
191
+ close: string;
192
+ };
193
+ export { brightCyan_1 as brightCyan };
194
+ let brightWhite_1: ((s: string) => string) & {
195
+ open: string;
196
+ close: string;
197
+ };
198
+ export { brightWhite_1 as brightWhite };
199
+ let _default_1: string;
200
+ export { _default_1 as default };
201
+ let rgb_1: (r: number, g: number, b: number) => (text: string) => string;
202
+ export { rgb_1 as rgb };
203
+ let x256_1: (r: number, g: number, b: number) => (text: string) => string;
204
+ export { x256_1 as x256 };
205
+ }
package/dist/sgr.js ADDED
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ /**
3
+ * @param {number | string} open
4
+ * @param {number | string} close
5
+ */
6
+ function sgr(open, close) {
7
+ const openSequence = `\x1b[${open}m`;
8
+ const closeSequence = `\x1b[${close}m`;
9
+ return Object.assign(
10
+ /** @type {(text: string) => string} */
11
+ s => openSequence +
12
+ s.replaceAll(openSequence, '').replaceAll(closeSequence, openSequence) +
13
+ closeSequence, { open: openSequence, close: closeSequence });
14
+ }
15
+ /** @type {(r: number, g: number, b: number, open: number, close: number) => (text: string) => string} */
16
+ const rgb = (r, g, b, open, close) => sgr(`${open};2;${r};${g};${b}`, close);
17
+ /** @type {(r: number, g: number, b: number, open: number, close: number) => (text: string) => string} */
18
+ function x256(r, g, b, open, close) {
19
+ if (r === g && g === b) {
20
+ // grayscale
21
+ return sgr(`${open};5;${232 + Math.floor(r * 24 / 256)}`, close);
22
+ }
23
+ // 6x6x6 color cube
24
+ return sgr(`${open};5;${16 +
25
+ Math.floor(r * 6 / 256) * 36 +
26
+ Math.floor(g * 6 / 256) * 6 +
27
+ Math.floor(b * 6 / 256)}`, close);
28
+ }
29
+ const reset = "\x1b[0m";
30
+ const style = {
31
+ bold: sgr(1, 22),
32
+ dim: sgr(2, 22),
33
+ italic: sgr(3, 23),
34
+ underline: sgr(4, 24),
35
+ inverse: sgr(7, 27),
36
+ hidden: sgr(8, 28),
37
+ strikethrough: sgr(9, 29),
38
+ doubleUnderline: sgr(21, 24),
39
+ frame: sgr(51, 54),
40
+ encircle: sgr(52, 54),
41
+ overline: sgr(53, 55),
42
+ };
43
+ const fg = {
44
+ black: sgr(30, 39),
45
+ red: sgr(31, 39),
46
+ green: sgr(32, 39),
47
+ yellow: sgr(33, 39),
48
+ blue: sgr(34, 39),
49
+ magenta: sgr(35, 39),
50
+ cyan: sgr(36, 39),
51
+ white: sgr(37, 39),
52
+ brightBlack: sgr(90, 39),
53
+ brightRed: sgr(91, 39),
54
+ brightGreen: sgr(92, 39),
55
+ brightYellow: sgr(93, 39),
56
+ brightBlue: sgr(94, 39),
57
+ brightMagenta: sgr(95, 39),
58
+ brightCyan: sgr(96, 39),
59
+ brightWhite: sgr(97, 39),
60
+ default: "\x1b[39m",
61
+ /** @type {(r: number, g: number, b: number) => (text: string) => string} */
62
+ rgb: (r, g, b) => rgb(r, g, b, 38, 39),
63
+ /** @type {(r: number, g: number, b: number) => (text: string) => string} */
64
+ x256: (r, g, b) => x256(r, g, b, 38, 39),
65
+ };
66
+ const bg = {
67
+ black: sgr(40, 49),
68
+ red: sgr(41, 49),
69
+ green: sgr(42, 49),
70
+ yellow: sgr(43, 49),
71
+ blue: sgr(44, 49),
72
+ magenta: sgr(45, 49),
73
+ cyan: sgr(46, 49),
74
+ white: sgr(47, 49),
75
+ brightBlack: sgr(100, 49),
76
+ brightRed: sgr(101, 49),
77
+ brightGreen: sgr(102, 49),
78
+ brightYellow: sgr(103, 49),
79
+ brightBlue: sgr(104, 49),
80
+ brightMagenta: sgr(105, 49),
81
+ brightCyan: sgr(106, 49),
82
+ brightWhite: sgr(107, 49),
83
+ default: "\x1b[49m",
84
+ /** @type {(r: number, g: number, b: number) => (text: string) => string} */
85
+ rgb: (r, g, b) => rgb(r, g, b, 48, 49),
86
+ /** @type {(r: number, g: number, b: number) => (text: string) => string} */
87
+ x256: (r, g, b) => x256(r, g, b, 48, 49),
88
+ };
89
+ module.exports = { reset, style, fg, bg };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Remove all ANSI escape codes from the given text.
3
+ * @type {(text: string) => string}
4
+ */
5
+ export const strip: (text: string) => string;
6
+ /**
7
+ * Remove all ANSI escape codes except SGR (m) codes, which are used for color and styling.
8
+ * @type {(text: string) => string}
9
+ */
10
+ export const sanitize: (text: string) => string;
11
+ export function slice(text: string, start: number, end: number): string;
package/dist/strip.js ADDED
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ const { reset } = require("./sgr");
3
+ const rxCSI = /\x1b\[[0-?]*[ -/]*[@-~]/g;
4
+ const rxUnsafe = /\x1b\[[0-?]*[ -/]*[@-ln-~]/g; // Everything except SGR (m) codes.
5
+ /**
6
+ * Remove all ANSI escape codes from the given text.
7
+ * @type {(text: string) => string}
8
+ */
9
+ const strip = text => text.replace(rxCSI, '');
10
+ /**
11
+ * Remove all ANSI escape codes except SGR (m) codes, which are used for color and styling.
12
+ * @type {(text: string) => string}
13
+ */
14
+ const sanitize = text => text.replace(rxUnsafe, '');
15
+ /** @type {(text: string, start: number, end: number) => string} */
16
+ function slice(text, start, end) {
17
+ if (start < 0 || end < 0) {
18
+ const visibleLength = strip(text).length;
19
+ if (start < 0)
20
+ start = Math.max(0, visibleLength + start);
21
+ if (end < 0)
22
+ end = Math.max(0, visibleLength + end);
23
+ }
24
+ if (start > end)
25
+ return "";
26
+ let visibleIndex = 0;
27
+ let startIndex = 0;
28
+ while (startIndex < text.length && visibleIndex < start) {
29
+ if (text[startIndex] === "\x1b") {
30
+ rxCSI.lastIndex = startIndex;
31
+ const match = rxCSI.exec(text);
32
+ if (match?.index === startIndex) {
33
+ startIndex += match[0].length;
34
+ continue;
35
+ }
36
+ }
37
+ startIndex++;
38
+ visibleIndex++;
39
+ }
40
+ let endIndex = startIndex;
41
+ let hasAnsi = false;
42
+ while (endIndex < text.length && visibleIndex < end) {
43
+ if (text[endIndex] === "\x1b") {
44
+ rxCSI.lastIndex = endIndex;
45
+ const match = rxCSI.exec(text);
46
+ if (match?.index === endIndex) {
47
+ endIndex += match[0].length;
48
+ hasAnsi = true;
49
+ continue;
50
+ }
51
+ }
52
+ endIndex++;
53
+ visibleIndex++;
54
+ }
55
+ return text.slice(startIndex, endIndex) + (hasAnsi ? reset : "");
56
+ }
57
+ ;
58
+ module.exports = { strip, sanitize, slice };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name" : "@peteanderson/ansi",
3
- "version" : "0.1.0",
3
+ "version" : "0.1.2",
4
4
  "description" : "ANSI escape sequence generation with automatic feature detection",
5
5
  "author" : {
6
6
  "name" : "Pete Anderson",
package/src/ansi.js CHANGED
@@ -1,89 +1,28 @@
1
1
  "use strict";
2
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
- }
3
+ const {colorDepth, enabled} = require("./caps");
4
+ const caret = require("./caret");
5
+ const erase = require("./erase");
6
+ const scroll = require("./scroll");
7
+ const {bg, fg, reset, style} = require("./sgr");
8
+ const {sanitize, slice, strip} = require("./strip");
17
9
 
18
10
  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",
11
+ colorDepth,
12
+ enabled,
65
13
 
66
- position: {
67
- get: "\x1b[6n",
68
- /** @type {(x: number, y: number) => string} */
69
- set: (x, y) => `\x1b[${y + 1};${x + 1}H`,
70
- },
14
+ caret,
15
+ erase,
16
+ scroll,
71
17
 
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
- },
18
+ reset,
19
+ style,
20
+ fg,
21
+ bg,
82
22
 
83
- /** @type {(lines: number) => string} */
84
- scrollUp : lines => `\x1b[${lines}S`,
85
- /** @type {(lines: number) => string} */
86
- scrollDown : lines => `\x1b[${lines}T`,
23
+ strip,
24
+ sanitize,
25
+ slice,
87
26
  };
88
27
 
89
28
  module.exports = ansi;
package/src/caps.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ /** @type {(stream: NodeJS.WriteStream) => number} */
4
+ const getColorDepth = (stream) =>
5
+ stream.isTTY ?
6
+ stream.getColorDepth() :
7
+ process.env.FORCE_COLOR !== undefined ?
8
+ ["1", "true", ""].includes(process.env.FORCE_COLOR) ?
9
+ 4 :
10
+ process.env.FORCE_COLOR === "2" ?
11
+ 8 :
12
+ process.env.FORCE_COLOR === "3" ?
13
+ 24 :
14
+ 1 :
15
+ 1;
16
+
17
+ const colorDepth = {
18
+ stdout: getColorDepth(process.stdout),
19
+ stderr: getColorDepth(process.stderr),
20
+ };
21
+
22
+ const enabled = {
23
+ stdout: colorDepth.stdout > 1,
24
+ stderr: colorDepth.stderr > 1,
25
+ };
26
+
27
+ module.exports = {colorDepth, enabled};
package/src/caret.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+
3
+ /** @type {(code: string) => (count?: number) => string} */
4
+ const move = code =>
5
+ count => `\x1b[${count === undefined ? "" : count ? count : "?"}${code}`;
6
+
7
+ const caret = {
8
+ up : move("A"),
9
+ down : move("B"),
10
+ forward : move("C"),
11
+ backward : move("D"),
12
+ nextLine : move("E"),
13
+ prevLine : move("F"),
14
+
15
+ /** @type {(x: number) => string} */
16
+ x: x => `\x1b[${x}G`,
17
+
18
+ // These are the old VT100 codes, but they're widely supported by old and new terminals. The
19
+ // newer VT220 codes are standardized by ECMA and ISO, and are also widely supported, but might
20
+ // not work in some older terminals. The VT220 codes are:
21
+ //
22
+ // ```
23
+ // save : "\x1b[s",
24
+ // restore : "\x1b[u",
25
+ // ```
26
+ save : "\x1b7",
27
+ restore : "\x1b8",
28
+
29
+ hide : "\x1b[?25l",
30
+ show : "\x1b[?25h",
31
+
32
+ position: {
33
+ get: "\x1b[6n",
34
+ /** @type {(x: number, y: number) => string} */
35
+ set: (x, y) => `\x1b[${y};${x}H`,
36
+ },
37
+
38
+ shape: {
39
+ steadyBlock : "\x1b[2 q",
40
+ steadyBar : "\x1b[6 q",
41
+ steadyUnderline : "\x1b[4 q",
42
+ blinkingBlock : "\x1b[1 q",
43
+ blinkingBar : "\x1b[5 q",
44
+ blinkingUnderline : "\x1b[3 q",
45
+ default : "\x1b[0 q",
46
+ },
47
+ };
48
+
49
+ module.exports = caret;
package/src/erase.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ const erase = {
4
+ line: {
5
+ toStart: "\x1b[1K",
6
+ toEnd : "\x1b[0K",
7
+ full : "\x1b[2K",
8
+ },
9
+ screen: {
10
+ toStart : "\x1b[1J",
11
+ toEnd : "\x1b[0J",
12
+ full : "\x1b[2J",
13
+ scrollback : "\x1b[3J",
14
+ },
15
+ };
16
+
17
+ module.exports = erase;
package/src/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require("./ansi");
package/src/scroll.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ const scroll = {
4
+ /** @type {(lines?: number) => string} */
5
+ up : lines => `\x1b[${lines ?? 1}S`,
6
+ /** @type {(lines?: number) => string} */
7
+ down : lines => `\x1b[${lines ?? 1}T`,
8
+ };
9
+
10
+ module.exports = scroll;