cli-progress
Advanced tools
Comparing version 2.0.0 to 2.1.0
## Branch 2.x ## | ||
### 2.1.0 ### | ||
* Added: `align` option to change the position of the progress bar (left, center, right) - thanks to [sidneys on GitHub](https://github.com/AndiDittrich/Node.CLI-Progress/pull/22) #22 | ||
* Changed: ETA value of type `Infinity` is displayed as **INF**, `NaN` as **NULL** - feature requested by [AxelTerizaki on GitHub](https://github.com/AndiDittrich/Node.CLI-Progress/issues/21) #21 | ||
* Changed: Limited the maximum ETA value to `100000s` (**INF** is displayed in this case) | ||
* Changed: ETA calculation moved to own scope | ||
* Bugfix: example `example-notty.php` was broken | ||
### 2.0.0 ### | ||
Upgrade is possible without any code modifications! requires **node.js 4** | ||
@@ -4,0 +14,0 @@ |
const _Terminal = require('./Terminal'); | ||
const _ETA = require('./ETA'); | ||
const _stringWidth = require('string-width'); | ||
@@ -57,2 +59,5 @@ // Progress-Bar constructor | ||
// position of the progress bar - 'left' (default), 'right' or 'center' | ||
this.align = getOption(options.align, 'left'); | ||
// hide the cursor ? | ||
@@ -78,6 +83,6 @@ this.hideCursor = getOption(options.hideCursor, false); | ||
// the number of results to average ETA over | ||
this.etaBuffer = getOption(options.etaBuffer, 10); | ||
this.etaBufferLength = getOption(options.etaBuffer, 10); | ||
// eta buffer with initial values | ||
this.eta = {}; | ||
// default eta calulator (will be re-create on start) | ||
this.eta = new _ETA(this.etaBufferLength, 0, 0); | ||
@@ -121,3 +126,3 @@ // payload data | ||
// calculate eta | ||
const eta = this.eta.time; | ||
const eta = this.eta.getTime(); | ||
const etaf = Bar.formatTime(eta, 5); | ||
@@ -141,2 +146,25 @@ | ||
// calculate available whitespace (2 characters margin of error) | ||
const fullMargin = Math.max(0, this.terminal.getWidth() - _stringWidth(s) -2); | ||
const halfMargin = Math.floor(fullMargin / 2); | ||
// distribute available whitespace according to position | ||
switch (this.align) { | ||
// fill start-of-line with whitespaces | ||
case 'right': | ||
s = (fullMargin > 0) ? ' '.repeat(fullMargin) + s : s; | ||
break; | ||
// distribute whitespaces to left+right | ||
case 'center': | ||
s = (halfMargin > 0) ? ' '.repeat(halfMargin) + s : s; | ||
break; | ||
// default: left align, no additional whitespaces | ||
case 'left': | ||
default: | ||
break; | ||
} | ||
// string changed ? only trigger redraw on change! | ||
@@ -216,7 +244,3 @@ if (this.lastDrawnString != s){ | ||
// initialize eta buffer | ||
this.eta = { | ||
valueBuffer: [this.value], | ||
timeBuffer: [this.startTime], | ||
time: 0 | ||
}; | ||
this.eta = new _ETA(this.etaBufferLength, this.startTime, this.value); | ||
@@ -271,5 +295,4 @@ // redraw on start! | ||
// add new values | ||
this.eta.valueBuffer.push(current); | ||
this.eta.timeBuffer.push(Date.now()); | ||
// add new value | ||
this.eta.push(Date.now(), current); | ||
@@ -284,3 +307,4 @@ // merge payload | ||
if (this.lastRedraw + this.throttleTime < Date.now()){ | ||
this.calculateETA(); | ||
// number of remaining elements required for calculation | ||
this.eta.calculate(this.total-this.value); | ||
this.render(); | ||
@@ -319,27 +343,2 @@ } | ||
} | ||
// internal - eta calculation | ||
calculateETA(){ | ||
const l = this.eta.valueBuffer.length; | ||
const buffer = Math.min(this.etaBuffer, l); | ||
const v_diff = this.eta.valueBuffer[l - 1] - this.eta.valueBuffer[l - buffer]; | ||
const t_diff = this.eta.timeBuffer[l - 1] - this.eta.timeBuffer[l - buffer]; | ||
// get progress per ms | ||
const vt_rate = v_diff/t_diff; | ||
// remaining | ||
const remaining = this.total-this.value; | ||
// eq: vt_rate *x = total | ||
const eta = (remaining/vt_rate/1000); | ||
this.eta = { | ||
valueBuffer: this.eta.valueBuffer.slice(-this.etaBuffer), | ||
timeBuffer: this.eta.timeBuffer.slice(-this.etaBuffer), | ||
time: Math.ceil(eta) | ||
} | ||
} | ||
} | ||
} |
@@ -48,6 +48,7 @@ const _readline = require('readline'); | ||
// write content to output stream | ||
// @TODO use string-width to strip length | ||
write(s){ | ||
// line wrapping enabled ? trim output | ||
if (this.linewrap === true){ | ||
this.stream.write(s.substr(0, process.stdout.columns || 9999)); | ||
this.stream.write(s.substr(0, this.getWidth())); | ||
}else{ | ||
@@ -73,4 +74,9 @@ this.stream.write(s); | ||
} | ||
// get terminal width | ||
getWidth(){ | ||
return this.stream.columns || 80; | ||
} | ||
} | ||
module.exports = Terminal; | ||
module.exports = Terminal; |
{ | ||
"name": "cli-progress", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Easy to use Progress-Bar for Command-Line/Terminal Applications", | ||
@@ -35,4 +35,5 @@ "keywords": [ | ||
"dependencies": { | ||
"colors": "^1.1.2" | ||
"colors": "^1.1.2", | ||
"string-width": "^2.1.1" | ||
} | ||
} |
@@ -72,2 +72,3 @@ CLI-Progress | ||
- `barsize` (type:int) - the length of the progress bar in chars (default: 40) | ||
- `align` (type:char) - position of the progress bar - 'left' (default), 'right' or 'center' | ||
- `barCompleteString` (type:char) - character to use as "complete" indicator in the bar (default: "=") | ||
@@ -160,3 +161,4 @@ - `barIncompleteString` (type:char) - character to use as "incomplete" indicator in the bar (default: "-") | ||
stream: process.stdout, | ||
barsize: 65 | ||
barsize: 65, | ||
position: 'center' | ||
}); | ||
@@ -171,3 +173,4 @@ ``` | ||
const bar = new _progress.Bar({ | ||
barsize: 65 | ||
barsize: 65, | ||
position: 'right' | ||
}, _progress.Presets.shades_grey); | ||
@@ -174,0 +177,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28672
13
413
248
2
+ Addedstring-width@^2.1.1
+ Addedansi-regex@3.0.1(transitive)
+ Addedis-fullwidth-code-point@2.0.0(transitive)
+ Addedstring-width@2.1.1(transitive)
+ Addedstrip-ansi@4.0.0(transitive)