[Pkg-javascript-commits] [node-progress] 01/04: Import Upstream version 1.1.8
Paolo Greppi
paolog-guest at moszumanska.debian.org
Fri Dec 23 10:31:01 UTC 2016
This is an automated email from the git hooks/post-receive script.
paolog-guest pushed a commit to branch master
in repository node-progress.
commit 2169f5fdebb7f13490dbb167966da66bb2eb32f4
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date: Fri Dec 23 09:59:35 2016 +0000
Import Upstream version 1.1.8
---
.gitignore | 4 ++
.npmignore | 4 ++
History.md | 77 +++++++++++++++++++++
LICENSE | 22 ++++++
Makefile | 8 +++
Readme.md | 103 ++++++++++++++++++++++++++++
examples/backnforth.js | 35 ++++++++++
examples/colors.js | 17 +++++
examples/download.js | 28 ++++++++
examples/exact.js | 26 +++++++
examples/formats.js | 78 +++++++++++++++++++++
examples/simple.js | 9 +++
examples/toolong.js | 23 +++++++
index.js | 1 +
lib/node-progress.js | 180 +++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 21 ++++++
16 files changed, 636 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0ae9b2b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+.DS_Store
+node_modules
+*.sock
+npm-debug.log
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..f1250e5
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..6a02e97
--- /dev/null
+++ b/History.md
@@ -0,0 +1,77 @@
+### 1.1.7 / 2014-06-30
+
+ * fixed a bug that occurs when a progress bar attempts to draw itself
+ on a console with very few columns
+
+### 1.1.6 / 2014-06-16
+
+ * now prevents progress bar from exceeding TTY width by limiting its width to
+ the with of the TTY
+
+### 1.1.5 / 2014-03-25
+
+ * updated documentation and various other repo maintenance
+ * updated makefile to run examples with `make`
+ * removed dependency on readline module
+
+### 1.1.4 / 2014-03-14
+
+ * now supports streams, for example output progress bar to stderr, while piping
+ stdout
+ * increases performance and flicker by remembering the last drawn progress bar
+
+### 1.1.3 / 2013-12-31
+
+ * fixes a bug where bar would bug when initializing
+ * allows to pass updated tokens when ticking or updating the bar
+ * fixes a bug where the bar would throw if skipping to far
+
+### 1.1.2 / 2013-10-17
+
+ * lets you pass an `fmt` and a `total` instead of an options object
+
+### 1.1.0 / 2013-09-18
+
+ * eta and elapsed tokens default to 0.0 instead of ?.?
+ * better JSDocs
+ * added back and forth example
+ * added method to update the progress bar to a specific percentage
+ * added an option to hide the bar on completion
+
+### 1.0.1 / 2013-08-07
+
+ * on os x readline now works, reverting the terminal hack
+
+### 1.0.0 / 2013-06-18
+
+ * remove .version
+ * merge pull request #15 from davglass/readline-osx
+ * on OSX revert back to terminal hack to avoid a readline bug
+
+### 0.1.0 / 2012-09-19
+
+ * fixed logic bug that caused bar to jump one extra space at the end [davglass]
+ * working with readline impl, even on Windows [davglass]
+ * using readline instead of the \r hack [davglass]
+
+### 0.0.5 / 2012-08-07
+
+ * add ability to tick by zero chunks - tick(0)
+ * fix ETA. Closes #4 [lwille]
+
+### 0.0.4 / 2011-11-14
+
+ * allow more recent versions of node
+
+### 0.0.3 / 2011-04-20
+
+ * changed; erase the line when complete
+
+### 0.0.2 / 2011-04-20
+
+ * added custom tokens support
+ * fixed; clear line before writing
+
+### 0.0.1 / 2010-01-03
+
+ * initial release
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..a7693b0
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2014 TJ Holowaychuk <tj at vision-media.ca>
+
+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/Makefile b/Makefile
new file mode 100644
index 0000000..f933be1
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
+
+EXAMPLES = $(foreach EXAMPLE, $(wildcard examples/*.js), $(EXAMPLE))
+
+.PHONY: test
+test: $(EXAMPLES)
+
+.PHONY: $(EXAMPLES)
+$(EXAMPLES): ; node $@ && echo
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 0000000..202cef3
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,103 @@
+Flexible ascii progress bar.
+
+## Installation
+
+```bash
+$ npm install progress
+```
+
+## Usage
+
+First we create a `ProgressBar`, giving it a format string
+as well as the `total`, telling the progress bar when it will
+be considered complete. After that all we need to do is `tick()` appropriately.
+
+```javascript
+var ProgressBar = require('progress');
+
+var bar = new ProgressBar(':bar', { total: 10 });
+var timer = setInterval(function () {
+ bar.tick();
+ if (bar.complete) {
+ console.log('\ncomplete\n');
+ clearInterval(timer);
+ }
+}, 100);
+```
+
+### Options
+
+These are keys in the options object you can pass to the progress bar along with
+`total` as seen in the example above.
+
+- `total` total number of ticks to complete
+- `width` the displayed width of the progress bar defaulting to total
+- `stream` the output stream defaulting to stderr
+- `complete` completion character defaulting to "="
+- `incomplete` incomplete character defaulting to "-"
+- `clear` option to clear the bar on completion defaulting to false
+- `callback` optional function to call when the progress bar completes
+
+### Tokens
+
+These are tokens you can use in the format of your progress bar.
+
+- `:bar` the progress bar itself
+- `:current` current tick number
+- `:total` total ticks
+- `:elapsed` time elapsed in seconds
+- `:percent` completion percentage
+- `:eta` estimated completion time in seconds
+
+## Examples
+
+### Download
+
+In our download example each tick has a variable influence, so we pass the chunk
+length which adjusts the progress bar appropriately relative to the total
+length.
+
+```javascript
+var ProgressBar = require('../');
+var https = require('https');
+
+var req = https.request({
+ host: 'download.github.com',
+ port: 443,
+ path: '/visionmedia-node-jscoverage-0d4608a.zip'
+});
+
+req.on('response', function(res){
+ var len = parseInt(res.headers['content-length'], 10);
+
+ console.log();
+ var bar = new ProgressBar(' downloading [:bar] :percent :etas', {
+ complete: '=',
+ incomplete: ' ',
+ width: 20,
+ total: len
+ });
+
+ res.on('data', function (chunk) {
+ bar.tick(chunk.length);
+ });
+
+ res.on('end', function () {
+ console.log('\n');
+ });
+});
+
+req.end();
+```
+
+The above example result in a progress bar like the one below.
+
+```
+downloading [===== ] 29% 3.7s
+```
+
+You can see more examples in the `examples` folder.
+
+## License
+
+MIT
diff --git a/examples/backnforth.js b/examples/backnforth.js
new file mode 100644
index 0000000..ffc0dc6
--- /dev/null
+++ b/examples/backnforth.js
@@ -0,0 +1,35 @@
+
+/**
+ * Module dependencies.
+ * Example to increase and decrease the value of a progress bar
+ * to show the functionality of `terminate()`.
+ */
+
+var ProgressBar = require('../');
+
+var bar = new ProgressBar(' :title [:bar] :percent', {
+ complete: '='
+ , incomplete: ' '
+ , width: 30
+ , total: 100
+});
+
+function forward() {
+ bar.tick(1, { title: 'forward ' });
+ if (bar.curr > 60) {
+ backward();
+ } else {
+ setTimeout(forward, 20);
+ }
+}
+
+function backward() {
+ bar.tick(-1, { title: 'backward' });
+ if (bar.curr == 0) {
+ bar.terminate();
+ } else {
+ setTimeout(backward, 20);
+ }
+}
+
+forward();
diff --git a/examples/colors.js b/examples/colors.js
new file mode 100644
index 0000000..5a1d1e3
--- /dev/null
+++ b/examples/colors.js
@@ -0,0 +1,17 @@
+var green = '\u001b[42m \u001b[0m';
+var red = '\u001b[41m \u001b[0m';
+
+var ProgressBar = require('../');
+
+var bar = new ProgressBar(' [:bar]', {
+ complete: green,
+ incomplete: red,
+ total: 20
+});
+
+var id = setInterval(function (){
+ bar.tick();
+ if (bar.complete) {
+ clearInterval(id);
+ }
+}, 100);
diff --git a/examples/download.js b/examples/download.js
new file mode 100644
index 0000000..5639d48
--- /dev/null
+++ b/examples/download.js
@@ -0,0 +1,28 @@
+
+/**
+ * Module dependencies.
+ */
+
+var ProgressBar = require('../');
+
+// simulated download, passing the chunk lengths to tick()
+
+var contentLength = 128 * 1024;
+
+var bar = new ProgressBar(' downloading [:bar] :percent :etas', {
+ complete: '='
+ , incomplete: ' '
+ , width: 20
+ , total: contentLength
+});
+
+(function next() {
+ if (contentLength) {
+ var chunk = Math.random() * 10 * 1024;
+ bar.tick(chunk);
+
+ if (!bar.complete) {
+ setTimeout(next, Math.random() * 1000);
+ }
+ }
+})();
diff --git a/examples/exact.js b/examples/exact.js
new file mode 100644
index 0000000..ce362b9
--- /dev/null
+++ b/examples/exact.js
@@ -0,0 +1,26 @@
+
+/**
+ * Module dependencies.
+ *
+ * Example to show off the `update` method, which allows the
+ * user to specify an exact progress.
+ */
+
+var ProgressBar = require('../');
+
+var bar = new ProgressBar(' progress [:bar] :percent :etas', {
+ complete: '='
+ , incomplete: ' '
+ , width: 40
+ , total: 100
+});
+
+var i = 0, steps = [0.1, 0.25, 0.6, 0.8, 0.4, 0.5, 0.6, 0.2, 0.8, 1.0];
+
+(function next() {
+ if (i >= steps.length) {
+ } else {
+ bar.update(steps[i++]);
+ setTimeout(next, 500);
+ }
+})();
diff --git a/examples/formats.js b/examples/formats.js
new file mode 100644
index 0000000..f07fa4f
--- /dev/null
+++ b/examples/formats.js
@@ -0,0 +1,78 @@
+
+/**
+ * Module dependencies.
+ */
+
+var ProgressBar = require('../');
+
+var bar = new ProgressBar(' :bar :title', { total: 10 });
+
+var id = setInterval(function(){
+ var randomTitle = ['some', 'random', 'title'][Math.random() * 3 | 0];
+ bar.tick({ title: randomTitle });
+ if (bar.complete) {
+ clearInterval(id);
+ bar2();
+ }
+}, 100);
+
+function bar2() {
+ var bar = new ProgressBar(' processing: [:bar]', {
+ total: 15
+ , complete: '*'
+ , incomplete: ' '
+ });
+
+ var id = setInterval(function(){
+ bar.tick();
+ if (bar.complete) {
+ clearInterval(id);
+ bar3();
+ }
+ }, 100);
+}
+
+function bar3() {
+ var bar = new ProgressBar(' download |:bar| :percent', {
+ complete: '='
+ , incomplete: ' '
+ , width: 40
+ , total: 20
+ });
+
+ var id = setInterval(function(){
+ bar.tick();
+ if (bar.complete) {
+ clearInterval(id);
+ bar4();
+ }
+ }, 100);
+}
+
+function bar4() {
+ var bar = new ProgressBar(' :current of :total :percent', {
+ total: 20
+ });
+
+ var id = setInterval(function(){
+ bar.tick();
+ if (bar.complete) {
+ clearInterval(id);
+ bar5();
+ }
+ }, 100);
+}
+
+function bar5() {
+ var bar = new ProgressBar(' [:bar] :elapseds elapsed, eta :etas', {
+ width: 8
+ , total: 50
+ });
+
+ var id = setInterval(function(){
+ bar.tick();
+ if (bar.complete) {
+ clearInterval(id);
+ }
+ }, 300);
+}
diff --git a/examples/simple.js b/examples/simple.js
new file mode 100644
index 0000000..00d29fb
--- /dev/null
+++ b/examples/simple.js
@@ -0,0 +1,9 @@
+var ProgressBar = require('../')
+ , bar = new ProgressBar(' [:bar]', 10);
+
+var id = setInterval(function (){
+ bar.tick();
+ if (bar.complete) {
+ clearInterval(id);
+ }
+}, 100);
diff --git a/examples/toolong.js b/examples/toolong.js
new file mode 100644
index 0000000..39b0878
--- /dev/null
+++ b/examples/toolong.js
@@ -0,0 +1,23 @@
+/**
+ * An example to show how node-progress handles user-specified widths
+ * which exceed the number of columns in the terminal
+ */
+
+var ProgressBar = require('../');
+
+// simulated download, passing the chunk lengths to tick()
+
+var bar = new ProgressBar(' downloading [:bar] :percent :etas', {
+ complete: '='
+ , incomplete: ' '
+ , width: 1024 /* something longer than the terminal width */
+ , total: 100
+});
+
+(function next() {
+ bar.tick(1);
+
+ if (!bar.complete) {
+ setTimeout(next, 10);
+ }
+})();
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..4449dd3
--- /dev/null
+++ b/index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/node-progress');
diff --git a/lib/node-progress.js b/lib/node-progress.js
new file mode 100644
index 0000000..0f47a9f
--- /dev/null
+++ b/lib/node-progress.js
@@ -0,0 +1,180 @@
+/*!
+ * node-progress
+ * Copyright(c) 2011 TJ Holowaychuk <tj at vision-media.ca>
+ * MIT Licensed
+ */
+
+/**
+ * Expose `ProgressBar`.
+ */
+
+exports = module.exports = ProgressBar;
+
+/**
+ * Initialize a `ProgressBar` with the given `fmt` string and `options` or
+ * `total`.
+ *
+ * Options:
+ *
+ * - `total` total number of ticks to complete
+ * - `width` the displayed width of the progress bar defaulting to total
+ * - `stream` the output stream defaulting to stderr
+ * - `complete` completion character defaulting to "="
+ * - `incomplete` incomplete character defaulting to "-"
+ * - `callback` optional function to call when the progress bar completes
+ * - `clear` will clear the progress bar upon termination
+ *
+ * Tokens:
+ *
+ * - `:bar` the progress bar itself
+ * - `:current` current tick number
+ * - `:total` total ticks
+ * - `:elapsed` time elapsed in seconds
+ * - `:percent` completion percentage
+ * - `:eta` eta in seconds
+ *
+ * @param {string} fmt
+ * @param {object|number} options or total
+ * @api public
+ */
+
+function ProgressBar(fmt, options) {
+ this.stream = options.stream || process.stderr;
+
+ if (typeof(options) == 'number') {
+ var total = options;
+ options = {};
+ options.total = total;
+ } else {
+ options = options || {};
+ if ('string' != typeof fmt) throw new Error('format required');
+ if ('number' != typeof options.total) throw new Error('total required');
+ }
+
+ this.fmt = fmt;
+ this.curr = 0;
+ this.total = options.total;
+ this.width = options.width || this.total;
+ this.clear = options.clear
+ this.chars = {
+ complete : options.complete || '=',
+ incomplete : options.incomplete || '-'
+ };
+ this.callback = options.callback || function () {};
+ this.lastDraw = '';
+}
+
+/**
+ * "tick" the progress bar with optional `len` and optional `tokens`.
+ *
+ * @param {number|object} len or tokens
+ * @param {object} tokens
+ * @api public
+ */
+
+ProgressBar.prototype.tick = function(len, tokens){
+ if (len !== 0)
+ len = len || 1;
+
+ // swap tokens
+ if ('object' == typeof len) tokens = len, len = 1;
+
+ // start time for eta
+ if (0 == this.curr) this.start = new Date;
+
+ this.curr += len
+ this.render(tokens);
+
+ // progress complete
+ if (this.curr >= this.total) {
+ this.complete = true;
+ this.terminate();
+ this.callback(this);
+ return;
+ }
+};
+
+/**
+ * Method to render the progress bar with optional `tokens` to place in the
+ * progress bar's `fmt` field.
+ *
+ * @param {object} tokens
+ * @api public
+ */
+
+ProgressBar.prototype.render = function (tokens) {
+ if (!this.stream.isTTY) return;
+
+ var ratio = this.curr / this.total;
+ ratio = Math.min(Math.max(ratio, 0), 1);
+
+ var percent = ratio * 100;
+ var incomplete, complete, completeLength;
+ var elapsed = new Date - this.start;
+ var eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1);
+
+ /* populate the bar template with percentages and timestamps */
+ var str = this.fmt
+ .replace(':current', this.curr)
+ .replace(':total', this.total)
+ .replace(':elapsed', isNaN(elapsed) ? '0.0' : (elapsed / 1000).toFixed(1))
+ .replace(':eta', (isNaN(eta) || !isFinite(eta)) ? '0.0' : (eta / 1000)
+ .toFixed(1))
+ .replace(':percent', percent.toFixed(0) + '%');
+
+ /* compute the available space (non-zero) for the bar */
+ var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length);
+ var width = Math.min(this.width, availableSpace);
+
+ /* TODO: the following assumes the user has one ':bar' token */
+ completeLength = Math.round(width * ratio);
+ complete = Array(completeLength + 1).join(this.chars.complete);
+ incomplete = Array(width - completeLength + 1).join(this.chars.incomplete);
+
+ /* fill in the actual progress bar */
+ str = str.replace(':bar', complete + incomplete);
+
+ /* replace the extra tokens */
+ if (tokens) for (var key in tokens) str = str.replace(':' + key, tokens[key]);
+
+ if (this.lastDraw !== str) {
+ this.stream.clearLine();
+ this.stream.cursorTo(0);
+ this.stream.write(str);
+ this.lastDraw = str;
+ }
+};
+
+/**
+ * "update" the progress bar to represent an exact percentage.
+ * The ratio (between 0 and 1) specified will be multiplied by `total` and
+ * floored, representing the closest available "tick." For example, if a
+ * progress bar has a length of 3 and `update(0.5)` is called, the progress
+ * will be set to 1.
+ *
+ * A ratio of 0.5 will attempt to set the progress to halfway.
+ *
+ * @param {number} ratio The ratio (between 0 and 1 inclusive) to set the
+ * overall completion to.
+ * @api public
+ */
+
+ProgressBar.prototype.update = function (ratio, tokens) {
+ var goal = Math.floor(ratio * this.total);
+ var delta = goal - this.curr;
+
+ this.tick(delta, tokens);
+};
+
+/**
+ * Terminates a progress bar.
+ *
+ * @api public
+ */
+
+ProgressBar.prototype.terminate = function () {
+ if (this.clear) {
+ this.stream.clearLine();
+ this.stream.cursorTo(0);
+ } else console.log();
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..5ec55a5
--- /dev/null
+++ b/package.json
@@ -0,0 +1,21 @@
+{
+ "name": "progress"
+ , "version": "1.1.8"
+ , "description": "Flexible ascii progress bar"
+ , "keywords": ["cli", "progress"]
+ , "author": "TJ Holowaychuk <tj at vision-media.ca>"
+ , "contributors": [
+ {
+ "name": "Christoffer Hallas"
+ , "email": "christoffer.hallas at gmail.com"
+ }
+ , {
+ "name": "Jordan Scales"
+ , "email": "scalesjordan at gmail.com"
+ }
+ ]
+ , "dependencies": {}
+ , "main": "index"
+ , "engines": { "node": ">=0.4.0" }
+ , "repository": "git://github.com/visionmedia/node-progress"
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-progress.git
More information about the Pkg-javascript-commits
mailing list