Skip to content

Commit

Permalink
added example howto use multibars in synchronous context
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDittrich committed Aug 16, 2020
1 parent c279c45 commit 1058f41
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Branch 3.x ##

### 3.9.0 ###

* Added: example howto use multibars in synchronous context

### 3.8.2 ###

* Bugfix: bar duration not stopped until all bars have finished - thanks to [omjadas on GitHub](https://github.com/AndiDittrich/Node.CLI-Progress/issues/71)
Expand Down
47 changes: 40 additions & 7 deletions examples/example-synchronous.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _progress = require('../cli-progress');
const _progress = require('../cli-progress');

// long running, recursive function (blocking operation)
// well it's no optimized and not for productive use but it's an easy way to implement a long-running & blocking operation without third-party modules
Expand All @@ -10,16 +10,16 @@ function fibonacci(n) {
}
}

function run(options, limit){
function runSinglebar(options, limit){
// create new progress bar using default values
var bar = new _progress.Bar(options);
const bar = new _progress.Bar(options);
bar.start(limit, 1);

var fibonacciNumbers = [];
const fibonacciNumbers = [];

// calculate the Nth fibonacci
// this loop is executed synchronous - no timer/interval callbacks will be executed
for (var i = 1; i <= limit; i++) {
for (let i = 1; i <= limit; i++) {
fibonacciNumbers.push(fibonacci(i));
bar.update(i);
}
Expand All @@ -30,18 +30,51 @@ function run(options, limit){
console.log('\nFibonacci (1-', fibonacciNumbers.length,'): ', fibonacciNumbers.join(', '), '\n');
}

function runMultibar(options, limit){
// create new progress bar using default values
const multibar = new _progress.MultiBar(options);

const bar1 = multibar.create(limit, 1);
const bar2 = multibar.create(2124, 541);

const fibonacciNumbers = [];

// calculate the Nth fibonacci
// this loop is executed synchronous - no timer/interval callbacks will be executed
for (let i = 1; i <= limit; i++) {
fibonacciNumbers.push(fibonacci(i));
bar1.update(i);
bar2.increment(89);

// force redraw
multibar.update();
}

// stop all bars
multibar.stop();

// display the numbers:
console.log('\nFibonacci (1-', fibonacciNumbers.length,'): ', fibonacciNumbers.join(', '), '\n');
}

console.log('\nCalculation without synchronous updates');
run({
runSinglebar({
format: 'Fibonacci Calculation Progress [{bar}] {percentage}% | ETA: {eta}s | Current: F({value})',
hideCursor: true,
synchronousUpdate: false
}, 40);

console.log('\nCalculation WITH synchronous updates');
run({
runSinglebar({
format: 'Fibonacci Calculation Progress [{bar}] {percentage}% | ETA: {eta}s | Current: F({value})',
hideCursor: true,
synchronousUpdate: true
}, 43);


console.log('\nMultibar calculation synchronous enforced updates (synchronous mode not available)');
runMultibar({
format: 'Fibonacci Calculation Progress [{bar}] {percentage}% | ETA: {eta}s | Current: F({value})',
hideCursor: true
}, 43);

0 comments on commit 1058f41

Please sign in to comment.