single-line-log
Advanced tools
Comparing version 0.3.1 to 0.4.0
66
index.js
@@ -5,42 +5,50 @@ var MOVE_LEFT = new Buffer('1b5b3130303044', 'hex').toString(); | ||
var write = process.stdout.write; | ||
var str; | ||
module.exports = function(stream) { | ||
var write = stream.write; | ||
var str; | ||
process.stdout.write = function(data) { | ||
if (str && data !== str) { | ||
str = null; | ||
write.call(this, '\n'); | ||
} | ||
write.apply(this, arguments); | ||
}; | ||
stream.write = function(data) { | ||
if (str && data !== str) { | ||
str = null; | ||
write.call(this, '\n'); | ||
} | ||
write.apply(this, arguments); | ||
}; | ||
process.on('exit', function() { | ||
if (str !== null) process.stdout.write(''); | ||
}); | ||
process.on('exit', function() { | ||
if (str !== null) stream.write(''); | ||
}); | ||
var prevLineCount = 0; | ||
var log = function() { | ||
str = ''; | ||
var nextStr = Array.prototype.join.call(arguments, ' '); | ||
var prevLineCount = 0; | ||
var log = function() { | ||
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 : ''); | ||
} | ||
// 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); | ||
// Actual log output | ||
str += nextStr; | ||
stream.write(str); | ||
// How many lines to remove on next clear screen | ||
prevLineCount = nextStr.split('\n').length; | ||
}; | ||
// How many lines to remove on next clear screen | ||
prevLineCount = nextStr.split('\n').length; | ||
}; | ||
module.exports = log; | ||
module.exports.clear = function() { | ||
process.stdout.write(''); | ||
log.clear = function() { | ||
stream.write(''); | ||
}; | ||
return log; | ||
}; | ||
module.exports.stdout = module.exports(process.stdout); | ||
module.exports.stderr = module.exports(process.stderr); | ||
if (require.main !== module) return; | ||
var log = module.exports.stdout; | ||
var i=0; | ||
@@ -47,0 +55,0 @@ setInterval(function() { |
@@ -17,5 +17,5 @@ { | ||
], | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"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. Supports multilines. | ||
Node.js module that keeps writing to the same line in the console (or a stream). Very useful when you write progress bars, or a status message during longer operations. Supports multilines. | ||
@@ -14,3 +14,3 @@ | ||
``` js | ||
var log = require('single-line-log'); | ||
var log = require('single-line-log').stdout; | ||
@@ -35,3 +35,3 @@ var read = 0; | ||
``` js | ||
var log = require('single-line-log'); | ||
var log = require('single-line-log').stdout; | ||
@@ -43,4 +43,15 @@ log('Line 1'); | ||
## .stdout | ||
Outputs to `process.stdout`. | ||
## .stderr | ||
Outputs to `process.stderr`. | ||
## License | ||
MIT |
2820
48
54