@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/README.md CHANGED
@@ -13,38 +13,63 @@ 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" + 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);
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(ansi.strip("styled text")); // remove all ANSI codes
21
+ console.log(ansi.slice("styled text", 0, 5)); // slice by visible length
20
22
  ```
21
23
 
22
24
  ## API
23
25
 
24
26
  ### `fg` — foreground colors
25
27
 
26
- Raw ANSI open sequences: `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `default`
28
+ Functions that wrap text with color codes:
29
+
30
+ - Basic colors: `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `default`
31
+ - Bright colors: `brightBlack` `brightRed` `brightGreen` `brightYellow` `brightBlue` `brightMagenta`
32
+ `brightCyan` `brightWhite`
33
+ - `fg.rgb(r, g, b)(text)` - 24-bit RGB color (r, g, b in 0–5 or grayscale 232–255)
34
+ - `fg.x256(code)(text)` - 256-color palette
35
+
36
+ All return functions with `open` and `close` properties for raw sequences.
27
37
 
28
38
  ### `bg` — background colors
29
39
 
30
- Raw ANSI open sequences: `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `default`
40
+ Functions that wrap text with color codes:
41
+
42
+ - Basic colors: `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `default`
43
+ - Bright colors: `brightBlack` `brightRed` `brightGreen` `brightYellow` `brightBlue` `brightMagenta`
44
+ `brightCyan` `brightWhite`
45
+ - `bg.rgb(r, g, b)(text)` - 24-bit RGB color
46
+ - `bg.x256(code)(text)` - 256-color palette
31
47
 
32
48
  ### `style`
33
49
 
34
- `underline` `reverse` (raw sequences)
50
+ Functions that wrap text with style codes:
51
+
52
+ `bold` `dim` `italic` `underline` `inverse` `hidden` `strikethrough` `doubleUnderline` `framed`
53
+ `encircled` `overline`
54
+
55
+ All return functions with `open` and `close` properties for raw sequences.
35
56
 
36
57
  ### `caret`
37
58
 
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
59
+ - `caret.show` / `caret.hide` - show or hide the cursor
60
+ - `caret.position.get` - get the current cursor position (terminal sends position to stdin)
61
+ - `caret.position.set(row, col)` - set cursor position (1-based indexing)
62
+ - `caret.shape.block` / `caret.shape.underline` / `caret.shape.bar` - change cursor shape
63
+ - `caret.up(n)` / `caret.down(n)` / `caret.forward(n)` / `caret.backward(n)` - move cursor
64
+ - `caret.nextLine(n)` / `caret.prevLine(n)` - move cursor to next/previous line
65
+ - `caret.x(col)` - move cursor to column (1-based)
66
+ - `caret.save` / `caret.restore` - save/restore cursor position (VT100)
42
67
 
43
68
  ### `erase`
44
69
 
45
- Raw sequences:
46
- - `erase.line.toStart` / `erase.line.toEnd` / `erase.line.full`
47
- - `erase.screen`
70
+ - `erase.line.toStart` / `erase.line.toEnd` / `erase.line.full` - erase line
71
+ - `erase.screen.toStart` / `erase.screen.toEnd` / `erase.screen.full` - erase screen
72
+ - `erase.screen.scrollback` - erase screen and scrollback buffer
48
73
 
49
74
  ### `scroll`
50
75
 
@@ -53,7 +78,16 @@ Raw sequences:
53
78
 
54
79
  ### `strip(text)`
55
80
 
56
- Removes ANSI CSI sequences from a string.
81
+ Removes all ANSI CSI sequences from a string.
82
+
83
+ ### `slice(text, start, end)`
84
+
85
+ Slices a string by visible characters, ignoring ANSI sequences. Works like `String.prototype.slice`
86
+ but counts only visible characters.
87
+
88
+ ### `sanitize(text)`
89
+
90
+ Removes "unsafe" CSI sequences (non-SGR), leaving only color and style codes.
57
91
 
58
92
  ## License
59
93
 
package/dist/ansi.d.ts CHANGED
@@ -1,64 +1,13 @@
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;
1
+ import { colorDepth } from "./caps";
2
+ import { enabled } from "./caps";
3
+ import caret = require("./caret");
4
+ import erase = require("./erase");
5
+ import scroll = require("./scroll");
6
+ import { reset } from "./sgr";
7
+ import { style } from "./sgr";
8
+ import { fg } from "./sgr";
9
+ import { bg } from "./sgr";
10
+ import { strip } from "./strip";
11
+ import { sanitize } from "./strip";
12
+ import { slice } from "./strip";
13
+ export { colorDepth, enabled, caret, erase, scroll, reset, style, fg, bg, strip, sanitize, slice };
package/dist/ansi.js CHANGED
@@ -1,75 +1,22 @@
1
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
- }
2
+ const { colorDepth, enabled } = require("./caps");
3
+ const caret = require("./caret");
4
+ const erase = require("./erase");
5
+ const scroll = require("./scroll");
6
+ const { bg, fg, reset, style } = require("./sgr");
7
+ const { sanitize, slice, strip } = require("./strip");
16
8
  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`,
