@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
package/dist/esm/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export { isUpKey, isDownKey, isSpaceKey, isBackspaceKey, isTabKey, isNumberKey, isEnterKey, } from "./lib/key.js";
|
|
2
|
-
export * from "./lib/errors.js";
|
|
3
|
-
export { usePrefix } from "./lib/use-prefix.js";
|
|
4
|
-
export { useState } from "./lib/use-state.js";
|
|
5
|
-
export { useEffect } from "./lib/use-effect.js";
|
|
6
|
-
export { useMemo } from "./lib/use-memo.js";
|
|
7
|
-
export { useRef } from "./lib/use-ref.js";
|
|
8
|
-
export { useKeypress } from "./lib/use-keypress.js";
|
|
9
|
-
export { makeTheme } from "./lib/make-theme.js";
|
|
10
|
-
export { usePagination } from "./lib/pagination/use-pagination.js";
|
|
11
|
-
export { createPrompt } from "./lib/create-prompt.js";
|
|
12
|
-
export { Separator } from "./lib/Separator.js";
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import colors from 'yoctocolors-cjs';
|
|
2
|
-
import figures from '@inquirer/figures';
|
|
3
|
-
/**
|
|
4
|
-
* Separator object
|
|
5
|
-
* Used to space/separate choices group
|
|
6
|
-
*/
|
|
7
|
-
export class Separator {
|
|
8
|
-
separator = colors.dim(Array.from({ length: 15 }).join(figures.line));
|
|
9
|
-
type = 'separator';
|
|
10
|
-
constructor(separator) {
|
|
11
|
-
if (separator) {
|
|
12
|
-
this.separator = separator;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
static isSeparator(choice) {
|
|
16
|
-
return Boolean(choice &&
|
|
17
|
-
typeof choice === 'object' &&
|
|
18
|
-
'type' in choice &&
|
|
19
|
-
choice.type === 'separator');
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { type Prompt, type Prettify } from '@inquirer/type';
|
|
2
|
-
type ViewFunction<Value, Config> = (config: Prettify<Config>, done: (value: Value) => void) => string | [string, string | undefined];
|
|
3
|
-
export declare function createPrompt<Value, Config>(view: ViewFunction<Value, Config>): Prompt<Value, Config>;
|
|
4
|
-
export {};
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import * as readline from 'node:readline';
|
|
2
|
-
import { AsyncResource } from 'node:async_hooks';
|
|
3
|
-
import MuteStream from 'mute-stream';
|
|
4
|
-
import { onExit as onSignalExit } from 'signal-exit';
|
|
5
|
-
import ScreenManager from "./screen-manager.js";
|
|
6
|
-
import { PromisePolyfill } from "./promise-polyfill.js";
|
|
7
|
-
import { withHooks, effectScheduler } from "./hook-engine.js";
|
|
8
|
-
import { AbortPromptError, CancelPromptError, ExitPromptError } from "./errors.js";
|
|
9
|
-
function getCallSites() {
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
11
|
-
const _prepareStackTrace = Error.prepareStackTrace;
|
|
12
|
-
let result = [];
|
|
13
|
-
try {
|
|
14
|
-
Error.prepareStackTrace = (_, callSites) => {
|
|
15
|
-
const callSitesWithoutCurrent = callSites.slice(1);
|
|
16
|
-
result = callSitesWithoutCurrent;
|
|
17
|
-
return callSitesWithoutCurrent;
|
|
18
|
-
};
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
20
|
-
new Error().stack;
|
|
21
|
-
}
|
|
22
|
-
catch {
|
|
23
|
-
// An error will occur if the Node flag --frozen-intrinsics is used.
|
|
24
|
-
// https://nodejs.org/api/cli.html#--frozen-intrinsics
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
Error.prepareStackTrace = _prepareStackTrace;
|
|
28
|
-
return result;
|
|
29
|
-
}
|
|
30
|
-
export function createPrompt(view) {
|
|
31
|
-
const callSites = getCallSites();
|
|
32
|
-
const prompt = (config, context = {}) => {
|
|
33
|
-
// Default `input` to stdin
|
|
34
|
-
const { input = process.stdin, signal } = context;
|
|
35
|
-
const cleanups = new Set();
|
|
36
|
-
// Add mute capabilities to the output
|
|
37
|
-
const output = new MuteStream();
|
|
38
|
-
output.pipe(context.output ?? process.stdout);
|
|
39
|
-
const rl = readline.createInterface({
|
|
40
|
-
terminal: true,
|
|
41
|
-
input,
|
|
42
|
-
output,
|
|
43
|
-
});
|
|
44
|
-
const screen = new ScreenManager(rl);
|
|
45
|
-
const { promise, resolve, reject } = PromisePolyfill.withResolver();
|
|
46
|
-
const cancel = () => reject(new CancelPromptError());
|
|
47
|
-
if (signal) {
|
|
48
|
-
const abort = () => reject(new AbortPromptError({ cause: signal.reason }));
|
|
49
|
-
if (signal.aborted) {
|
|
50
|
-
abort();
|
|
51
|
-
return Object.assign(promise, { cancel });
|
|
52
|
-
}
|
|
53
|
-
signal.addEventListener('abort', abort);
|
|
54
|
-
cleanups.add(() => signal.removeEventListener('abort', abort));
|
|
55
|
-
}
|
|
56
|
-
cleanups.add(onSignalExit((code, signal) => {
|
|
57
|
-
reject(new ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
|
|
58
|
-
}));
|
|
59
|
-
// SIGINT must be explicitly handled by the prompt so the ExitPromptError can be handled.
|
|
60
|
-
// Otherwise, the prompt will stop and in some scenarios never resolve.
|
|
61
|
-
// Ref issue #1741
|
|
62
|
-
const sigint = () => reject(new ExitPromptError(`User force closed the prompt with SIGINT`));
|
|
63
|
-
rl.on('SIGINT', sigint);
|
|
64
|
-
cleanups.add(() => rl.removeListener('SIGINT', sigint));
|
|
65
|
-
// Re-renders only happen when the state change; but the readline cursor could change position
|
|
66
|
-
// and that also requires a re-render (and a manual one because we mute the streams).
|
|
67
|
-
// We set the listener after the initial workLoop to avoid a double render if render triggered
|
|
68
|
-
// by a state change sets the cursor to the right position.
|
|
69
|
-
const checkCursorPos = () => screen.checkCursorPos();
|
|
70
|
-
rl.input.on('keypress', checkCursorPos);
|
|
71
|
-
cleanups.add(() => rl.input.removeListener('keypress', checkCursorPos));
|
|
72
|
-
return withHooks(rl, (cycle) => {
|
|
73
|
-
// The close event triggers immediately when the user press ctrl+c. SignalExit on the other hand
|
|
74
|
-
// triggers after the process is done (which happens after timeouts are done triggering.)
|
|
75
|
-
// We triggers the hooks cleanup phase on rl `close` so active timeouts can be cleared.
|
|
76
|
-
const hooksCleanup = AsyncResource.bind(() => effectScheduler.clearAll());
|
|
77
|
-
rl.on('close', hooksCleanup);
|
|
78
|
-
cleanups.add(() => rl.removeListener('close', hooksCleanup));
|
|
79
|
-
cycle(() => {
|
|
80
|
-
try {
|
|
81
|
-
const nextView = view(config, (value) => {
|
|
82
|
-
setImmediate(() => resolve(value));
|
|
83
|
-
});
|
|
84
|
-
// Typescript won't allow this, but not all users rely on typescript.
|
|
85
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
86
|
-
if (nextView === undefined) {
|
|
87
|
-
const callerFilename = callSites[1]?.getFileName();
|
|
88
|
-
throw new Error(`Prompt functions must return a string.\n at ${callerFilename}`);
|
|
89
|
-
}
|
|
90
|
-
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
91
|
-
screen.render(content, bottomContent);
|
|
92
|
-
effectScheduler.run();
|
|
93
|
-
}
|
|
94
|
-
catch (error) {
|
|
95
|
-
reject(error);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
return Object.assign(promise
|
|
99
|
-
.then((answer) => {
|
|
100
|
-
effectScheduler.clearAll();
|
|
101
|
-
return answer;
|
|
102
|
-
}, (error) => {
|
|
103
|
-
effectScheduler.clearAll();
|
|
104
|
-
throw error;
|
|
105
|
-
})
|
|
106
|
-
// Wait for the promise to settle, then cleanup.
|
|
107
|
-
.finally(() => {
|
|
108
|
-
cleanups.forEach((cleanup) => cleanup());
|
|
109
|
-
screen.done({ clearContent: Boolean(context.clearPromptOnDone) });
|
|
110
|
-
output.end();
|
|
111
|
-
})
|
|
112
|
-
// Once cleanup is done, let the expose promise resolve/reject to the internal one.
|
|
113
|
-
.then(() => promise), { cancel });
|
|
114
|
-
});
|
|
115
|
-
};
|
|
116
|
-
return prompt;
|
|
117
|
-
}
|
package/dist/esm/lib/errors.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export declare class AbortPromptError extends Error {
|
|
2
|
-
name: string;
|
|
3
|
-
message: string;
|
|
4
|
-
constructor(options?: {
|
|
5
|
-
cause?: unknown;
|
|
6
|
-
});
|
|
7
|
-
}
|
|
8
|
-
export declare class CancelPromptError extends Error {
|
|
9
|
-
name: string;
|
|
10
|
-
message: string;
|
|
11
|
-
}
|
|
12
|
-
export declare class ExitPromptError extends Error {
|
|
13
|
-
name: string;
|
|
14
|
-
}
|
|
15
|
-
export declare class HookError extends Error {
|
|
16
|
-
name: string;
|
|
17
|
-
}
|
|
18
|
-
export declare class ValidationError extends Error {
|
|
19
|
-
name: string;
|
|
20
|
-
}
|
package/dist/esm/lib/errors.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export class AbortPromptError extends Error {
|
|
2
|
-
name = 'AbortPromptError';
|
|
3
|
-
message = 'Prompt was aborted';
|
|
4
|
-
constructor(options) {
|
|
5
|
-
super();
|
|
6
|
-
this.cause = options?.cause;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
export class CancelPromptError extends Error {
|
|
10
|
-
name = 'CancelPromptError';
|
|
11
|
-
message = 'Prompt was canceled';
|
|
12
|
-
}
|
|
13
|
-
export class ExitPromptError extends Error {
|
|
14
|
-
name = 'ExitPromptError';
|
|
15
|
-
}
|
|
16
|
-
export class HookError extends Error {
|
|
17
|
-
name = 'HookError';
|
|
18
|
-
}
|
|
19
|
-
export class ValidationError extends Error {
|
|
20
|
-
name = 'ValidationError';
|
|
21
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { InquirerReadline } from '@inquirer/type';
|
|
2
|
-
export declare function withHooks<T>(rl: InquirerReadline, cb: (cycle: (render: () => void) => void) => T): T;
|
|
3
|
-
export declare function readline(): InquirerReadline;
|
|
4
|
-
export declare function withUpdates<Args extends unknown[], R>(fn: (...args: Args) => R): (...args: Args) => R;
|
|
5
|
-
type SetPointer<Value> = {
|
|
6
|
-
get(): Value;
|
|
7
|
-
set(value: Value): void;
|
|
8
|
-
initialized: true;
|
|
9
|
-
};
|
|
10
|
-
type UnsetPointer<Value> = {
|
|
11
|
-
get(): void;
|
|
12
|
-
set(value: Value): void;
|
|
13
|
-
initialized: false;
|
|
14
|
-
};
|
|
15
|
-
type Pointer<Value> = SetPointer<Value> | UnsetPointer<Value>;
|
|
16
|
-
export declare function withPointer<Value, ReturnValue>(cb: (pointer: Pointer<Value>) => ReturnValue): ReturnValue;
|
|
17
|
-
export declare function handleChange(): void;
|
|
18
|
-
export declare const effectScheduler: {
|
|
19
|
-
queue(cb: (readline: InquirerReadline) => void | (() => void)): void;
|
|
20
|
-
run(): void;
|
|
21
|
-
clearAll(): void;
|
|
22
|
-
};
|
|
23
|
-
export {};
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
/* eslint @typescript-eslint/no-explicit-any: ["off"] */
|
|
2
|
-
import { AsyncLocalStorage, AsyncResource } from 'node:async_hooks';
|
|
3
|
-
import { HookError, ValidationError } from "./errors.js";
|
|
4
|
-
const hookStorage = new AsyncLocalStorage();
|
|
5
|
-
function createStore(rl) {
|
|
6
|
-
const store = {
|
|
7
|
-
rl,
|
|
8
|
-
hooks: [],
|
|
9
|
-
hooksCleanup: [],
|
|
10
|
-
hooksEffect: [],
|
|
11
|
-
index: 0,
|
|
12
|
-
handleChange() { },
|
|
13
|
-
};
|
|
14
|
-
return store;
|
|
15
|
-
}
|
|
16
|
-
// Run callback in with the hook engine setup.
|
|
17
|
-
export function withHooks(rl, cb) {
|
|
18
|
-
const store = createStore(rl);
|
|
19
|
-
return hookStorage.run(store, () => {
|
|
20
|
-
function cycle(render) {
|
|
21
|
-
store.handleChange = () => {
|
|
22
|
-
store.index = 0;
|
|
23
|
-
render();
|
|
24
|
-
};
|
|
25
|
-
store.handleChange();
|
|
26
|
-
}
|
|
27
|
-
return cb(cycle);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
// Safe getStore utility that'll return the store or throw if undefined.
|
|
31
|
-
function getStore() {
|
|
32
|
-
const store = hookStorage.getStore();
|
|
33
|
-
if (!store) {
|
|
34
|
-
throw new HookError('[Inquirer] Hook functions can only be called from within a prompt');
|
|
35
|
-
}
|
|
36
|
-
return store;
|
|
37
|
-
}
|
|
38
|
-
export function readline() {
|
|
39
|
-
return getStore().rl;
|
|
40
|
-
}
|
|
41
|
-
// Merge state updates happening within the callback function to avoid multiple renders.
|
|
42
|
-
export function withUpdates(fn) {
|
|
43
|
-
const wrapped = (...args) => {
|
|
44
|
-
const store = getStore();
|
|
45
|
-
let shouldUpdate = false;
|
|
46
|
-
const oldHandleChange = store.handleChange;
|
|
47
|
-
store.handleChange = () => {
|
|
48
|
-
shouldUpdate = true;
|
|
49
|
-
};
|
|
50
|
-
const returnValue = fn(...args);
|
|
51
|
-
if (shouldUpdate) {
|
|
52
|
-
oldHandleChange();
|
|
53
|
-
}
|
|
54
|
-
store.handleChange = oldHandleChange;
|
|
55
|
-
return returnValue;
|
|
56
|
-
};
|
|
57
|
-
return AsyncResource.bind(wrapped);
|
|
58
|
-
}
|
|
59
|
-
export function withPointer(cb) {
|
|
60
|
-
const store = getStore();
|
|
61
|
-
const { index } = store;
|
|
62
|
-
const pointer = {
|
|
63
|
-
get() {
|
|
64
|
-
return store.hooks[index];
|
|
65
|
-
},
|
|
66
|
-
set(value) {
|
|
67
|
-
store.hooks[index] = value;
|
|
68
|
-
},
|
|
69
|
-
initialized: index in store.hooks,
|
|
70
|
-
};
|
|
71
|
-
const returnValue = cb(pointer);
|
|
72
|
-
store.index++;
|
|
73
|
-
return returnValue;
|
|
74
|
-
}
|
|
75
|
-
export function handleChange() {
|
|
76
|
-
getStore().handleChange();
|
|
77
|
-
}
|
|
78
|
-
export const effectScheduler = {
|
|
79
|
-
queue(cb) {
|
|
80
|
-
const store = getStore();
|
|
81
|
-
const { index } = store;
|
|
82
|
-
store.hooksEffect.push(() => {
|
|
83
|
-
store.hooksCleanup[index]?.();
|
|
84
|
-
const cleanFn = cb(readline());
|
|
85
|
-
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
86
|
-
throw new ValidationError('useEffect return value must be a cleanup function or nothing.');
|
|
87
|
-
}
|
|
88
|
-
store.hooksCleanup[index] = cleanFn;
|
|
89
|
-
});
|
|
90
|
-
},
|
|
91
|
-
run() {
|
|
92
|
-
const store = getStore();
|
|
93
|
-
withUpdates(() => {
|
|
94
|
-
store.hooksEffect.forEach((effect) => {
|
|
95
|
-
effect();
|
|
96
|
-
});
|
|
97
|
-
// Warning: Clean the hooks before exiting the `withUpdates` block.
|
|
98
|
-
// Failure to do so means an updates would hit the same effects again.
|
|
99
|
-
store.hooksEffect.length = 0;
|
|
100
|
-
})();
|
|
101
|
-
},
|
|
102
|
-
clearAll() {
|
|
103
|
-
const store = getStore();
|
|
104
|
-
store.hooksCleanup.forEach((cleanFn) => {
|
|
105
|
-
cleanFn?.();
|
|
106
|
-
});
|
|
107
|
-
store.hooksEffect.length = 0;
|
|
108
|
-
store.hooksCleanup.length = 0;
|
|
109
|
-
},
|
|
110
|
-
};
|
package/dist/esm/lib/key.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type KeypressEvent = {
|
|
2
|
-
name: string;
|
|
3
|
-
ctrl: boolean;
|
|
4
|
-
};
|
|
5
|
-
export type Keybinding = 'emacs' | 'vim';
|
|
6
|
-
export declare const isUpKey: (key: KeypressEvent, keybindings?: ReadonlyArray<Keybinding>) => boolean;
|
|
7
|
-
export declare const isDownKey: (key: KeypressEvent, keybindings?: ReadonlyArray<Keybinding>) => boolean;
|
|
8
|
-
export declare const isSpaceKey: (key: KeypressEvent) => boolean;
|
|
9
|
-
export declare const isBackspaceKey: (key: KeypressEvent) => boolean;
|
|
10
|
-
export declare const isTabKey: (key: KeypressEvent) => boolean;
|
|
11
|
-
export declare const isNumberKey: (key: KeypressEvent) => boolean;
|
|
12
|
-
export declare const isEnterKey: (key: KeypressEvent) => boolean;
|
package/dist/esm/lib/key.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export const isUpKey = (key, keybindings = []) =>
|
|
2
|
-
// The up key
|
|
3
|
-
key.name === 'up' ||
|
|
4
|
-
// Vim keybinding: hjkl keys map to left/down/up/right
|
|
5
|
-
(keybindings.includes('vim') && key.name === 'k') ||
|
|
6
|
-
// Emacs keybinding: Ctrl+P means "previous" in Emacs navigation conventions
|
|
7
|
-
(keybindings.includes('emacs') && key.ctrl && key.name === 'p');
|
|
8
|
-
export const isDownKey = (key, keybindings = []) =>
|
|
9
|
-
// The down key
|
|
10
|
-
key.name === 'down' ||
|
|
11
|
-
// Vim keybinding: hjkl keys map to left/down/up/right
|
|
12
|
-
(keybindings.includes('vim') && key.name === 'j') ||
|
|
13
|
-
// Emacs keybinding: Ctrl+N means "next" in Emacs navigation conventions
|
|
14
|
-
(keybindings.includes('emacs') && key.ctrl && key.name === 'n');
|
|
15
|
-
export const isSpaceKey = (key) => key.name === 'space';
|
|
16
|
-
export const isBackspaceKey = (key) => key.name === 'backspace';
|
|
17
|
-
export const isTabKey = (key) => key.name === 'tab';
|
|
18
|
-
export const isNumberKey = (key) => '1234567890'.includes(key.name);
|
|
19
|
-
export const isEnterKey = (key) => key.name === 'enter' || key.name === 'return';
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { defaultTheme } from "./theme.js";
|
|
2
|
-
function isPlainObject(value) {
|
|
3
|
-
if (typeof value !== 'object' || value === null)
|
|
4
|
-
return false;
|
|
5
|
-
let proto = value;
|
|
6
|
-
while (Object.getPrototypeOf(proto) !== null) {
|
|
7
|
-
proto = Object.getPrototypeOf(proto);
|
|
8
|
-
}
|
|
9
|
-
return Object.getPrototypeOf(value) === proto;
|
|
10
|
-
}
|
|
11
|
-
function deepMerge(...objects) {
|
|
12
|
-
const output = {};
|
|
13
|
-
for (const obj of objects) {
|
|
14
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
15
|
-
const prevValue = output[key];
|
|
16
|
-
output[key] =
|
|
17
|
-
isPlainObject(prevValue) && isPlainObject(value)
|
|
18
|
-
? deepMerge(prevValue, value)
|
|
19
|
-
: value;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return output;
|
|
23
|
-
}
|
|
24
|
-
export function makeTheme(...themes) {
|
|
25
|
-
const themesToMerge = [
|
|
26
|
-
defaultTheme,
|
|
27
|
-
...themes.filter((theme) => theme != null),
|
|
28
|
-
];
|
|
29
|
-
return deepMerge(...themesToMerge);
|
|
30
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { Prettify } from '@inquirer/type';
|
|
2
|
-
export declare function usePagination<T>({ items, active, renderItem, pageSize, loop, }: {
|
|
3
|
-
items: ReadonlyArray<T>;
|
|
4
|
-
/** The index of the active item. */
|
|
5
|
-
active: number;
|
|
6
|
-
/** Renders an item as part of a page. */
|
|
7
|
-
renderItem: (layout: Prettify<{
|
|
8
|
-
item: T;
|
|
9
|
-
index: number;
|
|
10
|
-
isActive: boolean;
|
|
11
|
-
}>) => string;
|
|
12
|
-
/** The size of the page. */
|
|
13
|
-
pageSize: number;
|
|
14
|
-
/** Allows creating an infinitely looping list. `true` if unspecified. */
|
|
15
|
-
loop?: boolean;
|
|
16
|
-
}): string;
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { useRef } from "../use-ref.js";
|
|
2
|
-
import { readlineWidth, breakLines } from "../utils.js";
|
|
3
|
-
function usePointerPosition({ active, renderedItems, pageSize, loop, }) {
|
|
4
|
-
const state = useRef({
|
|
5
|
-
lastPointer: active,
|
|
6
|
-
lastActive: undefined,
|
|
7
|
-
});
|
|
8
|
-
const { lastPointer, lastActive } = state.current;
|
|
9
|
-
const middle = Math.floor(pageSize / 2);
|
|
10
|
-
const renderedLength = renderedItems.reduce((acc, item) => acc + item.length, 0);
|
|
11
|
-
const defaultPointerPosition = renderedItems
|
|
12
|
-
.slice(0, active)
|
|
13
|
-
.reduce((acc, item) => acc + item.length, 0);
|
|
14
|
-
let pointer = defaultPointerPosition;
|
|
15
|
-
if (renderedLength > pageSize) {
|
|
16
|
-
if (loop) {
|
|
17
|
-
/**
|
|
18
|
-
* Creates the next position for the pointer considering an infinitely
|
|
19
|
-
* looping list of items to be rendered on the page.
|
|
20
|
-
*
|
|
21
|
-
* The goal is to progressively move the cursor to the middle position as the user move down, and then keep
|
|
22
|
-
* the cursor there. When the user move up, maintain the cursor position.
|
|
23
|
-
*/
|
|
24
|
-
// By default, keep the cursor position as-is.
|
|
25
|
-
pointer = lastPointer;
|
|
26
|
-
if (
|
|
27
|
-
// First render, skip this logic.
|
|
28
|
-
lastActive != null &&
|
|
29
|
-
// Only move the pointer down when the user moves down.
|
|
30
|
-
lastActive < active &&
|
|
31
|
-
// Check user didn't move up across page boundary.
|
|
32
|
-
active - lastActive < pageSize) {
|
|
33
|
-
pointer = Math.min(
|
|
34
|
-
// Furthest allowed position for the pointer is the middle of the list
|
|
35
|
-
middle, Math.abs(active - lastActive) === 1
|
|
36
|
-
? Math.min(
|
|
37
|
-
// Move the pointer at most the height of the last active item.
|
|
38
|
-
lastPointer + (renderedItems[lastActive]?.length ?? 0),
|
|
39
|
-
// If the user moved by one item, move the pointer to the natural position of the active item as
|
|
40
|
-
// long as it doesn't move the cursor up.
|
|
41
|
-
Math.max(defaultPointerPosition, lastPointer))
|
|
42
|
-
: // Otherwise, move the pointer down by the difference between the active and last active item.
|
|
43
|
-
lastPointer + active - lastActive);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
/**
|
|
48
|
-
* Creates the next position for the pointer considering a finite list of
|
|
49
|
-
* items to be rendered on a page.
|
|
50
|
-
*
|
|
51
|
-
* The goal is to keep the pointer in the middle of the page whenever possible, until
|
|
52
|
-
* we reach the bounds of the list (top or bottom). In which case, the cursor moves progressively
|
|
53
|
-
* to the bottom or top of the list.
|
|
54
|
-
*/
|
|
55
|
-
const spaceUnderActive = renderedItems
|
|
56
|
-
.slice(active)
|
|
57
|
-
.reduce((acc, item) => acc + item.length, 0);
|
|
58
|
-
pointer =
|
|
59
|
-
spaceUnderActive < pageSize - middle
|
|
60
|
-
? // If the active item is near the end of the list, progressively move the cursor towards the end.
|
|
61
|
-
pageSize - spaceUnderActive
|
|
62
|
-
: // Otherwise, progressively move the pointer to the middle of the list.
|
|
63
|
-
Math.min(defaultPointerPosition, middle);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
// Save state for the next render
|
|
67
|
-
state.current.lastPointer = pointer;
|
|
68
|
-
state.current.lastActive = active;
|
|
69
|
-
return pointer;
|
|
70
|
-
}
|
|
71
|
-
export function usePagination({ items, active, renderItem, pageSize, loop = true, }) {
|
|
72
|
-
const width = readlineWidth();
|
|
73
|
-
const bound = (num) => ((num % items.length) + items.length) % items.length;
|
|
74
|
-
const renderedItems = items.map((item, index) => {
|
|
75
|
-
if (item == null)
|
|
76
|
-
return [];
|
|
77
|
-
return breakLines(renderItem({ item, index, isActive: index === active }), width).split('\n');
|
|
78
|
-
});
|
|
79
|
-
const renderedLength = renderedItems.reduce((acc, item) => acc + item.length, 0);
|
|
80
|
-
const renderItemAtIndex = (index) => renderedItems[index] ?? [];
|
|
81
|
-
const pointer = usePointerPosition({ active, renderedItems, pageSize, loop });
|
|
82
|
-
// Render the active item to decide the position.
|
|
83
|
-
// If the active item fits under the pointer, we render it there.
|
|
84
|
-
// Otherwise, we need to render it to fit at the bottom of the page; moving the pointer up.
|
|
85
|
-
const activeItem = renderItemAtIndex(active).slice(0, pageSize);
|
|
86
|
-
const activeItemPosition = pointer + activeItem.length <= pageSize ? pointer : pageSize - activeItem.length;
|
|
87
|
-
// Create an array of lines for the page, and add the lines of the active item into the page
|
|
88
|
-
const pageBuffer = Array.from({ length: pageSize });
|
|
89
|
-
pageBuffer.splice(activeItemPosition, activeItem.length, ...activeItem);
|
|
90
|
-
// Store to prevent rendering the same item twice
|
|
91
|
-
const itemVisited = new Set([active]);
|
|
92
|
-
// Fill the page under the active item
|
|
93
|
-
let bufferPointer = activeItemPosition + activeItem.length;
|
|
94
|
-
let itemPointer = bound(active + 1);
|
|
95
|
-
while (bufferPointer < pageSize &&
|
|
96
|
-
!itemVisited.has(itemPointer) &&
|
|
97
|
-
(loop && renderedLength > pageSize ? itemPointer !== active : itemPointer > active)) {
|
|
98
|
-
const lines = renderItemAtIndex(itemPointer);
|
|
99
|
-
const linesToAdd = lines.slice(0, pageSize - bufferPointer);
|
|
100
|
-
pageBuffer.splice(bufferPointer, linesToAdd.length, ...linesToAdd);
|
|
101
|
-
// Move pointers for next iteration
|
|
102
|
-
itemVisited.add(itemPointer);
|
|
103
|
-
bufferPointer += linesToAdd.length;
|
|
104
|
-
itemPointer = bound(itemPointer + 1);
|
|
105
|
-
}
|
|
106
|
-
// Fill the page over the active item
|
|
107
|
-
bufferPointer = activeItemPosition - 1;
|
|
108
|
-
itemPointer = bound(active - 1);
|
|
109
|
-
while (bufferPointer >= 0 &&
|
|
110
|
-
!itemVisited.has(itemPointer) &&
|
|
111
|
-
(loop && renderedLength > pageSize ? itemPointer !== active : itemPointer < active)) {
|
|
112
|
-
const lines = renderItemAtIndex(itemPointer);
|
|
113
|
-
const linesToAdd = lines.slice(Math.max(0, lines.length - bufferPointer - 1));
|
|
114
|
-
pageBuffer.splice(bufferPointer - linesToAdd.length + 1, linesToAdd.length, ...linesToAdd);
|
|
115
|
-
// Move pointers for next iteration
|
|
116
|
-
itemVisited.add(itemPointer);
|
|
117
|
-
bufferPointer -= linesToAdd.length;
|
|
118
|
-
itemPointer = bound(itemPointer - 1);
|
|
119
|
-
}
|
|
120
|
-
return pageBuffer.filter((line) => typeof line === 'string').join('\n');
|
|
121
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// TODO: Remove this class once Node 22 becomes the minimum supported version.
|
|
2
|
-
export class PromisePolyfill extends Promise {
|
|
3
|
-
// Available starting from Node 22
|
|
4
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
|
|
5
|
-
static withResolver() {
|
|
6
|
-
let resolve;
|
|
7
|
-
let reject;
|
|
8
|
-
const promise = new Promise((res, rej) => {
|
|
9
|
-
resolve = res;
|
|
10
|
-
reject = rej;
|
|
11
|
-
});
|
|
12
|
-
return { promise, resolve: resolve, reject: reject };
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -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
|
-
}
|