@inquirer/core 4.0.0 → 4.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.
Files changed (53) hide show
  1. package/dist/cjs/index.js +17 -268
  2. package/dist/cjs/lib/Separator.js +2 -5
  3. package/dist/cjs/lib/create-prompt.js +111 -0
  4. package/dist/cjs/lib/hook-engine.js +100 -0
  5. package/dist/cjs/lib/read-line.type.js +2 -0
  6. package/dist/cjs/lib/screen-manager.js +3 -6
  7. package/dist/cjs/lib/use-effect.js +15 -0
  8. package/dist/cjs/lib/use-keypress.js +20 -0
  9. package/dist/cjs/lib/use-pagination.js +34 -0
  10. package/dist/cjs/lib/{prefix.js → use-prefix.js} +7 -9
  11. package/dist/cjs/lib/use-ref.js +8 -0
  12. package/dist/cjs/lib/use-state.js +28 -0
  13. package/dist/cjs/lib/utils.js +2 -5
  14. package/dist/cjs/types/index.d.ts +9 -36
  15. package/dist/cjs/types/lib/create-prompt.d.ts +11 -0
  16. package/dist/cjs/types/lib/hook-engine.d.ts +24 -0
  17. package/dist/cjs/types/lib/key.d.ts +1 -2
  18. package/dist/cjs/types/lib/read-line.type.d.ts +9 -0
  19. package/dist/cjs/types/lib/screen-manager.d.ts +1 -1
  20. package/dist/cjs/types/lib/use-effect.d.ts +2 -0
  21. package/dist/cjs/types/lib/use-keypress.d.ts +3 -0
  22. package/dist/cjs/types/lib/use-pagination.d.ts +4 -0
  23. package/dist/cjs/types/lib/use-ref.d.ts +3 -0
  24. package/dist/cjs/types/lib/use-state.d.ts +3 -0
  25. package/dist/esm/index.mjs +8 -243
  26. package/dist/esm/lib/create-prompt.mjs +99 -0
  27. package/dist/esm/lib/hook-engine.mjs +91 -0
  28. package/dist/esm/lib/read-line.type.mjs +1 -0
  29. package/dist/esm/lib/use-effect.mjs +11 -0
  30. package/dist/esm/lib/use-keypress.mjs +16 -0
  31. package/dist/esm/lib/use-pagination.mjs +30 -0
  32. package/dist/esm/lib/{prefix.mjs → use-prefix.mjs} +3 -2
  33. package/dist/esm/lib/use-ref.mjs +4 -0
  34. package/dist/esm/lib/use-state.mjs +24 -0
  35. package/dist/esm/lib/utils.mjs +1 -1
  36. package/dist/esm/types/index.d.mts +9 -36
  37. package/dist/esm/types/lib/create-prompt.d.mts +11 -0
  38. package/dist/esm/types/lib/hook-engine.d.mts +24 -0
  39. package/dist/esm/types/lib/key.d.mts +1 -2
  40. package/dist/esm/types/lib/read-line.type.d.mts +9 -0
  41. package/dist/esm/types/lib/screen-manager.d.mts +1 -1
  42. package/dist/esm/types/lib/use-effect.d.mts +2 -0
  43. package/dist/esm/types/lib/use-keypress.d.mts +3 -0
  44. package/dist/esm/types/lib/use-pagination.d.mts +4 -0
  45. package/dist/esm/types/lib/use-ref.d.mts +3 -0
  46. package/dist/esm/types/lib/use-state.d.mts +3 -0
  47. package/package.json +9 -9
  48. package/dist/cjs/lib/options.js +0 -19
  49. package/dist/cjs/types/lib/options.d.ts +0 -2
  50. package/dist/esm/lib/options.mjs +0 -8
  51. package/dist/esm/types/lib/options.d.mts +0 -2
  52. /package/dist/cjs/types/lib/{prefix.d.ts → use-prefix.d.ts} +0 -0
  53. /package/dist/esm/types/lib/{prefix.d.mts → use-prefix.d.mts} +0 -0