9
+ colorDepth,
10
+ enabled,
11
+ caret,
12
+ erase,
13
+ scroll,
14
+ reset,
15
+ style,
16
+ fg,
17
+ bg,
18
+ strip,
19
+ sanitize,
20
+ slice,
74
21
  };
75
22
  module.exports = ansi;
package/dist/caps.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export namespace colorDepth {
2
+ let stdout: number;
3
+ let stderr: number;
4
+ }
5
+ export namespace enabled {
6
+ let stdout_1: boolean;
7
+ export { stdout_1 as stdout };
8
+ let stderr_1: boolean;
9
+ export { stderr_1 as stderr };
10
+ }
package/dist/caps.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ /** @type {(stream: NodeJS.WriteStream) => number} */
3
+ const getColorDepth = (stream) => stream.isTTY ?
4
+ stream.getColorDepth() :
5
+ process.env.FORCE_COLOR !== undefined ?
6
+ ["1", "true", ""].includes(process.env.FORCE_COLOR) ?
7
+ 4 :
8
+ process.env.FORCE_COLOR === "2" ?
9
+ 8 :
10
+ process.env.FORCE_COLOR === "3" ?
11
+ 24 :
12
+ 1 :
13
+ 1;
14
+ const colorDepth = {
15
+ stdout: getColorDepth(process.stdout),
16
+ stderr: getColorDepth(process.stderr),
17
+ };
18
+ const enabled = {
19
+ stdout: colorDepth.stdout > 1,
20
+ stderr: colorDepth.stderr > 1,
21
+ };
22
+ module.exports = { colorDepth, enabled };
@@ -0,0 +1,25 @@
1
+ export let up: (count?: number) => string;
2
+ export let down: (count?: number) => string;
3
+ export let forward: (count?: number) => string;
4
+ export let backward: (count?: number) => string;
5
+ export let nextLine: (count?: number) => string;
6
+ export let prevLine: (count?: number) => string;
7
+ export let x: (x: number) => string;
8
+ export let save: string;
9
+ export let restore: string;
10
+ export let hide: string;
11
+ export let show: string;
12
+ export namespace position {
13
+ let get: string;
14
+ let set: (x: number, y: number) => string;
15
+ }
16
+ export namespace shape {
17
+ export let steadyBlock: string;
18
+ export let steadyBar: string;
19
+ export let steadyUnderline: string;
20
+ export let blinkingBlock: string;
21
+ export let blinkingBar: string;
22
+ export let blinkingUnderline: string;
23
+ let _default: string;
24
+ export { _default as default };
25
+ }
package/dist/caret.js ADDED
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ /** @type {(code: string) => (count?: number) => string} */
3
+ const move = code => count => `\x1b[${count === undefined ? "" : count ? count : "?"}${code}`;
4
+ const caret = {
5
+ up: move("A"),
6
+ down: move("B"),
7
+ forward: move("C"),
8
+ backward: move("D"),
9
+ nextLine: move("E"),
10
+ prevLine: move("F"),
11
+ /** @type {(x: number) => string} */
12
+ x: x => `\x1b[${x}G`,
13
+ // These are the old VT100 codes, but they're widely supported by old and new terminals. The
14
+ // newer VT220 codes are standardized by ECMA and ISO, and are also widely supported, but might
15
+ // not work in some older terminals. The VT220 codes are:
16
+ //
17
+ // ```
18
+ // save : "\x1b[s",
19
+ // restore : "\x1b[u",
20
+ // ```
21
+ save: "\x1b7",
22
+ restore: "\x1b8",
23
+ hide: "\x1b[?25l",
24
+ show: "\x1b[?25h",
25
+ position: {
26
+ get: "\x1b[6n",
27
+ /** @type {(x: number, y: number) => string} */
28
+ set: (x, y) => `\x1b[${y};${x}H`,
29
+ },
30
+ shape: {
31
+ steadyBlock: "\x1b[2 q",
32
+ steadyBar: "\x1b[6 q",
33
+ steadyUnderline: "\x1b[4 q",
34
+ blinkingBlock: "\x1b[1 q",
35
+ blinkingBar: "\x1b[5 q",
36
+ blinkingUnderline: "\x1b[3 q",
37
+ default: "\x1b[0 q",
38
+ },
39
+ };
40
+ module.exports = caret;
@@ -0,0 +1,14 @@
1
+ export namespace line {
2
+ let toStart: string;
3
+ let toEnd: string;
4
+ let full: string;
5
+ }
6
+ export namespace screen {
7
+ let toStart_1: string;
8
+ export { toStart_1 as toStart };
9
+ let toEnd_1: string;
10
+ export { toEnd_1 as toEnd };
11
+ let full_1: string;
12
+ export { full_1 as full };
13
+ export let scrollback: string;
14
+ }
package/dist/erase.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ const erase = {
3
+ line: {
4
+ toStart: "\x1b[1K",
5
+ toEnd: "\x1b[0K",
6
+ full: "\x1b[2K",
7
+ },
8
+ screen: {
9
+ toStart: "\x1b[1J",
10
+ toEnd: "\x1b[0J",
11
+ full: "\x1b[2J",
12
+ scrollback: "\x1b[3J",
13
+ },
14
+ };
15
+ module.exports = erase;
@@ -0,0 +1,242 @@
1
+ declare const _exports: {
2
+ colorDepth: {
3
+ stdout: number;
4
+ stderr: number;
5
+ };
6
+ enabled: {
7
+ stdout: boolean;
8
+ stderr: boolean;
9
+ };
10
+ caret: {
11
+ up: (count?: number) => string;
12
+ down: (count?: number) => string;
13
+ forward: (count?: number) => string;
14
+ backward: (count?: number) => string;
15
+ nextLine: (count?: number) => string;
16
+ prevLine: (count?: number) => string;
17
+ x: (x: number) => string;
18
+ save: string;
19
+ restore: string;
20
+ hide: string;
21
+ show: string;
22
+ position: {
23
+ get: string;
24
+ set: (x: number, y: number) => string;
25
+ };
26
+ shape: {
27
+ steadyBlock: string;
28
+ steadyBar: string;
29
+ steadyUnderline: string;
30
+ blinkingBlock: string;
31
+ blinkingBar: string;
32
+ blinkingUnderline: string;
33
+ default: string;
34
+ };
35
+ };
36
+ erase: {
37
+ line: {
38
+ toStart: string;
39
+ toEnd: string;
40
+ full: string;
41
+ };
42
+ screen: {
43
+ toStart: string;
44
+ toEnd: string;
45
+ full: string;
46
+ scrollback: string;
47
+ };
48
+ };
49
+ scroll: {
50
+ up: (lines?: number) => string;
51
+ down: (lines?: number) => string;
52
+ };
53
+ reset: string;
54
+ style: {
55
+ bold: ((s: string) => string) & {
56
+ open: string;
57
+ close: string;
58
+ };
59
+ dim: ((s: string) => string) & {
60
+ open: string;
61
+ close: string;
62
+ };
63
+ italic: ((s: string) => string) & {
64
+ open: string;
65
+ close: string;
66
+ };
67
+ underline: ((s: string) => string) & {
68
+ open: string;
69
+ close: string;
70
+ };
71
+ inverse: ((s: string) => string) & {
72
+ open: string;
73
+ close: string;
74
+ };
75
+ hidden: ((s: string) => string) & {
76
+ open: string;
77
+ close: string;
78
+ };
79
+ strikethrough: ((s: string) => string) & {
80
+ open: string;
81
+ close: string;
82
+ };
83
+ doubleUnderline: ((s: string) => string) & {
84
+ open: string;
85
+ close: string;
86
+ };
87
+ frame: ((s: string) => string) & {
88
+ open: string;
89
+ close: string;
90
+ };
91
+ encircle: ((s: string) => string) & {
92
+ open: string;
93
+ close: string;
94
+ };
95
+ overline: ((s: string) => string) & {
96
+ open: string;
97
+ close: string;
98
+ };
99
+ };
100
+ fg: {
101
+ black: ((s: string) => string) & {
102
+ open: string;
103
+ close: string;
104
+ };
105
+ red: ((s: string) => string) & {
106
+ open: string;
107
+ close: string;
108
+ };
109
+ green: ((s: string) => string) & {
110
+ open: string;
111
+ close: string;
112
+ };
113
+ yellow: ((s: string) => string) & {
114
+ open: string;
115
+ close: string;
116
+ };
117
+ blue: ((s: string) => string) & {
118
+ open: string;
119
+ close: string;
120
+ };
121
+ magenta: ((s: string) => string) & {
122
+ open: string;
123
+ close: string;
124
+ };
125
+ cyan: ((s: string) => string) & {
126
+ open: string;
127
+ close: string;
128
+ };
129
+ white: ((s: string) => string) & {
130
+ open: string;
131
+ close: string;
132
+ };
133
+ brightBlack: ((s: string) => string) & {
134
+ open: string;
135
+ close: string;
136
+ };
137
+ brightRed: ((s: string) => string) & {
138
+ open: string;
139
+ close: string;
140
+ };
141
+ brightGreen: ((s: string) => string) & {
142
+ open: string;
143
+ close: string;
144
+ };
145
+ brightYellow: ((s: string) => string) & {
146
+ open: string;
147
+ close: string;
148
+ };
149
+ brightBlue: ((s: string) => string) & {
150
+ open: string;
151
+ close: string;
152
+ };
153
+ brightMagenta: ((s: string) => string) & {
154
+ open: string;
155
+ close: string;
156
+ };
157
+ brightCyan: ((s: string) => string) & {
158
+ open: string;
159
+ close: string;
160
+ };
161
+ brightWhite: ((s: string) => string) & {
162
+ open: string;
163
+ close: string;
164
+ };
165
+ default: string;
166
+ rgb: (r: number, g: number, b: number) => (text: string) => string;
167
+ x256: (r: number, g: number, b: number) => (text: string) => string;
168
+ };
169
+ bg: {
170
+ black: ((s: string) => string) & {
171
+ open: string;
172
+ close: string;
173
+ };
174
+ red: ((s: string) => string) & {
175
+ open: string;
176
+ close: string;
177
+ };
178
+ green: ((s: string) => string) & {
179
+ open: string;
180
+ close: string;
181
+ };
182
+ yellow: ((s: string) => string) & {
183
+ open: string;
184
+ close: string;
185
+ };
186
+ blue: ((s: string) => string) & {
187
+ open: string;
188
+ close: string;
189
+ };
190
+ magenta: ((s: string) => string) & {
191
+ open: string;
192
+ close: string;
193
+ };
194
+ cyan: ((s: string) => string) & {
195
+ open: string;
196
+ close: string;
197
+ };
198
+ white: ((s: string) => string) & {
199
+ open: string;
200
+ close: string;
201
+ };
202
+ brightBlack: ((s: string) => string) & {
203
+ open: string;
204
+ close: string;
205
+ };
206
+ brightRed: ((s: string) => string) & {
207
+ open: string;
208
+ close: string;
209
+ };
210
+ brightGreen: ((s: string) => string) & {
211
+ open: string;
212
+ close: string;
213
+ };
214
+ brightYellow: ((s: string) => string) & {
215
+ open: string;
216
+ close: string;
217
+ };
218
+ brightBlue: ((s: string) => string) & {
219
+ open: string;
220
+ close: string;
221
+ };
222
+ brightMagenta: ((s: string) => string) & {
223
+ open: string;
224
+ close: string;
225
+ };
226
+ brightCyan: ((s: string) => string) & {
227
+ open: string;
228
+ close: string;
229
+ };
230
+ brightWhite: ((s: string) => string) & {
231
+ open: string;
232
+ close: string;
233
+ };
234
+ default: string;
235
+ rgb: (r: number, g: number, b: number) => (text: string) => string;
236
+ x256: (r: number, g: number, b: number) => (text: string) => string;
237
+ };
238
+ strip: (text: string) => string;
239
+ sanitize: (text: string) => string;
240
+ slice: typeof import("./strip").slice;
241
+ };
242
+ export = _exports;
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ module.exports = require("./ansi");
@@ -0,0 +1,2 @@
1
+ export let up: (lines?: number) => string;
2
+ export let down: (lines?: number) => string;
package/dist/scroll.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ const scroll = {
3
+ /** @type {(lines?: number) => string} */
4
+ up: lines => `\x1b[${lines ?? 1}S`,
5
+ /** @type {(lines?: number) => string} */
6
+ down: lines => `\x1b[${lines ?? 1}T`,
7
+ };
8
+ module.exports = scroll;