@inquirer/core 0.0.23-alpha.0 → 0.0.26-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/dist/index.d.ts CHANGED
@@ -5,9 +5,10 @@ import MuteStream from 'mute-stream';
5
5
  export { usePrefix } from './lib/prefix.js';
6
6
  export * from './lib/key.js';
7
7
  export * from './lib/Paginator.js';
8
- declare type StdioOptions = {
8
+ declare type Context = {
9
9
  input?: NodeJS.ReadableStream;
10
10
  output?: NodeJS.WritableStream;
11
+ clearPromptOnDone?: boolean;
11
12
  };
12
13
  export declare type InquirerReadline = readline.ReadLine & {
13
14
  output: MuteStream;
@@ -31,4 +32,5 @@ export declare type ResolvedPromptConfig = {
31
32
  message: string;
32
33
  validate: (value: string) => boolean | string | Promise<string | boolean>;
33
34
  };
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>;
35
+ export declare type Prompt<Value, Config> = (options: Config, context?: Context) => Promise<Value>;
36
+ export declare function createPrompt<Value, Config extends AsyncPromptConfig>(view: (config: Config & ResolvedPromptConfig, done: (value: Value) => void) => string | [string, string | undefined]): Prompt<Value, Config>;
package/dist/index.js CHANGED
@@ -18,10 +18,12 @@ const cleanupHook = (index) => {
18
18
  };
19
19
  export function useState(defaultValue) {
20
20
  const _idx = index;
21
- const value = _idx in hooks ? hooks[_idx] : defaultValue;
22
21
  index++;
22
+ if (!(_idx in hooks)) {
23
+ hooks[_idx] = defaultValue;
24
+ }
23
25
  return [
24
- value,
26
+ hooks[_idx],
25
27
  (newValue) => {
26
28
  hooks[_idx] = newValue;
27
29
  // Trigger re-render
@@ -31,6 +33,7 @@ export function useState(defaultValue) {
31
33
  }
32
34
  export function useEffect(cb, depArray) {
33
35
  const _idx = index;
36
+ index++;
34
37
  const oldDeps = hooks[_idx];
35
38
  let hasChanged = true;
36
39
  if (oldDeps) {
@@ -41,7 +44,6 @@ export function useEffect(cb, depArray) {
41
44
  hooksCleanup[_idx] = cb();
42
45
  }
43
46
  hooks[_idx] = depArray;
44
- index++;
45
47
  }
46
48
  export function useKeypress(userHandler) {
47
49
  const rl = sessionRl;
@@ -62,12 +64,12 @@ export function useRef(val) {
62
64
  return useState({ current: val })[0];
63
65
  }
64
66
  export function createPrompt(view) {
65
- return async function (options, stdio) {
67
+ const prompt = async (options, context) => {
66
68
  // Default `input` to stdin
67
- const input = stdio?.input ?? process.stdin;
69
+ const input = context?.input ?? process.stdin;
68
70
  // Add mute capabilities to the output
69
71
  const output = new MuteStream();
70
- output.pipe(stdio?.output ?? process.stdout);
72
+ output.pipe(context?.output ?? process.stdout);
71
73
  const rl = readline.createInterface({
72
74
  terminal: true,
73
75
  input,
@@ -78,10 +80,16 @@ export function createPrompt(view) {
78
80
  const resolvedConfig = await getPromptConfig(options);
79
81
  return new Promise((resolve) => {
80
82
  const done = (value) => {
81
- let len = cleanupHook.length;
83
+ let len = hooksCleanup.length;
82
84
  while (len--) {
83
85
  cleanupHook(len);
84
86
  }
87
+ if (context?.clearPromptOnDone) {
88
+ screen.clean();
89
+ }
90
+ else {
91
+ screen.clearContent();
92
+ }
85
93
  screen.done();
86
94
  // Reset hooks state
87
95
  hooks = [];
@@ -90,6 +98,7 @@ export function createPrompt(view) {
90
98
  // Finally we resolve our promise
91
99
  resolve(value);
92
100
  };
101
+ index = 0;
93
102
  hooks = [];
94
103
  const workLoop = (config) => {
95
104
  sessionRl = rl;
@@ -102,4 +111,5 @@ export function createPrompt(view) {
102
111
  workLoop(resolvedConfig);
103
112
  });
104
113
  };
114
+ return prompt;
105
115
  }
@@ -5,6 +5,7 @@ export default class ScreenManager {
5
5
  private extraLinesUnderPrompt;
6
6
  constructor(rl: InquirerReadline);
7
7
  render(content: string, bottomContent?: string): void;
8
- clean(extraLines: number): void;
8
+ clean(): void;
9
+ clearContent(): void;
9
10
  done(): void;
10
11
  }
@@ -30,8 +30,8 @@ export default class ScreenManager {
30
30
  this.rl = rl;
31
31
  }
32
32
  render(content, bottomContent = '') {
33
+ this.clean();
33
34
  this.rl.output.unmute();
34
- this.clean(this.extraLinesUnderPrompt);
35
35
  /**
36
36
  * Write message to screen and setPrompt to control backspace
37
37
  */
@@ -77,17 +77,25 @@ export default class ScreenManager {
77
77
  this.height = height(fullContent);
78
78
  this.rl.output.mute();
79
79
  }
80
- clean(extraLines) {
81
- util.down(this.rl, extraLines);
80
+ clean() {
81
+ this.rl.output.unmute();
82
+ util.down(this.rl, this.extraLinesUnderPrompt);
82
83
  util.clearLine(this.rl, this.height);
84
+ this.extraLinesUnderPrompt = 0;
85
+ this.rl.output.mute();
83
86
  }
84
- done() {
87
+ clearContent() {
88
+ this.rl.output.unmute();
85
89
  // Reset the cursor at the end of the previously displayed content
86
90
  util.down(this.rl, this.extraLinesUnderPrompt);
91
+ this.rl.output.write('\n');
92
+ this.rl.output.mute();
93
+ }
94
+ done() {
87
95
  this.rl.setPrompt('');
88
96
  this.rl.output.unmute();
89
- this.rl.output.write('\n');
90
97
  this.rl.output.write(ansiEscapes.cursorShow);
98
+ this.rl.output.end();
91
99
  this.rl.close();
92
100
  }
93
101
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
3
  "type": "module",
4
- "version": "0.0.23-alpha.0",
4
+ "version": "0.0.26-alpha.0",
5
5
  "description": "Core Inquirer prompt API",
6
6
  "main": "dist/index.js",
7
7
  "typings": "dist/index.d.ts",
@@ -29,11 +29,14 @@
29
29
  "strip-ansi": "^7.0.1",
30
30
  "wrap-ansi": "^8.0.1"
31
31
  },
32
+ "devDependencies": {
33
+ "@inquirer/testing": "^0.0.2-alpha.0"
34
+ },
32
35
  "scripts": {
33
36
  "tsc": "tsc"
34
37
  },
35
38
  "publishConfig": {
36
39
  "access": "public"
37
40
  },
38
- "gitHead": "a95b68efa6f407cefff63300e7e50da34c6b6693"
41
+ "gitHead": "3d7124020ee2a316a601f6006880018eec01f3a6"
39
42
  }