single-line-log
Advanced tools
Comparing version 0.3.0 to 0.3.1
33
index.js
var MOVE_LEFT = new Buffer('1b5b3130303044', 'hex').toString(); | ||
var MOVE_UP = new Buffer('1b5b3141', 'hex').toString(); | ||
var CLEAR_LINE = new Buffer('1b5b304b', 'hex').toString(); | ||
@@ -19,9 +20,18 @@ var write = process.stdout.write; | ||
var prevLineCount = 0; | ||
var log = function() { | ||
var prev = str || ''; | ||
var newLines = (str || '').split('\n').length-1; | ||
str = Array(newLines+1).join(MOVE_UP); | ||
str += MOVE_LEFT+Array.prototype.join.call(arguments, ' '); | ||
while (str.length < prev.length) str += ' '; | ||
str = ''; | ||
var nextStr = Array.prototype.join.call(arguments, ' '); | ||
// Clear screen | ||
for (var i=0; i<prevLineCount; i++) { | ||
str += MOVE_LEFT + CLEAR_LINE + (i < prevLineCount-1 ? MOVE_UP : ''); | ||
} | ||
// Actual log output | ||
str += nextStr; | ||
process.stdout.write(str); | ||
// How many lines to remove on next clear screen | ||
prevLineCount = nextStr.split('\n').length; | ||
}; | ||
@@ -36,6 +46,11 @@ | ||
var count=0; | ||
var i=0; | ||
setInterval(function() { | ||
if (count === 10000) return; | ||
log('#'+(count++)); | ||
}, 50); | ||
i++; | ||
var s = 'line 1 - '+Math.random(); | ||
if (i < 10) s += ' - '+Math.random(); | ||
s += '\nline 2 - '+Math.random(); | ||
if (i<20) s += '\nline 3 - '+Math.random()+'\nline 4 - '+Math.random(); | ||
log(s); | ||
}, 200); |
@@ -17,5 +17,5 @@ { | ||
], | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"repository": "git://github.com/freeall/single-line-log.git", | ||
"author": "Tobias Baunbæk <freeall@gmail.com>" | ||
} |
# single-line-log | ||
Small Node.js module that keeps writing to the same line in the console. Very useful when you write progress bars, or a status message during longer operations. | ||
Small Node.js module that keeps writing to the same line in the console. Very useful when you write progress bars, or a status message during longer operations. Supports multilines. | ||
@@ -24,4 +24,4 @@ | ||
// Keep writing to the same line in the console | ||
log('[' + percentage + '%]', read, 'bytes read'); | ||
// Keep writing to the same two lines in the console | ||
log('Writing to super large file\n[' + percentage + '%]', read, 'bytes read'); | ||
}); | ||
@@ -38,3 +38,3 @@ ``` | ||
log('Line 1'); | ||
log.clear() | ||
log.clear(); | ||
log('Line 2'); | ||
@@ -41,0 +41,0 @@ ``` |
2555
43