Skip to content

Commit

Permalink
Feat (Core): Reduce amount of times we write to the stdout per cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed May 29, 2023
1 parent 23c2a4b commit a9a0039
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 67 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(
rl.removeListener('SIGINT', onExit);
};

// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hid it.
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
process.on('exit', onExit);
rl.on('SIGINT', onExit);

Expand Down
55 changes: 0 additions & 55 deletions packages/core/src/lib/readline.mts

This file was deleted.

35 changes: 24 additions & 11 deletions packages/core/src/lib/screen-manager.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import cliWidth from 'cli-width';
import stripAnsi from 'strip-ansi';
import stringWidth from 'string-width';
import ansiEscapes from 'ansi-escapes';
import * as util from './readline.mjs';
import { breakLines } from './utils.mjs';
import { InquirerReadline } from '../index.mjs';

Expand Down Expand Up @@ -53,8 +52,7 @@ export default class ScreenManager {
content += '\n';
}

const fullContent = content + (bottomContent ? '\n' + bottomContent : '');
this.rl.output.write(fullContent);
let output = content + (bottomContent ? '\n' + bottomContent : '');

/**
* Re-adjust the cursor at the correct position.
Expand All @@ -67,24 +65,33 @@ export default class ScreenManager {
promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);

// Return cursor to the input position (on top of the bottomContent)
util.up(this.rl, bottomContentHeight);
if (bottomContentHeight > 0) output += ansiEscapes.cursorUp(bottomContentHeight);

// Move cursor at the start of the line, then return to the initial left offset.
util.left(this.rl, stringWidth(lastLine(fullContent)));
util.right(this.rl, cursorPos.cols);
const backward = stringWidth(lastLine(output));
if (backward > 0) output += ansiEscapes.cursorBackward(backward);
if (cursorPos.cols > 0) output += ansiEscapes.cursorForward(cursorPos.cols);

/**
* Set up state for next re-rendering
*/
this.extraLinesUnderPrompt = bottomContentHeight;
this.height = height(fullContent);
this.height = height(output);

this.rl.output.write(output);
this.rl.output.mute();
}

clean() {
this.rl.output.unmute();
util.down(this.rl, this.extraLinesUnderPrompt);
util.clearLine(this.rl, this.height);
this.rl.output.write(
[
this.extraLinesUnderPrompt > 0
? ansiEscapes.cursorDown(this.extraLinesUnderPrompt)
: '',
ansiEscapes.eraseLines(this.height),
].join('')
);

this.extraLinesUnderPrompt = 0;
this.rl.output.mute();
Expand All @@ -93,8 +100,14 @@ export default class ScreenManager {
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.write(
[
this.extraLinesUnderPrompt > 0
? ansiEscapes.cursorDown(this.extraLinesUnderPrompt)
: '',
'\n',
].join('')
);
this.rl.output.mute();
}

Expand Down

0 comments on commit a9a0039

Please sign in to comment.