termkit-ui 0.0.0-stage → 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.
- package/CHANGELOG.md +43 -0
- package/LICENSE +9 -0
- package/README.md +153 -2
- package/dist/components/display/layout.d.ts +91 -0
- package/dist/components/display/table.d.ts +53 -0
- package/dist/components/progress/index.d.ts +2 -0
- package/dist/components/progress/progressBar.d.ts +102 -0
- package/dist/components/progress/spinner.d.ts +107 -0
- package/dist/components/prompts/base.d.ts +53 -0
- package/dist/components/prompts/confirm.d.ts +46 -0
- package/dist/components/prompts/index.d.ts +6 -0
- package/dist/components/prompts/input.d.ts +46 -0
- package/dist/components/prompts/multiselect.d.ts +100 -0
- package/dist/components/prompts/number.d.ts +58 -0
- package/dist/components/prompts/password.d.ts +45 -0
- package/dist/components/prompts/select.d.ts +84 -0
- package/dist/core/ansi/escapes.d.ts +154 -0
- package/dist/core/caps/detect.d.ts +74 -0
- package/dist/core/errors/index.d.ts +73 -0
- package/dist/core/styles/index.d.ts +12 -0
- package/dist/core/width/string-width.d.ts +42 -0
- package/dist/engine/guard/terminal-guard.d.ts +84 -0
- package/dist/engine/keyboard/parser.d.ts +79 -0
- package/dist/engine/renderer/coordinator.d.ts +85 -0
- package/dist/index.d.ts +13 -0
- package/dist/testing/index.d.ts +127 -0
- package/package.json +77 -4
- package/src/components/display/layout.js +224 -0
- package/src/components/display/table.js +179 -0
- package/src/components/progress/index.js +6 -0
- package/src/components/progress/progressBar.js +153 -0
- package/src/components/progress/spinner.js +195 -0
- package/src/components/prompts/base.js +237 -0
- package/src/components/prompts/confirm.js +68 -0
- package/src/components/prompts/index.js +10 -0
- package/src/components/prompts/input.js +107 -0
- package/src/components/prompts/multiselect.js +205 -0
- package/src/components/prompts/number.js +140 -0
- package/src/components/prompts/password.js +77 -0
- package/src/components/prompts/select.js +171 -0
- package/src/core/ansi/escapes.js +52 -0
- package/src/core/caps/detect.js +167 -0
- package/src/core/errors/index.js +106 -0
- package/src/core/styles/index.js +174 -0
- package/src/core/width/string-width.js +244 -0
- package/src/engine/guard/terminal-guard.js +188 -0
- package/src/engine/keyboard/parser.js +285 -0
- package/src/engine/renderer/coordinator.js +263 -0
- package/src/index.js +17 -0
- package/src/testing/index.js +250 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Number input prompt component.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { style } from '../../core/styles/index.js';
|
|
6
|
+
import { runPrompt } from './base.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {Object} NumberOptions
|
|
10
|
+
* @property {string} message - Prompt label
|
|
11
|
+
* @property {number} [default] - Default number value
|
|
12
|
+
* @property {number} [min] - Minimum allowed value
|
|
13
|
+
* @property {number} [max] - Maximum allowed value
|
|
14
|
+
* @property {number} [step=1] - Increment/decrement step
|
|
15
|
+
* @property {boolean} [float=false] - Whether decimal numbers are allowed
|
|
16
|
+
* @property {(value: number) => boolean | string | Promise<boolean | string>} [validate]
|
|
17
|
+
* @property {NodeJS.ReadableStream} [input]
|
|
18
|
+
* @property {NodeJS.WritableStream} [output]
|
|
19
|
+
* @property {AbortSignal} [signal]
|
|
20
|
+
* @property {boolean} [accessible]
|
|
21
|
+
* @property {'throw' | 'return-null'} [onCancel]
|
|
22
|
+
* @property {Partial<import('../../core/caps/detect.js').TerminalCapabilities>} [capabilities]
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Prompt user for a numeric value with bounds checking and keyboard stepping.
|
|
27
|
+
* @template {'throw' | 'return-null'} [TCancel='throw']
|
|
28
|
+
* @param {NumberOptions & { onCancel?: TCancel }} options
|
|
29
|
+
* @returns {Promise<TCancel extends 'return-null' ? number | null : number>}
|
|
30
|
+
*/
|
|
31
|
+
export async function number(options) {
|
|
32
|
+
const step = options.step !== undefined ? options.step : 1;
|
|
33
|
+
const isFloat = Boolean(options.float);
|
|
34
|
+
const initialValue = options.default !== undefined ? String(options.default) : '';
|
|
35
|
+
|
|
36
|
+
const res = await runPrompt({
|
|
37
|
+
name: 'number',
|
|
38
|
+
context: options,
|
|
39
|
+
initialState: {
|
|
40
|
+
buffer: ''
|
|
41
|
+
},
|
|
42
|
+
getValue: (state) => {
|
|
43
|
+
const parsed = isFloat ? parseFloat(state.buffer) : parseInt(state.buffer, 10);
|
|
44
|
+
if (isNaN(parsed) && options.default !== undefined) {
|
|
45
|
+
return options.default;
|
|
46
|
+
}
|
|
47
|
+
return parsed;
|
|
48
|
+
},
|
|
49
|
+
formatSubmitted: (val) => String(val),
|
|
50
|
+
renderView: (state, caps) => {
|
|
51
|
+
const isAccessible = caps.reducedMotion || caps.isDumb;
|
|
52
|
+
const prefix = isAccessible ? '[?]' : style.cyan('?');
|
|
53
|
+
let displayInput = state.buffer;
|
|
54
|
+
|
|
55
|
+
if (displayInput.length === 0 && options.default !== undefined) {
|
|
56
|
+
displayInput = style.dim(`(${options.default})`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let hints = [];
|
|
60
|
+
if (options.min !== undefined && options.max !== undefined) {
|
|
61
|
+
hints.push(`${options.min}..${options.max}`);
|
|
62
|
+
} else if (options.min !== undefined) {
|
|
63
|
+
hints.push(`>= ${options.min}`);
|
|
64
|
+
} else if (options.max !== undefined) {
|
|
65
|
+
hints.push(`<= ${options.max}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const hintText = hints.length > 0 ? style.dim(`[${hints.join(', ')}]`) : '';
|
|
69
|
+
const chevron = caps.unicode ? '›' : '>';
|
|
70
|
+
const lines = [`${prefix} ${options.message} ${hintText} ${chevron} ${displayInput}`];
|
|
71
|
+
|
|
72
|
+
if (state.error) {
|
|
73
|
+
const errPrefix = isAccessible ? '[ERROR]' : style.red('✖');
|
|
74
|
+
lines.push(` ${errPrefix} ${style.red(state.error)}`);
|
|
75
|
+
}
|
|
76
|
+
return lines;
|
|
77
|
+
},
|
|
78
|
+
onKey: (key, state) => {
|
|
79
|
+
if (key.name === 'return') {
|
|
80
|
+
const val = isFloat ? parseFloat(state.buffer) : parseInt(state.buffer, 10);
|
|
81
|
+
if (state.buffer.length === 0 && options.default !== undefined) {
|
|
82
|
+
return 'submit';
|
|
83
|
+
}
|
|
84
|
+
if (isNaN(val)) {
|
|
85
|
+
state.error = 'Please enter a valid number.';
|
|
86
|
+
return 'update';
|
|
87
|
+
}
|
|
88
|
+
if (options.min !== undefined && val < options.min) {
|
|
89
|
+
state.error = `Value must be at least ${options.min}.`;
|
|
90
|
+
return 'update';
|
|
91
|
+
}
|
|
92
|
+
if (options.max !== undefined && val > options.max) {
|
|
93
|
+
state.error = `Value must be at most ${options.max}.`;
|
|
94
|
+
return 'update';
|
|
95
|
+
}
|
|
96
|
+
return 'submit';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (key.name === 'up') {
|
|
100
|
+
const current = (isFloat ? parseFloat(state.buffer) : parseInt(state.buffer, 10)) || options.default || 0;
|
|
101
|
+
let next = current + step;
|
|
102
|
+
if (options.max !== undefined) next = Math.min(next, options.max);
|
|
103
|
+
state.buffer = String(next);
|
|
104
|
+
return 'update';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (key.name === 'down') {
|
|
108
|
+
const current = (isFloat ? parseFloat(state.buffer) : parseInt(state.buffer, 10)) || options.default || 0;
|
|
109
|
+
let next = current - step;
|
|
110
|
+
if (options.min !== undefined) next = Math.max(next, options.min);
|
|
111
|
+
state.buffer = String(next);
|
|
112
|
+
return 'update';
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (key.name === 'backspace') {
|
|
116
|
+
if (state.buffer.length > 0) {
|
|
117
|
+
state.buffer = state.buffer.slice(0, -1);
|
|
118
|
+
}
|
|
119
|
+
return 'update';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Digits, negative sign, and decimal point
|
|
123
|
+
if (key.name >= '0' && key.name <= '9') {
|
|
124
|
+
state.buffer += key.name;
|
|
125
|
+
return 'update';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (key.name === '-' && state.buffer.length === 0 && (options.min === undefined || options.min < 0)) {
|
|
129
|
+
state.buffer = '-';
|
|
130
|
+
return 'update';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (key.name === '.' && isFloat && !state.buffer.includes('.')) {
|
|
134
|
+
state.buffer = state.buffer.length === 0 ? '0.' : state.buffer + '.';
|
|
135
|
+
return 'update';
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return /** @type {any} */ (res);
|
|
140
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Password input prompt component.
|
|
3
|
+
* Masked input with strict zero-leakage security.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { style } from '../../core/styles/index.js';
|
|
7
|
+
import { runPrompt } from './base.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {Object} PasswordOptions
|
|
11
|
+
* @property {string} message - Prompt label
|
|
12
|
+
* @property {string} [default] - Fallback value
|
|
13
|
+
* @property {string} [mask='*'] - Mask character
|
|
14
|
+
* @property {(value: string) => boolean | string | Promise<boolean | string>} [validate]
|
|
15
|
+
* @property {NodeJS.ReadableStream} [input]
|
|
16
|
+
* @property {NodeJS.WritableStream} [output]
|
|
17
|
+
* @property {AbortSignal} [signal]
|
|
18
|
+
* @property {boolean} [accessible]
|
|
19
|
+
* @property {'throw' | 'return-null'} [onCancel]
|
|
20
|
+
* @property {Partial<import('../../core/caps/detect.js').TerminalCapabilities>} [capabilities]
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Prompt user for a secret password.
|
|
25
|
+
* Never leaks the secret value in logs, errors, or snapshots.
|
|
26
|
+
*
|
|
27
|
+
* @template {'throw' | 'return-null'} [TCancel='throw']
|
|
28
|
+
* @param {PasswordOptions & { onCancel?: TCancel }} options
|
|
29
|
+
* @returns {Promise<TCancel extends 'return-null' ? string | null : string>}
|
|
30
|
+
*/
|
|
31
|
+
export async function password(options) {
|
|
32
|
+
const maskChar = options.mask !== undefined ? options.mask : '*';
|
|
33
|
+
|
|
34
|
+
const res = await runPrompt({
|
|
35
|
+
name: 'password',
|
|
36
|
+
context: options,
|
|
37
|
+
initialState: {
|
|
38
|
+
buffer: ''
|
|
39
|
+
},
|
|
40
|
+
getValue: (state) => (state.buffer.length > 0 ? state.buffer : options.default || ''),
|
|
41
|
+
formatSubmitted: (val) => maskChar.repeat(val.length || 6),
|
|
42
|
+
renderView: (state, caps) => {
|
|
43
|
+
const isAccessible = caps.reducedMotion || caps.isDumb;
|
|
44
|
+
const chevron = caps.unicode ? '›' : '>';
|
|
45
|
+
const prefix = isAccessible ? '[?]' : style.cyan('?');
|
|
46
|
+
const masked = maskChar.repeat(state.buffer.length);
|
|
47
|
+
|
|
48
|
+
const lines = [`${prefix} ${options.message} ${chevron} ${masked}`];
|
|
49
|
+
if (state.error) {
|
|
50
|
+
const errPrefix = isAccessible ? '[ERROR]' : style.red('✖');
|
|
51
|
+
lines.push(` ${errPrefix} ${style.red(state.error)}`);
|
|
52
|
+
}
|
|
53
|
+
return lines;
|
|
54
|
+
},
|
|
55
|
+
onKey: (key, state) => {
|
|
56
|
+
if (key.name === 'return') {
|
|
57
|
+
return 'submit';
|
|
58
|
+
}
|
|
59
|
+
if (key.isPaste && key.pasteText) {
|
|
60
|
+
state.buffer += key.pasteText;
|
|
61
|
+
return 'update';
|
|
62
|
+
}
|
|
63
|
+
if (key.name === 'backspace') {
|
|
64
|
+
if (state.buffer.length > 0) {
|
|
65
|
+
state.buffer = state.buffer.slice(0, -1);
|
|
66
|
+
}
|
|
67
|
+
return 'update';
|
|
68
|
+
}
|
|
69
|
+
// Printable characters
|
|
70
|
+
if (!key.ctrl && !key.meta && key.name.length === 1) {
|
|
71
|
+
state.buffer += key.name;
|
|
72
|
+
return 'update';
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return /** @type {any} */ (res);
|
|
77
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Select single choice prompt component.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { style } from '../../core/styles/index.js';
|
|
6
|
+
import { runPrompt } from './base.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @template [T=any]
|
|
10
|
+
* @typedef {Object} SelectItem
|
|
11
|
+
* @property {string} label - Display label
|
|
12
|
+
* @property {T} value - Selected value
|
|
13
|
+
* @property {string} [hint] - Optional muted hint
|
|
14
|
+
* @property {boolean | string} [disabled] - Whether item is disabled
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @template [T=any]
|
|
19
|
+
* @typedef {Object} SelectOptions
|
|
20
|
+
* @property {string} message - Prompt label
|
|
21
|
+
* @property {readonly SelectItem<T>[] | readonly { readonly label: string; readonly value: T; readonly hint?: string; readonly disabled?: boolean | string }[]} items - List of choices
|
|
22
|
+
* @property {T} [default] - Default selected value
|
|
23
|
+
* @property {number} [pageSize=7] - Number of items displayed at once
|
|
24
|
+
* @property {boolean} [loop=true] - Loop navigation
|
|
25
|
+
* @property {NodeJS.ReadableStream} [input]
|
|
26
|
+
* @property {NodeJS.WritableStream} [output]
|
|
27
|
+
* @property {AbortSignal} [signal]
|
|
28
|
+
* @property {boolean} [accessible]
|
|
29
|
+
* @property {'throw' | 'return-null'} [onCancel]
|
|
30
|
+
* @property {Partial<import('../../core/caps/detect.js').TerminalCapabilities>} [capabilities]
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Single item select prompt with arrow navigation.
|
|
35
|
+
* @template T
|
|
36
|
+
* @template {'throw' | 'return-null'} [TCancel='throw']
|
|
37
|
+
* @param {SelectOptions<T> & { onCancel?: TCancel }} options
|
|
38
|
+
* @returns {Promise<TCancel extends 'return-null' ? T | null : T>}
|
|
39
|
+
*/
|
|
40
|
+
export async function select(options) {
|
|
41
|
+
const items = options.items || [];
|
|
42
|
+
const loop = options.loop !== undefined ? options.loop : true;
|
|
43
|
+
const pageSize = options.pageSize || 7;
|
|
44
|
+
|
|
45
|
+
// Find initial selected index
|
|
46
|
+
let initialIndex = 0;
|
|
47
|
+
if (options.default !== undefined) {
|
|
48
|
+
const foundIdx = items.findIndex((it) => it.value === options.default && !it.disabled);
|
|
49
|
+
if (foundIdx !== -1) initialIndex = foundIdx;
|
|
50
|
+
} else {
|
|
51
|
+
const firstEnabled = items.findIndex((it) => !it.disabled);
|
|
52
|
+
if (firstEnabled !== -1) initialIndex = firstEnabled;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const res = await runPrompt({
|
|
56
|
+
name: 'select',
|
|
57
|
+
context: options,
|
|
58
|
+
initialState: {
|
|
59
|
+
cursor: initialIndex
|
|
60
|
+
},
|
|
61
|
+
getValue: (state) => items[state.cursor]?.value,
|
|
62
|
+
formatSubmitted: (val, state) => items[state.cursor]?.label || String(val),
|
|
63
|
+
renderView: (state, caps) => {
|
|
64
|
+
const isAccessible = caps.reducedMotion || caps.isDumb;
|
|
65
|
+
const prefix = isAccessible ? '[?]' : style.cyan('?');
|
|
66
|
+
const navHint = style.dim('(Use arrow keys)');
|
|
67
|
+
const lines = [`${prefix} ${options.message} ${navHint}`];
|
|
68
|
+
|
|
69
|
+
// Calculate window / pagination
|
|
70
|
+
const total = items.length;
|
|
71
|
+
let start = Math.max(0, state.cursor - Math.floor(pageSize / 2));
|
|
72
|
+
let end = Math.min(total, start + pageSize);
|
|
73
|
+
if (end - start < pageSize) {
|
|
74
|
+
start = Math.max(0, end - pageSize);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
for (let i = start; i < end; i++) {
|
|
78
|
+
const item = items[i];
|
|
79
|
+
const isSelected = i === state.cursor;
|
|
80
|
+
const isDisabled = Boolean(item.disabled);
|
|
81
|
+
|
|
82
|
+
let pointer = ' ';
|
|
83
|
+
let bullet = caps.unicode ? '◯' : '( )';
|
|
84
|
+
if (isSelected) {
|
|
85
|
+
pointer = style.cyan(caps.unicode ? '❯' : '>');
|
|
86
|
+
bullet = style.cyan(caps.unicode ? '◉' : '(*)');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (isAccessible) {
|
|
90
|
+
bullet = isSelected ? '(*)' : '( )';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let itemText = item.label;
|
|
94
|
+
if (isDisabled) {
|
|
95
|
+
const reason = typeof item.disabled === 'string' ? ` (${item.disabled})` : ' (disabled)';
|
|
96
|
+
itemText = style.dim(`${item.label}${reason}`);
|
|
97
|
+
} else if (isSelected) {
|
|
98
|
+
itemText = style.bold.cyan(item.label);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const hintText = item.hint ? ` ${style.dim(`- ${item.hint}`)}` : '';
|
|
102
|
+
lines.push(` ${pointer} ${bullet} ${itemText}${hintText}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (state.error) {
|
|
106
|
+
const errPrefix = isAccessible ? '[ERROR]' : style.red('✖');
|
|
107
|
+
lines.push(` ${errPrefix} ${style.red(state.error)}`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return lines;
|
|
111
|
+
},
|
|
112
|
+
onKey: (key, state) => {
|
|
113
|
+
if (key.name === 'return') {
|
|
114
|
+
const item = items[state.cursor];
|
|
115
|
+
if (item && !item.disabled) {
|
|
116
|
+
return 'submit';
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (key.name === 'down' || key.name === 'j') {
|
|
122
|
+
let next = state.cursor + 1;
|
|
123
|
+
while (next < items.length && items[next].disabled) {
|
|
124
|
+
next++;
|
|
125
|
+
}
|
|
126
|
+
if (next >= items.length) {
|
|
127
|
+
if (loop) {
|
|
128
|
+
next = 0;
|
|
129
|
+
while (next < items.length && items[next].disabled) next++;
|
|
130
|
+
} else {
|
|
131
|
+
next = state.cursor;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
state.cursor = next;
|
|
135
|
+
return 'update';
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (key.name === 'up' || key.name === 'k') {
|
|
139
|
+
let prev = state.cursor - 1;
|
|
140
|
+
while (prev >= 0 && items[prev].disabled) {
|
|
141
|
+
prev--;
|
|
142
|
+
}
|
|
143
|
+
if (prev < 0) {
|
|
144
|
+
if (loop) {
|
|
145
|
+
prev = items.length - 1;
|
|
146
|
+
while (prev >= 0 && items[prev].disabled) prev--;
|
|
147
|
+
} else {
|
|
148
|
+
prev = state.cursor;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
state.cursor = prev;
|
|
152
|
+
return 'update';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (key.name === 'home') {
|
|
156
|
+
let first = 0;
|
|
157
|
+
while (first < items.length && items[first].disabled) first++;
|
|
158
|
+
state.cursor = first;
|
|
159
|
+
return 'update';
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (key.name === 'end') {
|
|
163
|
+
let last = items.length - 1;
|
|
164
|
+
while (last >= 0 && items[last].disabled) last--;
|
|
165
|
+
state.cursor = last;
|
|
166
|
+
return 'update';
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
return /** @type {any} */ (res);
|
|
171
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview ANSI control sequences and terminal codes.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// SGR (Select Graphic Rendition) codes
|
|
6
|
+
export const SGR = {
|
|
7
|
+
reset: '\x1b[0m',
|
|
8
|
+
bold: { open: '\x1b[1m', close: '\x1b[22m' },
|
|
9
|
+
dim: { open: '\x1b[2m', close: '\x1b[22m' },
|
|
10
|
+
italic: { open: '\x1b[3m', close: '\x1b[23m' },
|
|
11
|
+
underline: { open: '\x1b[4m', close: '\x1b[24m' },
|
|
12
|
+
inverse: { open: '\x1b[7m', close: '\x1b[27m' },
|
|
13
|
+
strikethrough: { open: '\x1b[9m', close: '\x1b[29m' },
|
|
14
|
+
|
|
15
|
+
// Standard Foreground Colors (30-37, 90-97)
|
|
16
|
+
black: { open: '\x1b[30m', close: '\x1b[39m' },
|
|
17
|
+
red: { open: '\x1b[31m', close: '\x1b[39m' },
|
|
18
|
+
green: { open: '\x1b[32m', close: '\x1b[39m' },
|
|
19
|
+
yellow: { open: '\x1b[33m', close: '\x1b[39m' },
|
|
20
|
+
blue: { open: '\x1b[34m', close: '\x1b[39m' },
|
|
21
|
+
magenta: { open: '\x1b[35m', close: '\x1b[39m' },
|
|
22
|
+
cyan: { open: '\x1b[36m', close: '\x1b[39m' },
|
|
23
|
+
white: { open: '\x1b[37m', close: '\x1b[39m' },
|
|
24
|
+
gray: { open: '\x1b[90m', close: '\x1b[39m' },
|
|
25
|
+
|
|
26
|
+
// Background Colors (40-47, 100-107)
|
|
27
|
+
bgBlack: { open: '\x1b[40m', close: '\x1b[49m' },
|
|
28
|
+
bgRed: { open: '\x1b[41m', close: '\x1b[49m' },
|
|
29
|
+
bgGreen: { open: '\x1b[42m', close: '\x1b[49m' },
|
|
30
|
+
bgYellow: { open: '\x1b[43m', close: '\x1b[49m' },
|
|
31
|
+
bgBlue: { open: '\x1b[44m', close: '\x1b[49m' },
|
|
32
|
+
bgMagenta: { open: '\x1b[45m', close: '\x1b[49m' },
|
|
33
|
+
bgCyan: { open: '\x1b[46m', close: '\x1b[49m' },
|
|
34
|
+
bgWhite: { open: '\x1b[47m', close: '\x1b[49m' }
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Cursor Control
|
|
38
|
+
export const CURSOR = {
|
|
39
|
+
hide: '\x1b[?25l',
|
|
40
|
+
show: '\x1b[?25h',
|
|
41
|
+
save: '\x1b7',
|
|
42
|
+
restore: '\x1b8',
|
|
43
|
+
up: (/** @type {number} */ n = 1) => `\x1b[${n}A`,
|
|
44
|
+
down: (/** @type {number} */ n = 1) => `\x1b[${n}B`,
|
|
45
|
+
forward: (/** @type {number} */ n = 1) => `\x1b[${n}C`,
|
|
46
|
+
back: (/** @type {number} */ n = 1) => `\x1b[${n}D`,
|
|
47
|
+
toColumn: (/** @type {number} */ n = 1) => `\x1b[${n}G`,
|
|
48
|
+
clearLine: '\x1b[2K',
|
|
49
|
+
clearLineToEnd: '\x1b[0K',
|
|
50
|
+
clearLineToStart: '\x1b[1K',
|
|
51
|
+
clearDown: '\x1b[J'
|
|
52
|
+
};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Terminal capability detection (color depth, Unicode, TTY, CI, NO_COLOR, FORCE_COLOR).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} TerminalCapabilities
|
|
7
|
+
* @property {number} colorLevel - 0 (none), 1 (16 colors), 2 (256 colors), 3 (16M truecolor)
|
|
8
|
+
* @property {boolean} hasColor - Whether basic colors are supported
|
|
9
|
+
* @property {boolean} has256 - Whether 256 colors are supported
|
|
10
|
+
* @property {boolean} hasTrueColor - Whether 24-bit TrueColor is supported
|
|
11
|
+
* @property {boolean} unicode - Whether UTF-8 / Unicode characters are supported
|
|
12
|
+
* @property {boolean} isTTY - Whether the stream is an interactive terminal
|
|
13
|
+
* @property {boolean} isCI - Whether running in a CI environment
|
|
14
|
+
* @property {boolean} isDumb - Whether the terminal is TERM=dumb
|
|
15
|
+
* @property {number} columns - Viewport columns width
|
|
16
|
+
* @property {number} rows - Viewport rows height
|
|
17
|
+
* @property {boolean} reducedMotion - Whether animations should be suppressed
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Detect capabilities of a given output stream and environment.
|
|
22
|
+
* @param {Object} [options]
|
|
23
|
+
* @param {NodeJS.WritableStream} [options.stream] - Defaults to process.stderr
|
|
24
|
+
* @param {Record<string, string | undefined>} [options.env] - Defaults to process.env
|
|
25
|
+
* @returns {TerminalCapabilities}
|
|
26
|
+
*/
|
|
27
|
+
export function detectCapabilities(options = {}) {
|
|
28
|
+
const stream = options.stream || process.stderr;
|
|
29
|
+
|
|
30
|
+
// Stream-attached capabilities (e.g. from VirtualTerminal harness)
|
|
31
|
+
const streamCaps = /** @type {any} */ (stream)?.capabilities || /** @type {any} */ (stream)?._capabilities;
|
|
32
|
+
if (!options.env && streamCaps) {
|
|
33
|
+
const custom = streamCaps;
|
|
34
|
+
return {
|
|
35
|
+
colorLevel: custom.colorLevel ?? 0,
|
|
36
|
+
hasColor: (custom.colorLevel ?? 0) > 0,
|
|
37
|
+
has256: (custom.colorLevel ?? 0) >= 2,
|
|
38
|
+
hasTrueColor: (custom.colorLevel ?? 0) >= 3,
|
|
39
|
+
unicode: custom.unicode ?? true,
|
|
40
|
+
isTTY: custom.isTTY ?? Boolean(/** @type {any} */ (stream).isTTY),
|
|
41
|
+
isCI: custom.isCI ?? false,
|
|
42
|
+
isDumb: custom.isDumb ?? false,
|
|
43
|
+
columns: custom.columns ?? (/** @type {any} */ (stream).columns || 80),
|
|
44
|
+
rows: custom.rows ?? (/** @type {any} */ (stream).rows || 24),
|
|
45
|
+
reducedMotion: custom.reducedMotion ?? false,
|
|
46
|
+
...custom
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const env = options.env || /** @type {any} */ (stream)?._env || process.env;
|
|
51
|
+
|
|
52
|
+
// 1. TTY & Dumb Terminal Detection
|
|
53
|
+
const isTTY = Boolean(/** @type {any} */ (stream).isTTY);
|
|
54
|
+
const term = (env.TERM || '').toLowerCase();
|
|
55
|
+
const isDumb = term === 'dumb';
|
|
56
|
+
|
|
57
|
+
// 2. CI Detection
|
|
58
|
+
const isCI = Boolean(
|
|
59
|
+
env.CI === 'true' ||
|
|
60
|
+
env.CI === '1' ||
|
|
61
|
+
env.GITHUB_ACTIONS ||
|
|
62
|
+
env.GITLAB_CI ||
|
|
63
|
+
env.TRAVIS ||
|
|
64
|
+
env.CIRCLECI ||
|
|
65
|
+
env.BUILDKITE ||
|
|
66
|
+
env.JENKINS_URL ||
|
|
67
|
+
env.APPVEYOR
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// 3. Color Support Detection
|
|
71
|
+
let colorLevel = 0;
|
|
72
|
+
|
|
73
|
+
// Check NO_COLOR (https://no-color.org)
|
|
74
|
+
const noColor = 'NO_COLOR' in env && env.NO_COLOR !== '';
|
|
75
|
+
|
|
76
|
+
// Check FORCE_COLOR (1, 2, 3, or true)
|
|
77
|
+
const forceColor = env.FORCE_COLOR;
|
|
78
|
+
|
|
79
|
+
if (forceColor !== undefined) {
|
|
80
|
+
if (forceColor === '0' || forceColor === 'false') {
|
|
81
|
+
colorLevel = 0;
|
|
82
|
+
} else if (forceColor === '1') {
|
|
83
|
+
colorLevel = 1;
|
|
84
|
+
} else if (forceColor === '2') {
|
|
85
|
+
colorLevel = 2;
|
|
86
|
+
} else if (forceColor === '3') {
|
|
87
|
+
colorLevel = 3;
|
|
88
|
+
} else {
|
|
89
|
+
colorLevel = 1; // Default truthy FORCE_COLOR is level 1
|
|
90
|
+
}
|
|
91
|
+
} else if (noColor || isDumb) {
|
|
92
|
+
colorLevel = 0;
|
|
93
|
+
} else if (!isTTY && !isCI) {
|
|
94
|
+
// Non-TTY without FORCE_COLOR gets no color
|
|
95
|
+
colorLevel = 0;
|
|
96
|
+
} else {
|
|
97
|
+
// Node.js stream color depth detection
|
|
98
|
+
const getColorDepth = /** @type {any} */ (stream).getColorDepth;
|
|
99
|
+
if (typeof getColorDepth === 'function') {
|
|
100
|
+
const depth = getColorDepth.call(stream, env);
|
|
101
|
+
if (depth >= 24) {
|
|
102
|
+
colorLevel = 3;
|
|
103
|
+
} else if (depth >= 8) {
|
|
104
|
+
colorLevel = 2;
|
|
105
|
+
} else if (depth >= 4) {
|
|
106
|
+
colorLevel = 1;
|
|
107
|
+
} else {
|
|
108
|
+
colorLevel = 0;
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
// Fallback environment inspections
|
|
112
|
+
if (
|
|
113
|
+
env.COLORTERM === 'truecolor' ||
|
|
114
|
+
env.COLORTERM === '24bit' ||
|
|
115
|
+
term.includes('truecolor') ||
|
|
116
|
+
term.includes('24bit')
|
|
117
|
+
) {
|
|
118
|
+
colorLevel = 3;
|
|
119
|
+
} else if (term.includes('256') || term.includes('xterm-256color')) {
|
|
120
|
+
colorLevel = 2;
|
|
121
|
+
} else if (isTTY || isCI) {
|
|
122
|
+
colorLevel = 1;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 4. Unicode Support Detection
|
|
128
|
+
let unicode = true;
|
|
129
|
+
if (process.platform === 'win32') {
|
|
130
|
+
// Windows Terminal and ConEmu support unicode
|
|
131
|
+
const isWindowsTerminal = Boolean(env.WT_SESSION);
|
|
132
|
+
const isConEmu = env.ConEmuANSI === 'ON';
|
|
133
|
+
const isVSCode = env.VSCODE_INJECTION === '1';
|
|
134
|
+
if (!isWindowsTerminal && !isConEmu && !isVSCode) {
|
|
135
|
+
// Legacy cmd.exe without UTF-8 codepage
|
|
136
|
+
unicode = false;
|
|
137
|
+
}
|
|
138
|
+
} else if (isDumb) {
|
|
139
|
+
unicode = false;
|
|
140
|
+
} else {
|
|
141
|
+
const lang = (env.LC_ALL || env.LC_CTYPE || env.LANG || '').toLowerCase();
|
|
142
|
+
if (lang && !lang.includes('utf-8') && !lang.includes('utf8')) {
|
|
143
|
+
unicode = false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// 5. Terminal Dimensions
|
|
148
|
+
const columns = /** @type {any} */ (stream).columns || 80;
|
|
149
|
+
const rows = /** @type {any} */ (stream).rows || 24;
|
|
150
|
+
|
|
151
|
+
// 6. Reduced Motion Detection
|
|
152
|
+
const reducedMotion = isDumb || env.REDUCED_MOTION === '1';
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
colorLevel,
|
|
156
|
+
hasColor: colorLevel > 0,
|
|
157
|
+
has256: colorLevel >= 2,
|
|
158
|
+
hasTrueColor: colorLevel >= 3,
|
|
159
|
+
unicode,
|
|
160
|
+
isTTY,
|
|
161
|
+
isCI,
|
|
162
|
+
isDumb,
|
|
163
|
+
columns,
|
|
164
|
+
rows,
|
|
165
|
+
reducedMotion
|
|
166
|
+
};
|
|
167
|
+
}
|