@@ -0,0 +1,30 @@
1
+ import chalk from 'chalk';
2
+ import cliWidth from 'cli-width';
3
+ import { breakLines } from './utils.mjs';
4
+ import { readline } from './hook-engine.mjs';
5
+ import { useRef } from './use-ref.mjs';
6
+ export function usePagination(output, { active, pageSize = 7, }) {
7
+ const rl = readline();
8
+ const state = useRef({
9
+ pointer: 0,
10
+ lastIndex: 0,
11
+ });
12
+ const width = cliWidth({ defaultWidth: 80, output: rl.output });
13
+ const lines = breakLines(output, width).split('\n');
14
+ // Make sure there's enough lines to paginate
15
+ if (lines.length <= pageSize) {
16
+ return output;
17
+ }
18
+ const middleOfList = Math.floor(pageSize / 2);
19
+ // Move the pointer only when the user go down and limit it to the middle of the list
20
+ const { pointer: prevPointer, lastIndex } = state.current;
21
+ if (prevPointer < middleOfList && lastIndex < active && active - lastIndex < pageSize) {
22
+ state.current.pointer = Math.min(middleOfList, prevPointer + active - lastIndex);
23
+ }
24
+ state.current.lastIndex = active;
25
+ // Duplicate the lines so it give an infinite list look
26
+ const infinite = [lines, lines, lines].flat();
27
+ const topIndex = Math.max(0, active + lines.length - state.current.pointer);
28
+ const section = infinite.splice(topIndex, pageSize).join('\n');
29
+ return section + '\n' + chalk.dim('(Use arrow keys to reveal more choices)');
30
+ }
@@ -1,6 +1,7 @@
1
1
  import chalk from 'chalk';
2
- import spinners from 'cli-spinners';
3
- import { useState, useEffect } from '../index.mjs';
2
+ import * as spinners from 'cli-spinners';
3
+ import { useState } from './use-state.mjs';
4
+ import { useEffect } from './use-effect.mjs';
4
5
  const spinner = spinners.dots;
5
6
  export function usePrefix(isLoading = false) {
6
7
  const [tick, setTick] = useState(0);
@@ -0,0 +1,4 @@
1
+ import { useState } from './use-state.mjs';
2
+ export function useRef(val) {
3
+ return useState({ current: val })[0];
4
+ }
@@ -0,0 +1,24 @@
1
+ import { withPointer, handleChange } from './hook-engine.mjs';
2
+ export function useState(defaultValue) {
3
+ return withPointer((pointer) => {
4
+ if (!pointer.initialized) {
5
+ if (typeof defaultValue === 'function') {
6
+ pointer.set(defaultValue());
7
+ }
8
+ else {
9
+ pointer.set(defaultValue);
10
+ }
11
+ }
12
+ return [
13
+ pointer.get(),
14
+ (newValue) => {
15
+ // Noop if the value is still the same.
16
+ if (pointer.get() !== newValue) {
17
+ pointer.set(newValue);
18
+ // Trigger re-render
19
+ handleChange();
20
+ }
21
+ },
22
+ ];
23
+ });
24
+ }
@@ -10,5 +10,5 @@ export const breakLines = (content, width) => content
10
10
  .split('\n')
11
11
  .flatMap((line) => wrapAnsi(line, width, { trim: false, hard: true })
12
12
  .split('\n')
13
- .map((line) => line.trimEnd()))
13
+ .map((str) => str.trimEnd()))
14
14
  .join('\n');
@@ -1,37 +1,10 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- /// <reference types="node" resolution-mode="require"/>
3
- import * as readline from 'node:readline';
4
- import { type Prompt } from '@inquirer/type';
5
- import MuteStream from 'mute-stream';
6
- export { usePrefix } from './lib/prefix.mjs';
7
1
  export * from './lib/key.mjs';
