@inquirer/core 11.0.0 → 11.0.2
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/package.json +13 -16
- package/dist/commonjs/index.d.ts +0 -13
- package/dist/commonjs/index.js +0 -46
- package/dist/commonjs/lib/Separator.d.ts +0 -10
- package/dist/commonjs/lib/Separator.js +0 -28
- package/dist/commonjs/lib/create-prompt.d.ts +0 -4
- package/dist/commonjs/lib/create-prompt.js +0 -156
- package/dist/commonjs/lib/errors.d.ts +0 -20
- package/dist/commonjs/lib/errors.js +0 -29
- package/dist/commonjs/lib/hook-engine.d.ts +0 -23
- package/dist/commonjs/lib/hook-engine.js +0 -118
- package/dist/commonjs/lib/key.d.ts +0 -12
- package/dist/commonjs/lib/key.js +0 -29
- package/dist/commonjs/lib/make-theme.d.ts +0 -3
- package/dist/commonjs/lib/make-theme.js +0 -33
- package/dist/commonjs/lib/pagination/use-pagination.d.ts +0 -16
- package/dist/commonjs/lib/pagination/use-pagination.js +0 -124
- package/dist/commonjs/lib/promise-polyfill.d.ts +0 -7
- package/dist/commonjs/lib/promise-polyfill.js +0 -18
- package/dist/commonjs/lib/screen-manager.d.ts +0 -14
- package/dist/commonjs/lib/screen-manager.js +0 -82
- package/dist/commonjs/lib/theme.d.ts +0 -155
- package/dist/commonjs/lib/theme.js +0 -27
- package/dist/commonjs/lib/use-effect.d.ts +0 -2
- package/dist/commonjs/lib/use-effect.js +0 -14
- package/dist/commonjs/lib/use-keypress.d.ts +0 -3
- package/dist/commonjs/lib/use-keypress.js +0 -23
- package/dist/commonjs/lib/use-memo.d.ts +0 -1
- package/dist/commonjs/lib/use-memo.js +0 -17
- package/dist/commonjs/lib/use-prefix.d.ts +0 -5
- package/dist/commonjs/lib/use-prefix.js +0 -38
- package/dist/commonjs/lib/use-ref.d.ts +0 -6
- package/dist/commonjs/lib/use-ref.js +0 -7
- package/dist/commonjs/lib/use-state.d.ts +0 -4
- package/dist/commonjs/lib/use-state.js +0 -23
- package/dist/commonjs/lib/utils.d.ts +0 -13
- package/dist/commonjs/lib/utils.js +0 -32
- package/dist/commonjs/package.json +0 -3
- package/dist/esm/index.d.ts +0 -13
- package/dist/esm/index.js +0 -12
- package/dist/esm/lib/Separator.d.ts +0 -10
- package/dist/esm/lib/Separator.js +0 -21
- package/dist/esm/lib/create-prompt.d.ts +0 -4
- package/dist/esm/lib/create-prompt.js +0 -117
- package/dist/esm/lib/errors.d.ts +0 -20
- package/dist/esm/lib/errors.js +0 -21
- package/dist/esm/lib/hook-engine.d.ts +0 -23
- package/dist/esm/lib/hook-engine.js +0 -110
- package/dist/esm/lib/key.d.ts +0 -12
- package/dist/esm/lib/key.js +0 -19
- package/dist/esm/lib/make-theme.d.ts +0 -3
- package/dist/esm/lib/make-theme.js +0 -30
- package/dist/esm/lib/pagination/use-pagination.d.ts +0 -16
- package/dist/esm/lib/pagination/use-pagination.js +0 -121
- package/dist/esm/lib/promise-polyfill.d.ts +0 -7
- package/dist/esm/lib/promise-polyfill.js +0 -14
- package/dist/esm/lib/screen-manager.d.ts +0 -14
- package/dist/esm/lib/screen-manager.js +0 -79
- package/dist/esm/lib/theme.d.ts +0 -155
- package/dist/esm/lib/theme.js +0 -21
- package/dist/esm/lib/use-effect.d.ts +0 -2
- package/dist/esm/lib/use-effect.js +0 -11
- package/dist/esm/lib/use-keypress.d.ts +0 -3
- package/dist/esm/lib/use-keypress.js +0 -20
- package/dist/esm/lib/use-memo.d.ts +0 -1
- package/dist/esm/lib/use-memo.js +0 -14
- package/dist/esm/lib/use-prefix.d.ts +0 -5
- package/dist/esm/lib/use-prefix.js +0 -35
- package/dist/esm/lib/use-ref.d.ts +0 -6
- package/dist/esm/lib/use-ref.js +0 -4
- package/dist/esm/lib/use-state.d.ts +0 -4
- package/dist/esm/lib/use-state.js +0 -20
- package/dist/esm/lib/utils.d.ts +0 -13
- package/dist/esm/lib/utils.js +0 -25
- package/dist/esm/package.json +0 -3
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.usePagination = usePagination;
|
|
4
|
-
const use_ref_ts_1 = require("../use-ref.js");
|
|
5
|
-
const utils_ts_1 = require("../utils.js");
|
|
6
|
-
function usePointerPosition({ active, renderedItems, pageSize, loop, }) {
|
|
7
|
-
const state = (0, use_ref_ts_1.useRef)({
|
|
8
|
-
lastPointer: active,
|
|
9
|
-
lastActive: undefined,
|
|
10
|
-
});
|
|
11
|
-
const { lastPointer, lastActive } = state.current;
|
|
12
|
-
const middle = Math.floor(pageSize / 2);
|
|
13
|
-
const renderedLength = renderedItems.reduce((acc, item) => acc + item.length, 0);
|
|
14
|
-
const defaultPointerPosition = renderedItems
|
|
15
|
-
.slice(0, active)
|
|
16
|
-
.reduce((acc, item) => acc + item.length, 0);
|
|
17
|
-
let pointer = defaultPointerPosition;
|
|
18
|
-
if (renderedLength > pageSize) {
|
|
19
|
-
if (loop) {
|
|
20
|
-
/**
|
|
21
|
-
* Creates the next position for the pointer considering an infinitely
|
|
22
|
-
* looping list of items to be rendered on the page.
|
|
23
|
-
*
|
|
24
|
-
* The goal is to progressively move the cursor to the middle position as the user move down, and then keep
|
|
25
|
-
* the cursor there. When the user move up, maintain the cursor position.
|
|
26
|
-
*/
|
|
27
|
-
// By default, keep the cursor position as-is.
|
|
28
|
-
pointer = lastPointer;
|
|
29
|
-
if (
|
|
30
|
-
// First render, skip this logic.
|
|
31
|
-
lastActive != null &&
|
|
32
|
-
// Only move the pointer down when the user moves down.
|
|
33
|
-
lastActive < active &&
|
|
34
|
-
// Check user didn't move up across page boundary.
|
|
35
|
-
active - lastActive < pageSize) {
|
|
36
|
-
pointer = Math.min(
|
|
37
|
-
// Furthest allowed position for the pointer is the middle of the list
|
|
38
|
-
middle, Math.abs(active - lastActive) === 1
|
|
39
|
-
? Math.min(
|
|
40
|
-
// Move the pointer at most the height of the last active item.
|
|
41
|
-
lastPointer + (renderedItems[lastActive]?.length ?? 0),
|
|
42
|
-
// If the user moved by one item, move the pointer to the natural position of the active item as
|
|
43
|
-
// long as it doesn't move the cursor up.
|
|
44
|
-
Math.max(defaultPointerPosition, lastPointer))
|
|
45
|
-
: // Otherwise, move the pointer down by the difference between the active and last active item.
|
|
46
|
-
lastPointer + active - lastActive);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
/**
|
|
51
|
-
* Creates the next position for the pointer considering a finite list of
|
|
52
|
-
* items to be rendered on a page.
|
|
53
|
-
*
|
|
54
|
-
* The goal is to keep the pointer in the middle of the page whenever possible, until
|
|
55
|
-
* we reach the bounds of the list (top or bottom). In which case, the cursor moves progressively
|
|
56
|
-
* to the bottom or top of the list.
|
|
57
|
-
*/
|
|
58
|
-
const spaceUnderActive = renderedItems
|
|
59
|
-
.slice(active)
|
|
60
|
-
.reduce((acc, item) => acc + item.length, 0);
|
|
61
|
-
pointer =
|
|
62
|
-
spaceUnderActive < pageSize - middle
|
|
63
|
-
? // If the active item is near the end of the list, progressively move the cursor towards the end.
|
|
64
|
-
pageSize - spaceUnderActive
|
|
65
|
-
: // Otherwise, progressively move the pointer to the middle of the list.
|
|
66
|
-
Math.min(defaultPointerPosition, middle);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
// Save state for the next render
|
|
70
|
-
state.current.lastPointer = pointer;
|
|
71
|
-
state.current.lastActive = active;
|
|
72
|
-
return pointer;
|
|
73
|
-
}
|
|
74
|
-
function usePagination({ items, active, renderItem, pageSize, loop = true, }) {
|
|
75
|
-
const width = (0, utils_ts_1.readlineWidth)();
|
|
76
|
-
const bound = (num) => ((num % items.length) + items.length) % items.length;
|
|
77
|
-
const renderedItems = items.map((item, index) => {
|
|
78
|
-
if (item == null)
|
|
79
|
-
return [];
|
|
80
|
-
return (0, utils_ts_1.breakLines)(renderItem({ item, index, isActive: index === active }), width).split('\n');
|
|
81
|
-
});
|
|
82
|
-
const renderedLength = renderedItems.reduce((acc, item) => acc + item.length, 0);
|
|
83
|
-
const renderItemAtIndex = (index) => renderedItems[index] ?? [];
|
|
84
|
-
const pointer = usePointerPosition({ active, renderedItems, pageSize, loop });
|
|
85
|
-
// Render the active item to decide the position.
|
|
86
|
-
// If the active item fits under the pointer, we render it there.
|
|
87
|
-
// Otherwise, we need to render it to fit at the bottom of the page; moving the pointer up.
|
|
88
|
-
const activeItem = renderItemAtIndex(active).slice(0, pageSize);
|
|
89
|
-
const activeItemPosition = pointer + activeItem.length <= pageSize ? pointer : pageSize - activeItem.length;
|
|
90
|
-
// Create an array of lines for the page, and add the lines of the active item into the page
|
|
91
|
-
const pageBuffer = Array.from({ length: pageSize });
|
|
92
|
-
pageBuffer.splice(activeItemPosition, activeItem.length, ...activeItem);
|
|
93
|
-
// Store to prevent rendering the same item twice
|
|
94
|
-
const itemVisited = new Set([active]);
|
|
95
|
-
// Fill the page under the active item
|
|
96
|
-
let bufferPointer = activeItemPosition + activeItem.length;
|
|
97
|
-
let itemPointer = bound(active + 1);
|
|
98
|
-
while (bufferPointer < pageSize &&
|
|
99
|
-
!itemVisited.has(itemPointer) &&
|
|
100
|
-
(loop && renderedLength > pageSize ? itemPointer !== active : itemPointer > active)) {
|
|
101
|
-
const lines = renderItemAtIndex(itemPointer);
|
|
102
|
-
const linesToAdd = lines.slice(0, pageSize - bufferPointer);
|
|
103
|
-
pageBuffer.splice(bufferPointer, linesToAdd.length, ...linesToAdd);
|
|
104
|
-
// Move pointers for next iteration
|
|
105
|
-
itemVisited.add(itemPointer);
|
|
106
|
-
bufferPointer += linesToAdd.length;
|
|
107
|
-
itemPointer = bound(itemPointer + 1);
|
|
108
|
-
}
|
|
109
|
-
// Fill the page over the active item
|
|
110
|
-
bufferPointer = activeItemPosition - 1;
|
|
111
|
-
itemPointer = bound(active - 1);
|
|
112
|
-
while (bufferPointer >= 0 &&
|
|
113
|
-
!itemVisited.has(itemPointer) &&
|
|
114
|
-
(loop && renderedLength > pageSize ? itemPointer !== active : itemPointer < active)) {
|
|
115
|
-
const lines = renderItemAtIndex(itemPointer);
|
|
116
|
-
const linesToAdd = lines.slice(Math.max(0, lines.length - bufferPointer - 1));
|
|
117
|
-
pageBuffer.splice(bufferPointer - linesToAdd.length + 1, linesToAdd.length, ...linesToAdd);
|
|
118
|
-
// Move pointers for next iteration
|
|
119
|
-
itemVisited.add(itemPointer);
|
|
120
|
-
bufferPointer -= linesToAdd.length;
|
|
121
|
-
itemPointer = bound(itemPointer - 1);
|
|
122
|
-
}
|
|
123
|
-
return pageBuffer.filter((line) => typeof line === 'string').join('\n');
|
|
124
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PromisePolyfill = void 0;
|
|
4
|
-
// TODO: Remove this class once Node 22 becomes the minimum supported version.
|
|
5
|
-
class PromisePolyfill extends Promise {
|
|
6
|
-
// Available starting from Node 22
|
|
7
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
|
|
8
|
-
static withResolver() {
|
|
9
|
-
let resolve;
|
|
10
|
-
let reject;
|
|
11
|
-
const promise = new Promise((res, rej) => {
|
|
12
|
-
resolve = res;
|
|
13
|
-
reject = rej;
|
|
14
|
-
});
|
|
15
|
-
return { promise, resolve: resolve, reject: reject };
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.PromisePolyfill = PromisePolyfill;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { InquirerReadline } from '@inquirer/type';
|
|
2
|
-
export default class ScreenManager {
|
|
3
|
-
private height;
|
|
4
|
-
private extraLinesUnderPrompt;
|
|
5
|
-
private cursorPos;
|
|
6
|
-
private readonly rl;
|
|
7
|
-
constructor(rl: InquirerReadline);
|
|
8
|
-
write(content: string): void;
|
|
9
|
-
render(content: string, bottomContent?: string): void;
|
|
10
|
-
checkCursorPos(): void;
|
|
11
|
-
done({ clearContent }: {
|
|
12
|
-
clearContent: boolean;
|
|
13
|
-
}): void;
|
|
14
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const node_util_1 = require("node:util");
|
|
4
|
-
const utils_ts_1 = require("./utils.js");
|
|
5
|
-
const ansi_1 = require("@inquirer/ansi");
|
|
6
|
-
const height = (content) => content.split('\n').length;
|
|
7
|
-
const lastLine = (content) => content.split('\n').pop() ?? '';
|
|
8
|
-
class ScreenManager {
|
|
9
|
-
// These variables are keeping information to allow correct prompt re-rendering
|
|
10
|
-
height = 0;
|
|
11
|
-
extraLinesUnderPrompt = 0;
|
|
12
|
-
cursorPos;
|
|
13
|
-
rl;
|
|
14
|
-
constructor(rl) {
|
|
15
|
-
this.rl = rl;
|
|
16
|
-
this.cursorPos = rl.getCursorPos();
|
|
17
|
-
}
|
|
18
|
-
write(content) {
|
|
19
|
-
this.rl.output.unmute();
|
|
20
|
-
this.rl.output.write(content);
|
|
21
|
-
this.rl.output.mute();
|
|
22
|
-
}
|
|
23
|
-
render(content, bottomContent = '') {
|
|
24
|
-
// Write message to screen and setPrompt to control backspace
|
|
25
|
-
const promptLine = lastLine(content);
|
|
26
|
-
const rawPromptLine = (0, node_util_1.stripVTControlCharacters)(promptLine);
|
|
27
|
-
// Remove the rl.line from our prompt. We can't rely on the content of
|
|
28
|
-
// rl.line (mainly because of the password prompt), so just rely on it's
|
|
29
|
-
// length.
|
|
30
|
-
let prompt = rawPromptLine;
|
|
31
|
-
if (this.rl.line.length > 0) {
|
|
32
|
-
prompt = prompt.slice(0, -this.rl.line.length);
|
|
33
|
-
}
|
|
34
|
-
this.rl.setPrompt(prompt);
|
|
35
|
-
// SetPrompt will change cursor position, now we can get correct value
|
|
36
|
-
this.cursorPos = this.rl.getCursorPos();
|
|
37
|
-
const width = (0, utils_ts_1.readlineWidth)();
|
|
38
|
-
content = (0, utils_ts_1.breakLines)(content, width);
|
|
39
|
-
bottomContent = (0, utils_ts_1.breakLines)(bottomContent, width);
|
|
40
|
-
// Manually insert an extra line if we're at the end of the line.
|
|
41
|
-
// This prevent the cursor from appearing at the beginning of the
|
|
42
|
-
// current line.
|
|
43
|
-
if (rawPromptLine.length % width === 0) {
|
|
44
|
-
content += '\n';
|
|
45
|
-
}
|
|
46
|
-
let output = content + (bottomContent ? '\n' + bottomContent : '');
|
|
47
|
-
/**
|
|
48
|
-
* Re-adjust the cursor at the correct position.
|
|
49
|
-
*/
|
|
50
|
-
// We need to consider parts of the prompt under the cursor as part of the bottom
|
|
51
|
-
// content in order to correctly cleanup and re-render.
|
|
52
|
-
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - this.cursorPos.rows;
|
|
53
|
-
const bottomContentHeight = promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
|
|
54
|
-
// Return cursor to the input position (on top of the bottomContent)
|
|
55
|
-
if (bottomContentHeight > 0)
|
|
56
|
-
output += (0, ansi_1.cursorUp)(bottomContentHeight);
|
|
57
|
-
// Return cursor to the initial left offset.
|
|
58
|
-
output += (0, ansi_1.cursorTo)(this.cursorPos.cols);
|
|
59
|
-
/**
|
|
60
|
-
* Render and store state for future re-rendering
|
|
61
|
-
*/
|
|
62
|
-
this.write((0, ansi_1.cursorDown)(this.extraLinesUnderPrompt) + (0, ansi_1.eraseLines)(this.height) + output);
|
|
63
|
-
this.extraLinesUnderPrompt = bottomContentHeight;
|
|
64
|
-
this.height = height(output);
|
|
65
|
-
}
|
|
66
|
-
checkCursorPos() {
|
|
67
|
-
const cursorPos = this.rl.getCursorPos();
|
|
68
|
-
if (cursorPos.cols !== this.cursorPos.cols) {
|
|
69
|
-
this.write((0, ansi_1.cursorTo)(cursorPos.cols));
|
|
70
|
-
this.cursorPos = cursorPos;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
done({ clearContent }) {
|
|
74
|
-
this.rl.setPrompt('');
|
|
75
|
-
let output = (0, ansi_1.cursorDown)(this.extraLinesUnderPrompt);
|
|
76
|
-
output += clearContent ? (0, ansi_1.eraseLines)(this.height) : '\n';
|
|
77
|
-
output += ansi_1.cursorShow;
|
|
78
|
-
this.write(output);
|
|
79
|
-
this.rl.close();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
exports.default = ScreenManager;
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import type { Prettify } from '@inquirer/type';
|
|
2
|
-
/**
|
|
3
|
-
* Union type representing the possible statuses of a prompt.
|
|
4
|
-
*
|
|
5
|
-
* - `'loading'`: The prompt is currently loading.
|
|
6
|
-
* - `'idle'`: The prompt is loaded and currently waiting for the user to
|
|
7
|
-
* submit an answer.
|
|
8
|
-
* - `'done'`: The user has submitted an answer and the prompt is finished.
|
|
9
|
-
* - `string`: Any other string: The prompt is in a custom state.
|
|
10
|
-
*/
|
|
11
|
-
export type Status = 'loading' | 'idle' | 'done' | (string & {});
|
|
12
|
-
type DefaultTheme = {
|
|
13
|
-
/**
|
|
14
|
-
* Prefix to prepend to the message. If a function is provided, it will be
|
|
15
|
-
* called with the current status of the prompt, and the return value will be
|
|
16
|
-
* used as the prefix.
|
|
17
|
-
*
|
|
18
|
-
* @remarks
|
|
19
|
-
* If `status === 'loading'`, this property is ignored and the spinner (styled
|
|
20
|
-
* by the `spinner` property) will be displayed instead.
|
|
21
|
-
*
|
|
22
|
-
* @defaultValue
|
|
23
|
-
* ```ts
|
|
24
|
-
* // import colors from 'yoctocolors-cjs';
|
|
25
|
-
* (status) => status === 'done' ? colors.green('✔') : colors.blue('?')
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
prefix: string | Prettify<Omit<Record<Status, string>, 'loading'>>;
|
|
29
|
-
/**
|
|
30
|
-
* Configuration for the spinner that is displayed when the prompt is in the
|
|
31
|
-
* `'loading'` state.
|
|
32
|
-
*
|
|
33
|
-
* We recommend the use of {@link https://github.com/sindresorhus/cli-spinners|cli-spinners} for a list of available spinners.
|
|
34
|
-
*/
|
|
35
|
-
spinner: {
|
|
36
|
-
/**
|
|
37
|
-
* The time interval between frames, in milliseconds.
|
|
38
|
-
*
|
|
39
|
-
* @defaultValue
|
|
40
|
-
* ```ts
|
|
41
|
-
* 80
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
interval: number;
|
|
45
|
-
/**
|
|
46
|
-
* A list of frames to show for the spinner.
|
|
47
|
-
*
|
|
48
|
-
* @defaultValue
|
|
49
|
-
* ```ts
|
|
50
|
-
* ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
frames: string[];
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Object containing functions to style different parts of the prompt.
|
|
57
|
-
*/
|
|
58
|
-
style: {
|
|
59
|
-
/**
|
|
60
|
-
* Style to apply to the user's answer once it has been submitted.
|
|
61
|
-
*
|
|
62
|
-
* @param text - The user's answer.
|
|
63
|
-
* @returns The styled answer.
|
|
64
|
-
*
|
|
65
|
-
* @defaultValue
|
|
66
|
-
* ```ts
|
|
67
|
-
* // import colors from 'yoctocolors-cjs';
|
|
68
|
-
* (text) => colors.cyan(text)
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
answer: (text: string) => string;
|
|
72
|
-
/**
|
|
73
|
-
* Style to apply to the message displayed to the user.
|
|
74
|
-
*
|
|
75
|
-
* @param text - The message to style.
|
|
76
|
-
* @param status - The current status of the prompt.
|
|
77
|
-
* @returns The styled message.
|
|
78
|
-
*
|
|
79
|
-
* @defaultValue
|
|
80
|
-
* ```ts
|
|
81
|
-
* // import colors from 'yoctocolors-cjs';
|
|
82
|
-
* (text, status) => colors.bold(text)
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
|
-
message: (text: string, status: Status) => string;
|
|
86
|
-
/**
|
|
87
|
-
* Style to apply to error messages.
|
|
88
|
-
*
|
|
89
|
-
* @param text - The error message.
|
|
90
|
-
* @returns The styled error message.
|
|
91
|
-
*
|
|
92
|
-
* @defaultValue
|
|
93
|
-
* ```ts
|
|
94
|
-
* // import colors from 'yoctocolors-cjs';
|
|
95
|
-
* (text) => colors.red(`> ${text}`)
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
error: (text: string) => string;
|
|
99
|
-
/**
|
|
100
|
-
* Style to apply to the default answer when one is provided.
|
|
101
|
-
*
|
|
102
|
-
* @param text - The default answer.
|
|
103
|
-
* @returns The styled default answer.
|
|
104
|
-
*
|
|
105
|
-
* @defaultValue
|
|
106
|
-
* ```ts
|
|
107
|
-
* // import colors from 'yoctocolors-cjs';
|
|
108
|
-
* (text) => colors.dim(`(${text})`)
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
|
-
defaultAnswer: (text: string) => string;
|
|
112
|
-
/**
|
|
113
|
-
* Style to apply to help text.
|
|
114
|
-
*
|
|
115
|
-
* @param text - The help text.
|
|
116
|
-
* @returns The styled help text.
|
|
117
|
-
*
|
|
118
|
-
* @defaultValue
|
|
119
|
-
* ```ts
|
|
120
|
-
* // import colors from 'yoctocolors-cjs';
|
|
121
|
-
* (text) => colors.dim(text)
|
|
122
|
-
* ```
|
|
123
|
-
*/
|
|
124
|
-
help: (text: string) => string;
|
|
125
|
-
/**
|
|
126
|
-
* Style to apply to highlighted text.
|
|
127
|
-
*
|
|
128
|
-
* @param text - The text to highlight.
|
|
129
|
-
* @returns The highlighted text.
|
|
130
|
-
*
|
|
131
|
-
* @defaultValue
|
|
132
|
-
* ```ts
|
|
133
|
-
* // import colors from 'yoctocolors-cjs';
|
|
134
|
-
* (text) => colors.cyan(text)
|
|
135
|
-
* ```
|
|
136
|
-
*/
|
|
137
|
-
highlight: (text: string) => string;
|
|
138
|
-
/**
|
|
139
|
-
* Style to apply to keyboard keys referred to in help texts.
|
|
140
|
-
*
|
|
141
|
-
* @param text - The key to style.
|
|
142
|
-
* @returns The styled key.
|
|
143
|
-
*
|
|
144
|
-
* @defaultValue
|
|
145
|
-
* ```ts
|
|
146
|
-
* // import colors from 'yoctocolors-cjs';
|
|
147
|
-
* (text) => colors.cyan(colors.bold(`<${text}>`))
|
|
148
|
-
* ```
|
|
149
|
-
*/
|
|
150
|
-
key: (text: string) => string;
|
|
151
|
-
};
|
|
152
|
-
};
|
|
153
|
-
export type Theme<Extension extends object = object> = Prettify<Extension & DefaultTheme>;
|
|
154
|
-
export declare const defaultTheme: DefaultTheme;
|
|
155
|
-
export {};
|
|
@@ -1,27 +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.defaultTheme = void 0;
|
|
7
|
-
const yoctocolors_cjs_1 = __importDefault(require("yoctocolors-cjs"));
|
|
8
|
-
const figures_1 = __importDefault(require("@inquirer/figures"));
|
|
9
|
-
exports.defaultTheme = {
|
|
10
|
-
prefix: {
|
|
11
|
-
idle: yoctocolors_cjs_1.default.blue('?'),
|
|
12
|
-
done: yoctocolors_cjs_1.default.green(figures_1.default.tick),
|
|
13
|
-
},
|
|
14
|
-
spinner: {
|
|
15
|
-
interval: 80,
|
|
16
|
-
frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'].map((frame) => yoctocolors_cjs_1.default.yellow(frame)),
|
|
17
|
-
},
|
|
18
|
-
style: {
|
|
19
|
-
answer: yoctocolors_cjs_1.default.cyan,
|
|
20
|
-
message: yoctocolors_cjs_1.default.bold,
|
|
21
|
-
error: (text) => yoctocolors_cjs_1.default.red(`> ${text}`),
|
|
22
|
-
defaultAnswer: (text) => yoctocolors_cjs_1.default.dim(`(${text})`),
|
|
23
|
-
help: yoctocolors_cjs_1.default.dim,
|
|
24
|
-
highlight: yoctocolors_cjs_1.default.cyan,
|
|
25
|
-
key: (text) => yoctocolors_cjs_1.default.cyan(yoctocolors_cjs_1.default.bold(`<${text}>`)),
|
|
26
|
-
},
|
|
27
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useEffect = useEffect;
|
|
4
|
-
const hook_engine_ts_1 = require("./hook-engine.js");
|
|
5
|
-
function useEffect(cb, depArray) {
|
|
6
|
-
(0, hook_engine_ts_1.withPointer)((pointer) => {
|
|
7
|
-
const oldDeps = pointer.get();
|
|
8
|
-
const hasChanged = !Array.isArray(oldDeps) || depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
9
|
-
if (hasChanged) {
|
|
10
|
-
hook_engine_ts_1.effectScheduler.queue(cb);
|
|
11
|
-
}
|
|
12
|
-
pointer.set(depArray);
|
|
13
|
-
});
|
|
14
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useKeypress = useKeypress;
|
|
4
|
-
const use_ref_ts_1 = require("./use-ref.js");
|
|
5
|
-
const use_effect_ts_1 = require("./use-effect.js");
|
|
6
|
-
const hook_engine_ts_1 = require("./hook-engine.js");
|
|
7
|
-
function useKeypress(userHandler) {
|
|
8
|
-
const signal = (0, use_ref_ts_1.useRef)(userHandler);
|
|
9
|
-
signal.current = userHandler;
|
|
10
|
-
(0, use_effect_ts_1.useEffect)((rl) => {
|
|
11
|
-
let ignore = false;
|
|
12
|
-
const handler = (0, hook_engine_ts_1.withUpdates)((_input, event) => {
|
|
13
|
-
if (ignore)
|
|
14
|
-
return;
|
|
15
|
-
void signal.current(event, rl);
|
|
16
|
-
});
|
|
17
|
-
rl.input.on('keypress', handler);
|
|
18
|
-
return () => {
|
|
19
|
-
ignore = true;
|
|
20
|
-
rl.input.removeListener('keypress', handler);
|
|
21
|
-
};
|
|
22
|
-
}, []);
|
|
23
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function useMemo<Value>(fn: () => Value, dependencies: ReadonlyArray<unknown>): Value;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useMemo = useMemo;
|
|
4
|
-
const hook_engine_ts_1 = require("./hook-engine.js");
|
|
5
|
-
function useMemo(fn, dependencies) {
|
|
6
|
-
return (0, hook_engine_ts_1.withPointer)((pointer) => {
|
|
7
|
-
const prev = pointer.get();
|
|
8
|
-
if (!prev ||
|
|
9
|
-
prev.dependencies.length !== dependencies.length ||
|
|
10
|
-
prev.dependencies.some((dep, i) => dep !== dependencies[i])) {
|
|
11
|
-
const value = fn();
|
|
12
|
-
pointer.set({ value, dependencies });
|
|
13
|
-
return value;
|
|
14
|
-
}
|
|
15
|
-
return prev.value;
|
|
16
|
-
});
|
|
17
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.usePrefix = usePrefix;
|
|
4
|
-
const use_state_ts_1 = require("./use-state.js");
|
|
5
|
-
const use_effect_ts_1 = require("./use-effect.js");
|
|
6
|
-
const make_theme_ts_1 = require("./make-theme.js");
|
|
7
|
-
function usePrefix({ status = 'idle', theme, }) {
|
|
8
|
-
const [showLoader, setShowLoader] = (0, use_state_ts_1.useState)(false);
|
|
9
|
-
const [tick, setTick] = (0, use_state_ts_1.useState)(0);
|
|
10
|
-
const { prefix, spinner } = (0, make_theme_ts_1.makeTheme)(theme);
|
|
11
|
-
(0, use_effect_ts_1.useEffect)(() => {
|
|
12
|
-
if (status === 'loading') {
|
|
13
|
-
let tickInterval;
|
|
14
|
-
let inc = -1;
|
|
15
|
-
// Delay displaying spinner by 300ms, to avoid flickering
|
|
16
|
-
const delayTimeout = setTimeout(() => {
|
|
17
|
-
setShowLoader(true);
|
|
18
|
-
tickInterval = setInterval(() => {
|
|
19
|
-
inc = inc + 1;
|
|
20
|
-
setTick(inc % spinner.frames.length);
|
|
21
|
-
}, spinner.interval);
|
|
22
|
-
}, 300);
|
|
23
|
-
return () => {
|
|
24
|
-
clearTimeout(delayTimeout);
|
|
25
|
-
clearInterval(tickInterval);
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
setShowLoader(false);
|
|
30
|
-
}
|
|
31
|
-
}, [status]);
|
|
32
|
-
if (showLoader) {
|
|
33
|
-
return spinner.frames[tick];
|
|
34
|
-
}
|
|
35
|
-
// There's a delay before we show the loader. So we want to ignore `loading` here, and pass idle instead.
|
|
36
|
-
const iconName = status === 'loading' ? 'idle' : status;
|
|
37
|
-
return typeof prefix === 'string' ? prefix : (prefix[iconName] ?? prefix['idle']);
|
|
38
|
-
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
type NotFunction<T> = T extends (...args: never) => unknown ? never : T;
|
|
2
|
-
export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
|
|
3
|
-
export declare function useState<Value>(defaultValue?: NotFunction<Value> | (() => Value)): [Value | undefined, (newValue?: Value) => void];
|
|
4
|
-
export {};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useState = useState;
|
|
4
|
-
const node_async_hooks_1 = require("node:async_hooks");
|
|
5
|
-
const hook_engine_ts_1 = require("./hook-engine.js");
|
|
6
|
-
function useState(defaultValue) {
|
|
7
|
-
return (0, hook_engine_ts_1.withPointer)((pointer) => {
|
|
8
|
-
const setState = node_async_hooks_1.AsyncResource.bind(function setState(newValue) {
|
|
9
|
-
// Noop if the value is still the same.
|
|
10
|
-
if (pointer.get() !== newValue) {
|
|
11
|
-
pointer.set(newValue);
|
|
12
|
-
// Trigger re-render
|
|
13
|
-
(0, hook_engine_ts_1.handleChange)();
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
if (pointer.initialized) {
|
|
17
|
-
return [pointer.get(), setState];
|
|
18
|
-
}
|
|
19
|
-
const value = typeof defaultValue === 'function' ? defaultValue() : defaultValue;
|
|
20
|
-
pointer.set(value);
|
|
21
|
-
return [value, setState];
|
|
22
|
-
});
|
|
23
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
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 function breakLines(content: string, width: number): string;
|
|
9
|
-
/**
|
|
10
|
-
* Returns the width of the active readline, or 80 as default value.
|
|
11
|
-
* @returns {number}
|
|
12
|
-
*/
|
|
13
|
-
export declare function readlineWidth(): number;
|
|
@@ -1,32 +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.breakLines = breakLines;
|
|
7
|
-
exports.readlineWidth = readlineWidth;
|
|
8
|
-
const cli_width_1 = __importDefault(require("cli-width"));
|
|
9
|
-
const wrap_ansi_1 = __importDefault(require("wrap-ansi"));
|
|
10
|
-
const hook_engine_ts_1 = require("./hook-engine.js");
|
|
11
|
-
/**
|
|
12
|
-
* Force line returns at specific width. This function is ANSI code friendly and it'll
|
|
13
|
-
* ignore invisible codes during width calculation.
|
|
14
|
-
* @param {string} content
|
|
15
|
-
* @param {number} width
|
|
16
|
-
* @return {string}
|
|
17
|
-
*/
|
|
18
|
-
function breakLines(content, width) {
|
|
19
|
-
return content
|
|
20
|
-
.split('\n')
|
|
21
|
-
.flatMap((line) => (0, wrap_ansi_1.default)(line, width, { trim: false, hard: true })
|
|
22
|
-
.split('\n')
|
|
23
|
-
.map((str) => str.trimEnd()))
|
|
24
|
-
.join('\n');
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Returns the width of the active readline, or 80 as default value.
|
|
28
|
-
* @returns {number}
|
|
29
|
-
*/
|
|
30
|
-
function readlineWidth() {
|
|
31
|
-
return (0, cli_width_1.default)({ defaultWidth: 80, output: (0, hook_engine_ts_1.readline)().output });
|
|
32
|
-
}
|
package/dist/esm/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export { isUpKey, isDownKey, isSpaceKey, isBackspaceKey, isTabKey, isNumberKey, isEnterKey, type KeypressEvent, type Keybinding, } from './lib/key.ts';
|
|
2
|
-
export * from './lib/errors.ts';
|
|
3
|
-
export { usePrefix } from './lib/use-prefix.ts';
|
|
4
|
-
export { useState } from './lib/use-state.ts';
|
|
5
|
-
export { useEffect } from './lib/use-effect.ts';
|
|
6
|
-
export { useMemo } from './lib/use-memo.ts';
|
|
7
|
-
export { useRef } from './lib/use-ref.ts';
|
|
8
|
-
export { useKeypress } from './lib/use-keypress.ts';
|
|
9
|
-
export { makeTheme } from './lib/make-theme.ts';
|
|
10
|
-
export type { Theme, Status } from './lib/theme.ts';
|
|
11
|
-
export { usePagination } from './lib/pagination/use-pagination.ts';
|
|
12
|
-
export { createPrompt } from './lib/create-prompt.ts';
|
|
13
|
-
export { Separator } from './lib/Separator.ts';
|