@peteanderson/ansi 0.1.1 → 0.2.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/src/sgr.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * @param {number | string} open
5
+ * @param {number | string} close
6
+ */
7
+ function sgr(open, close) {
8
+ const openSequence = `\x1b[${open}m`;
9
+ const closeSequence = `\x1b[${close}m`;
10
+
11
+ return Object.assign(
12
+ /** @type {(text: string) => string} */
13
+ s =>
14
+ openSequence +
15
+ s.replaceAll(openSequence, '').replaceAll(closeSequence, openSequence) +
16
+ closeSequence,
17
+ {open: openSequence, close: closeSequence}
18
+ );
19
+ }
20
+
21
+ /** @type {(r: number, g: number, b: number, open: number, close: number) => (text: string) => string} */
22
+ const rgb = (r, g, b, open, close) => sgr(`${open};2;${r};${g};${b}`, close);
23
+
24
+ /** @type {(r: number, g: number, b: number, open: number, close: number) => (text: string) => string} */
25
+ function x256(r, g, b, open, close) {
26
+ if (r === g && g === b) {
27
+ // grayscale
28
+ return sgr(`${open};5;${232 + Math.floor(r * 24 / 256)}`, close);
29
+ }
30
+
31
+ // 6x6x6 color cube
32
+ return sgr(
33
+ `${open};5;${
34
+ 16 +
35
+ Math.floor(r * 6 / 256) * 36 +
36
+ Math.floor(g * 6 / 256) * 6 +
37
+ Math.floor(b * 6 / 256)
38
+ }`,
39
+ close,
40
+ );
41
+ }
42
+
43
+ const reset = "\x1b[0m";
44
+
45
+ const style = {
46
+ bold : sgr(1, 22),
47
+ dim : sgr(2, 22),
48
+ italic : sgr(3, 23),
49
+ underline : sgr(4, 24),
50
+ inverse : sgr(7, 27),
51
+ hidden : sgr(8, 28),
52
+ strikethrough : sgr(9, 29),
53
+ doubleUnderline : sgr(21, 24),
54
+ frame : sgr(51, 54),
55
+ encircle : sgr(52, 54),
56
+ overline : sgr(53, 55),
57
+ };
58
+
59
+ const fg = {
60
+ black : sgr(30, 39),
61
+ red : sgr(31, 39),
62
+ green : sgr(32, 39),
63
+ yellow : sgr(33, 39),
64
+ blue : sgr(34, 39),
65
+ magenta : sgr(35, 39),
66
+ cyan : sgr(36, 39),
67
+ white : sgr(37, 39),
68
+
69
+ brightBlack : sgr(90, 39),
70
+ brightRed : sgr(91, 39),
71
+ brightGreen : sgr(92, 39),
72
+ brightYellow : sgr(93, 39),
73
+ brightBlue : sgr(94, 39),
74
+ brightMagenta : sgr(95, 39),
75
+ brightCyan : sgr(96, 39),
76
+ brightWhite : sgr(97, 39),
77
+
78
+ default : "\x1b[39m",
79
+
80
+ /** @type {(r: number, g: number, b: number) => (text: string) => string} */
81
+ rgb : (r, g, b) => rgb(r, g, b, 38, 39),
82
+ /** @type {(r: number, g: number, b: number) => (text: string) => string} */
83
+ x256 : (r, g, b) => x256(r, g, b, 38, 39),
84
+ };
85
+
86
+ const bg = {
87
+ black : sgr(40, 49),
88
+ red : sgr(41, 49),
89
+ green : sgr(42, 49),
90
+ yellow : sgr(43, 49),
91
+ blue : sgr(44, 49),
92
+ magenta : sgr(45, 49),
93
+ cyan : sgr(46, 49),
94
+ white : sgr(47, 49),
95
+
96
+ brightBlack : sgr(100, 49),
97
+ brightRed : sgr(101, 49),
98
+ brightGreen : sgr(102, 49),
99
+ brightYellow : sgr(103, 49),
100
+ brightBlue : sgr(104, 49),
101
+ brightMagenta : sgr(105, 49),
102
+ brightCyan : sgr(106, 49),
103
+ brightWhite : sgr(107, 49),
104
+
105
+ default : "\x1b[49m",
106
+
107
+ /** @type {(r: number, g: number, b: number) => (text: string) => string} */
108
+ rgb : (r, g, b) => rgb(r, g, b, 48, 49),
109
+ /** @type {(r: number, g: number, b: number) => (text: string) => string} */
110
+ x256 : (r, g, b) => x256(r, g, b, 48, 49),
111
+ };
112
+
113
+ module.exports = {reset, style, fg, bg};
package/src/strip.js ADDED
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ const {reset} = require("./sgr");
4
+
5
+ const rxCSI = /\x1b\[[0-?]*[ -/]*[@-~]/g;
6
+ const rxUnsafe = /\x1b\[[0-?]*[ -/]*[@-ln-~]/g; // Everything except SGR (m) codes.
7
+
8
+ /**
9
+ * Remove all ANSI escape codes from the given text.
10
+ * @type {(text: string) => string}
11
+ */
12
+ const strip = text => text.replace(rxCSI, '');
13
+
14
+ /**
15
+ * Remove all ANSI escape codes except SGR (m) codes, which are used for color and styling.
16
+ * @type {(text: string) => string}
17
+ */
18
+ const sanitize = text => text.replace(rxUnsafe, '');
19
+
20
+ /** @type {(text: string, start: number, end: number) => string} */
21
+ function slice(text, start, end) {
22
+ if (start < 0 || end < 0) {
23
+ const visibleLength = strip(text).length;
24
+ if (start < 0)
25
+ start = Math.max(0, visibleLength + start);
26
+ if (end < 0)
27
+ end = Math.max(0, visibleLength + end);
28
+ }
29
+ if (start > end)
30
+ return "";
31
+
32
+ let visibleIndex = 0;
33
+ let startIndex = 0;
34
+
35
+ while (startIndex < text.length && visibleIndex < start) {
36
+ if (text[startIndex] === "\x1b") {
37
+ rxCSI.lastIndex = startIndex;
38
+ const match = rxCSI.exec(text);
39
+ if (match?.index === startIndex) {
40
+ startIndex += match[0].length;
41
+ continue;
42
+ }
43
+ }
44
+
45
+ startIndex++;
46
+ visibleIndex++;
47
+ }
48
+
49
+ let endIndex = startIndex;
50
+ let hasAnsi = false;
51
+ while (endIndex < text.length && visibleIndex < end) {
52
+ if (text[endIndex] === "\x1b") {
53
+ rxCSI.lastIndex = endIndex;
54
+ const match = rxCSI.exec(text);
55
+ if (match?.index === endIndex) {
56
+ endIndex += match[0].length;
57
+ hasAnsi = true;
58
+ continue;
59
+ }
60
+ }
61
+
62
+ endIndex++;
63
+ visibleIndex++;
64
+ }
65
+
66
+ return text.slice(startIndex, endIndex) + (hasAnsi ? reset : "");
67
+ };
68
+
69
+ module.exports = {strip, sanitize, slice};