@inquirer/core 3.0.0 → 3.1.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/README.md +11 -5
- package/dist/cjs/index.js +27 -4
- package/dist/cjs/lib/screen-manager.js +20 -9
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/cjs/types/lib/screen-manager.d.ts +2 -0
- package/dist/esm/index.mjs +14 -3
- package/dist/esm/lib/screen-manager.mjs +15 -9
- package/dist/esm/types/index.d.mts +1 -1
- package/dist/esm/types/lib/screen-manager.d.mts +2 -0
- package/package.json +7 -8
package/README.md
CHANGED
|
@@ -108,6 +108,7 @@ Those hooks are matching the React hooks API:
|
|
|
108
108
|
And those are custom utilities from Inquirer:
|
|
109
109
|
|
|
110
110
|
- `useKeypress`
|
|
111
|
+
- `usePagination`
|
|
111
112
|
- `usePrefix`
|
|
112
113
|
|
|
113
114
|
### Key utilities
|
|
@@ -121,17 +122,22 @@ Listening for keypress events inside an inquirer prompt is a very common pattern
|
|
|
121
122
|
- `isDownKey()` - Note: this utility will handle vim and emacs keybindings (down, `j`, and `ctrl+n`)
|
|
122
123
|
- `isNumberKey()` one of 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
|
|
123
124
|
|
|
124
|
-
### `
|
|
125
|
+
### `usePagination`
|
|
125
126
|
|
|
126
|
-
When looping through a long list of options (like in the `select` prompt), paginating the results appearing on the screen at once can be necessary. The `
|
|
127
|
+
When looping through a long list of options (like in the `select` prompt), paginating the results appearing on the screen at once can be necessary. The `usePagination` hook is the utility used within the `select` and `checkbox` prompt to cycle through the list of options.
|
|
127
128
|
|
|
128
129
|
```js
|
|
129
130
|
export default createPrompt((config, done) => {
|
|
130
|
-
const
|
|
131
|
+
const [cursorPosition, setCursorPosition] = useState(0);
|
|
131
132
|
|
|
132
|
-
const
|
|
133
|
+
const allChoices = config.choices.map((choice) => choice.name);
|
|
133
134
|
|
|
134
|
-
|
|
135
|
+
const windowedChoices = usePagination(allChoices, {
|
|
136
|
+
active: cursorPosition,
|
|
137
|
+
pageSize: config.pageSize,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return `... ${windowedChoices}`;
|
|
135
141
|
});
|
|
136
142
|
```
|
|
137
143
|
|
package/dist/cjs/index.js
CHANGED
|
@@ -10,6 +10,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
13
25
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
27
|
};
|
|
@@ -18,7 +30,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
18
30
|
};
|
|
19
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
32
|
exports.createPrompt = exports.usePagination = exports.useRef = exports.useKeypress = exports.useEffect = exports.useState = exports.usePrefix = void 0;
|
|
21
|
-
const
|
|
33
|
+
const readline = __importStar(require("node:readline"));
|
|
22
34
|
const type_1 = require("@inquirer/type");
|
|
23
35
|
const chalk_1 = __importDefault(require("chalk"));
|
|
24
36
|
const cli_width_1 = __importDefault(require("cli-width"));
|
|
@@ -175,7 +187,7 @@ function createPrompt(view) {
|
|
|
175
187
|
// Add mute capabilities to the output
|
|
176
188
|
const output = new mute_stream_1.default();
|
|
177
189
|
output.pipe((_b = context === null || context === void 0 ? void 0 : context.output) !== null && _b !== void 0 ? _b : process.stdout);
|
|
178
|
-
sessionRl =
|
|
190
|
+
sessionRl = readline.createInterface({
|
|
179
191
|
terminal: true,
|
|
180
192
|
input,
|
|
181
193
|
output,
|
|
@@ -183,6 +195,9 @@ function createPrompt(view) {
|
|
|
183
195
|
const screen = new screen_manager_mjs_1.default(sessionRl);
|
|
184
196
|
let cancel = () => { };
|
|
185
197
|
const answer = new type_1.CancelablePromise((resolve, reject) => {
|
|
198
|
+
const checkCursorPos = () => {
|
|
199
|
+
screen.checkCursorPos();
|
|
200
|
+
};
|
|
186
201
|
const onExit = () => {
|
|
187
202
|
try {
|
|
188
203
|
let len = hooksCleanup.length;
|
|
@@ -200,8 +215,9 @@ function createPrompt(view) {
|
|
|
200
215
|
screen.clearContent();
|
|
201
216
|
}
|
|
202
217
|
screen.done();
|
|
203
|
-
resetHookState();
|
|
204
218
|
process.removeListener('SIGINT', onForceExit);
|
|
219
|
+
sessionRl === null || sessionRl === void 0 ? void 0 : sessionRl.input.removeListener('keypress', checkCursorPos);
|
|
220
|
+
resetHookState();
|
|
205
221
|
};
|
|
206
222
|
cancel = () => {
|
|
207
223
|
onExit();
|
|
@@ -243,7 +259,14 @@ function createPrompt(view) {
|
|
|
243
259
|
}
|
|
244
260
|
};
|
|
245
261
|
// TODO: we should display a loader while we get the default options.
|
|
246
|
-
(0, options_mjs_1.getPromptConfig)(config).then(
|
|
262
|
+
(0, options_mjs_1.getPromptConfig)(config).then((resolvedConfig) => {
|
|
263
|
+
workLoop(resolvedConfig);
|
|
264
|
+
// Re-renders only happen when the state change; but the readline cursor could change position
|
|
265
|
+
// and that also requires a re-render (and a manual one because we mute the streams).
|
|
266
|
+
// We set the listener after the initial workLoop to avoid a double render if render triggered
|
|
267
|
+
// by a state change sets the cursor to the right position.
|
|
268
|
+
sessionRl === null || sessionRl === void 0 ? void 0 : sessionRl.input.on('keypress', checkCursorPos);
|
|
269
|
+
}, reject);
|
|
247
270
|
});
|
|
248
271
|
answer.catch(() => {
|
|
249
272
|
resetHookState();
|
|
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const cli_width_1 = __importDefault(require("cli-width"));
|
|
7
7
|
const strip_ansi_1 = __importDefault(require("strip-ansi"));
|
|
8
|
-
const string_width_1 = __importDefault(require("string-width"));
|
|
9
8
|
const ansi_escapes_1 = __importDefault(require("ansi-escapes"));
|
|
10
9
|
const utils_mjs_1 = require('./utils.js');
|
|
11
10
|
const height = (content) => content.split('\n').length;
|
|
@@ -31,7 +30,14 @@ class ScreenManager {
|
|
|
31
30
|
writable: true,
|
|
32
31
|
value: 0
|
|
33
32
|
});
|
|
33
|
+
Object.defineProperty(this, "cursorPos", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: void 0
|
|
38
|
+
});
|
|
34
39
|
this.rl = rl;
|
|
40
|
+
this.cursorPos = rl.getCursorPos();
|
|
35
41
|
}
|
|
36
42
|
render(content, bottomContent = '') {
|
|
37
43
|
this.clean();
|
|
@@ -50,7 +56,7 @@ class ScreenManager {
|
|
|
50
56
|
}
|
|
51
57
|
this.rl.setPrompt(prompt);
|
|
52
58
|
// SetPrompt will change cursor position, now we can get correct value
|
|
53
|
-
|
|
59
|
+
this.cursorPos = this.rl.getCursorPos();
|
|
54
60
|
const width = (0, cli_width_1.default)({ defaultWidth: 80, output: this.rl.output });
|
|
55
61
|
content = (0, utils_mjs_1.breakLines)(content, width);
|
|
56
62
|
bottomContent = (0, utils_mjs_1.breakLines)(bottomContent, width);
|
|
@@ -66,17 +72,13 @@ class ScreenManager {
|
|
|
66
72
|
*/
|
|
67
73
|
// We need to consider parts of the prompt under the cursor as part of the bottom
|
|
68
74
|
// content in order to correctly cleanup and re-render.
|
|
69
|
-
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - cursorPos.rows;
|
|
75
|
+
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - this.cursorPos.rows;
|
|
70
76
|
const bottomContentHeight = promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
|
|
71
77
|
// Return cursor to the input position (on top of the bottomContent)
|
|
72
78
|
if (bottomContentHeight > 0)
|
|
73
79
|
output += ansi_escapes_1.default.cursorUp(bottomContentHeight);
|
|
74
|
-
//
|
|
75
|
-
|
|
76
|
-
if (backward > 0)
|
|
77
|
-
output += ansi_escapes_1.default.cursorBackward(backward);
|
|
78
|
-
if (cursorPos.cols > 0)
|
|
79
|
-
output += ansi_escapes_1.default.cursorForward(cursorPos.cols);
|
|
80
|
+
// Return cursor to the initial left offset.
|
|
81
|
+
output += ansi_escapes_1.default.cursorTo(this.cursorPos.cols);
|
|
80
82
|
/**
|
|
81
83
|
* Set up state for next re-rendering
|
|
82
84
|
*/
|
|
@@ -85,6 +87,15 @@ class ScreenManager {
|
|
|
85
87
|
this.rl.output.write(output);
|
|
86
88
|
this.rl.output.mute();
|
|
87
89
|
}
|
|
90
|
+
checkCursorPos() {
|
|
91
|
+
const cursorPos = this.rl.getCursorPos();
|
|
92
|
+
if (cursorPos.cols !== this.cursorPos.cols) {
|
|
93
|
+
this.rl.output.unmute();
|
|
94
|
+
this.rl.output.write(ansi_escapes_1.default.cursorTo(cursorPos.cols));
|
|
95
|
+
this.rl.output.mute();
|
|
96
|
+
this.cursorPos = cursorPos;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
88
99
|
clean() {
|
|
89
100
|
this.rl.output.unmute();
|
|
90
101
|
this.rl.output.write([
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import readline from 'node:readline';
|
|
3
|
+
import * as readline from 'node:readline';
|
|
4
4
|
import { type Prompt } from '@inquirer/type';
|
|
5
5
|
import MuteStream from 'mute-stream';
|
|
6
6
|
export { usePrefix } from './lib/prefix.js';
|
|
@@ -3,8 +3,10 @@ export default class ScreenManager {
|
|
|
3
3
|
private readonly rl;
|
|
4
4
|
private height;
|
|
5
5
|
private extraLinesUnderPrompt;
|
|
6
|
+
private cursorPos;
|
|
6
7
|
constructor(rl: InquirerReadline);
|
|
7
8
|
render(content: string, bottomContent?: string): void;
|
|
9
|
+
checkCursorPos(): void;
|
|
8
10
|
clean(): void;
|
|
9
11
|
clearContent(): void;
|
|
10
12
|
done(): void;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import readline from 'node:readline';
|
|
1
|
+
import * as readline from 'node:readline';
|
|
2
2
|
import { CancelablePromise } from '@inquirer/type';
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import cliWidth from 'cli-width';
|
|
@@ -156,6 +156,9 @@ export function createPrompt(view) {
|
|
|
156
156
|
const screen = new ScreenManager(sessionRl);
|
|
157
157
|
let cancel = () => { };
|
|
158
158
|
const answer = new CancelablePromise((resolve, reject) => {
|
|
159
|
+
const checkCursorPos = () => {
|
|
160
|
+
screen.checkCursorPos();
|
|
161
|
+
};
|
|
159
162
|
const onExit = () => {
|
|
160
163
|
try {
|
|
161
164
|
let len = hooksCleanup.length;
|
|
@@ -173,8 +176,9 @@ export function createPrompt(view) {
|
|
|
173
176
|
screen.clearContent();
|
|
174
177
|
}
|
|
175
178
|
screen.done();
|
|
176
|
-
resetHookState();
|
|
177
179
|
process.removeListener('SIGINT', onForceExit);
|
|
180
|
+
sessionRl?.input.removeListener('keypress', checkCursorPos);
|
|
181
|
+
resetHookState();
|
|
178
182
|
};
|
|
179
183
|
cancel = () => {
|
|
180
184
|
onExit();
|
|
@@ -216,7 +220,14 @@ export function createPrompt(view) {
|
|
|
216
220
|
}
|
|
217
221
|
};
|
|
218
222
|
// TODO: we should display a loader while we get the default options.
|
|
219
|
-
getPromptConfig(config).then(
|
|
223
|
+
getPromptConfig(config).then((resolvedConfig) => {
|
|
224
|
+
workLoop(resolvedConfig);
|
|
225
|
+
// Re-renders only happen when the state change; but the readline cursor could change position
|
|
226
|
+
// and that also requires a re-render (and a manual one because we mute the streams).
|
|
227
|
+
// We set the listener after the initial workLoop to avoid a double render if render triggered
|
|
228
|
+
// by a state change sets the cursor to the right position.
|
|
229
|
+
sessionRl?.input.on('keypress', checkCursorPos);
|
|
230
|
+
}, reject);
|
|
220
231
|
});
|
|
221
232
|
answer.catch(() => {
|
|
222
233
|
resetHookState();
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import cliWidth from 'cli-width';
|
|
2
2
|
import stripAnsi from 'strip-ansi';
|
|
3
|
-
import stringWidth from 'string-width';
|
|
4
3
|
import ansiEscapes from 'ansi-escapes';
|
|
5
4
|
import { breakLines } from './utils.mjs';
|
|
6
5
|
const height = (content) => content.split('\n').length;
|
|
@@ -10,9 +9,11 @@ export default class ScreenManager {
|
|
|
10
9
|
// These variables are keeping information to allow correct prompt re-rendering
|
|
11
10
|
height = 0;
|
|
12
11
|
extraLinesUnderPrompt = 0;
|
|
12
|
+
cursorPos;
|
|
13
13
|
constructor(rl) {
|
|
14
14
|
this.rl = rl;
|
|
15
15
|
this.rl = rl;
|
|
16
|
+
this.cursorPos = rl.getCursorPos();
|
|
16
17
|
}
|
|
17
18
|
render(content, bottomContent = '') {
|
|
18
19
|
this.clean();
|
|
@@ -31,7 +32,7 @@ export default class ScreenManager {
|
|
|
31
32
|
}
|
|
32
33
|
this.rl.setPrompt(prompt);
|
|
33
34
|
// SetPrompt will change cursor position, now we can get correct value
|
|
34
|
-
|
|
35
|
+
this.cursorPos = this.rl.getCursorPos();
|
|
35
36
|
const width = cliWidth({ defaultWidth: 80, output: this.rl.output });
|
|
36
37
|
content = breakLines(content, width);
|
|
37
38
|
bottomContent = breakLines(bottomContent, width);
|
|
@@ -47,17 +48,13 @@ export default class ScreenManager {
|
|
|
47
48
|
*/
|
|
48
49
|
// We need to consider parts of the prompt under the cursor as part of the bottom
|
|
49
50
|
// content in order to correctly cleanup and re-render.
|
|
50
|
-
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - cursorPos.rows;
|
|
51
|
+
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - this.cursorPos.rows;
|
|
51
52
|
const bottomContentHeight = promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
|
|
52
53
|
// Return cursor to the input position (on top of the bottomContent)
|
|
53
54
|
if (bottomContentHeight > 0)
|
|
54
55
|
output += ansiEscapes.cursorUp(bottomContentHeight);
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
if (backward > 0)
|
|
58
|
-
output += ansiEscapes.cursorBackward(backward);
|
|
59
|
-
if (cursorPos.cols > 0)
|
|
60
|
-
output += ansiEscapes.cursorForward(cursorPos.cols);
|
|
56
|
+
// Return cursor to the initial left offset.
|
|
57
|
+
output += ansiEscapes.cursorTo(this.cursorPos.cols);
|
|
61
58
|
/**
|
|
62
59
|
* Set up state for next re-rendering
|
|
63
60
|
*/
|
|
@@ -66,6 +63,15 @@ export default class ScreenManager {
|
|
|
66
63
|
this.rl.output.write(output);
|
|
67
64
|
this.rl.output.mute();
|
|
68
65
|
}
|
|
66
|
+
checkCursorPos() {
|
|
67
|
+
const cursorPos = this.rl.getCursorPos();
|
|
68
|
+
if (cursorPos.cols !== this.cursorPos.cols) {
|
|
69
|
+
this.rl.output.unmute();
|
|
70
|
+
this.rl.output.write(ansiEscapes.cursorTo(cursorPos.cols));
|
|
71
|
+
this.rl.output.mute();
|
|
72
|
+
this.cursorPos = cursorPos;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
69
75
|
clean() {
|
|
70
76
|
this.rl.output.unmute();
|
|
71
77
|
this.rl.output.write([
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
-
import readline from 'node:readline';
|
|
3
|
+
import * as readline from 'node:readline';
|
|
4
4
|
import { type Prompt } from '@inquirer/type';
|
|
5
5
|
import MuteStream from 'mute-stream';
|
|
6
6
|
export { usePrefix } from './lib/prefix.mjs';
|
|
@@ -3,8 +3,10 @@ export default class ScreenManager {
|
|
|
3
3
|
private readonly rl;
|
|
4
4
|
private height;
|
|
5
5
|
private extraLinesUnderPrompt;
|
|
6
|
+
private cursorPos;
|
|
6
7
|
constructor(rl: InquirerReadline);
|
|
7
8
|
render(content: string, bottomContent?: string): void;
|
|
9
|
+
checkCursorPos(): void;
|
|
8
10
|
clean(): void;
|
|
9
11
|
clearContent(): void;
|
|
10
12
|
done(): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=14.18.0"
|
|
6
6
|
},
|
|
@@ -59,21 +59,20 @@
|
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@inquirer/type": "^1.1.1",
|
|
61
61
|
"@types/mute-stream": "^0.0.1",
|
|
62
|
-
"@types/node": "^20.4.
|
|
62
|
+
"@types/node": "^20.4.8",
|
|
63
63
|
"@types/wrap-ansi": "^3.0.0",
|
|
64
64
|
"ansi-escapes": "^4.3.2",
|
|
65
65
|
"chalk": "^4.1.2",
|
|
66
|
-
"cli-spinners": "^2.
|
|
67
|
-
"cli-width": "^4.
|
|
66
|
+
"cli-spinners": "^2.9.0",
|
|
67
|
+
"cli-width": "^4.1.0",
|
|
68
68
|
"figures": "^3.2.0",
|
|
69
69
|
"mute-stream": "^1.0.0",
|
|
70
70
|
"run-async": "^3.0.0",
|
|
71
|
-
"string-width": "^4.2.3",
|
|
72
71
|
"strip-ansi": "^6.0.1",
|
|
73
|
-
"wrap-ansi": "^6.0
|
|
72
|
+
"wrap-ansi": "^6.2.0"
|
|
74
73
|
},
|
|
75
74
|
"devDependencies": {
|
|
76
|
-
"@inquirer/testing": "^2.1.
|
|
75
|
+
"@inquirer/testing": "^2.1.2"
|
|
77
76
|
},
|
|
78
77
|
"scripts": {
|
|
79
78
|
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
|
|
@@ -97,5 +96,5 @@
|
|
|
97
96
|
}
|
|
98
97
|
}
|
|
99
98
|
},
|
|
100
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "dd31d57ed3b1d0771e24cc34bc72e5bedb63a1d7"
|
|
101
100
|
}
|