inquirerer 4.9.2 → 4.9.4
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/esm/keypress.js +64 -5
- package/esm/prompt.js +9 -1
- package/esm/resolvers/git.js +2 -2
- package/keypress.d.ts +10 -0
- package/keypress.js +65 -5
- package/package.json +2 -2
- package/prompt.js +9 -1
- package/resolvers/git.js +1 -1
package/esm/keypress.js
CHANGED
|
@@ -58,6 +58,50 @@ export const KEY_CODES = {
|
|
|
58
58
|
ALT_UP: '\u001b[1;3A',
|
|
59
59
|
ALT_DOWN: '\u001b[1;3B',
|
|
60
60
|
};
|
|
61
|
+
const ESC = '\u001b';
|
|
62
|
+
const CSI_FINAL_MIN = 0x40;
|
|
63
|
+
const CSI_FINAL_MAX = 0x7e;
|
|
64
|
+
const isCsiFinal = (char) => {
|
|
65
|
+
const code = char.codePointAt(0);
|
|
66
|
+
return code >= CSI_FINAL_MIN && code <= CSI_FINAL_MAX;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Split one stdin data chunk into individual keys.
|
|
70
|
+
*
|
|
71
|
+
* A chunk is not one key: pasting, fast typing, or a terminal batching its
|
|
72
|
+
* output all deliver several keys at once. Escape sequences are kept whole —
|
|
73
|
+
* CSI (`ESC [ … final`), SS3 (`ESC O x`), and meta (`ESC x`) — and everything
|
|
74
|
+
* else is split per code point so astral characters survive.
|
|
75
|
+
*/
|
|
76
|
+
export function segmentKeys(chunk) {
|
|
77
|
+
const chars = Array.from(chunk);
|
|
78
|
+
if (chars.length <= 1)
|
|
79
|
+
return chars.length === 1 ? [chunk] : [];
|
|
80
|
+
const keys = [];
|
|
81
|
+
let i = 0;
|
|
82
|
+
while (i < chars.length) {
|
|
83
|
+
if (chars[i] !== ESC || i + 1 >= chars.length) {
|
|
84
|
+
keys.push(chars[i]);
|
|
85
|
+
i++;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
let end;
|
|
89
|
+
if (chars[i + 1] === '[') {
|
|
90
|
+
end = i + 2;
|
|
91
|
+
while (end < chars.length && !isCsiFinal(chars[end]))
|
|
92
|
+
end++;
|
|
93
|
+
end++;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// SS3 (ESC O x) and meta (ESC x) are both ESC plus one character.
|
|
97
|
+
end = i + 3 <= chars.length && chars[i + 1] === 'O' ? i + 3 : i + 2;
|
|
98
|
+
}
|
|
99
|
+
end = Math.min(end, chars.length);
|
|
100
|
+
keys.push(chars.slice(i, end).join(''));
|
|
101
|
+
i = end;
|
|
102
|
+
}
|
|
103
|
+
return keys;
|
|
104
|
+
}
|
|
61
105
|
/**
|
|
62
106
|
* Handles keyboard input for interactive prompts.
|
|
63
107
|
*
|
|
@@ -90,17 +134,32 @@ export class TerminalKeypress {
|
|
|
90
134
|
return !this.noTty;
|
|
91
135
|
}
|
|
92
136
|
setupListeners() {
|
|
93
|
-
this.dataHandler = (
|
|
137
|
+
this.dataHandler = (chunk) => {
|
|
94
138
|
if (!this.active)
|
|
95
139
|
return;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
140
|
+
// A chunk carrying several keypresses (paste, fast typing) matches no
|
|
141
|
+
// single registered key, so dispatch it key by key. An exact match wins
|
|
142
|
+
// first: consumers may register sequences the segmenter does not know.
|
|
143
|
+
const keys = this.listeners[chunk] ? [chunk] : segmentKeys(chunk);
|
|
144
|
+
for (const key of keys) {
|
|
145
|
+
if (!this.active)
|
|
146
|
+
return;
|
|
147
|
+
this.dispatch(key);
|
|
100
148
|
}
|
|
101
149
|
};
|
|
102
150
|
this.input.on('data', this.dataHandler);
|
|
103
151
|
}
|
|
152
|
+
dispatch(key) {
|
|
153
|
+
const handlers = this.listeners[key];
|
|
154
|
+
if (handlers) {
|
|
155
|
+
// Copy: a handler may register or remove handlers for the same key.
|
|
156
|
+
for (const handler of [...handlers])
|
|
157
|
+
handler();
|
|
158
|
+
}
|
|
159
|
+
if (key === KEY_CODES.CTRL_C) {
|
|
160
|
+
this.proc.exit(0);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
104
163
|
on(key, callback) {
|
|
105
164
|
if (!this.listeners[key]) {
|
|
106
165
|
this.listeners[key] = [];
|
package/esm/prompt.js
CHANGED
|
@@ -145,6 +145,10 @@ export class PromptTimeoutError extends Error {
|
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
const DEFAULT_NON_TTY_TIMEOUT = 15_000;
|
|
148
|
+
/** True when the stream is a terminal a person can type into. */
|
|
149
|
+
function isInteractiveInput(input) {
|
|
150
|
+
return input.isTTY === true;
|
|
151
|
+
}
|
|
148
152
|
export class Inquirerer {
|
|
149
153
|
rl;
|
|
150
154
|
keypress;
|
|
@@ -172,7 +176,11 @@ export class Inquirerer {
|
|
|
172
176
|
if (timeout !== undefined) {
|
|
173
177
|
this.timeout = timeout;
|
|
174
178
|
}
|
|
175
|
-
else if (!noTty) {
|
|
179
|
+
else if (!noTty && !isInteractiveInput(input)) {
|
|
180
|
+
// The default timeout exists to fail a CI run that prompts with nothing
|
|
181
|
+
// to answer it, and its error text says so. A person at a real terminal
|
|
182
|
+
// is allowed to think for as long as they like, so only arm it when the
|
|
183
|
+
// input stream is not a TTY while the prompter still expects input.
|
|
176
184
|
this.timeout = DEFAULT_NON_TTY_TIMEOUT;
|
|
177
185
|
}
|
|
178
186
|
if (!noTty) {
|
package/esm/resolvers/git.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { execFileSync } from 'child_process';
|
|
2
2
|
/**
|
|
3
3
|
* Retrieves a git config value.
|
|
4
4
|
* @param key The git config key (e.g., 'user.name', 'user.email')
|
|
@@ -6,7 +6,7 @@ import { execSync } from 'child_process';
|
|
|
6
6
|
*/
|
|
7
7
|
export function getGitConfig(key) {
|
|
8
8
|
try {
|
|
9
|
-
const result =
|
|
9
|
+
const result = execFileSync('git', ['config', '--global', '--', key], {
|
|
10
10
|
encoding: 'utf8',
|
|
11
11
|
stdio: ['pipe', 'pipe', 'ignore'] // Suppress stderr
|
|
12
12
|
});
|
package/keypress.d.ts
CHANGED
|
@@ -50,6 +50,15 @@ export declare const KEY_CODES: {
|
|
|
50
50
|
ALT_UP: string;
|
|
51
51
|
ALT_DOWN: string;
|
|
52
52
|
};
|
|
53
|
+
/**
|
|
54
|
+
* Split one stdin data chunk into individual keys.
|
|
55
|
+
*
|
|
56
|
+
* A chunk is not one key: pasting, fast typing, or a terminal batching its
|
|
57
|
+
* output all deliver several keys at once. Escape sequences are kept whole —
|
|
58
|
+
* CSI (`ESC [ … final`), SS3 (`ESC O x`), and meta (`ESC x`) — and everything
|
|
59
|
+
* else is split per code point so astral characters survive.
|
|
60
|
+
*/
|
|
61
|
+
export declare function segmentKeys(chunk: string): string[];
|
|
53
62
|
/**
|
|
54
63
|
* Handles keyboard input for interactive prompts.
|
|
55
64
|
*
|
|
@@ -71,6 +80,7 @@ export declare class TerminalKeypress {
|
|
|
71
80
|
constructor(noTty?: boolean, input?: Readable, proc?: ProcessWrapper);
|
|
72
81
|
isTTY(): boolean;
|
|
73
82
|
private setupListeners;
|
|
83
|
+
private dispatch;
|
|
74
84
|
on(key: string, callback: KeyHandler): void;
|
|
75
85
|
off(key: string, callback: KeyHandler): void;
|
|
76
86
|
clearHandlers(): void;
|
package/keypress.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TerminalKeypress = exports.KEY_CODES = void 0;
|
|
4
|
+
exports.segmentKeys = segmentKeys;
|
|
4
5
|
const defaultProcessWrapper = {
|
|
5
6
|
exit: (code) => process.exit(code)
|
|
6
7
|
};
|
|
@@ -61,6 +62,50 @@ exports.KEY_CODES = {
|
|
|
61
62
|
ALT_UP: '\u001b[1;3A',
|
|
62
63
|
ALT_DOWN: '\u001b[1;3B',
|
|
63
64
|
};
|
|
65
|
+
const ESC = '\u001b';
|
|
66
|
+
const CSI_FINAL_MIN = 0x40;
|
|
67
|
+
const CSI_FINAL_MAX = 0x7e;
|
|
68
|
+
const isCsiFinal = (char) => {
|
|
69
|
+
const code = char.codePointAt(0);
|
|
70
|
+
return code >= CSI_FINAL_MIN && code <= CSI_FINAL_MAX;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Split one stdin data chunk into individual keys.
|
|
74
|
+
*
|
|
75
|
+
* A chunk is not one key: pasting, fast typing, or a terminal batching its
|
|
76
|
+
* output all deliver several keys at once. Escape sequences are kept whole —
|
|
77
|
+
* CSI (`ESC [ … final`), SS3 (`ESC O x`), and meta (`ESC x`) — and everything
|
|
78
|
+
* else is split per code point so astral characters survive.
|
|
79
|
+
*/
|
|
80
|
+
function segmentKeys(chunk) {
|
|
81
|
+
const chars = Array.from(chunk);
|
|
82
|
+
if (chars.length <= 1)
|
|
83
|
+
return chars.length === 1 ? [chunk] : [];
|
|
84
|
+
const keys = [];
|
|
85
|
+
let i = 0;
|
|
86
|
+
while (i < chars.length) {
|
|
87
|
+
if (chars[i] !== ESC || i + 1 >= chars.length) {
|
|
88
|
+
keys.push(chars[i]);
|
|
89
|
+
i++;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
let end;
|
|
93
|
+
if (chars[i + 1] === '[') {
|
|
94
|
+
end = i + 2;
|
|
95
|
+
while (end < chars.length && !isCsiFinal(chars[end]))
|
|
96
|
+
end++;
|
|
97
|
+
end++;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
// SS3 (ESC O x) and meta (ESC x) are both ESC plus one character.
|
|
101
|
+
end = i + 3 <= chars.length && chars[i + 1] === 'O' ? i + 3 : i + 2;
|
|
102
|
+
}
|
|
103
|
+
end = Math.min(end, chars.length);
|
|
104
|
+
keys.push(chars.slice(i, end).join(''));
|
|
105
|
+
i = end;
|
|
106
|
+
}
|
|
107
|
+
return keys;
|
|
108
|
+
}
|
|
64
109
|
/**
|
|
65
110
|
* Handles keyboard input for interactive prompts.
|
|
66
111
|
*
|
|
@@ -93,17 +138,32 @@ class TerminalKeypress {
|
|
|
93
138
|
return !this.noTty;
|
|
94
139
|
}
|
|
95
140
|
setupListeners() {
|
|
96
|
-
this.dataHandler = (
|
|
141
|
+
this.dataHandler = (chunk) => {
|
|
97
142
|
if (!this.active)
|
|
98
143
|
return;
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
144
|
+
// A chunk carrying several keypresses (paste, fast typing) matches no
|
|
145
|
+
// single registered key, so dispatch it key by key. An exact match wins
|
|
146
|
+
// first: consumers may register sequences the segmenter does not know.
|
|
147
|
+
const keys = this.listeners[chunk] ? [chunk] : segmentKeys(chunk);
|
|
148
|
+
for (const key of keys) {
|
|
149
|
+
if (!this.active)
|
|
150
|
+
return;
|
|
151
|
+
this.dispatch(key);
|
|
103
152
|
}
|
|
104
153
|
};
|
|
105
154
|
this.input.on('data', this.dataHandler);
|
|
106
155
|
}
|
|
156
|
+
dispatch(key) {
|
|
157
|
+
const handlers = this.listeners[key];
|
|
158
|
+
if (handlers) {
|
|
159
|
+
// Copy: a handler may register or remove handlers for the same key.
|
|
160
|
+
for (const handler of [...handlers])
|
|
161
|
+
handler();
|
|
162
|
+
}
|
|
163
|
+
if (key === exports.KEY_CODES.CTRL_C) {
|
|
164
|
+
this.proc.exit(0);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
107
167
|
on(key, callback) {
|
|
108
168
|
if (!this.listeners[key]) {
|
|
109
169
|
this.listeners[key] = [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "4.9.
|
|
3
|
+
"version": "4.9.4",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "TypeScript-first library for building beautiful CLI interfaces with interactive prompts",
|
|
6
6
|
"main": "index.js",
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"makage": "0.1.10",
|
|
47
47
|
"string-width": "4.2.3"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "4499936e61a93af9713b916bb1845b15f42c7aa6"
|
|
50
50
|
}
|
package/prompt.js
CHANGED
|
@@ -153,6 +153,10 @@ class PromptTimeoutError extends Error {
|
|
|
153
153
|
}
|
|
154
154
|
exports.PromptTimeoutError = PromptTimeoutError;
|
|
155
155
|
const DEFAULT_NON_TTY_TIMEOUT = 15_000;
|
|
156
|
+
/** True when the stream is a terminal a person can type into. */
|
|
157
|
+
function isInteractiveInput(input) {
|
|
158
|
+
return input.isTTY === true;
|
|
159
|
+
}
|
|
156
160
|
class Inquirerer {
|
|
157
161
|
rl;
|
|
158
162
|
keypress;
|
|
@@ -180,7 +184,11 @@ class Inquirerer {
|
|
|
180
184
|
if (timeout !== undefined) {
|
|
181
185
|
this.timeout = timeout;
|
|
182
186
|
}
|
|
183
|
-
else if (!noTty) {
|
|
187
|
+
else if (!noTty && !isInteractiveInput(input)) {
|
|
188
|
+
// The default timeout exists to fail a CI run that prompts with nothing
|
|
189
|
+
// to answer it, and its error text says so. A person at a real terminal
|
|
190
|
+
// is allowed to think for as long as they like, so only arm it when the
|
|
191
|
+
// input stream is not a TTY while the prompter still expects input.
|
|
184
192
|
this.timeout = DEFAULT_NON_TTY_TIMEOUT;
|
|
185
193
|
}
|
|
186
194
|
if (!noTty) {
|
package/resolvers/git.js
CHANGED
|
@@ -10,7 +10,7 @@ const child_process_1 = require("child_process");
|
|
|
10
10
|
*/
|
|
11
11
|
function getGitConfig(key) {
|
|
12
12
|
try {
|
|
13
|
-
const result = (0, child_process_1.
|
|
13
|
+
const result = (0, child_process_1.execFileSync)('git', ['config', '--global', '--', key], {
|
|
14
14
|
encoding: 'utf8',
|
|
15
15
|
stdio: ['pipe', 'pipe', 'ignore'] // Suppress stderr
|
|
16
16
|
});
|