Skip to content

Commit

Permalink
Fix stdout when lines are cleared (#70)
Browse files Browse the repository at this point in the history
Some ANSI code where not well processed like those that clear the entire line or move the cursor around.
Still need to support moving the cursor to far left as its conflicting with the coloredPrefix.
https://en.wikipedia.org/wiki/ANSI_escape_code
  • Loading branch information
aecz authored and gustavohenke committed Jul 10, 2017
1 parent d88989a commit 16260ff
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,31 +478,35 @@ function logError(prefix, prefixColor, text) {
logWithPrefix(prefix, prefixColor, text, chalk.red.bold);
}

var lastChar;

function logWithPrefix(prefix, prefixColor, text, color) {
var lastChar = text[text.length - 1];
if (config.raw) {
if (lastChar !== '\n') {
text += '\n';
}

if (config.raw) {
process.stdout.write(text);
return;
}

if (lastChar === '\n') {
// Remove extra newline from the end to prevent extra newlines in input
text = text.slice(0, text.length - 1);
}
text = text.replace(/\u2026/g,'...'); // Ellipsis

var lines = text.split('\n');
// Do not bgColor trailing space
var coloredPrefix = colorText(prefix.replace(/ $/, ''), prefixColor) + ' ';
var paddedLines = _.map(lines, function(line, i) {
var paddedLines = _.map(lines, function(line, index) {
var coloredLine = color ? colorText(line, color) : line;
return coloredPrefix + coloredLine;
if (index !== 0 && index !== (lines.length - 1)) {
coloredLine = coloredPrefix + coloredLine;
}
return coloredLine;
});

console.log(paddedLines.join('\n'));
if (!lastChar || lastChar == '\n' ){
process.stdout.write(coloredPrefix);
}

lastChar = text[text.length - 1];

process.stdout.write(paddedLines.join('\n'));
}

main();

0 comments on commit 16260ff

Please sign in to comment.