Skip to content

Commit

Permalink
Allow clearing prompts once they're answered
Browse files Browse the repository at this point in the history
Fix #746
  • Loading branch information
SBoudrias committed Aug 31, 2022
1 parent 92932d2 commit 2723670
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
6 changes: 6 additions & 0 deletions packages/confirm/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ import confirm from './src/index.js';
default: false,
})
);

console.log('This next prompt will be cleared on exit');
console.log(
'Cleared prompt answer:',
await confirm({ message: 'Confirm?' }, { clearPromptOnDone: true })
);
})();
16 changes: 11 additions & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ export { usePrefix } from './lib/prefix.js';
export * from './lib/key.js';
export * from './lib/Paginator.js';

type StdioOptions = {
type Context = {
input?: NodeJS.ReadableStream;
output?: NodeJS.WritableStream;
clearPromptOnDone?: boolean;
};

export type InquirerReadline = readline.ReadLine & {
Expand Down Expand Up @@ -107,7 +108,7 @@ export type ResolvedPromptConfig = {

export type Prompt<Value, Config> = (
options: Config,
stdio?: StdioOptions
context?: Context
) => Promise<Value>;

export function createPrompt<Value, Config extends AsyncPromptConfig>(
Expand All @@ -116,13 +117,13 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(
done: (value: Value) => void
) => string | [string, string | undefined]
) {
const prompt: Prompt<Value, Config> = async (options, stdio) => {
const prompt: Prompt<Value, Config> = async (options, context) => {
// Default `input` to stdin
const input = stdio?.input ?? process.stdin;
const input = context?.input ?? process.stdin;

// Add mute capabilities to the output
const output = new MuteStream();
output.pipe(stdio?.output ?? process.stdout);
output.pipe(context?.output ?? process.stdout);

const rl = readline.createInterface({
terminal: true,
Expand All @@ -140,6 +141,11 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(
while (len--) {
cleanupHook(len);
}
if (context?.clearPromptOnDone) {
screen.clean();
} else {
screen.clearContent();
}
screen.done();

// Reset hooks state
Expand Down
20 changes: 14 additions & 6 deletions packages/core/src/lib/screen-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default class ScreenManager {
}

render(content: string, bottomContent: string = '') {
this.rl.output.unmute();
this.clean(this.extraLinesUnderPrompt);
this.clean();

this.rl.output.unmute();
/**
* Write message to screen and setPrompt to control backspace
*/
Expand Down Expand Up @@ -81,18 +81,26 @@ export default class ScreenManager {
this.rl.output.mute();
}

clean(extraLines: number) {
util.down(this.rl, extraLines);
clean() {
this.rl.output.unmute();
util.down(this.rl, this.extraLinesUnderPrompt);
util.clearLine(this.rl, this.height);

this.extraLinesUnderPrompt = 0;
this.rl.output.mute();
}

done() {
clearContent() {
this.rl.output.unmute();
// Reset the cursor at the end of the previously displayed content
util.down(this.rl, this.extraLinesUnderPrompt);
this.rl.output.write('\n');
this.rl.output.mute();
}

done() {
this.rl.setPrompt('');
this.rl.output.unmute();
this.rl.output.write('\n');
this.rl.output.write(ansiEscapes.cursorShow);
this.rl.output.end();
this.rl.close();
Expand Down

0 comments on commit 2723670

Please sign in to comment.