Skip to content

Commit 2723670

Browse files
committed
Allow clearing prompts once they're answered
Fix #746
1 parent 92932d2 commit 2723670

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

packages/confirm/demo.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ import confirm from './src/index.js';
1515
default: false,
1616
})
1717
);
18+
19+
console.log('This next prompt will be cleared on exit');
20+
console.log(
21+
'Cleared prompt answer:',
22+
await confirm({ message: 'Confirm?' }, { clearPromptOnDone: true })
23+
);
1824
})();

packages/core/src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ export { usePrefix } from './lib/prefix.js';
77
export * from './lib/key.js';
88
export * from './lib/Paginator.js';
99

10-
type StdioOptions = {
10+
type Context = {
1111
input?: NodeJS.ReadableStream;
1212
output?: NodeJS.WritableStream;
13+
clearPromptOnDone?: boolean;
1314
};
1415

1516
export type InquirerReadline = readline.ReadLine & {
@@ -107,7 +108,7 @@ export type ResolvedPromptConfig = {
107108

108109
export type Prompt<Value, Config> = (
109110
options: Config,
110-
stdio?: StdioOptions
111+
context?: Context
111112
) => Promise<Value>;
112113

113114
export function createPrompt<Value, Config extends AsyncPromptConfig>(
@@ -116,13 +117,13 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(
116117
done: (value: Value) => void
117118
) => string | [string, string | undefined]
118119
) {
119-
const prompt: Prompt<Value, Config> = async (options, stdio) => {
120+
const prompt: Prompt<Value, Config> = async (options, context) => {
120121
// Default `input` to stdin
121-
const input = stdio?.input ?? process.stdin;
122+
const input = context?.input ?? process.stdin;
122123

123124
// Add mute capabilities to the output
124125
const output = new MuteStream();
125-
output.pipe(stdio?.output ?? process.stdout);
126+
output.pipe(context?.output ?? process.stdout);
126127

127128
const rl = readline.createInterface({
128129
terminal: true,
@@ -140,6 +141,11 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(
140141
while (len--) {
141142
cleanupHook(len);
142143
}
144+
if (context?.clearPromptOnDone) {
145+
screen.clean();
146+
} else {
147+
screen.clearContent();
148+
}
143149
screen.done();
144150

145151
// Reset hooks state

packages/core/src/lib/screen-manager.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export default class ScreenManager {
1919
}
2020

2121
render(content: string, bottomContent: string = '') {
22-
this.rl.output.unmute();
23-
this.clean(this.extraLinesUnderPrompt);
22+
this.clean();
2423

24+
this.rl.output.unmute();
2525
/**
2626
* Write message to screen and setPrompt to control backspace
2727
*/
@@ -81,18 +81,26 @@ export default class ScreenManager {
8181
this.rl.output.mute();
8282
}
8383

84-
clean(extraLines: number) {
85-
util.down(this.rl, extraLines);
84+
clean() {
85+
this.rl.output.unmute();
86+
util.down(this.rl, this.extraLinesUnderPrompt);
8687
util.clearLine(this.rl, this.height);
88+
89+
this.extraLinesUnderPrompt = 0;
90+
this.rl.output.mute();
8791
}
8892

89-
done() {
93+
clearContent() {
94+
this.rl.output.unmute();
9095
// Reset the cursor at the end of the previously displayed content
9196
util.down(this.rl, this.extraLinesUnderPrompt);
97+
this.rl.output.write('\n');
98+
this.rl.output.mute();
99+
}
92100

101+
done() {
93102
this.rl.setPrompt('');
94103
this.rl.output.unmute();
95-
this.rl.output.write('\n');
96104
this.rl.output.write(ansiEscapes.cursorShow);
97105
this.rl.output.end();
98106
this.rl.close();

0 commit comments

Comments
 (0)