2022-01-21 08:28:41 +00:00
|
|
|
'use strict';
|
2022-06-08 10:36:39 +00:00
|
|
|
const util = require('./readline');
|
|
|
|
const cliWidth = require('cli-width');
|
|
|
|
const wrapAnsi = require('wrap-ansi');
|
|
|
|
const stripAnsi = require('strip-ansi');
|
|
|
|
const stringWidth = require('string-width');
|
|
|
|
const ora = require('ora');
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
function height(content) {
|
|
|
|
return content.split('\n').length;
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
/** @param {string} content */
|
2022-01-21 08:28:41 +00:00
|
|
|
function lastLine(content) {
|
2022-06-08 10:36:39 +00:00
|
|
|
return content.split('\n').pop();
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ScreenManager {
|
|
|
|
constructor(rl) {
|
|
|
|
// These variables are keeping information to allow correct prompt re-rendering
|
|
|
|
this.height = 0;
|
|
|
|
this.extraLinesUnderPrompt = 0;
|
|
|
|
|
|
|
|
this.rl = rl;
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
renderWithSpinner(content, bottomContent) {
|
|
|
|
if (this.spinnerId) {
|
|
|
|
clearInterval(this.spinnerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
let spinner;
|
|
|
|
let contentFunc;
|
|
|
|
let bottomContentFunc;
|
|
|
|
|
|
|
|
if (bottomContent) {
|
|
|
|
spinner = ora(bottomContent);
|
|
|
|
contentFunc = () => content;
|
|
|
|
bottomContentFunc = () => spinner.frame();
|
|
|
|
} else {
|
|
|
|
spinner = ora(content);
|
|
|
|
contentFunc = () => spinner.frame();
|
|
|
|
bottomContentFunc = () => '';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.spinnerId = setInterval(
|
|
|
|
() => this.render(contentFunc(), bottomContentFunc(), true),
|
|
|
|
spinner.interval
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render(content, bottomContent, spinning = false) {
|
|
|
|
if (this.spinnerId && !spinning) {
|
|
|
|
clearInterval(this.spinnerId);
|
|
|
|
}
|
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
this.rl.output.unmute();
|
|
|
|
this.clean(this.extraLinesUnderPrompt);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write message to screen and setPrompt to control backspace
|
|
|
|
*/
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
const promptLine = lastLine(content);
|
|
|
|
const rawPromptLine = stripAnsi(promptLine);
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
// Remove the rl.line from our prompt. We can't rely on the content of
|
|
|
|
// rl.line (mainly because of the password prompt), so just rely on it's
|
|
|
|
// length.
|
2022-06-08 10:36:39 +00:00
|
|
|
let prompt = rawPromptLine;
|
2022-01-21 08:28:41 +00:00
|
|
|
if (this.rl.line.length) {
|
|
|
|
prompt = prompt.slice(0, -this.rl.line.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rl.setPrompt(prompt);
|
|
|
|
|
|
|
|
// SetPrompt will change cursor position, now we can get correct value
|
2022-06-08 10:36:39 +00:00
|
|
|
const cursorPos = this.rl._getCursorPos();
|
|
|
|
const width = this.normalizedCliWidth();
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
content = this.forceLineReturn(content, width);
|
|
|
|
if (bottomContent) {
|
|
|
|
bottomContent = this.forceLineReturn(bottomContent, width);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually insert an extra line if we're at the end of the line.
|
|
|
|
// This prevent the cursor from appearing at the beginning of the
|
|
|
|
// current line.
|
|
|
|
if (rawPromptLine.length % width === 0) {
|
|
|
|
content += '\n';
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
const fullContent = content + (bottomContent ? '\n' + bottomContent : '');
|
2022-01-21 08:28:41 +00:00
|
|
|
this.rl.output.write(fullContent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re-adjust the cursor at the correct position.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// We need to consider parts of the prompt under the cursor as part of the bottom
|
|
|
|
// content in order to correctly cleanup and re-render.
|
2022-06-08 10:36:39 +00:00
|
|
|
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - cursorPos.rows;
|
|
|
|
const bottomContentHeight =
|
2022-01-21 08:28:41 +00:00
|
|
|
promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
|
|
|
|
if (bottomContentHeight > 0) {
|
|
|
|
util.up(this.rl, bottomContentHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset cursor at the beginning of the line
|
|
|
|
util.left(this.rl, stringWidth(lastLine(fullContent)));
|
|
|
|
|
|
|
|
// Adjust cursor on the right
|
|
|
|
if (cursorPos.cols > 0) {
|
|
|
|
util.right(this.rl, cursorPos.cols);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up state for next re-rendering
|
|
|
|
*/
|
|
|
|
this.extraLinesUnderPrompt = bottomContentHeight;
|
|
|
|
this.height = height(fullContent);
|
|
|
|
|
|
|
|
this.rl.output.mute();
|
|
|
|
}
|
|
|
|
|
|
|
|
clean(extraLines) {
|
|
|
|
if (extraLines > 0) {
|
|
|
|
util.down(this.rl, extraLines);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.clearLine(this.rl, this.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
done() {
|
|
|
|
this.rl.setPrompt('');
|
|
|
|
this.rl.output.unmute();
|
|
|
|
this.rl.output.write('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseCursor() {
|
|
|
|
if (this.extraLinesUnderPrompt > 0) {
|
|
|
|
util.down(this.rl, this.extraLinesUnderPrompt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
normalizedCliWidth() {
|
2022-06-08 10:36:39 +00:00
|
|
|
const width = cliWidth({
|
2022-01-21 08:28:41 +00:00
|
|
|
defaultWidth: 80,
|
2022-06-08 10:36:39 +00:00
|
|
|
output: this.rl.output,
|
2022-01-21 08:28:41 +00:00
|
|
|
});
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
/**
|
|
|
|
* @param {string[]} lines
|
|
|
|
*/
|
|
|
|
breakLines(lines, width = this.normalizedCliWidth()) {
|
2022-01-21 08:28:41 +00:00
|
|
|
// Break lines who're longer than the cli width so we can normalize the natural line
|
|
|
|
// returns behavior across terminals.
|
2022-06-08 10:36:39 +00:00
|
|
|
// re: trim: false; by default, `wrap-ansi` trims whitespace, which
|
|
|
|
// is not what we want.
|
|
|
|
// re: hard: true; by default', `wrap-ansi` does soft wrapping
|
|
|
|
return lines.map((line) =>
|
|
|
|
wrapAnsi(line, width, { trim: false, hard: true }).split('\n')
|
|
|
|
);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
/**
|
|
|
|
* @param {string} content
|
|
|
|
*/
|
|
|
|
forceLineReturn(content, width = this.normalizedCliWidth()) {
|
|
|
|
return this.breakLines(content.split('\n'), width).flat().join('\n');
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScreenManager;
|