@inquirer/core 2.3.0 → 3.0.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/README.md CHANGED
@@ -51,7 +51,7 @@ const confirm = createPrompt<boolean, { message: string; default?: boolean }>(
51
51
 
52
52
  const message = chalk.bold(config.message);
53
53
  return `${prefix} ${message}${defaultValue} ${formattedValue}`;
54
- }
54
+ },
55
55
  );
56
56
 
57
57
  /**
package/dist/cjs/index.js CHANGED
@@ -17,16 +17,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.createPrompt = exports.useRef = exports.useKeypress = exports.useEffect = exports.useState = exports.usePrefix = void 0;
20
+ exports.createPrompt = exports.usePagination = exports.useRef = exports.useKeypress = exports.useEffect = exports.useState = exports.usePrefix = void 0;
21
21
  const node_readline_1 = __importDefault(require("node:readline"));
22
22
  const type_1 = require("@inquirer/type");
23
+ const chalk_1 = __importDefault(require("chalk"));
24
+ const cli_width_1 = __importDefault(require("cli-width"));
23
25
  const mute_stream_1 = __importDefault(require("mute-stream"));
24
26
  const screen_manager_mjs_1 = __importDefault(require('./lib/screen-manager.js'));
25
27
  const options_mjs_1 = require('./lib/options.js');
28
+ const utils_mjs_1 = require('./lib/utils.js');
26
29
  var prefix_mjs_1 = require('./lib/prefix.js');
27
30
  Object.defineProperty(exports, "usePrefix", { enumerable: true, get: function () { return prefix_mjs_1.usePrefix; } });
28
31
  __exportStar(require('./lib/key.js'), exports);
29
- __exportStar(require('./lib/Paginator.js'), exports);
30
32
  __exportStar(require('./lib/Separator.js'), exports);
31
33
  const hooks = [];
32
34
  const hooksCleanup = [];
@@ -133,6 +135,35 @@ function useRef(val) {
133
135
  return useState({ current: val })[0];
134
136
  }
135
137
  exports.useRef = useRef;
138
+ function usePagination(output, { active, pageSize = 7, }) {
139
+ const state = useRef({
140
+ pointer: 0,
141
+ lastIndex: 0,
142
+ });
143
+ const rl = sessionRl;
144
+ if (!rl) {
145
+ throw new Error('usePagination must be used within a prompt');
146
+ }
147
+ const width = (0, cli_width_1.default)({ defaultWidth: 80, output: rl.output });
148
+ const lines = (0, utils_mjs_1.breakLines)(output, width).split('\n');
149
+ // Make sure there's enough lines to paginate
150
+ if (lines.length <= pageSize) {
151
+ return output;
152
+ }
153
+ const middleOfList = Math.floor(pageSize / 2);
154
+ // Move the pointer only when the user go down and limit it to the middle of the list
155
+ const { pointer: prevPointer, lastIndex } = state.current;
156
+ if (prevPointer < middleOfList && lastIndex < active && active - lastIndex < pageSize) {
157
+ state.current.pointer = Math.min(middleOfList, prevPointer + active - lastIndex);
158
+ }
159
+ state.current.lastIndex = active;
160
+ // Duplicate the lines so it give an infinite list look
161
+ const infinite = [lines, lines, lines].flat();
162
+ const topIndex = Math.max(0, active + lines.length - state.current.pointer);
163
+ const section = infinite.splice(topIndex, pageSize).join('\n');
164
+ return section + '\n' + chalk_1.default.dim('(Move up and down to reveal more choices)');
165
+ }
166
+ exports.usePagination = usePagination;
136
167
  function createPrompt(view) {
137
168
  const prompt = (config, context) => {
138
169
  var _a, _b;
@@ -14,8 +14,8 @@ const wrap_ansi_1 = __importDefault(require("wrap-ansi"));
14
14
  */
15
15
  const breakLines = (content, width) => content
16
16
  .split('\n')
17
- .map((line) => (0, wrap_ansi_1.default)(line, width, { trim: false, hard: true }).split('\n'))
18
- .flat()
19
- .map((line) => line.trimEnd())
17
+ .flatMap((line) => (0, wrap_ansi_1.default)(line, width, { trim: false, hard: true })
18
+ .split('\n')
19
+ .map((line) => line.trimEnd()))
20
20
  .join('\n');
