neon-terminal 0.1.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.
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,65 @@
1
+ // neon-terminal — tiny ANSI color + TTY helpers for terminal output.
2
+
3
+ // Raw ANSI codes. `reset` closes any style.
4
+ const codes = {
5
+ reset: '\x1b[0m',
6
+ bold: '\x1b[1m',
7
+ dim: '\x1b[2m',
8
+ italic: '\x1b[3m',
9
+ underline: '\x1b[4m',
10
+
11
+ black: '\x1b[30m',
12
+ red: '\x1b[31m',
13
+ green: '\x1b[32m',
14
+ yellow: '\x1b[33m',
15
+ blue: '\x1b[34m',
16
+ magenta: '\x1b[35m',
17
+ cyan: '\x1b[36m',
18
+ white: '\x1b[37m',
19
+ grey: '\x1b[90m',
20
+ gray: '\x1b[90m',
21
+ };
22
+
23
+ /**
24
+ * Whether ANSI color should be emitted for a given stream.
25
+ *
26
+ * Honors the conventional env switches:
27
+ * - NO_COLOR (any value) → always false
28
+ * - FORCE_COLOR (any value) → always true
29
+ * Otherwise falls back to `stream.isTTY`.
30
+ *
31
+ * @param {{ isTTY?: boolean }} [stream] defaults to process.stdout
32
+ * @returns {boolean}
33
+ */
34
+ function supportsColor(stream = process.stdout) {
35
+ const env = (typeof process !== 'undefined' && process.env) || {};
36
+ if ('NO_COLOR' in env) return false;
37
+ if ('FORCE_COLOR' in env) return true;
38
+ return Boolean(stream && stream.isTTY);
39
+ }
40
+
41
+ /**
42
+ * Wrap `text` in the ANSI code(s) named by `style`, followed by a reset.
43
+ * Returns `text` untouched when the style is unknown or `enabled` is false.
44
+ *
45
+ * @param {string} text
46
+ * @param {string | string[]} style
47
+ * @param {boolean} [enabled] defaults to supportsColor()
48
+ * @returns {string}
49
+ */
50
+ function colorize(text, style, enabled = supportsColor()) {
51
+ const names = Array.isArray(style) ? style : [style];
52
+ const open = names.map((n) => codes[n]).filter(Boolean).join('');
53
+ if (!enabled || !open) return String(text);
54
+ return `${open}${text}${codes.reset}`;
55
+ }
56
+
57
+ // Convenience: paint.red('hi'), paint.bold('hi'), ...
58
+ const paint = Object.fromEntries(
59
+ Object.keys(codes)
60
+ .filter((name) => name !== 'reset')
61
+ .map((name) => [name, (text, enabled = true) => colorize(text, name, enabled)])
62
+ );
63
+
64
+ module.exports = { codes, supportsColor, colorize, paint };
65
+ module.exports.default = module.exports;
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "neon-terminal",
3
+ "version": "0.1.0",
4
+ "description": "Tiny ANSI color and TTY helpers for terminal output.",
5
+ "type": "module",
6
+ "module": "src/index.js",
7
+ "main": "cjs/src/index.js",
8
+ "exports": {
9
+ "types": "./types/index.d.ts",
10
+ "import": "./src/index.js",
11
+ "require": "./cjs/src/index.js",
12
+ "default": "./cjs/src/index.js"
13
+ },
14
+ "types": "types/index.d.ts",
15
+ "typings": "types/index.d.ts",
16
+ "engines": {
17
+ "node": ">=12"
18
+ },
19
+ "scripts": {
20
+ "test": "npm run test:esm && npm run test:cjs",
21
+ "test:esm": "node test/terminal.test.js",
22
+ "test:cjs": "node cjs/test/terminal.test.js"
23
+ },
24
+ "files": [
25
+ "/src",
26
+ "/cjs/src",
27
+ "/cjs/package.json",
28
+ "/types"
29
+ ],
30
+ "license": "MIT",
31
+ "keywords": [
32
+ "terminal",
33
+ "ansi",
34
+ "color",
35
+ "colour",
36
+ "tty",
37
+ "neon"
38
+ ]
39
+ }
package/src/index.js ADDED
@@ -0,0 +1,66 @@
1
+ // neon-terminal — tiny ANSI color + TTY helpers for terminal output.
2
+
3
+ // Raw ANSI codes. `reset` closes any style.
4
+ export const codes = {
5
+ reset: '\x1b[0m',
6
+ bold: '\x1b[1m',
7
+ dim: '\x1b[2m',
8
+ italic: '\x1b[3m',
9
+ underline: '\x1b[4m',
10
+
11
+ black: '\x1b[30m',
12
+ red: '\x1b[31m',
13
+ green: '\x1b[32m',
14
+ yellow: '\x1b[33m',
15
+ blue: '\x1b[34m',
16
+ magenta: '\x1b[35m',
17
+ cyan: '\x1b[36m',
18
+ white: '\x1b[37m',
19
+ grey: '\x1b[90m',
20
+ gray: '\x1b[90m',
21
+ };
22
+
23
+ /**
24
+ * Whether ANSI color should be emitted for a given stream.
25
+ *
26
+ * Honors the conventional env switches:
27
+ * - NO_COLOR (any value) → always false
28
+ * - FORCE_COLOR (any value) → always true
29
+ * Otherwise falls back to `stream.isTTY`.
30
+ *
31
+ * @param {{ isTTY?: boolean }} [stream] defaults to process.stdout
32
+ * @returns {boolean}
33
+ */
34
+ export function supportsColor(stream = process.stdout) {
35
+ const env = (typeof process !== 'undefined' && process.env) || {};
36
+ if ('NO_COLOR' in env) return false;
37
+ if ('FORCE_COLOR' in env) return true;
38
+ return Boolean(stream && stream.isTTY);
39
+ }
40
+
41
+ /**
42
+ * Wrap `text` in the ANSI code(s) named by `style`, followed by a reset.
43
+ * Returns `text` untouched when the style is unknown or `enabled` is false.
44
+ *
45
+ * @param {string} text
46
+ * @param {keyof codes | Array<keyof codes>} style
47
+ * @param {boolean} [enabled] defaults to supportsColor()
48
+ * @returns {string}
49
+ */
50
+ export function colorize(text, style, enabled = supportsColor()) {
51
+ const names = Array.isArray(style) ? style : [style];
52
+ const open = names.map((n) => codes[n]).filter(Boolean).join('');
53
+ if (!enabled || !open) return String(text);
54
+ return `${open}${text}${codes.reset}`;
55
+ }
56
+
57
+ // Convenience: paint.red('hi'), paint.bold('hi'), ...
58
+ // Each ignores the ambient TTY check unless you pass a second arg, so it always
59
+ // colors — handy when you've already decided color is on.
60
+ export const paint = Object.fromEntries(
61
+ Object.keys(codes)
62
+ .filter((name) => name !== 'reset')
63
+ .map((name) => [name, (text, enabled = true) => colorize(text, name, enabled)])
64
+ );
65
+
66
+ export default { codes, supportsColor, colorize, paint };
@@ -0,0 +1,38 @@
1
+ export type StyleName =
2
+ | 'reset'
3
+ | 'bold'
4
+ | 'dim'
5
+ | 'italic'
6
+ | 'underline'
7
+ | 'black'
8
+ | 'red'
9
+ | 'green'
10
+ | 'yellow'
11
+ | 'blue'
12
+ | 'magenta'
13
+ | 'cyan'
14
+ | 'white'
15
+ | 'grey'
16
+ | 'gray';
17
+
18
+ export type PaintName = Exclude<StyleName, 'reset'>;
19
+
20
+ export const codes: Record<StyleName, string>;
21
+
22
+ export function supportsColor(stream?: { isTTY?: boolean }): boolean;
23
+
24
+ export function colorize(
25
+ text: unknown,
26
+ style: StyleName | StyleName[],
27
+ enabled?: boolean
28
+ ): string;
29
+
30
+ export const paint: Record<PaintName, (text: unknown, enabled?: boolean) => string>;
31
+
32
+ declare const _default: {
33
+ codes: typeof codes;
34
+ supportsColor: typeof supportsColor;
35
+ colorize: typeof colorize;
36
+ paint: typeof paint;
37
+ };
38
+ export default _default;