Comparing version 0.0.3 to 0.1.0
@@ -6,2 +6,4 @@ /* | ||
var mod_readline = require('readline'); | ||
var mod_fs = require('fs'); | ||
var mod_tty = require('tty'); | ||
@@ -20,2 +22,6 @@ var mod_assert = require('assert-plus'); | ||
var THOUSAND = 1000; | ||
var MILLION = THOUSAND * 1000; | ||
var BILLION = MILLION * 1000; | ||
/* | ||
@@ -123,2 +129,16 @@ * How long should we wait (in ms) before considering our average/rate | ||
function | ||
formatcount(elems) | ||
{ | ||
if (elems >= BILLION) { | ||
return ((elems / BILLION).toFixed(2) + 'B'); | ||
} else if (elems >= MILLION) { | ||
return ((elems / MILLION).toFixed(2) + 'M'); | ||
} else if (elems >= THOUSAND) { | ||
return ((elems / THOUSAND).toFixed(2) + 'K'); | ||
} else { | ||
return (elems); | ||
} | ||
} | ||
function | ||
init_readline(_tty) | ||
@@ -157,2 +177,4 @@ { | ||
mod_assert.optionalNumber(options.maxdrawfreq, 'options.maxdrawfreq'); | ||
mod_assert.optionalBool(options.bytes, 'options.bytes'); | ||
mod_assert.optionalBool(options.devtty, 'options.devtty'); | ||
@@ -168,2 +190,8 @@ if (options.nosize) { | ||
if (options.bytes === false) { | ||
self.pb_fmtfunc = formatcount; | ||
} else { | ||
self.pb_fmtfunc = formatsize; | ||
} | ||
self.pb_hide_cursor = options.hidecursor ? true : false; | ||
@@ -182,3 +210,25 @@ | ||
self.pb_ttyfd = -1; | ||
self.pb_tty = process.stderr; | ||
if (options.devtty) { | ||
/* | ||
* Some processes will have their stdio file descriptors | ||
* redirected, for example to a log file capturing both stdout | ||
* and stderr. In this event, on UNIX systems, it is possible | ||
* to open the controlling terminal via the special device | ||
* "/dev/tty". We do not do this by default as it can violate | ||
* the principle of least astonishment for users. | ||
* | ||
* We can make direct use of the less-than-public WriteStream | ||
* class in the "tty" module to get some semblance of regular | ||
* node TTY handling (including reading the terminal size). | ||
* This has been tested on node 0.8.26 and node 0.10.18, but | ||
* may require changes in the future. | ||
*/ | ||
mod_assert.ok(!USE_READLINE, 'devtty only valid on UNIX ' + | ||
'systems'); | ||
self.pb_ttyfd = mod_fs.openSync('/dev/tty', 'r+'); | ||
self.pb_tty = new mod_tty.WriteStream(self.pb_ttyfd); | ||
} | ||
self.pb_rlif = null; | ||
@@ -189,2 +239,10 @@ if (USE_READLINE || !ECMA48_TERMINAL) { | ||
/* | ||
* Node itself will handle resize signals on the "process.stdout" tty | ||
* stream. Register a resize handler for whatever tty we end up | ||
* binding to: | ||
*/ | ||
self.pb_sigwinch = self._sigwinch.bind(self); | ||
process.on('SIGWINCH', self.pb_sigwinch); | ||
if (options.maxdrawfreq !== undefined) { | ||
@@ -199,2 +257,3 @@ mod_assert.ok(options.maxdrawfreq > 0, | ||
self.pb_lastdrawtime = 0; | ||
self.pb_lastdrawwidth = 0; | ||
self.pb_startat = +Date.now(); | ||
@@ -204,2 +263,13 @@ self.pb_readings = 0; | ||
ProgressBar.prototype._sigwinch = function | ||
_sigwinch() | ||
{ | ||
var self = this; | ||
try { | ||
self.pb_tty._refreshSize(); | ||
} catch (ex) { | ||
} | ||
}; | ||
ProgressBar.prototype._cleanup = function | ||
@@ -213,4 +283,20 @@ _cleanup() | ||
rlif.close(); | ||
} | ||
if (self.pb_sigwinch !== null) { | ||
process.removeListener('SIGWINCH', self.pb_sigwinch); | ||
self.pb_sigwinch = null; | ||
} | ||
/* | ||
* We opened a file descriptor for access to /dev/tty, so we must | ||
* clean up the TTY WriteStream here: | ||
*/ | ||
if (self.pb_ttyfd !== -1) { | ||
self.pb_tty.end(); | ||
self.pb_tty = null; | ||
self.pb_ttyfd = -1; | ||
} | ||
}; | ||
ProgressBar.prototype._write = function | ||
@@ -254,3 +340,3 @@ _write(data, clear_first) | ||
} | ||
} | ||
}; | ||
@@ -280,2 +366,26 @@ ProgressBar.prototype.end = function | ||
ProgressBar.prototype.resize = function | ||
resize(newsize) | ||
{ | ||
var self = this; | ||
mod_assert.ok(!self.pb_done, 'cannot resize a finished progress bar'); | ||
if (!newsize) { | ||
self.pb_nosize = true; | ||
self.pb_size = 0; | ||
} else { | ||
mod_assert.number(newsize, 'new size must be false or a ' + | ||
'number'); | ||
self.pb_nosize = false; | ||
self.pb_size = newsize; | ||
} | ||
/* | ||
* Redraw with the new configuration: | ||
*/ | ||
self.advance(0); | ||
} | ||
ProgressBar.prototype.advance = function | ||
@@ -327,3 +437,3 @@ advance(progress) | ||
var rate = Math.floor(self.pb_progress / period); | ||
ratestr = formatsize(rate) + '/s'; | ||
ratestr = self.pb_fmtfunc(rate) + '/s'; | ||
if (!self.pb_nosize && self.pb_progress < self.pb_size) { | ||
@@ -344,3 +454,3 @@ var remaining = Math.floor((self.pb_size - | ||
infostr = sprintf(' %8s %10s %6s', | ||
formatsize(self.pb_progress), ratestr, etastr); | ||
self.pb_fmtfunc(self.pb_progress), ratestr, etastr); | ||
@@ -353,3 +463,3 @@ filewidth = self.pb_tty.columns - infostr.length - 2; | ||
infostr = sprintf(' %3d%% %8s %10s %6s', percent, | ||
formatsize(self.pb_progress), ratestr, etastr); | ||
self.pb_fmtfunc(self.pb_progress), ratestr, etastr); | ||
@@ -374,5 +484,46 @@ filewidth = Math.floor(self.pb_tty.columns / 4); | ||
self._write(filestr + bar + infostr, true); | ||
self.pb_lastdrawwidth = filestr.length + bar.length + infostr.length; | ||
self.pb_lastdrawtime = +Date.now(); | ||
}; | ||
/* | ||
* Write a line of log-style text so that it scrolls up the screen _above_ | ||
* the progress bar, then redraw the progress bar. | ||
*/ | ||
ProgressBar.prototype.log = function | ||
log(str) | ||
{ | ||
var self = this; | ||
mod_assert.string(str, 'must provide a string to log'); | ||
/* | ||
* The logged string must not contain newlines: | ||
*/ | ||
mod_assert.ok(!str.match(/\n/), 'log string must not contain ' + | ||
'newlines'); | ||
if (self.pb_done) { | ||
/* | ||
* We've already drawn the final progress bar line, so | ||
* just write and get out. | ||
*/ | ||
self._write(str + '\n', false); | ||
return; | ||
} | ||
if (self.pb_lastdrawwidth !== 0) { | ||
/* | ||
* We already drew a progress bar. Make sure the log message | ||
* will overwrite the whole thing. If not, extend it with | ||
* spaces to cover the shortfall. | ||
*/ | ||
while (str.length < self.pb_lastdrawwidth) | ||
str += ' '; | ||
} | ||
self._write(str + '\n', true); | ||
self.draw(); | ||
}; | ||
module.exports = { | ||
@@ -379,0 +530,0 @@ ProgressBar: ProgressBar |
{ | ||
"name": "progbar", | ||
"author": "Joshua M. Clulow <jmc@joyent.com>", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "terminal progress bar", | ||
@@ -14,4 +14,4 @@ "main": "./lib/progbar.js", | ||
"dependencies": { | ||
"assert-plus": "0.1.2", | ||
"sprintf": "0.1.1" | ||
"assert-plus": "~0.1.5", | ||
"sprintf": "~0.1.3" | ||
}, | ||
@@ -18,0 +18,0 @@ |
@@ -39,1 +39,6 @@ #!/usr/bin/env node | ||
new_bar(); | ||
setInterval(function () { | ||
if (bar) | ||
bar.log('now @ ' + (new Date()).toISOString()); | ||
}, 750); |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
14974
483
10
+ Addedassert-plus@0.1.5(transitive)
+ Addedsprintf@0.1.5(transitive)
- Removedassert-plus@0.1.2(transitive)
- Removedsprintf@0.1.1(transitive)
Updatedassert-plus@~0.1.5
Updatedsprintf@~0.1.3