Skip to content

Commit 1058f41

Browse files
committed
added example howto use multibars in synchronous context
1 parent c279c45 commit 1058f41

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Branch 3.x ##
22

3+
### 3.9.0 ###
4+
5+
* Added: example howto use multibars in synchronous context
6+
37
### 3.8.2 ###
48

59
* Bugfix: bar duration not stopped until all bars have finished - thanks to [omjadas on GitHub](https://github.com/AndiDittrich/Node.CLI-Progress/issues/71)

examples/example-synchronous.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _progress = require('../cli-progress');
1+
const _progress = require('../cli-progress');
22

33
// long running, recursive function (blocking operation)
44
// 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
@@ -10,16 +10,16 @@ function fibonacci(n) {
1010
}
1111
}
1212

13-
function run(options, limit){
13+
function runSinglebar(options, limit){
1414
// create new progress bar using default values
15-
var bar = new _progress.Bar(options);
15+
const bar = new _progress.Bar(options);
1616
bar.start(limit, 1);
1717

18-
var fibonacciNumbers = [];
18+
const fibonacciNumbers = [];
1919

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

33+
function runMultibar(options, limit){
34+
// create new progress bar using default values
35+
const multibar = new _progress.MultiBar(options);
36+
37+
const bar1 = multibar.create(limit, 1);
38+
const bar2 = multibar.create(2124, 541);
39+
40+
const fibonacciNumbers = [];
41+
42+
// calculate the Nth fibonacci
43+
// this loop is executed synchronous - no timer/interval callbacks will be executed
44+
for (let i = 1; i <= limit; i++) {
45+
fibonacciNumbers.push(fibonacci(i));
46+
bar1.update(i);
47+
bar2.increment(89);
48+
49+
// force redraw
50+
multibar.update();
51+
}
52+
53+
// stop all bars
54+
multibar.stop();
55+
56+
// display the numbers:
57+
console.log('\nFibonacci (1-', fibonacciNumbers.length,'): ', fibonacciNumbers.join(', '), '\n');
58+
}
59+
3360
console.log('\nCalculation without synchronous updates');
34-
run({
61+
runSinglebar({
3562
format: 'Fibonacci Calculation Progress [{bar}] {percentage}% | ETA: {eta}s | Current: F({value})',
3663
hideCursor: true,
3764
synchronousUpdate: false
3865
}, 40);
3966

4067
console.log('\nCalculation WITH synchronous updates');
41-
run({
68+
runSinglebar({
4269
format: 'Fibonacci Calculation Progress [{bar}] {percentage}% | ETA: {eta}s | Current: F({value})',
4370
hideCursor: true,
4471
synchronousUpdate: true
4572
}, 43);
4673

4774

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

0 commit comments

Comments
 (0)