@inquirer/core 2.3.1 → 3.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/dist/cjs/index.js +46 -4
- package/dist/cjs/lib/screen-manager.js +20 -9
- package/dist/cjs/lib/utils.js +3 -3
- package/dist/cjs/types/index.d.ts +4 -1
- package/dist/cjs/types/lib/screen-manager.d.ts +2 -0
- package/dist/esm/index.mjs +44 -3
- package/dist/esm/lib/screen-manager.mjs +15 -9
- package/dist/esm/lib/utils.mjs +3 -3
- package/dist/esm/types/index.d.mts +4 -1
- package/dist/esm/types/lib/screen-manager.d.mts +2 -0
- package/package.json +2 -3
- package/dist/cjs/lib/Paginator.js +0 -53
- package/dist/cjs/types/lib/Paginator.d.ts +0 -9
- package/dist/esm/lib/Paginator.mjs +0 -34
- package/dist/esm/types/lib/Paginator.d.mts +0 -9
package/dist/cjs/index.js
CHANGED
|
@@ -17,16 +17,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.createPrompt = exports.useRef = exports.useKeypress = exports.useEffect = exports.useState = exports.usePrefix = void 0;
|
|
20
|
+
exports.createPrompt = exports.usePagination = exports.useRef = exports.useKeypress = exports.useEffect = exports.useState = exports.usePrefix = void 0;
|
|
21
21
|
const node_readline_1 = __importDefault(require("node:readline"));
|
|
22
22
|
const type_1 = require("@inquirer/type");
|
|
23
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
24
|
+
const cli_width_1 = __importDefault(require("cli-width"));
|
|
23
25
|
const mute_stream_1 = __importDefault(require("mute-stream"));
|
|
24
26
|
const screen_manager_mjs_1 = __importDefault(require('./lib/screen-manager.js'));
|
|
25
27
|
const options_mjs_1 = require('./lib/options.js');
|
|
28
|
+
const utils_mjs_1 = require('./lib/utils.js');
|
|
26
29
|
var prefix_mjs_1 = require('./lib/prefix.js');
|
|
27
30
|
Object.defineProperty(exports, "usePrefix", { enumerable: true, get: function () { return prefix_mjs_1.usePrefix; } });
|
|
28
31
|
__exportStar(require('./lib/key.js'), exports);
|
|
29
|
-
__exportStar(require('./lib/Paginator.js'), exports);
|
|
30
32
|
__exportStar(require('./lib/Separator.js'), exports);
|
|
31
33
|
const hooks = [];
|
|
32
34
|
const hooksCleanup = [];
|
|
@@ -133,6 +135,35 @@ function useRef(val) {
|
|
|
133
135
|
return useState({ current: val })[0];
|
|
134
136
|
}
|
|
135
137
|
exports.useRef = useRef;
|
|
138
|
+
function usePagination(output, { active, pageSize = 7, }) {
|
|
139
|
+
const state = useRef({
|
|
140
|
+
pointer: 0,
|
|
141
|
+
lastIndex: 0,
|
|
142
|
+
});
|
|
143
|
+
const rl = sessionRl;
|
|
144
|
+
if (!rl) {
|
|
145
|
+
throw new Error('usePagination must be used within a prompt');
|
|
146
|
+
}
|
|
147
|
+
const width = (0, cli_width_1.default)({ defaultWidth: 80, output: rl.output });
|
|
148
|
+
const lines = (0, utils_mjs_1.breakLines)(output, width).split('\n');
|
|
149
|
+
// Make sure there's enough lines to paginate
|
|
150
|
+
if (lines.length <= pageSize) {
|
|
151
|
+
return output;
|
|
152
|
+
}
|
|
153
|
+
const middleOfList = Math.floor(pageSize / 2);
|
|
154
|
+
// Move the pointer only when the user go down and limit it to the middle of the list
|
|
155
|
+
const { pointer: prevPointer, lastIndex } = state.current;
|
|
156
|
+
if (prevPointer < middleOfList && lastIndex < active && active - lastIndex < pageSize) {
|
|
157
|
+
state.current.pointer = Math.min(middleOfList, prevPointer + active - lastIndex);
|
|
158
|
+
}
|
|
159
|
+
state.current.lastIndex = active;
|
|
160
|
+
// Duplicate the lines so it give an infinite list look
|
|
161
|
+
const infinite = [lines, lines, lines].flat();
|
|
162
|
+
const topIndex = Math.max(0, active + lines.length - state.current.pointer);
|
|
163
|
+
const section = infinite.splice(topIndex, pageSize).join('\n');
|
|
164
|
+
return section + '\n' + chalk_1.default.dim('(Move up and down to reveal more choices)');
|
|
165
|
+
}
|
|
166
|
+
exports.usePagination = usePagination;
|
|
136
167
|
function createPrompt(view) {
|
|
137
168
|
const prompt = (config, context) => {
|
|
138
169
|
var _a, _b;
|
|
@@ -152,6 +183,9 @@ function createPrompt(view) {
|
|
|
152
183
|
const screen = new screen_manager_mjs_1.default(sessionRl);
|
|
153
184
|
let cancel = () => { };
|
|
154
185
|
const answer = new type_1.CancelablePromise((resolve, reject) => {
|
|
186
|
+
const checkCursorPos = () => {
|
|
187
|
+
screen.checkCursorPos();
|
|
188
|
+
};
|
|
155
189
|
const onExit = () => {
|
|
156
190
|
try {
|
|
157
191
|
let len = hooksCleanup.length;
|
|
@@ -169,8 +203,9 @@ function createPrompt(view) {
|
|
|
169
203
|
screen.clearContent();
|
|
170
204
|
}
|
|
171
205
|
screen.done();
|
|
172
|
-
resetHookState();
|
|
173
206
|
process.removeListener('SIGINT', onForceExit);
|
|
207
|
+
sessionRl === null || sessionRl === void 0 ? void 0 : sessionRl.input.removeListener('keypress', checkCursorPos);
|
|
208
|
+
resetHookState();
|
|
174
209
|
};
|
|
175
210
|
cancel = () => {
|
|
176
211
|
onExit();
|
|
@@ -212,7 +247,14 @@ function createPrompt(view) {
|
|
|
212
247
|
}
|
|
213
248
|
};
|
|
214
249
|
// TODO: we should display a loader while we get the default options.
|
|
215
|
-
(0, options_mjs_1.getPromptConfig)(config).then(
|
|
250
|
+
(0, options_mjs_1.getPromptConfig)(config).then((resolvedConfig) => {
|
|
251
|
+
workLoop(resolvedConfig);
|
|
252
|
+
// Re-renders only happen when the state change; but the readline cursor could change position
|
|
253
|
+
// and that also requires a re-render (and a manual one because we mute the streams).
|
|
254
|
+
// We set the listener after the initial workLoop to avoid a double render if render triggered
|
|
255
|
+
// by a state change sets the cursor to the right position.
|
|
256
|
+
sessionRl === null || sessionRl === void 0 ? void 0 : sessionRl.input.on('keypress', checkCursorPos);
|
|
257
|
+
}, reject);
|
|
216
258
|
});
|
|
217
259
|
answer.catch(() => {
|
|
218
260
|
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([
|
package/dist/cjs/lib/utils.js
CHANGED
|
@@ -14,8 +14,8 @@ const wrap_ansi_1 = __importDefault(require("wrap-ansi"));
|
|
|
14
14
|
*/
|
|
15
15
|
const breakLines = (content, width) => content
|
|
16
16
|
.split('\n')
|
|
17
|
-
.
|
|
18
|
-
.
|
|
19
|
-
.map((line) => line.trimEnd())
|
|
17
|
+
.flatMap((line) => (0, wrap_ansi_1.default)(line, width, { trim: false, hard: true })
|
|
18
|
+
.split('\n')
|
|
19
|
+
.map((line) => line.trimEnd()))
|
|
20
20
|
.join('\n');
|
|
21
21
|
exports.breakLines = breakLines;
|
|
@@ -5,7 +5,6 @@ import { type Prompt } from '@inquirer/type';
|
|
|
5
5
|
import MuteStream from 'mute-stream';
|
|
6
6
|
export { usePrefix } from './lib/prefix.js';
|
|
7
7
|
export * from './lib/key.js';
|
|
8
|
-
export * from './lib/Paginator.js';
|
|
9
8
|
export * from './lib/Separator.js';
|
|
10
9
|
export type InquirerReadline = readline.ReadLine & {
|
|
11
10
|
output: MuteStream;
|
|
@@ -23,6 +22,10 @@ export declare function useKeypress(userHandler: (event: KeypressEvent, rl: Inqu
|
|
|
23
22
|
export declare function useRef<Value>(val: Value): {
|
|
24
23
|
current: Value;
|
|
25
24
|
};
|
|
25
|
+
export declare function usePagination(output: string, { active, pageSize, }: {
|
|
26
|
+
active: number;
|
|
27
|
+
pageSize?: number;
|
|
28
|
+
}): string;
|
|
26
29
|
export type AsyncPromptConfig = {
|
|
27
30
|
message: string | Promise<string> | (() => Promise<string>);
|
|
28
31
|
validate?: (value: string) => boolean | string | Promise<string | boolean>;
|
|
@@ -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,11 +1,13 @@
|
|
|
1
1
|
import readline from 'node:readline';
|
|
2
2
|
import { CancelablePromise } from '@inquirer/type';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import cliWidth from 'cli-width';
|
|
3
5
|
import MuteStream from 'mute-stream';
|
|
4
6
|
import ScreenManager from './lib/screen-manager.mjs';
|
|
5
7
|
import { getPromptConfig } from './lib/options.mjs';
|
|
8
|
+
import { breakLines } from './lib/utils.mjs';
|
|
6
9
|
export { usePrefix } from './lib/prefix.mjs';
|
|
7
10
|
export * from './lib/key.mjs';
|
|
8
|
-
export * from './lib/Paginator.mjs';
|
|
9
11
|
export * from './lib/Separator.mjs';
|
|
10
12
|
const hooks = [];
|
|
11
13
|
const hooksCleanup = [];
|
|
@@ -108,6 +110,34 @@ export function useKeypress(userHandler) {
|
|
|
108
110
|
export function useRef(val) {
|
|
109
111
|
return useState({ current: val })[0];
|
|
110
112
|
}
|
|
113
|
+
export function usePagination(output, { active, pageSize = 7, }) {
|
|
114
|
+
const state = useRef({
|
|
115
|
+
pointer: 0,
|
|
116
|
+
lastIndex: 0,
|
|
117
|
+
});
|
|
118
|
+
const rl = sessionRl;
|
|
119
|
+
if (!rl) {
|
|
120
|
+
throw new Error('usePagination must be used within a prompt');
|
|
121
|
+
}
|
|
122
|
+
const width = cliWidth({ defaultWidth: 80, output: rl.output });
|
|
123
|
+
const lines = breakLines(output, width).split('\n');
|
|
124
|
+
// Make sure there's enough lines to paginate
|
|
125
|
+
if (lines.length <= pageSize) {
|
|
126
|
+
return output;
|
|
127
|
+
}
|
|
128
|
+
const middleOfList = Math.floor(pageSize / 2);
|
|
129
|
+
// Move the pointer only when the user go down and limit it to the middle of the list
|
|
130
|
+
const { pointer: prevPointer, lastIndex } = state.current;
|
|
131
|
+
if (prevPointer < middleOfList && lastIndex < active && active - lastIndex < pageSize) {
|
|
132
|
+
state.current.pointer = Math.min(middleOfList, prevPointer + active - lastIndex);
|
|
133
|
+
}
|
|
134
|
+
state.current.lastIndex = active;
|
|
135
|
+
// Duplicate the lines so it give an infinite list look
|
|
136
|
+
const infinite = [lines, lines, lines].flat();
|
|
137
|
+
const topIndex = Math.max(0, active + lines.length - state.current.pointer);
|
|
138
|
+
const section = infinite.splice(topIndex, pageSize).join('\n');
|
|
139
|
+
return section + '\n' + chalk.dim('(Move up and down to reveal more choices)');
|
|
140
|
+
}
|
|
111
141
|
export function createPrompt(view) {
|
|
112
142
|
const prompt = (config, context) => {
|
|
113
143
|
if (sessionRl) {
|
|
@@ -126,6 +156,9 @@ export function createPrompt(view) {
|
|
|
126
156
|
const screen = new ScreenManager(sessionRl);
|
|
127
157
|
let cancel = () => { };
|
|
128
158
|
const answer = new CancelablePromise((resolve, reject) => {
|
|
159
|
+
const checkCursorPos = () => {
|
|
160
|
+
screen.checkCursorPos();
|
|
161
|
+
};
|
|
129
162
|
const onExit = () => {
|
|
130
163
|
try {
|
|
131
164
|
let len = hooksCleanup.length;
|
|
@@ -143,8 +176,9 @@ export function createPrompt(view) {
|
|
|
143
176
|
screen.clearContent();
|
|
144
177
|
}
|
|
145
178
|
screen.done();
|
|
146
|
-
resetHookState();
|
|
147
179
|
process.removeListener('SIGINT', onForceExit);
|
|
180
|
+
sessionRl?.input.removeListener('keypress', checkCursorPos);
|
|
181
|
+
resetHookState();
|
|
148
182
|
};
|
|
149
183
|
cancel = () => {
|
|
150
184
|
onExit();
|
|
@@ -186,7 +220,14 @@ export function createPrompt(view) {
|
|
|
186
220
|
}
|
|
187
221
|
};
|
|
188
222
|
// TODO: we should display a loader while we get the default options.
|
|
189
|
-
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);
|
|
190
231
|
});
|
|
191
232
|
answer.catch(() => {
|
|
192
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([
|
package/dist/esm/lib/utils.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import wrapAnsi from 'wrap-ansi';
|
|
|
8
8
|
*/
|
|
9
9
|
export const breakLines = (content, width) => content
|
|
10
10
|
.split('\n')
|
|
11
|
-
.
|
|
12
|
-
.
|
|
13
|
-
.map((line) => line.trimEnd())
|
|
11
|
+
.flatMap((line) => wrapAnsi(line, width, { trim: false, hard: true })
|
|
12
|
+
.split('\n')
|
|
13
|
+
.map((line) => line.trimEnd()))
|
|
14
14
|
.join('\n');
|
|
@@ -5,7 +5,6 @@ import { type Prompt } from '@inquirer/type';
|
|
|
5
5
|
import MuteStream from 'mute-stream';
|
|
6
6
|
export { usePrefix } from './lib/prefix.mjs';
|
|
7
7
|
export * from './lib/key.mjs';
|
|
8
|
-
export * from './lib/Paginator.mjs';
|
|
9
8
|
export * from './lib/Separator.mjs';
|
|
10
9
|
export type InquirerReadline = readline.ReadLine & {
|
|
11
10
|
output: MuteStream;
|
|
@@ -23,6 +22,10 @@ export declare function useKeypress(userHandler: (event: KeypressEvent, rl: Inqu
|
|
|
23
22
|
export declare function useRef<Value>(val: Value): {
|
|
24
23
|
current: Value;
|
|
25
24
|
};
|
|
25
|
+
export declare function usePagination(output: string, { active, pageSize, }: {
|
|
26
|
+
active: number;
|
|
27
|
+
pageSize?: number;
|
|
28
|
+
}): string;
|
|
26
29
|
export type AsyncPromptConfig = {
|
|
27
30
|
message: string | Promise<string> | (() => Promise<string>);
|
|
28
31
|
validate?: (value: string) => boolean | string | Promise<string | boolean>;
|
|
@@ -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
|
+
"version": "3.1.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=14.18.0"
|
|
6
6
|
},
|
|
@@ -68,7 +68,6 @@
|
|
|
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
72
|
"wrap-ansi": "^6.0.1"
|
|
74
73
|
},
|
|
@@ -97,5 +96,5 @@
|
|
|
97
96
|
}
|
|
98
97
|
}
|
|
99
98
|
},
|
|
100
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "ab9dd2f301e02d2cc0fec699ce100b174f2b798e"
|
|
101
100
|
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Paginator = void 0;
|
|
7
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
-
const cli_width_1 = __importDefault(require("cli-width"));
|
|
9
|
-
const utils_mjs_1 = require('./utils.js');
|
|
10
|
-
/**
|
|
11
|
-
* The paginator keeps track of a pointer index in a list and returns
|
|
12
|
-
* a subset of the choices if the list is too long.
|
|
13
|
-
*/
|
|
14
|
-
class Paginator {
|
|
15
|
-
constructor() {
|
|
16
|
-
Object.defineProperty(this, "pointer", {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
configurable: true,
|
|
19
|
-
writable: true,
|
|
20
|
-
value: 0
|
|
21
|
-
});
|
|
22
|
-
Object.defineProperty(this, "lastIndex", {
|
|
23
|
-
enumerable: true,
|
|
24
|
-
configurable: true,
|
|
25
|
-
writable: true,
|
|
26
|
-
value: 0
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
paginate(output, active, pageSize = 7) {
|
|
30
|
-
const middleOfList = Math.floor(pageSize / 2);
|
|
31
|
-
// TODO: I've remove the dependency on readline here. But we should refactor the
|
|
32
|
-
// paginator to also rely on hook.
|
|
33
|
-
const width = (0, cli_width_1.default)({ defaultWidth: 80, output: process.stdout });
|
|
34
|
-
const lines = (0, utils_mjs_1.breakLines)(output, width).split('\n');
|
|
35
|
-
// Make sure there's enough lines to paginate
|
|
36
|
-
if (lines.length <= pageSize) {
|
|
37
|
-
return output;
|
|
38
|
-
}
|
|
39
|
-
// Move the pointer only when the user go down and limit it to the middle of the list
|
|
40
|
-
if (this.pointer < middleOfList &&
|
|
41
|
-
this.lastIndex < active &&
|
|
42
|
-
active - this.lastIndex < pageSize) {
|
|
43
|
-
this.pointer = Math.min(middleOfList, this.pointer + active - this.lastIndex);
|
|
44
|
-
}
|
|
45
|
-
this.lastIndex = active;
|
|
46
|
-
// Duplicate the lines so it give an infinite list look
|
|
47
|
-
const infinite = [lines, lines, lines].flat();
|
|
48
|
-
const topIndex = Math.max(0, active + lines.length - this.pointer);
|
|
49
|
-
const section = infinite.splice(topIndex, pageSize).join('\n');
|
|
50
|
-
return section + '\n' + chalk_1.default.dim('(Move up and down to reveal more choices)');
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
exports.Paginator = Paginator;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The paginator keeps track of a pointer index in a list and returns
|
|
3
|
-
* a subset of the choices if the list is too long.
|
|
4
|
-
*/
|
|
5
|
-
export declare class Paginator {
|
|
6
|
-
pointer: number;
|
|
7
|
-
lastIndex: number;
|
|
8
|
-
paginate(output: string, active: number, pageSize?: number): string;
|
|
9
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import cliWidth from 'cli-width';
|
|
3
|
-
import { breakLines } from './utils.mjs';
|
|
4
|
-
/**
|
|
5
|
-
* The paginator keeps track of a pointer index in a list and returns
|
|
6
|
-
* a subset of the choices if the list is too long.
|
|
7
|
-
*/
|
|
8
|
-
export class Paginator {
|
|
9
|
-
pointer = 0;
|
|
10
|
-
lastIndex = 0;
|
|
11
|
-
paginate(output, active, pageSize = 7) {
|
|
12
|
-
const middleOfList = Math.floor(pageSize / 2);
|
|
13
|
-
// TODO: I've remove the dependency on readline here. But we should refactor the
|
|
14
|
-
// paginator to also rely on hook.
|
|
15
|
-
const width = cliWidth({ defaultWidth: 80, output: process.stdout });
|
|
16
|
-
const lines = breakLines(output, width).split('\n');
|
|
17
|
-
// Make sure there's enough lines to paginate
|
|
18
|
-
if (lines.length <= pageSize) {
|
|
19
|
-
return output;
|
|
20
|
-
}
|
|
21
|
-
// Move the pointer only when the user go down and limit it to the middle of the list
|
|
22
|
-
if (this.pointer < middleOfList &&
|
|
23
|
-
this.lastIndex < active &&
|
|
24
|
-
active - this.lastIndex < pageSize) {
|
|
25
|
-
this.pointer = Math.min(middleOfList, this.pointer + active - this.lastIndex);
|
|
26
|
-
}
|
|
27
|
-
this.lastIndex = active;
|
|
28
|
-
// Duplicate the lines so it give an infinite list look
|
|
29
|
-
const infinite = [lines, lines, lines].flat();
|
|
30
|
-
const topIndex = Math.max(0, active + lines.length - this.pointer);
|
|
31
|
-
const section = infinite.splice(topIndex, pageSize).join('\n');
|
|
32
|
-
return section + '\n' + chalk.dim('(Move up and down to reveal more choices)');
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The paginator keeps track of a pointer index in a list and returns
|
|
3
|
-
* a subset of the choices if the list is too long.
|
|
4
|
-
*/
|
|
5
|
-
export declare class Paginator {
|
|
6
|
-
pointer: number;
|
|
7
|
-
lastIndex: number;
|
|
8
|
-
paginate(output: string, active: number, pageSize?: number): string;
|
|
9
|
-
}
|