8
- export * from './lib/Separator.mjs';
9
- export type InquirerReadline = readline.ReadLine & {
10
- output: MuteStream;
11
- input: NodeJS.ReadableStream;
12
- clearLine: (dir: 0 | 1 | -1) => void;
13
- };
14
- export type KeypressEvent = {
15
- name: string;
16
- ctrl: boolean;
17
- };
18
- type NotFunction<T> = T extends Function ? never : T;
19
- export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
20
- export declare function useEffect(cb: (rl: InquirerReadline) => void | (() => void), depArray: unknown[]): void;
21
- export declare function useRef<Value>(val: Value): {
22
- current: Value;
23
- };
24
- export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
25
- export declare function usePagination(output: string, { active, pageSize, }: {
26
- active: number;
27
- pageSize?: number;
28
- }): string;
29
- export type AsyncPromptConfig = {
30
- message: string | Promise<string> | (() => Promise<string>);
31
- validate?: (value: string) => boolean | string | Promise<string | boolean>;
32
- };
33
- export type ResolvedPromptConfig = {
34
- message: string;
35
- validate: (value: string) => boolean | string | Promise<string | boolean>;
36
- };
37
- export declare function createPrompt<Value, Config extends AsyncPromptConfig>(view: (config: Config & ResolvedPromptConfig, done: (value: Value) => void) => string | [string, string | undefined]): Prompt<Value, Config>;
2
+ export { usePrefix } from './lib/use-prefix.mjs';
3
+ export { useState } from './lib/use-state.mjs';
4
+ export { useEffect } from './lib/use-effect.mjs';
5
+ export { useRef } from './lib/use-ref.mjs';
6
+ export { useKeypress } from './lib/use-keypress.mjs';
7
+ export { usePagination } from './lib/use-pagination.mjs';
8
+ export { createPrompt, type AsyncPromptConfig } from './lib/create-prompt.mjs';
9
+ export { Separator } from './lib/Separator.mjs';
10
+ export { type InquirerReadline } from './lib/read-line.type.mjs';
@@ -0,0 +1,11 @@
1
+ import { type Prompt } from '@inquirer/type';
2
+ export type AsyncPromptConfig = {
3
+ message: string | Promise<string> | (() => Promise<string>);
4
+ validate?: (value: string) => boolean | string | Promise<string | boolean>;
5
+ };
6
+ type ResolvedPromptConfig = {
7
+ message: string;
8
+ validate: (value: string) => boolean | string | Promise<string | boolean>;
9
+ };
10
+ export declare function createPrompt<Value, Config extends AsyncPromptConfig>(view: (config: Config & ResolvedPromptConfig, done: (value: Value) => void) => string | [string, string | undefined]): Prompt<Value, Config>;
11
+ export {};
@@ -0,0 +1,24 @@
1
+ import type { InquirerReadline } from './read-line.type.mjs';
2
+ type HookStore = {
3
+ rl: InquirerReadline;
4
+ hooks: any[];
5
+ hooksCleanup: Array<void | (() => void)>;
6
+ hooksEffect: Array<() => void>;
7
+ index: number;
8
+ handleChange: () => void;
9
+ };
10
+ type Pointer = {
11
+ get(): any;
12
+ set(value: any): void;
13
+ initialized: boolean;
14
+ };
15
+ export declare function withHooks(rl: InquirerReadline, cb: (store: HookStore) => void): void;
16
+ export declare function readline(): InquirerReadline;
17
+ export declare function withUpdates<T extends (...args: any) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
18
+ export declare function withPointer<Value>(cb: (pointer: Pointer) => Value): Value;
19
+ export declare function handleChange(): void;
20
+ export declare const effectScheduler: {
21
+ queue(cb: (readline: InquirerReadline) => void): void;
22
+ run(): void;
23
+ };
24
+ export {};
@@ -1,4 +1,4 @@
1
- type KeypressEvent = {
1
+ export type KeypressEvent = {
2
2
  name: string;
3
3
  ctrl: boolean;
4
4
  };
@@ -8,4 +8,3 @@ export declare const isSpaceKey: (key: KeypressEvent) => boolean;
8
8
  export declare const isBackspaceKey: (key: KeypressEvent) => boolean;
9
9
  export declare const isNumberKey: (key: KeypressEvent) => boolean;
10
10
  export declare const isEnterKey: (key: KeypressEvent) => boolean;
11
- export {};
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ import * as readline from 'node:readline';
4
+ import MuteStream from 'mute-stream';
5
+ export type InquirerReadline = readline.ReadLine & {
6
+ output: MuteStream;
7
+ input: NodeJS.ReadableStream;
8
+ clearLine: (dir: 0 | 1 | -1) => void;
9
+ };
@@ -1,4 +1,4 @@
1
- import { InquirerReadline } from '../index.mjs';
1
+ import type { InquirerReadline } from './read-line.type.mjs';
2
2
  export default class ScreenManager {
3
3
  private readonly rl;
4
4
  private height;
@@ -0,0 +1,2 @@
1
+ import type { InquirerReadline } from './read-line.type.mjs';
2
+ export declare function useEffect(cb: (rl: InquirerReadline) => void | (() => void), depArray: unknown[]): void;
@@ -0,0 +1,3 @@
1
+ import { type InquirerReadline } from './read-line.type.mjs';
2
+ import { type KeypressEvent } from './key.mjs';
3
+ export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
@@ -0,0 +1,4 @@
1
+ export declare function usePagination(output: string, { active, pageSize, }: {
2
+ active: number;
3
+ pageSize?: number;
4
+ }): string;
@@ -0,0 +1,3 @@
1
+ export declare function useRef<Value>(val: Value): {
2
+ current: Value;
3
+ };
@@ -0,0 +1,3 @@
1
+ type NotFunction<T> = T extends Function ? never : T;
2
+ export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
3
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "engines": {
5
5
  "node": ">=14.18.0"
6
6
  },
@@ -57,9 +57,9 @@
57
57
  "license": "MIT",
58
58
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/core/README.md",
59
59
  "dependencies": {
60
- "@inquirer/type": "^1.1.2",
60
+ "@inquirer/type": "^1.1.3",
61
61
  "@types/mute-stream": "^0.0.1",
62
- "@types/node": "^20.5.6",
62
+ "@types/node": "^20.6.0",
63
63
  "@types/wrap-ansi": "^3.0.0",
64
64
  "ansi-escapes": "^4.3.2",
65
65
  "chalk": "^4.1.2",
@@ -68,17 +68,17 @@
68
68
  "figures": "^3.2.0",
69
69
  "mute-stream": "^1.0.0",
70
70
  "run-async": "^3.0.0",
71
+ "signal-exit": "^4.1.0",
71
72
  "strip-ansi": "^6.0.1",
72
73
  "wrap-ansi": "^6.2.0"
73
74
  },
74
75
  "devDependencies": {
75
- "@inquirer/testing": "^2.1.4"
76
+ "@inquirer/testing": "^2.1.5"
76
77
  },
77
78
  "scripts": {
78
- "tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
79
- "clean": "rm -rf dist",
80
- "tsc:esm": "tsc -p ./tsconfig.json",
81
- "tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
79
+ "tsc": "yarn run tsc:esm && yarn run tsc:cjs",
80
+ "tsc:esm": "rm -rf dist/esm && tsc -p ./tsconfig.json",
81
+ "tsc:cjs": "rm -rf dist/cjs && tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
82
82
  },
83
83
  "publishConfig": {
84
84
  "access": "public"
@@ -95,5 +95,5 @@
95
95
  }
96
96
  }
97
97
  },
