@peteanderson/ansi 0.0.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/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2026 Pete Anderson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # @peteanderson/ansi
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.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @peteanderson/ansi
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ const ansi = require("@peteanderson/ansi");
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"
22
+ ```
23
+
24
+ ## API
25
+
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
+ ### `fg` — foreground colors
29
+
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
+ ```
36
+
37
+ ### `bg` — background colors
38
+
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
+ ```
45
+
46
+ ### `style`
47
+
48
+ `bold` `dim` `italic` `underline` `inverse` `hidden` `strikethrough`
49
+
50
+ ```js
51
+ ansi.style.bold("text")
52
+ ansi.style.italic(ansi.style.bold("text"))
53
+ ```
54
+
55
+ ### `reset`
56
+
57
+ The SGR reset sequence (`\x1b[0m`). An empty string when color is disabled.
58
+
59
+ ### `stripAnsiSequences(text)`
60
+
61
+ Removes ANSI SGR escape sequences from a string.
62
+
63
+ ## License
64
+
65
+ MIT
@@ -0,0 +1,44 @@
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 ADDED
@@ -0,0 +1,85 @@
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/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name" : "@peteanderson/ansi",
3
+ "version" : "0.0.1",
4
+ "description" : "ANSI escape sequence generation with automatic feature detection",
5
+ "author" : {
6
+ "name" : "Pete Anderson",
7
+ "email" : "pete.the.anderson@gmail.com"
8
+ },
9
+ "repository" : {
10
+ "type" : "git",
11
+ "url" : "git+https://github.com/anderson-pete/ansi.git"
12
+ },
13
+ "keywords" : ["ansi", "nodejs", "color", "terminal", "cli"],
14
+ "engines" : {"node": ">=18"},
15
+ "files" : ["dist", "src"],
16
+ "main" : "dist/color.js",
17
+ "types" : "dist/color.d.ts",
18
+ "exports" : {
19
+ ".": {
20
+ "types" : "./dist/color.d.ts",
21
+ "require" : "./dist/color.js"
22
+ }
23
+ },
24
+ "license" : "MIT",
25
+ "scripts" : {
26
+ "prebuild" : "node -e \"require('node:fs').rmSync('dist', {force: true, recursive: true})\"",
27
+ "build" : "tsc -p tsconfig.build.json",
28
+ "prepack" : "npm run build",
29
+ "tag" : "git tag -a v$npm_package_version -m v$npm_package_version"
30
+ },
31
+ "devDependencies" : {
32
+ "@types/node" : "^26.0.0",
33
+ "typescript" : "^6.0.3"
34
+ }
35
+ }
package/src/color.js ADDED
@@ -0,0 +1,101 @@
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
+ };