diff options
Diffstat (limited to 'node_modules/single-line-log')
-rw-r--r-- | node_modules/single-line-log/.npmignore | 1 | ||||
-rw-r--r-- | node_modules/single-line-log/LICENSE | 21 | ||||
-rw-r--r-- | node_modules/single-line-log/README.md | 56 | ||||
-rw-r--r-- | node_modules/single-line-log/index.js | 51 | ||||
-rw-r--r-- | node_modules/single-line-log/package.json | 27 | ||||
-rw-r--r-- | node_modules/single-line-log/test.js | 21 |
6 files changed, 177 insertions, 0 deletions
diff --git a/node_modules/single-line-log/.npmignore b/node_modules/single-line-log/.npmignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/node_modules/single-line-log/.npmignore @@ -0,0 +1 @@ +node_modules
\ No newline at end of file diff --git a/node_modules/single-line-log/LICENSE b/node_modules/single-line-log/LICENSE new file mode 100644 index 0000000..57dffb7 --- /dev/null +++ b/node_modules/single-line-log/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Tobias Baunbæk <freeall@gmail.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/node_modules/single-line-log/README.md b/node_modules/single-line-log/README.md new file mode 100644 index 0000000..f142e30 --- /dev/null +++ b/node_modules/single-line-log/README.md @@ -0,0 +1,56 @@ +# single-line-log + +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. + + +## Installation + + npm install single-line-log + + +## Usage + +``` js +var log = require('single-line-log').stdout; +// or pass any stream: +// var log = require('single-line-log')(process.stdout); + +var read = 0; +var size = fs.statSync('super-large-file').size; + +var rs = fs.createReadStream('super-large-file'); +rs.on('data', function(data) { + read += data.length; + var percentage = Math.floor(100*read/size); + + // Keep writing to the same two lines in the console + log('Writing to super large file\n[' + percentage + '%]', read, 'bytes read'); +}); +``` + +## .clear() + +Clears the log (i.e., writes a newline). + +``` js +var log = require('single-line-log').stdout; + +log('Line 1'); +log.clear(); +log('Line 2'); +``` + + +## .stdout + +Outputs to `process.stdout`. + + +## .stderr + +Outputs to `process.stderr`. + + +## License + +MIT
\ No newline at end of file diff --git a/node_modules/single-line-log/index.js b/node_modules/single-line-log/index.js new file mode 100644 index 0000000..c1201be --- /dev/null +++ b/node_modules/single-line-log/index.js @@ -0,0 +1,51 @@ +var MOVE_LEFT = new Buffer('1b5b3130303044', 'hex').toString(); +var MOVE_UP = new Buffer('1b5b3141', 'hex').toString(); +var CLEAR_LINE = new Buffer('1b5b304b', 'hex').toString(); +var stringWidth = require('string-width'); + +module.exports = function(stream) { + var write = stream.write; + var str; + + stream.write = function(data) { + if (str && data !== str) str = null; + return write.apply(this, arguments); + }; + + if (stream === process.stderr || stream === process.stdout) { + process.on('exit', function() { + if (str !== null) stream.write(''); + }); + } + + 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 : ''); + } + + // Actual log output + str += nextStr; + stream.write(str); + + // How many lines to remove on next clear screen + var prevLines = nextStr.split('\n'); + prevLineCount = 0; + for (var i=0; i < prevLines.length; i++) { + prevLineCount += Math.ceil(stringWidth(prevLines[i]) / stream.columns) || 1; + } + }; + + log.clear = function() { + stream.write(''); + }; + + return log; +}; + +module.exports.stdout = module.exports(process.stdout); +module.exports.stderr = module.exports(process.stderr); diff --git a/node_modules/single-line-log/package.json b/node_modules/single-line-log/package.json new file mode 100644 index 0000000..077faab --- /dev/null +++ b/node_modules/single-line-log/package.json @@ -0,0 +1,27 @@ +{ + "name": "single-line-log", + "description": "Keep writing to the same line in the terminal. Very useful when you write progress bars, or a status message during longer operations", + "keywords": [ + "single", + "line", + "log", + "output", + "overwrite", + "collapse", + "stdout", + "terminal", + "tty", + "cli", + "shell" + ], + "version": "1.1.2", + "repository": "git://github.com/freeall/single-line-log.git", + "license": "MIT", + "author": "Tobias Baunbæk <freeall@gmail.com>", + "dependencies": { + "string-width": "^1.0.1" + }, + "scripts": { + "test": "node test.js" + } +} diff --git a/node_modules/single-line-log/test.js b/node_modules/single-line-log/test.js new file mode 100644 index 0000000..c8a2e79 --- /dev/null +++ b/node_modules/single-line-log/test.js @@ -0,0 +1,21 @@ +var log = require('./index').stdout; + +var i = 0; +setInterval(function() { + i++; + + var s = 'line 1 - ' + Math.random(); + + if (i < 10) s += ' - ' + Math.random(); + + if (i < 40) s += '\nline 2 - ' + Math.random(); + if (i < 30) s += '\nline 3 - ' + Math.random(); + if (i < 20) s += '\nline 4 - ' + Math.random(); + + log(s); + + if (i === 50) { + log.clear(); + process.exit(0); + } +}, 200); |