@inquirer/core 0.0.18-alpha.0 → 0.0.21-alpha.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/LICENSE +2 -2
- package/dist/index.d.ts +34 -0
- package/dist/index.js +105 -0
- package/dist/lib/Paginator.d.ts +9 -0
- package/dist/lib/Paginator.js +46 -0
- package/dist/lib/key.d.ts +11 -0
- package/dist/lib/key.js +6 -0
- package/dist/lib/options.d.ts +2 -0
- package/dist/lib/options.js +8 -0
- package/dist/lib/prefix.d.ts +1 -0
- package/dist/lib/prefix.js +20 -0
- package/{lib/readline.js → dist/lib/readline.d.ts} +6 -25
- package/dist/lib/readline.js +49 -0
- package/dist/lib/screen-manager.d.ts +10 -0
- package/dist/lib/screen-manager.js +93 -0
- package/dist/lib/utils.d.ts +8 -0
- package/dist/lib/utils.js +13 -0
- package/package.json +14 -8
- package/src/index.ts +163 -0
- package/{lib/Paginator.js → src/lib/Paginator.ts} +7 -15
- package/src/lib/index.d.ts +3 -0
- package/src/lib/key.ts +20 -0
- package/src/lib/options.ts +14 -0
- package/{lib/prefix.js → src/lib/prefix.ts} +8 -6
- package/src/lib/readline.ts +55 -0
- package/{lib/screen-manager.js → src/lib/screen-manager.ts} +31 -44
- package/src/lib/utils.ts +15 -0
- package/test.ts +92 -0
- package/tsconfig.json +7 -0
- package/hooks.js +0 -119
- package/index.js +0 -217
- package/lib/key.js +0 -13
- package/lib/options.js +0 -12
- package/lib/utils.js +0 -19
- package/test.js +0 -265
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c)
|
|
1
|
+
Copyright (c) 2022 Simon Boudrias
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person
|
|
4
4
|
obtaining a copy of this software and associated documentation
|
|
@@ -19,4 +19,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
19
19
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
20
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
21
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
+
import readline from 'node:readline';
|
|
4
|
+
import MuteStream from 'mute-stream';
|
|
5
|
+
export { usePrefix } from './lib/prefix.js';
|
|
6
|
+
export * from './lib/key.js';
|
|
7
|
+
export * from './lib/Paginator.js';
|
|
8
|
+
declare type StdioOptions = {
|
|
9
|
+
input?: NodeJS.ReadableStream;
|
|
10
|
+
output?: NodeJS.WritableStream;
|
|
11
|
+
};
|
|
12
|
+
export declare type InquirerReadline = readline.ReadLine & {
|
|
13
|
+
output: MuteStream;
|
|
14
|
+
input: NodeJS.ReadableStream;
|
|
15
|
+
};
|
|
16
|
+
export declare type KeypressEvent = {
|
|
17
|
+
name: string;
|
|
18
|
+
ctrl: boolean;
|
|
19
|
+
};
|
|
20
|
+
export declare function useState<Value>(defaultValue: Value): [Value, (newValue: Value) => void];
|
|
21
|
+
export declare function useEffect(cb: () => void | (() => void), depArray: unknown[]): void;
|
|
22
|
+
export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
|
|
23
|
+
export declare function useRef<Value>(val: Value): {
|
|
24
|
+
current: Value;
|
|
25
|
+
};
|
|
26
|
+
export declare type AsyncPromptConfig = {
|
|
27
|
+
message: string | Promise<string> | (() => Promise<string>);
|
|
28
|
+
validate?: () => boolean;
|
|
29
|
+
};
|
|
30
|
+
export declare type ResolvedPromptConfig = {
|
|
31
|
+
message: string;
|
|
32
|
+
validate: () => boolean;
|
|
33
|
+
};
|
|
34
|
+
export declare function createPrompt<Value, Config extends AsyncPromptConfig>(view: (config: Config & ResolvedPromptConfig, done: (value: Value) => void) => string | [string, string | undefined]): (options: Config, stdio?: StdioOptions) => Promise<Value>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import readline from 'node:readline';
|
|
2
|
+
import MuteStream from 'mute-stream';
|
|
3
|
+
import ScreenManager from './lib/screen-manager.js';
|
|
4
|
+
import { getPromptConfig } from './lib/options.js';
|
|
5
|
+
export { usePrefix } from './lib/prefix.js';
|
|
6
|
+
export * from './lib/key.js';
|
|
7
|
+
export * from './lib/Paginator.js';
|
|
8
|
+
let sessionRl;
|
|
9
|
+
let hooks = [];
|
|
10
|
+
const hooksCleanup = [];
|
|
11
|
+
let index = 0;
|
|
12
|
+
let handleChange = () => { };
|
|
13
|
+
const cleanupHook = (index) => {
|
|
14
|
+
const cleanFn = hooksCleanup[index];
|
|
15
|
+
if (typeof cleanFn === 'function') {
|
|
16
|
+
cleanFn();
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
export function useState(defaultValue) {
|
|
20
|
+
const _idx = index;
|
|
21
|
+
const value = _idx in hooks ? hooks[_idx] : defaultValue;
|
|
22
|
+
index++;
|
|
23
|
+
return [
|
|
24
|
+
value,
|
|
25
|
+
(newValue) => {
|
|
26
|
+
hooks[_idx] = newValue;
|
|
27
|
+
// Trigger re-render
|
|
28
|
+
handleChange();
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
export function useEffect(cb, depArray) {
|
|
33
|
+
const _idx = index;
|
|
34
|
+
const oldDeps = hooks[_idx];
|
|
35
|
+
let hasChanged = true;
|
|
36
|
+
if (oldDeps) {
|
|
37
|
+
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
38
|
+
}
|
|
39
|
+
if (hasChanged) {
|
|
40
|
+
cleanupHook(_idx);
|
|
41
|
+
hooksCleanup[_idx] = cb();
|
|
42
|
+
}
|
|
43
|
+
hooks[_idx] = depArray;
|
|
44
|
+
index++;
|
|
45
|
+
}
|
|
46
|
+
export function useKeypress(userHandler) {
|
|
47
|
+
const rl = sessionRl;
|
|
48
|
+
if (!rl) {
|
|
49
|
+
throw new Error('useKeypress must be used within a prompt');
|
|
50
|
+
}
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
const handler = (_input, event) => {
|
|
53
|
+
userHandler(event, rl);
|
|
54
|
+
};
|
|
55
|
+
rl.input.on('keypress', handler);
|
|
56
|
+
return () => {
|
|
57
|
+
rl.input.removeListener('keypress', handler);
|
|
58
|
+
};
|
|
59
|
+
}, [userHandler]);
|
|
60
|
+
}
|
|
61
|
+
export function useRef(val) {
|
|
62
|
+
return useState({ current: val })[0];
|
|
63
|
+
}
|
|
64
|
+
export function createPrompt(view) {
|
|
65
|
+
return async function (options, stdio) {
|
|
66
|
+
// Default `input` to stdin
|
|
67
|
+
const input = stdio?.input ?? process.stdin;
|
|
68
|
+
// Add mute capabilities to the output
|
|
69
|
+
const output = new MuteStream();
|
|
70
|
+
output.pipe(stdio?.output ?? process.stdout);
|
|
71
|
+
const rl = readline.createInterface({
|
|
72
|
+
terminal: true,
|
|
73
|
+
input,
|
|
74
|
+
output,
|
|
75
|
+
});
|
|
76
|
+
const screen = new ScreenManager(rl);
|
|
77
|
+
// TODO: we should display a loader while we get the default options.
|
|
78
|
+
const resolvedConfig = await getPromptConfig(options);
|
|
79
|
+
return new Promise((resolve) => {
|
|
80
|
+
const done = (value) => {
|
|
81
|
+
let len = cleanupHook.length;
|
|
82
|
+
while (len--) {
|
|
83
|
+
cleanupHook(len);
|
|
84
|
+
}
|
|
85
|
+
screen.done();
|
|
86
|
+
// Reset hooks state
|
|
87
|
+
hooks = [];
|
|
88
|
+
index = 0;
|
|
89
|
+
sessionRl = undefined;
|
|
90
|
+
// Finally we resolve our promise
|
|
91
|
+
resolve(value);
|
|
92
|
+
};
|
|
93
|
+
hooks = [];
|
|
94
|
+
const workLoop = (config) => {
|
|
95
|
+
sessionRl = rl;
|
|
96
|
+
index = 0;
|
|
97
|
+
handleChange = () => workLoop(config);
|
|
98
|
+
const nextView = view(config, done);
|
|
99
|
+
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
100
|
+
screen.render(content, bottomContent);
|
|
101
|
+
};
|
|
102
|
+
workLoop(resolvedConfig);
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import cliWidth from 'cli-width';
|
|
3
|
+
import { breakLines } from './utils.js';
|
|
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
|
+
constructor() {
|
|
10
|
+
Object.defineProperty(this, "pointer", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
value: 0
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(this, "lastIndex", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: 0
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
paginate(output, active, pageSize = 7) {
|
|
24
|
+
const middleOfList = Math.floor(pageSize / 2);
|
|
25
|
+
// TODO: I've remove the dependency on readline here. But we should refactor the
|
|
26
|
+
// paginator to also rely on hook.
|
|
27
|
+
const width = cliWidth({ defaultWidth: 80, output: process.stdout });
|
|
28
|
+
const lines = breakLines(output, width).split('\n');
|
|
29
|
+
// Make sure there's enough lines to paginate
|
|
30
|
+
if (lines.length <= pageSize) {
|
|
31
|
+
return output;
|
|
32
|
+
}
|
|
33
|
+
// Move the pointer only when the user go down and limit it to the middle of the list
|
|
34
|
+
if (this.pointer < middleOfList &&
|
|
35
|
+
this.lastIndex < active &&
|
|
36
|
+
active - this.lastIndex < pageSize) {
|
|
37
|
+
this.pointer = Math.min(middleOfList, this.pointer + active - this.lastIndex);
|
|
38
|
+
}
|
|
39
|
+
this.lastIndex = active;
|
|
40
|
+
// Duplicate the lines so it give an infinite list look
|
|
41
|
+
const infinite = [lines, lines, lines].flat();
|
|
42
|
+
const topIndex = Math.max(0, active + lines.length - this.pointer);
|
|
43
|
+
const section = infinite.splice(topIndex, pageSize).join('\n');
|
|
44
|
+
return section + '\n' + chalk.dim('(Move up and down to reveal more choices)');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare type KeypressEvent = {
|
|
2
|
+
name: string;
|
|
3
|
+
ctrl: boolean;
|
|
4
|
+
};
|
|
5
|
+
export declare const isUpKey: (key: KeypressEvent) => boolean;
|
|
6
|
+
export declare const isDownKey: (key: KeypressEvent) => boolean;
|
|
7
|
+
export declare const isSpaceKey: (key: KeypressEvent) => boolean;
|
|
8
|
+
export declare const isBackspaceKey: (key: KeypressEvent) => boolean;
|
|
9
|
+
export declare const isNumberKey: (key: KeypressEvent) => boolean;
|
|
10
|
+
export declare const isEnterKey: (key: KeypressEvent) => boolean;
|
|
11
|
+
export {};
|
package/dist/lib/key.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const isUpKey = (key) => key.name === 'up' || key.name === 'k' || (key.name === 'p' && key.ctrl);
|
|
2
|
+
export const isDownKey = (key) => key.name === 'down' || key.name === 'j' || (key.name === 'n' && key.ctrl);
|
|
3
|
+
export const isSpaceKey = (key) => key.name === 'space';
|
|
4
|
+
export const isBackspaceKey = (key) => key.name === 'backspace';
|
|
5
|
+
export const isNumberKey = (key) => '123456789'.includes(key.name);
|
|
6
|
+
export const isEnterKey = (key) => key.name === 'enter' || key.name === 'return';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function usePrefix(isLoading?: boolean): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import spinners from 'cli-spinners';
|
|
3
|
+
import { useState, useEffect } from '../index.js';
|
|
4
|
+
const spinner = spinners.dots;
|
|
5
|
+
export function usePrefix(isLoading = false) {
|
|
6
|
+
const [tick, setTick] = useState(0);
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
if (isLoading) {
|
|
9
|
+
const timeout = setTimeout(() => {
|
|
10
|
+
setTick(tick + 1);
|
|
11
|
+
}, spinner.interval);
|
|
12
|
+
return () => clearTimeout(timeout);
|
|
13
|
+
}
|
|
14
|
+
}, [isLoading, tick]);
|
|
15
|
+
if (isLoading) {
|
|
16
|
+
const frame = tick % spinner.frames.length;
|
|
17
|
+
return chalk.yellow(spinner.frames[frame]);
|
|
18
|
+
}
|
|
19
|
+
return chalk.green('?');
|
|
20
|
+
}
|
|
@@ -1,50 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { InquirerReadline } from '../index.js';
|
|
3
2
|
/**
|
|
4
3
|
* Move cursor left by `x`
|
|
5
4
|
* @param {Readline} rl - Readline instance
|
|
6
5
|
* @param {Number} x - How far to go left (default to 1)
|
|
7
6
|
*/
|
|
8
|
-
|
|
9
|
-
exports.left = function (rl, x) {
|
|
10
|
-
rl.output.write(ansiEscapes.cursorBackward(x));
|
|
11
|
-
};
|
|
12
|
-
|
|
7
|
+
export declare function left(rl: InquirerReadline, x: number): void;
|
|
13
8
|
/**
|
|
14
9
|
* Move cursor right by `x`
|
|
15
10
|
* @param {Readline} rl - Readline instance
|
|
16
11
|
* @param {Number} x - How far to go left (default to 1)
|
|
17
12
|
*/
|
|
18
|
-
|
|
19
|
-
exports.right = function (rl, x) {
|
|
20
|
-
rl.output.write(ansiEscapes.cursorForward(x));
|
|
21
|
-
};
|
|
22
|
-
|
|
13
|
+
export declare function right(rl: InquirerReadline, x: number): void;
|
|
23
14
|
/**
|
|
24
15
|
* Move cursor up by `x`
|
|
25
16
|
* @param {Readline} rl - Readline instance
|
|
26
17
|
* @param {Number} x - How far to go up (default to 1)
|
|
27
18
|
*/
|
|
28
|
-
|
|
29
|
-
exports.up = function (rl, x) {
|
|
30
|
-
rl.output.write(ansiEscapes.cursorUp(x));
|
|
31
|
-
};
|
|
32
|
-
|
|
19
|
+
export declare function up(rl: InquirerReadline, x: number): void;
|
|
33
20
|
/**
|
|
34
21
|
* Move cursor down by `x`
|
|
35
22
|
* @param {Readline} rl - Readline instance
|
|
36
23
|
* @param {Number} x - How far to go down (default to 1)
|
|
37
24
|
*/
|
|
38
|
-
|
|
39
|
-
exports.down = function (rl, x) {
|
|
40
|
-
rl.output.write(ansiEscapes.cursorDown(x));
|
|
41
|
-
};
|
|
42
|
-
|
|
25
|
+
export declare function down(rl: InquirerReadline, x: number): void;
|
|
43
26
|
/**
|
|
44
27
|
* Clear current line
|
|
45
28
|
* @param {Readline} rl - Readline instance
|
|
46
29
|
* @param {Number} len - number of line to delete
|
|
47
30
|
*/
|
|
48
|
-
|
|
49
|
-
rl.output.write(ansiEscapes.eraseLines(len));
|
|
50
|
-
};
|
|
31
|
+
export declare function clearLine(rl: InquirerReadline, len: number): void;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import ansiEscapes from 'ansi-escapes';
|
|
2
|
+
/**
|
|
3
|
+
* Move cursor left by `x`
|
|
4
|
+
* @param {Readline} rl - Readline instance
|
|
5
|
+
* @param {Number} x - How far to go left (default to 1)
|
|
6
|
+
*/
|
|
7
|
+
export function left(rl, x) {
|
|
8
|
+
if (x > 0) {
|
|
9
|
+
rl.output.write(ansiEscapes.cursorBackward(x));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Move cursor right by `x`
|
|
14
|
+
* @param {Readline} rl - Readline instance
|
|
15
|
+
* @param {Number} x - How far to go left (default to 1)
|
|
16
|
+
*/
|
|
17
|
+
export function right(rl, x) {
|
|
18
|
+
if (x > 0) {
|
|
19
|
+
rl.output.write(ansiEscapes.cursorForward(x));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Move cursor up by `x`
|
|
24
|
+
* @param {Readline} rl - Readline instance
|
|
25
|
+
* @param {Number} x - How far to go up (default to 1)
|
|
26
|
+
*/
|
|
27
|
+
export function up(rl, x) {
|
|
28
|
+
if (x > 0) {
|
|
29
|
+
rl.output.write(ansiEscapes.cursorUp(x));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Move cursor down by `x`
|
|
34
|
+
* @param {Readline} rl - Readline instance
|
|
35
|
+
* @param {Number} x - How far to go down (default to 1)
|
|
36
|
+
*/
|
|
37
|
+
export function down(rl, x) {
|
|
38
|
+
if (x > 0) {
|
|
39
|
+
rl.output.write(ansiEscapes.cursorDown(x));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Clear current line
|
|
44
|
+
* @param {Readline} rl - Readline instance
|
|
45
|
+
* @param {Number} len - number of line to delete
|
|
46
|
+
*/
|
|
47
|
+
export function clearLine(rl, len) {
|
|
48
|
+
rl.output.write(ansiEscapes.eraseLines(len));
|
|
49
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { InquirerReadline } from '../index.js';
|
|
2
|
+
export default class ScreenManager {
|
|
3
|
+
private readonly rl;
|
|
4
|
+
private height;
|
|
5
|
+
private extraLinesUnderPrompt;
|
|
6
|
+
constructor(rl: InquirerReadline);
|
|
7
|
+
render(content: string, bottomContent?: string): void;
|
|
8
|
+
clean(extraLines: number): void;
|
|
9
|
+
done(): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import cliWidth from 'cli-width';
|
|
2
|
+
import stripAnsi from 'strip-ansi';
|
|
3
|
+
import stringWidth from 'string-width';
|
|
4
|
+
import ansiEscapes from 'ansi-escapes';
|
|
5
|
+
import * as util from './readline.js';
|
|
6
|
+
import { breakLines } from './utils.js';
|
|
7
|
+
const height = (content) => content.split('\n').length;
|
|
8
|
+
const lastLine = (content) => content.split('\n').pop() ?? '';
|
|
9
|
+
export default class ScreenManager {
|
|
10
|
+
constructor(rl) {
|
|
11
|
+
Object.defineProperty(this, "rl", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: rl
|
|
16
|
+
});
|
|
17
|
+
// These variables are keeping information to allow correct prompt re-rendering
|
|
18
|
+
Object.defineProperty(this, "height", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: 0
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "extraLinesUnderPrompt", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: 0
|
|
29
|
+
});
|
|
30
|
+
this.rl = rl;
|
|
31
|
+
}
|
|
32
|
+
render(content, bottomContent = '') {
|
|
33
|
+
this.rl.output.unmute();
|
|
34
|
+
this.clean(this.extraLinesUnderPrompt);
|
|
35
|
+
/**
|
|
36
|
+
* Write message to screen and setPrompt to control backspace
|
|
37
|
+
*/
|
|
38
|
+
const promptLine = lastLine(content);
|
|
39
|
+
const rawPromptLine = stripAnsi(promptLine);
|
|
40
|
+
// Remove the rl.line from our prompt. We can't rely on the content of
|
|
41
|
+
// rl.line (mainly because of the password prompt), so just rely on it's
|
|
42
|
+
// length.
|
|
43
|
+
let prompt = rawPromptLine;
|
|
44
|
+
if (this.rl.line.length) {
|
|
45
|
+
prompt = prompt.slice(0, -this.rl.line.length);
|
|
46
|
+
}
|
|
47
|
+
this.rl.setPrompt(prompt);
|
|
48
|
+
// SetPrompt will change cursor position, now we can get correct value
|
|
49
|
+
const cursorPos = this.rl._getCursorPos();
|
|
50
|
+
const width = cliWidth({ defaultWidth: 80, output: this.rl.output });
|
|
51
|
+
content = breakLines(content, width);
|
|
52
|
+
bottomContent = breakLines(bottomContent, width);
|
|
53
|
+
// Manually insert an extra line if we're at the end of the line.
|
|
54
|
+
// This prevent the cursor from appearing at the beginning of the
|
|
55
|
+
// current line.
|
|
56
|
+
if (rawPromptLine.length % width === 0) {
|
|
57
|
+
content += '\n';
|
|
58
|
+
}
|
|
59
|
+
const fullContent = content + (bottomContent ? '\n' + bottomContent : '');
|
|
60
|
+
this.rl.output.write(fullContent);
|
|
61
|
+
/**
|
|
62
|
+
* Re-adjust the cursor at the correct position.
|
|
63
|
+
*/
|
|
64
|
+
// We need to consider parts of the prompt under the cursor as part of the bottom
|
|
65
|
+
// content in order to correctly cleanup and re-render.
|
|
66
|
+
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - cursorPos.rows;
|
|
67
|
+
const bottomContentHeight = promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
|
|
68
|
+
// Return cursor to the input position (on top of the bottomContent)
|
|
69
|
+
util.up(this.rl, bottomContentHeight);
|
|
70
|
+
// Move cursor at the start of the line, then return to the initial left offset.
|
|
71
|
+
util.left(this.rl, stringWidth(lastLine(fullContent)));
|
|
72
|
+
util.right(this.rl, cursorPos.cols);
|
|
73
|
+
/**
|
|
74
|
+
* Set up state for next re-rendering
|
|
75
|
+
*/
|
|
76
|
+
this.extraLinesUnderPrompt = bottomContentHeight;
|
|
77
|
+
this.height = height(fullContent);
|
|
78
|
+
this.rl.output.mute();
|
|
79
|
+
}
|
|
80
|
+
clean(extraLines) {
|
|
81
|
+
util.down(this.rl, extraLines);
|
|
82
|
+
util.clearLine(this.rl, this.height);
|
|
83
|
+
}
|
|
84
|
+
done() {
|
|
85
|
+
// Reset the cursor at the end of the previously displayed content
|
|
86
|
+
util.down(this.rl, this.extraLinesUnderPrompt);
|
|
87
|
+
this.rl.setPrompt('');
|
|
88
|
+
this.rl.output.unmute();
|
|
89
|
+
this.rl.output.write('\n');
|
|
90
|
+
this.rl.output.write(ansiEscapes.cursorShow);
|
|
91
|
+
this.rl.close();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Force line returns at specific width. This function is ANSI code friendly and it'll
|
|
3
|
+
* ignore invisible codes during width calculation.
|
|
4
|
+
* @param {string} content
|
|
5
|
+
* @param {number} width
|
|
6
|
+
* @return {string}
|
|
7
|
+
*/
|
|
8
|
+
export declare const breakLines: (content: string, width: number) => string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import wrapAnsi from 'wrap-ansi';
|
|
2
|
+
/**
|
|
3
|
+
* Force line returns at specific width. This function is ANSI code friendly and it'll
|
|
4
|
+
* ignore invisible codes during width calculation.
|
|
5
|
+
* @param {string} content
|
|
6
|
+
* @param {number} width
|
|
7
|
+
* @return {string}
|
|
8
|
+
*/
|
|
9
|
+
export const breakLines = (content, width) => content
|
|
10
|
+
.split('\n')
|
|
11
|
+
// @ts-expect-error broken type check from library
|
|
12
|
+
.map((line) => wrapAnsi(line, width, { trim: false, hard: true }).split('\n'))
|
|
13
|
+
.join('\n');
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/core",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.21-alpha.0",
|
|
4
5
|
"description": "Core Inquirer prompt API",
|
|
5
|
-
"main": "index.js",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"typings": "dist/index.d.ts",
|
|
6
8
|
"repository": "SBoudrias/Inquirer.js",
|
|
7
9
|
"keywords": [
|
|
8
10
|
"cli",
|
|
@@ -13,18 +15,22 @@
|
|
|
13
15
|
"author": "Simon Boudrias",
|
|
14
16
|
"license": "MIT",
|
|
15
17
|
"dependencies": {
|
|
16
|
-
"ansi-escapes": "^
|
|
17
|
-
"chalk": "^
|
|
18
|
+
"ansi-escapes": "^5.0.0",
|
|
19
|
+
"chalk": "^5.0.1",
|
|
18
20
|
"cli-spinners": "^2.6.1",
|
|
19
|
-
"cli-width": "^
|
|
21
|
+
"cli-width": "^4.0.0",
|
|
20
22
|
"lodash": "^4.17.21",
|
|
21
23
|
"mute-stream": "^0.0.8",
|
|
22
24
|
"run-async": "^2.3.0",
|
|
23
|
-
"string-width": "^
|
|
24
|
-
"strip-ansi": "^
|
|
25
|
+
"string-width": "^5.1.2",
|
|
26
|
+
"strip-ansi": "^7.0.1",
|
|
27
|
+
"wrap-ansi": "^8.0.1"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"tsc": "tsc"
|
|
25
31
|
},
|
|
26
32
|
"publishConfig": {
|
|
27
33
|
"access": "public"
|
|
28
34
|
},
|
|
29
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "cd0d1465327c0e98a89c937a80b7c473b6d00f74"
|
|
30
36
|
}
|