Skip to content

Commit

Permalink
Add break lines option and do some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcarpanelli committed May 21, 2019
1 parent a3c9b5a commit f94ba0d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const chalk = require('chalk');
const cliCursor = require('cli-cursor');
const dots = require('./spinner');

const { purgeSpinnerOptions, purgeSpinnersOptions, colorOptions, breakText } = require('./utils');
const { preventBreakLines, writeStream, cleanStream } = require('./utils');
const { purgeSpinnerOptions, purgeSpinnersOptions, colorOptions, breakText, getLinesLength } = require('./utils');
const { preventLineBreaks, writeStream, cleanStream } = require('./utils');

class Spinners {
constructor(options = {}) {
Expand All @@ -17,20 +17,21 @@ class Spinners {
successColor: 'green',
failColor: 'red',
spinner: dots,
preventLineBreaks: true,
...options
};
this.spinners = {};
this.isCursorHidden = false;
this.currentInterval = null;
preventBreakLines();
if (this.options.preventLineBreaks) preventLineBreaks();
}

pickSpinner(name) {
return this.spinners[name];
}

add(name, options = {}) {
process.stdin.resume();
if (this.options.preventLineBreaks) process.stdin.resume();
if (typeof name !== 'string') throw Error('A spinner reference name must be specified');
if (!options.text) options.text = name;
const spinnerProperties = {
Expand Down Expand Up @@ -106,10 +107,10 @@ class Spinners {
setStream(frame = '') {
let line;
let stream = '';
const rawLines = [];
const linesLength = [];
Object.values(this.spinners).map(({ text, status, color, spinnerColor, successColor, failColor }) => {
text = breakText(text);
rawLines.push(...(text.split('\n').map(line => line.length + frame.length)));
linesLength.push(...getLinesLength(text));
if (status === 'spinning') {
line = `${chalk[spinnerColor](frame)} ${chalk[color](text)}`;
} else if (status === 'success') {
Expand All @@ -122,8 +123,8 @@ class Spinners {
stream += `${line}\n`;
});

cleanStream(rawLines);
writeStream(stream, rawLines);
cleanStream(linesLength);
writeStream(stream, linesLength);
}

checkIfActiveSpinners() {
Expand All @@ -135,7 +136,7 @@ class Spinners {
this.spinners = {};
cliCursor.show();
this.isCursorHidden = false;
process.stdin.pause();
if (this.options.preventLineBreaks) process.stdin.pause();
}
}

Expand Down
22 changes: 14 additions & 8 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ function purgeSpinnerOptions(options) {
}

function purgeSpinnersOptions(options) {
const { spinner } = options;
const colors = colorOptions(options)
const { spinner, preventLineBreaks } = options;
const colors = colorOptions(options);
const lineBreaksOption = typeof preventLineBreaks === 'boolean' ? { preventLineBreaks } : {};

return isValidSpinner(spinner) ? { ...colors, spinner } : colors;
return isValidSpinner(spinner) ? { ...colors, ...lineBreaksOption, spinner } : colors;
}

function isValidSpinner(spinner = {}) {
Expand All @@ -44,10 +45,14 @@ function breakText(text) {
: text;
}

function preventBreakLines() {
function getLinesLength(text) {
return text.split('\n').map((line, index) => index === 0 ? line.length + 2 : line.length);
}

function preventLineBreaks() {
readline.emitKeypressEvents(process.stdin);
process.stdin.on('keypress', (string, key) => {
if(key.sequence === '\n' || key.sequence === '\r') {
process.stdin.on('keypress', (string, { sequence }) => {
if(sequence === '\n' || sequence === '\r') {
readline.moveCursor(process.stderr, 0, -1);
}
});
Expand All @@ -60,7 +65,7 @@ function writeStream(stream, rawLines) {

function cleanStream(rawLines) {
rawLines.forEach((lineLength, index) => {
readline.moveCursor(process.stderr, lineLength , index);
readline.moveCursor(process.stderr, lineLength, index);
readline.clearLine(process.stderr, 1);
readline.moveCursor(process.stderr, -lineLength, -index);
});
Expand All @@ -74,7 +79,8 @@ module.exports = {
purgeSpinnerOptions,
colorOptions,
breakText,
preventBreakLines,
preventLineBreaks,
getLinesLength,
writeStream,
cleanStream,
}

0 comments on commit f94ba0d

Please sign in to comment.