Skip to content

Commit

Permalink
Fixes #274: remove internal use of process.stdout (#275)
Browse files Browse the repository at this point in the history
* Fix(select, multiselect) duplicate output when stdout is redirected (fixes #274)

* Remove process.stdout from util functions internals
  • Loading branch information
AumyF committed Oct 6, 2020
1 parent a90d118 commit 9628ebe
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/elements/autocomplete.js
Expand Up @@ -47,7 +47,7 @@ class AutocompletePrompt extends Prompt {
this.scale = this.transform.scale;
this.render = this.render.bind(this);
this.complete = this.complete.bind(this);
this.clear = clear('');
this.clear = clear('', this.out.columns);
this.complete(this.render);
this.render();
}
Expand Down Expand Up @@ -216,7 +216,7 @@ class AutocompletePrompt extends Prompt {
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(clear(this.outputText));
else this.out.write(clear(this.outputText, this.out.columns));
super.render();

let { startIndex, endIndex } = entriesToDisplay(this.select, this.choices.length, this.limit);
Expand Down
4 changes: 2 additions & 2 deletions lib/elements/autocompleteMultiselect.js
Expand Up @@ -21,7 +21,7 @@ class AutocompleteMultiselectPrompt extends MultiselectPrompt {
opts.overrideRender = true;
super(opts);
this.inputValue = '';
this.clear = clear('');
this.clear = clear('', this.out.columns);
this.filteredOptions = this.value;
this.render();
}
Expand Down Expand Up @@ -187,7 +187,7 @@ Filtered results for: ${this.inputValue ? this.inputValue : color.gray('Enter so
prompt += this.renderOptions(this.filteredOptions);

this.out.write(this.clear + prompt);
this.clear = clear(prompt);
this.clear = clear(prompt, this.out.columns);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/elements/confirm.js
Expand Up @@ -67,7 +67,7 @@ class ConfirmPrompt extends Prompt {
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(clear(this.outputText));
else this.out.write(clear(this.outputText, this.out.columns));
super.render();

this.outputText = [
Expand Down
4 changes: 2 additions & 2 deletions lib/elements/date.js
Expand Up @@ -50,7 +50,7 @@ class DatePrompt extends Prompt {
this.errorMsg = opts.error || 'Please Enter A Valid Value';
this.validator = opts.validate || (() => true);
this.mask = opts.mask || 'YYYY-MM-DD HH:mm:ss';
this.clear = clear('');
this.clear = clear('', this.out.columns);
this.render();
}

Expand Down Expand Up @@ -180,7 +180,7 @@ class DatePrompt extends Prompt {
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(clear(this.outputText));
else this.out.write(clear(this.outputText, this.out.columns));
super.render();

// Print prompt
Expand Down
4 changes: 2 additions & 2 deletions lib/elements/multiselect.js
Expand Up @@ -42,7 +42,7 @@ class MultiselectPrompt extends Prompt {
disabled: ch && ch.disabled
};
});
this.clear = clear('');
this.clear = clear('', this.out.columns);
if (!opts.overrideRender) {
this.render();
}
Expand Down Expand Up @@ -260,7 +260,7 @@ class MultiselectPrompt extends Prompt {
prompt += this.renderOptions(this.value);

this.out.write(this.clear + prompt);
this.clear = clear(prompt);
this.clear = clear(prompt, this.out.columns);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/elements/number.js
Expand Up @@ -181,8 +181,8 @@ class NumberPrompt extends Prompt {
if (this.closed) return;
if (!this.firstRender) {
if (this.outputError)
this.out.write(cursor.down(lines(this.outputError) - 1) + clear(this.outputError));
this.out.write(clear(this.outputText));
this.out.write(cursor.down(lines(this.outputError, this.out.columns) - 1) + clear(this.outputError, this.out.columns));
this.out.write(clear(this.outputText, this.out.columns));
}
super.render();
this.outputError = '';
Expand Down
4 changes: 2 additions & 2 deletions lib/elements/select.js
Expand Up @@ -36,7 +36,7 @@ class SelectPrompt extends Prompt {
});
this.optionsPerPage = opts.optionsPerPage || 10;
this.value = (this.choices[this.cursor] || {}).value;
this.clear = clear('');
this.clear = clear('', this.out.columns);
this.render();
}

Expand Down Expand Up @@ -116,7 +116,7 @@ class SelectPrompt extends Prompt {
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(clear(this.outputText));
else this.out.write(clear(this.outputText, this.out.columns));
super.render();

let { startIndex, endIndex } = entriesToDisplay(this.cursor, this.choices.length, this.optionsPerPage);
Expand Down
6 changes: 3 additions & 3 deletions lib/elements/text.js
Expand Up @@ -25,7 +25,7 @@ class TextPrompt extends Prompt {
this.value = ``;
this.errorMsg = opts.error || `Please Enter A Valid Value`;
this.cursor = Number(!!this.initial);
this.clear = clear(``);
this.clear = clear(``, this.out.columns);
this.render();
}

Expand Down Expand Up @@ -156,8 +156,8 @@ class TextPrompt extends Prompt {
if (this.closed) return;
if (!this.firstRender) {
if (this.outputError)
this.out.write(cursor.down(lines(this.outputError) - 1) + clear(this.outputError));
this.out.write(clear(this.outputText));
this.out.write(cursor.down(lines(this.outputError, this.out.columns) - 1) + clear(this.outputError, this.out.columns));
this.out.write(clear(this.outputText, this.out.columns));
}
super.render();
this.outputError = '';
Expand Down
2 changes: 1 addition & 1 deletion lib/elements/toggle.js
Expand Up @@ -95,7 +95,7 @@ class TogglePrompt extends Prompt {
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(clear(this.outputText));
else this.out.write(clear(this.outputText, this.out.columns));
super.render();

this.outputText = [
Expand Down
6 changes: 5 additions & 1 deletion lib/util/clear.js
Expand Up @@ -5,7 +5,11 @@ const { erase, cursor } = require('sisteransi');

const width = str => [...strip(str)].length;

module.exports = function(prompt, perLine = process.stdout.columns) {
/**
* @param {string} prompt
* @param {number} perLine
*/
module.exports = function(prompt, perLine) {
if (!perLine) return erase.line + cursor.to(0);

let rows = 0;
Expand Down
6 changes: 5 additions & 1 deletion lib/util/lines.js
Expand Up @@ -2,7 +2,11 @@

const strip = require('./strip');

module.exports = function (msg, perLine = process.stdout.columns) {
/**
* @param {string} msg
* @param {number} perLine
*/
module.exports = function (msg, perLine) {
let lines = String(strip(msg) || '').split(/\r?\n/);

if (!perLine) return lines.length;
Expand Down
6 changes: 3 additions & 3 deletions lib/util/wrap.js
Expand Up @@ -2,16 +2,16 @@

/**
* @param {string} msg The message to wrap
* @param {object} [opts]
* @param {object} opts
* @param {number|string} [opts.margin] Left margin
* @param {number} [opts.width] Maximum characters per line including the margin
* @param {number} opts.width Maximum characters per line including the margin
*/
module.exports = (msg, opts = {}) => {
const tab = Number.isSafeInteger(parseInt(opts.margin))
? new Array(parseInt(opts.margin)).fill(' ').join('')
: (opts.margin || '');

const width = opts.width || process.stdout.columns;
const width = opts.width;

return (msg || '').split(/\r?\n/g)
.map(line => line
Expand Down

0 comments on commit 9628ebe

Please sign in to comment.