[Pkg-javascript-commits] [single-line-log] 01/09: Import Upstream version 1.1.2
Ross Gammon
ross-guest at moszumanska.debian.org
Tue Jan 31 18:50:51 UTC 2017
This is an automated email from the git hooks/post-receive script.
ross-guest pushed a commit to branch master
in repository single-line-log.
commit aefc1fd510490106bf545ae328ce98ae29a8217a
Author: Nupur Malpani <malpaninupur325 at gmail.com>
Date: Thu Jan 5 11:26:51 2017 +0000
Import Upstream version 1.1.2
---
.gitignore | 1 +
LICENSE | 21 +++++++++++++++++++++
README.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 27 +++++++++++++++++++++++++++
test.js | 21 +++++++++++++++++++++
6 files changed, 177 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b512c09
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..57dffb7
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) Tobias Baunbæk <freeall at 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/README.md b/README.md
new file mode 100644
index 0000000..f142e30
--- /dev/null
+++ b/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/index.js b/index.js
new file mode 100644
index 0000000..c1201be
--- /dev/null
+++ b/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/package.json b/package.json
new file mode 100644
index 0000000..077faab
--- /dev/null
+++ b/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 at gmail.com>",
+ "dependencies": {
+ "string-width": "^1.0.1"
+ },
+ "scripts": {
+ "test": "node test.js"
+ }
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..c8a2e79
--- /dev/null
+++ b/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);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/single-line-log.git
More information about the Pkg-javascript-commits
mailing list