Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support for barCompleteChar being an array of string plus others associated #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ The following options can be changed
- `stopOnComplete` (type:boolean) - automatically call `stop()` when the value reaches the total (default: false)
- `clearOnComplete` (type:boolean) - clear the progress bar on complete / `stop()` call (default: false)
- `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'
- `barCompleteChar` (type:char) - character to use as "complete" indicator in the bar (default: "=")
- `barIncompleteChar` (type:char) - character to use as "incomplete" indicator in the bar (default: "-")
- `align` (type:string) - position of the progress bar - 'left' (default), 'right' or 'center'
- `barCompleteChar` (type:string|string[]) - character to use as "complete" indicator in the bar (default: "=")
- `barIncompleteChar` (type:string) - character to use as "incomplete" indicator in the bar (default: "-")
- `hideCursor` (type:boolean) - hide the cursor during progress operation; restored on complete (default: false) - pass `null` to keep terminal settings
- `linewrap` (type:boolean) - disable line wrapping (default: false) - pass `null` to keep terminal settings; pass `true` to add linebreaks automatically (not recommended)
- `gracefulExit` (type:boolean) - stop the bars in case of `SIGINT` or `SIGTERM` - this restores most cursor settings before exiting (default: `false` - subjected to change)
Expand Down Expand Up @@ -450,7 +450,7 @@ The following presets are included by default
* **shades-classic** - Unicode background shades are used for the bar
* **shades-grey** - Unicode background shades with grey bar
* **rect** - Unicode Rectangles

* **braille-patterns** - Unicode Braille Pattern Dots

Compatibility
---------------------------------------------
Expand Down
30 changes: 14 additions & 16 deletions cli-progress.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
const _SingleBar = require('./lib/single-bar');
const _MultiBar = require('./lib/multi-bar');
const _Presets = require('./presets/index');
const _Formatter = require('./lib/formatter');
const _defaultFormatValue = require('./lib/format-value');
const _defaultFormatBar = require('./lib/format-bar');
const _defaultFormatTime = require('./lib/format-time');
const SingleBar = require('./lib/single-bar.js');
const MultiBar = require('./lib/multi-bar.js');
const Presets = require('./presets/index.js');
const Formatter = require('./lib/formatter.js');
const ValueFormat = require('./lib/format-value.js');
const BarFormat = require('./lib/format-bar.js');
const TimeFormat = require('./lib/format-time.js');

// sub-module access
module.exports = {
Bar: _SingleBar,
SingleBar: _SingleBar,
MultiBar: _MultiBar,
Presets: _Presets,
Bar: SingleBar,
SingleBar,
MultiBar,
Presets,
Format: {
Formatter: _Formatter,
BarFormat: _defaultFormatBar,
ValueFormat: _defaultFormatValue,
TimeFormat: _defaultFormatTime
Formatter,
BarFormat, ValueFormat, TimeFormat,
}
};
};
28 changes: 20 additions & 8 deletions lib/format-bar.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
// format bar
module.exports = function formatBar(progress, options){
// calculate barsize
const completeSize = Math.round(progress*options.barsize);
const incompleteSize = options.barsize-completeSize;

// generate bar string by stripping the pre-rendered strings
return options.barCompleteString.substr(0, completeSize) +
if (Array.isArray(options.barCompleteChar) && options.barCompleteChar.length > 1) {
const completeSize = Math.floor(progress * options.barsize);
const incompleteSize = Math.floor(options.barsize * (1 - progress));
const remainder = progress * options.barsize - completeSize;
const remainderChar = remainder > 0 ?
options.barCompleteChar[Math.round(remainder * options.barCompleteChar.length) - 1] :
"";
return options.barCompleteString.slice(0, completeSize) +
remainderChar +
options.barGlue +
options.barIncompleteString.substr(0, incompleteSize);
}
options.barIncompleteString.slice(0, incompleteSize);
}

// calculate barsize
const completeSize = Math.round(progress * options.barsize);
const incompleteSize = options.barsize - completeSize;
// generate bar string by stripping the pre-rendered strings
return options.barCompleteString.slice(0, completeSize) +
options.barGlue +
options.barIncompleteString.slice(0, incompleteSize);
}
28 changes: 14 additions & 14 deletions lib/generic-bar.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const _ETA = require('./eta');
const _Terminal = require('./terminal');
const _formatter = require('./formatter');
const _options = require('./options');
const _EventEmitter = require('events');
const EventEmitter = require('node:events');

const ETA = require('./eta.js');
const Terminal = require('./terminal.js');
const formatter = require('./formatter.js');

// Progress-Bar constructor
module.exports = class GenericBar extends _EventEmitter{
module.exports = class GenericBar extends EventEmitter {

constructor(options){
constructor(options) {
super();

// store options and assign derived ones (instance specific)
this.options = _options.assignDerivedOptions(options);
this.options = options;

// store terminal instance
this.terminal = (this.options.terminal) ? this.options.terminal : new _Terminal(this.options.stream);
this.terminal = (this.options.terminal) ? this.options.terminal : new Terminal(this.options.stream);

// the current bar value
this.value = 0;
Expand All @@ -38,7 +38,7 @@ module.exports = class GenericBar extends _EventEmitter{
this.lastRedraw = Date.now();

// default eta calculator (will be re-create on start)
this.eta = new _ETA(this.options.etaBufferLength, 0, 0);
this.eta = new ETA(this.options.etaBufferLength, 0, 0);

// payload data
this.payload = {};
Expand All @@ -47,7 +47,7 @@ module.exports = class GenericBar extends _EventEmitter{
this.isActive = false;

// use default formatter or custom one ?
this.formatter = (typeof this.options.format === 'function') ? this.options.format : _formatter;
this.formatter = (typeof this.options.format === 'function') ? this.options.format : formatter;
}

// internal render function
Expand Down Expand Up @@ -123,7 +123,7 @@ module.exports = class GenericBar extends _EventEmitter{
this.lastDrawnString = '';

// initialize eta buffer
this.eta = new _ETA(this.options.etaBufferLength, this.startTime, this.value);
this.eta = new ETA(this.options.etaBufferLength, this.startTime, this.value);

// set flag
this.isActive = true;
Expand All @@ -136,7 +136,7 @@ module.exports = class GenericBar extends _EventEmitter{
stop(){
// set flag
this.isActive = false;

// store stop timestamp to get total duration
this.stopTime = Date.now();

Expand Down Expand Up @@ -207,7 +207,7 @@ module.exports = class GenericBar extends _EventEmitter{
// handle the use case when `step` is omitted but payload is passed
if (typeof arg0 === 'object') {
this.update(this.value + 1, arg0);

// increment([step=1], [payload={}])
}else{
this.update(this.value + arg0, arg1);
Expand Down
43 changes: 22 additions & 21 deletions lib/multi-bar.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
const _Terminal = require('./terminal');
const _BarElement = require('./generic-bar');
const _options = require('./options');
const _EventEmitter = require('events');
const EventEmitter = require('node:events');

const Terminal = require('./terminal.js');
const BarElement = require('./generic-bar.js');
const { parse, } = require('./options.js');

// Progress-Bar constructor
module.exports = class MultiBar extends _EventEmitter{
module.exports = class MultiBar extends EventEmitter {

constructor(options, preset){
constructor(options, preset) {
super();

// list of bars
this.bars = [];

// parse+store options
this.options = _options.parse(options, preset);
this.options = parse(options, preset);

// disable synchronous updates
this.options.synchronousUpdate = false;

// store terminal instance
this.terminal = (this.options.terminal) ? this.options.terminal : new _Terminal(this.options.stream);
this.terminal = (this.options.terminal) ? this.options.terminal : new Terminal(this.options.stream);

// the update timer
this.timer = null;
Expand All @@ -38,14 +39,14 @@ module.exports = class MultiBar extends _EventEmitter{
}

// add a new bar to the stack
create(total, startValue, payload, barOptions={}){
create(total, startValue, payload, barOptions={}) {
// create new bar element and merge global options + overrides
// use the same global terminal instance for all instances
const bar = new _BarElement(Object.assign(
{},
const bar = new BarElement(Object.assign(
{},

// global options
this.options,
this.options,

// terminal instance
{
Expand All @@ -70,19 +71,19 @@ module.exports = class MultiBar extends _EventEmitter{
process.once('SIGINT', this.sigintCallback);
process.once('SIGTERM', this.sigintCallback);
}

// multiprogress already active ?
if (!this.isActive){
if (!this.isActive) {
// hide the cursor ?
if (this.options.hideCursor === true){
if (this.options.hideCursor === true) {
this.terminal.cursor(false);
}

// disable line wrapping ?
if (this.options.linewrap === false){
if (this.options.linewrap === false) {
this.terminal.lineWrapping(false);
}

// initialize update timer
this.timer = setTimeout(this.update.bind(this), this.schedulingRate);
}
Expand All @@ -101,12 +102,12 @@ module.exports = class MultiBar extends _EventEmitter{
}

// remove a bar from the stack
remove(bar){
remove(bar) {
// find element
const index = this.bars.indexOf(bar);

// element found ?
if (index < 0){
if (index < 0) {
return false;
}

Expand All @@ -133,7 +134,7 @@ module.exports = class MultiBar extends _EventEmitter{

// trigger event
this.emit('update-pre');

// reset cursor
this.terminal.cursorRelativeReset();

Expand Down Expand Up @@ -218,7 +219,7 @@ module.exports = class MultiBar extends _EventEmitter{
if (this.options.clearOnComplete){
// clear all bars
this.terminal.clearBottom();

// or show final progress ?
}else{
// update each bar
Expand Down