98
- "gitHead": "ea5baff21fb29ed3c0931ea4030cc1dd1e92fc8d"
98
+ "gitHead": "f5c544a8ded1c7dfb6ac8364759955e8f2ecfb0c"
99
99
  }
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getPromptConfig = void 0;
13
- function getPromptConfig(option) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- const message = typeof option.message === 'function' ? option.message() : option.message;
16
- return Object.assign(Object.assign({ validate: () => true }, option), { message: yield message });
17
- });
18
- }
19
- exports.getPromptConfig = getPromptConfig;
@@ -1,2 +0,0 @@
1
- import type { AsyncPromptConfig, ResolvedPromptConfig } from '../index.js';
2
- export declare function getPromptConfig<In extends AsyncPromptConfig>(option: In): Promise<In & ResolvedPromptConfig>;
@@ -1,8 +0,0 @@
1
- export async function getPromptConfig(option) {
2
- const message = typeof option.message === 'function' ? option.message() : option.message;
3
- return {
4
- validate: () => true,
5
- ...option,
6
- message: await message,
7
- };
8
- }
@@ -1,2 +0,0 @@
1
- import type { AsyncPromptConfig, ResolvedPromptConfig } from '../index.mjs';
2
- export declare function getPromptConfig<In extends AsyncPromptConfig>(option: In): Promise<In & ResolvedPromptConfig>;