├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Tobias Baunbæk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # single-line-log 2 | 3 | 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. 4 | 5 | 6 | ## Installation 7 | 8 | npm install single-line-log 9 | 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var log = require('single-line-log').stdout; 15 | // or pass any stream: 16 | // var log = require('single-line-log')(process.stdout); 17 | 18 | var read = 0; 19 | var size = fs.statSync('super-large-file').size; 20 | 21 | var rs = fs.createReadStream('super-large-file'); 22 | rs.on('data', function(data) { 23 | read += data.length; 24 | var percentage = Math.floor(100*read/size); 25 | 26 | // Keep writing to the same two lines in the console 27 | log('Writing to super large file\n[' + percentage + '%]', read, 'bytes read'); 28 | }); 29 | ``` 30 | 31 | ## .clear() 32 | 33 | Clears the log (i.e., writes a newline). 34 | 35 | ``` js 36 | var log = require('single-line-log').stdout; 37 | 38 | log('Line 1'); 39 | log.clear(); 40 | log('Line 2'); 41 | ``` 42 | 43 | 44 | ## .stdout 45 | 46 | Outputs to `process.stdout`. 47 | 48 | 49 | ## .stderr 50 | 51 | Outputs to `process.stderr`. 52 | 53 | 54 | ## License 55 | 56 | MIT -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var MOVE_LEFT = new Buffer('1b5b3130303044', 'hex').toString(); 2 | var MOVE_UP = new Buffer('1b5b3141', 'hex').toString(); 3 | var CLEAR_LINE = new Buffer('1b5b304b', 'hex').toString(); 4 | var stringWidth = require('string-width'); 5 | 6 | module.exports = function(stream) { 7 | var write = stream.write; 8 | var str; 9 | 10 | stream.write = function(data) { 11 | if (str && data !== str) str = null; 12 | return write.apply(this, arguments); 13 | }; 14 | 15 | if (stream === process.stderr || stream === process.stdout) { 16 | process.on('exit', function() { 17 | if (str !== null) stream.write(''); 18 | }); 19 | } 20 | 21 | var prevLineCount = 0; 22 | var log = function() { 23 | str = ''; 24 | var nextStr = Array.prototype.join.call(arguments, ' '); 25 | 26 | // Clear screen 27 | for (var i=0; i", 21 | "dependencies": { 22 | "string-width": "^1.0.1" 23 | }, 24 | "scripts": { 25 | "test": "node test.js" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var log = require('./index').stdout; 2 | 3 | var i = 0; 4 | setInterval(function() { 5 | i++; 6 | 7 | var s = 'line 1 - ' + Math.random(); 8 | 9 | if (i < 10) s += ' - ' + Math.random(); 10 | 11 | if (i < 40) s += '\nline 2 - ' + Math.random(); 12 | if (i < 30) s += '\nline 3 - ' + Math.random(); 13 | if (i < 20) s += '\nline 4 - ' + Math.random(); 14 | 15 | log(s); 16 | 17 | if (i === 50) { 18 | log.clear(); 19 | process.exit(0); 20 | } 21 | }, 200); 22 | --------------------------------------------------------------------------------