21
21
  exports.breakLines = breakLines;
@@ -3,10 +3,9 @@
3
3
  import readline from 'node:readline';
4
4
  import { type Prompt } from '@inquirer/type';
5
5
  import MuteStream from 'mute-stream';
6
- export { usePrefix } from './lib/prefix.d.cts';
7
- export * from './lib/key.d.cts';
8
- export * from './lib/Paginator.d.cts';
9
- export * from './lib/Separator.d.cts';
6
+ export { usePrefix } from './lib/prefix.js';
7
+ export * from './lib/key.js';
8
+ export * from './lib/Separator.js';
10
9
  export type InquirerReadline = readline.ReadLine & {
11
10
  output: MuteStream;
12
11
  input: NodeJS.ReadableStream;
@@ -23,6 +22,10 @@ export declare function useKeypress(userHandler: (event: KeypressEvent, rl: Inqu
23
22
  export declare function useRef<Value>(val: Value): {
24
23
  current: Value;
25
24
  };
25
+ export declare function usePagination(output: string, { active, pageSize, }: {
26
+ active: number;
27
+ pageSize?: number;
28
+ }): string;
26
29
  export type AsyncPromptConfig = {
27
30
  message: string | Promise<string> | (() => Promise<string>);
28
31
  validate?: (value: string) => boolean | string | Promise<string | boolean>;
@@ -1,2 +1,2 @@
1
- import type { AsyncPromptConfig, ResolvedPromptConfig } from '../index.d.cts';
1
+ import type { AsyncPromptConfig, ResolvedPromptConfig } from '../index.js';
2
2
  export declare function getPromptConfig<In extends AsyncPromptConfig>(option: In): Promise<In & ResolvedPromptConfig>;
@@ -1,4 +1,4 @@
1
- import { InquirerReadline } from '../index.d.cts';
1
+ import { InquirerReadline } from '../index.js';
2
2
  export default class ScreenManager {
3
3
  private readonly rl;
4
4
  private height;
@@ -1,11 +1,13 @@
1
1
  import readline from 'node:readline';
2
2
  import { CancelablePromise } from '@inquirer/type';
3
+ import chalk from 'chalk';
4
+ import cliWidth from 'cli-width';
3
5
  import MuteStream from 'mute-stream';
4
6
  import ScreenManager from './lib/screen-manager.mjs';
5
7
  import { getPromptConfig } from './lib/options.mjs';
8
+ import { breakLines } from './lib/utils.mjs';
6
9
  export { usePrefix } from './lib/prefix.mjs';
7
10
  export * from './lib/key.mjs';
8
- export * from './lib/Paginator.mjs';
9
11
  export * from './lib/Separator.mjs';
10
12
  const hooks = [];
11
13
  const hooksCleanup = [];
@@ -108,6 +110,34 @@ export function useKeypress(userHandler) {
108
110
  export function useRef(val) {
109
111
  return useState({ current: val })[0];
110
112
  }
113
+ export function usePagination(output, { active, pageSize = 7, }) {
114
+ const state = useRef({
115
+ pointer: 0,
116
+ lastIndex: 0,
117
+ });
118
+ const rl = sessionRl;
119
+ if (!rl) {
120
+ throw new Error('usePagination must be used within a prompt');
121
+ }
122
+ const width = cliWidth({ defaultWidth: 80, output: rl.output });
123
+ const lines = breakLines(output, width).split('\n');
124
+ // Make sure there's enough lines to paginate
125
+ if (lines.length <= pageSize) {
126
+ return output;
127
+ }
128
+ const middleOfList = Math.floor(pageSize / 2);
129
+ // Move the pointer only when the user go down and limit it to the middle of the list
130
+ const { pointer: prevPointer, lastIndex } = state.current;
131
+ if (prevPointer < middleOfList && lastIndex < active && active - lastIndex < pageSize) {
132
+ state.current.pointer = Math.min(middleOfList, prevPointer + active - lastIndex);
133
+ }
134
+ state.current.lastIndex = active;
135
+ // Duplicate the lines so it give an infinite list look
136
+ const infinite = [lines, lines, lines].flat();
137
+ const topIndex = Math.max(0, active + lines.length - state.current.pointer);
138
+ const section = infinite.splice(topIndex, pageSize).join('\n');
139
+ return section + '\n' + chalk.dim('(Move up and down to reveal more choices)');
140
+ }
111
141
  export function createPrompt(view) {
112
142
  const prompt = (config, context) => {
113
143
  if (sessionRl) {
@@ -8,7 +8,7 @@ import wrapAnsi from 'wrap-ansi';
8
8
  */
9
9
  export const breakLines = (content, width) => content
10
10
  .split('\n')
11
- .map((line) => wrapAnsi(line, width, { trim: false, hard: true }).split('\n'))
12
- .flat()
13
- .map((line) => line.trimEnd())
11
+ .flatMap((line) => wrapAnsi(line, width, { trim: false, hard: true })
12
+ .split('\n')
13
+ .map((line) => line.trimEnd()))
14
14
  .join('\n');
@@ -5,7 +5,6 @@ import { type Prompt } from '@inquirer/type';
5
5
  import MuteStream from 'mute-stream';
6
6
  export { usePrefix } from './lib/prefix.mjs';
7
7
  export * from './lib/key.mjs';
8
- export * from './lib/Paginator.mjs';
9
8
  export * from './lib/Separator.mjs';
10
9
  export type InquirerReadline = readline.ReadLine & {
11
10
  output: MuteStream;
@@ -23,6 +22,10 @@ export declare function useKeypress(userHandler: (event: KeypressEvent, rl: Inqu
23
22
  export declare function useRef<Value>(val: Value): {
24
23
  current: Value;
25
24
  };
25
+ export declare function usePagination(output: string, { active, pageSize, }: {
26
+ active: number;
27
+ pageSize?: number;
28
+ }): string;
26
29
  export type AsyncPromptConfig = {
27
30
  message: string | Promise<string> | (() => Promise<string>);
28
31
  validate?: (value: string) => boolean | string | Promise<string | boolean>;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "2.3.0",
3
+ "version": "3.0.0",
4
4
  "engines": {
5
5
  "node": ">=14.18.0"
6
6
  },
7
7
  "description": "Core Inquirer prompt API",
8
8
  "main": "./dist/cjs/index.js",
9
- "typings": "./dist/cjs/types/index.d.cts",
9
+ "typings": "./dist/cjs/types/index.d.ts",
10
10
  "files": [
11
11
  "dist/**/*"
12
12
  ],
@@ -57,7 +57,10 @@
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.0",
60
+ "@inquirer/type": "^1.1.1",
61
+ "@types/mute-stream": "^0.0.1",
62
+ "@types/node": "^20.4.2",
63
+ "@types/wrap-ansi": "^3.0.0",
61
64
  "ansi-escapes": "^4.3.2",
62
65
  "chalk": "^4.1.2",
63
66
  "cli-spinners": "^2.8.0",
@@ -70,7 +73,7 @@
70
73
  "wrap-ansi": "^6.0.1"
71
74
  },
72
75
  "devDependencies": {
73
- "@inquirer/testing": "^2.1.0"
76
+ "@inquirer/testing": "^2.1.1"
74
77
  },
75
78
  "scripts": {
76
79
  "tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
@@ -89,10 +92,10 @@
89
92
  "default": "./dist/esm/index.mjs"
90
93
  },
91
94
  "require": {
92
- "types": "./dist/cjs/types/index.d.cts",
95
+ "types": "./dist/cjs/types/index.d.ts",
93
96
  "default": "./dist/cjs/index.js"
94
97
  }
95
98
  }
96
99
  },
97
- "gitHead": "4c139f50414bd538eae1d48e61fd2586581d3e70"
100
+ "gitHead": "18144628b03b62c346cb7d9d55a2c526b1ed5d8a"
98
101
  }
@@ -1,53 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Paginator = void 0;
7
- const chalk_1 = __importDefault(require("chalk"));
8
- const cli_width_1 = __importDefault(require("cli-width"));
9
- const utils_mjs_1 = require('./utils.js');
10
- /**
11
- * The paginator keeps track of a pointer index in a list and returns
12
- * a subset of the choices if the list is too long.
13
- */
14
- class Paginator {
15
- constructor() {
16
- Object.defineProperty(this, "pointer", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: 0
21
- });
22
- Object.defineProperty(this, "lastIndex", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: 0
27
- });
28
- }
29
- paginate(output, active, pageSize = 7) {
30
- const middleOfList = Math.floor(pageSize / 2);
31
- // TODO: I've remove the dependency on readline here. But we should refactor the
32
- // paginator to also rely on hook.
33
- const width = (0, cli_width_1.default)({ defaultWidth: 80, output: process.stdout });
34
- const lines = (0, utils_mjs_1.breakLines)(output, width).split('\n');
35
- // Make sure there's enough lines to paginate
36
- if (lines.length <= pageSize) {
37
- return output;
38
- }
39
- // Move the pointer only when the user go down and limit it to the middle of the list
40
- if (this.pointer < middleOfList &&
41
- this.lastIndex < active &&
42
- active - this.lastIndex < pageSize) {
43
- this.pointer = Math.min(middleOfList, this.pointer + active - this.lastIndex);
44
- }
45
- this.lastIndex = active;
46
- // Duplicate the lines so it give an infinite list look
47
- const infinite = [lines, lines, lines].flat();
48
- const topIndex = Math.max(0, active + lines.length - this.pointer);
49
- const section = infinite.splice(topIndex, pageSize).join('\n');
50
- return section + '\n' + chalk_1.default.dim('(Move up and down to reveal more choices)');
51
- }
52
- }
53
- exports.Paginator = Paginator;
@@ -1,9 +0,0 @@
1
- /**
2
- * The paginator keeps track of a pointer index in a list and returns
3
- * a subset of the choices if the list is too long.
4
- */
5
- export declare class Paginator {
6
- pointer: number;
7
- lastIndex: number;
8
- paginate(output: string, active: number, pageSize?: number): string;
9
- }
@@ -1,34 +0,0 @@
1
- import chalk from 'chalk';
2
- import cliWidth from 'cli-width';
3
- import { breakLines } from './utils.mjs';
4
- /**
5
- * The paginator keeps track of a pointer index in a list and returns
6
- * a subset of the choices if the list is too long.
7
- */
8
- export class Paginator {
9
- pointer = 0;
10
- lastIndex = 0;
11
- paginate(output, active, pageSize = 7) {
12
- const middleOfList = Math.floor(pageSize / 2);
13
- // TODO: I've remove the dependency on readline here. But we should refactor the
14
- // paginator to also rely on hook.
15
- const width = cliWidth({ defaultWidth: 80, output: process.stdout });
16
- const lines = breakLines(output, width).split('\n');
17
- // Make sure there's enough lines to paginate
18
- if (lines.length <= pageSize) {
19
- return output;
20
- }
21
- // Move the pointer only when the user go down and limit it to the middle of the list
22
- if (this.pointer < middleOfList &&
23
- this.lastIndex < active &&
24
- active - this.lastIndex < pageSize) {
25
- this.pointer = Math.min(middleOfList, this.pointer + active - this.lastIndex);
26
- }
27
- this.lastIndex = active;
28
- // Duplicate the lines so it give an infinite list look
29
- const infinite = [lines, lines, lines].flat();
30
- const topIndex = Math.max(0, active + lines.length - this.pointer);
31
- const section = infinite.splice(topIndex, pageSize).join('\n');
32
- return section + '\n' + chalk.dim('(Move up and down to reveal more choices)');
33
- }
34
- }
@@ -1,9 +0,0 @@
1
- /**
2
- * The paginator keeps track of a pointer index in a list and returns
3
- * a subset of the choices if the list is too long.
4
- */
5
- export declare class Paginator {
6
- pointer: number;
7
- lastIndex: number;
8
- paginate(output: string, active: number, pageSize?: number): string;
9
- }
File without changes
File without changes