1 | [](https://travis-ci.org/npkgz/baro)
|
2 |
|
3 | [Single Baro](#single-bar-mode) | [Multi Baro](#multi-bar-mode) | [Options](#config-1) | [Examples](archive/examples/) | [Layouts](presets/) | [Events](archive/docs/events.md)
|
4 |
|
5 | baro
|
6 | ============
|
7 | easy to use progress-bar for command-line/terminal applications
|
8 |
|
9 | 
|
10 |
|
11 | 
|
12 |
|
13 | Install
|
14 | --------
|
15 |
|
16 | ```bash
|
17 | $ yarn add baro
|
18 | $ npm install baro --save
|
19 | ```
|
20 |
|
21 | Features
|
22 | --------
|
23 |
|
24 | * **Simple**, **Robust** and **Easy** to use
|
25 | * Full customizable output format (constious placeholders are available)
|
26 | * Single progressbar mode
|
27 | * Multi progessbar mode
|
28 | * Custom Baro Characters
|
29 | * FPS limiter
|
30 | * ETA calculation based on elapsed time
|
31 | * Custom Tokens to display additional data (payload) within the bar
|
32 | * TTY and NOTTY mode
|
33 | * No callbacks required - designed as pure, external controlled UI widget
|
34 | * Works in Asynchronous and Synchronous tasks
|
35 | * Layout/Theme support
|
36 | * Custom bar formatters (via callback)
|
37 |
|
38 | Usage
|
39 | ------------
|
40 |
|
41 | Multiple examples are available e.g. [example.js](https://github.com/npkgz/baro/blob/master/examples/example.js) - just try it `$ node example.js`
|
42 |
|
43 | ```js
|
44 | const cliProgress = require('baro');
|
45 |
|
46 | // create a new progress bar instance and use shades_classic theme
|
47 | const bar1 = new cliProgress.SingleBar({}, cliProgress.Layouts.shades_classic);
|
48 |
|
49 | // start the progress bar with a total value of 200 and start value of 0
|
50 | bar1.init(200, 0);
|
51 |
|
52 | // update the current value in your application..
|
53 | bar1.update(100);
|
54 |
|
55 | // stop the progress bar
|
56 | bar1.stop();
|
57 | ```
|
58 |
|
59 | Single Baro Mode
|
60 | -----------------------------------
|
61 |
|
62 | 
|
63 |
|
64 | ### Example ###
|
65 |
|
66 | ```js
|
67 | const cliProgress = require('baro');
|
68 |
|
69 | // create new progress bar
|
70 | const b1 = new cliProgress.SingleBar({
|
71 | format: 'CLI Progress |' + _colors.cyan('{bar}') + '| {progress}% || {value}/{total} Chunks || Speed: {speed}',
|
72 | proChar: '\u2588',
|
73 | negChar: '\u2591',
|
74 | hideCursor: true
|
75 | });
|
76 |
|
77 | // initialize the bar - defining payload token "speed" with the default value "N/A"
|
78 | b1.init(200, 0, {
|
79 | speed: "N/A"
|
80 | });
|
81 |
|
82 | // update values
|
83 | b1.increment();
|
84 | b1.update(20);
|
85 |
|
86 | // stop the bar
|
87 | b1.stop();
|
88 | ```
|
89 |
|
90 | ### Constructor ###
|
91 |
|
92 | Initialize a new Progress bar. An instance can be used **multiple** times! it's not required to re-create it!
|
93 |
|
94 | ```js
|
95 | const cliProgress = require('baro');
|
96 |
|
97 | const <instance> = new cliProgress.SingleBar(config:object [, preset:object]);
|
98 | ```
|
99 |
|
100 | #### Options ####
|
101 |
|
102 |
|
103 | ### ::initialize() ###
|
104 |
|
105 | Starts the progress bar and set the total and initial value
|
106 |
|
107 | ```js
|
108 | <instance>.initialize(totalValue:int, startValue:int [, payload:object = {}]);
|
109 | ```
|
110 |
|
111 | ### ::update() ###
|
112 |
|
113 | Sets the current progress value and optionally the payload with values of custom tokens as a second parameter. To update payload only, set currentValue to `null`.
|
114 |
|
115 | ```js
|
116 | <instance>.update([currentValue:int [, payload:object = {}]]);
|
117 |
|
118 | // update progress without altering value
|
119 | <instance>.update([payload:object = {}]);
|
120 | ```
|
121 |
|
122 | ### ::increment() ###
|
123 |
|
124 | Increases the current progress value by a specified amount (default +1). Update payload optionally
|
125 |
|
126 | ```js
|
127 | <instance>.increment([delta:int [, payload:object = {}]]);
|
128 |
|
129 | // delta=1 assumed
|
130 | <instance>.increment(payload:object = {}]);
|
131 | ```
|
132 |
|
133 | ### ::setTotal() ###
|
134 |
|
135 | Sets the total progress value while progressbar is active. Especially useful handling dynamic tasks.
|
136 |
|
137 | ```js
|
138 | <instance>.setTotal(totalValue:int);
|
139 | ```
|
140 |
|
141 | ### ::stop() ###
|
142 |
|
143 | Stops the progress bar and go to next line
|
144 |
|
145 | ```js
|
146 | <instance>.stop();
|
147 | ```
|
148 |
|
149 | ### ::updateETA() ###
|
150 |
|
151 | Force eta calculation update (long running processes) without altering the progress values.
|
152 |
|
153 | Note: you may want to increase `etaCapacity` size - otherwise it can cause `INF` eta values in case the value didn't updated within the time series.
|
154 |
|
155 | ```js
|
156 | <instance>.updateETA();
|
157 | ```
|
158 |
|
159 |
|
160 | Multi Baro Mode
|
161 | -----------------------------------
|
162 |
|
163 | 
|
164 |
|
165 | ### Example ###
|
166 |
|
167 | ```js
|
168 | const cliProgress = require('baro');
|
169 |
|
170 | // create new container
|
171 | const multibar = new cliProgress.MultiBar({
|
172 | autoClear: false,
|
173 | hideCursor: true
|
174 |
|
175 | }, cliProgress.Layouts.shades_grey);
|
176 |
|
177 | // add bars
|
178 | const b1 = multibar.create(200, 0);
|
179 | const b2 = multibar.create(1000, 0);
|
180 |
|
181 | // control bars
|
182 | b1.increment();
|
183 | b2.update(20, {filename: "helloworld.txt"});
|
184 |
|
185 | // stop all bars
|
186 | multibar.stop();
|
187 | ```
|
188 |
|
189 | ### Constructor ###
|
190 |
|
191 | Initialize a new multiprogress container. Bars need to be added. The config/presets are used for each single bar!
|
192 |
|
193 | ```js
|
194 | const cliProgress = require('baro');
|
195 |
|
196 | const <instance> = new cliProgress.MultiBar(config:object [, preset:object]);
|
197 | ```
|
198 |
|
199 | ### ::create() ###
|
200 |
|
201 | Adds a new progress bar to the container and starts the bar. Returns regular `SingleBar` object which can be individually controlled.
|
202 |
|
203 | ```js
|
204 | const <barInstance> = <instance>.create(totalValue:int, startValue:int [, payload:object = {}]);
|
205 | ```
|
206 |
|
207 | ### ::remove() ###
|
208 |
|
209 | Removes an existing bar from the multi progress container.
|
210 |
|
211 | ```js
|
212 | <instance>.remove(<barInstance>:object);
|
213 | ```
|
214 |
|
215 | ### ::stop() ###
|
216 |
|
217 | Stops the all progress bars
|
218 |
|
219 | ```js
|
220 | <instance>.stop();
|
221 | ```
|
222 |
|
223 | Options
|
224 | -----------------------------------
|
225 |
|
226 | The following config can be updated
|
227 |
|
228 | - `format` (type:string|function) - progress bar output format @see format section
|
229 | - `fps` (type:float) - the maximum update rate (default: 10)
|
230 | - `stream` (type:stream) - output stream to use (default: `process.stderr`)
|
231 | - `autoStop` (type:boolean) - automatically call `stop()` when the value reaches the total (default: false)
|
232 | - `autoClear` (type:boolean) - clear the progress bar on complete / `stop()` call (default: false)
|
233 | - `barSize` (type:int) - the length of the progress bar in chars (default: 40)
|
234 | - `align` (type:char) - position of the progress bar - 'left' (default), 'right' or 'center'
|
235 | - `proChar` (type:char) - character to use as "complete" indicator in the bar (default: "=")
|
236 | - `negChar` (type:char) - character to use as "incomplete" indicator in the bar (default: "-")
|
237 | - `hideCursor` (type:boolean) - hide the cursor during progress operation; restored on complete (default: false) - pass `null` to keep terminal settings
|
238 | - `linewrap` (type:boolean) - disable line wrapping (default: false) - pass `null` to keep terminal settings; pass `true` to add linebreaks automatically (not recommended)
|
239 | - `etaCapacity` (type:int) - number of updates with which to calculate the eta; higher numbers give a more stable eta (default: 10)
|
240 | - `etaAutoUpdate` (type:boolean) - trigger an eta calculation update during asynchronous rendering trigger using the current value - should only be used for long running processes in conjunction with lof `fps` values and large `etaCapacity` (default: false)
|
241 | - `syncUpdate` (type:boolean) - trigger redraw during `update()` in case threshold time x2 is exceeded (default: true) - limited to single bar usage
|
242 | - `noTTYOutput` (type:boolean) - enable scheduled output to noTTY streams - e.g. redirect to files (default: false)
|
243 | - `notTTYSchedule` (type:int) - set the output schedule/interval for noTTY output in `ms` (default: 2000ms)
|
244 | - `autoZero` (type:boolean) - display progress bars with 'total' of zero(0) as empty, not full (default: false)
|
245 | - `forceRedraw` (type:boolean) - trigger redraw on every frame even if progress remains the same; can be useful if progress bar gets overwritten by other concurrent writes to the terminal (default: false)
|
246 | - `barGlue` (type:string) - a "glue" string between the complete and incomplete bar elements used to insert ascii control sequences for colorization (default: empty) - Note: in case you add visible "glue" characters the barSize will be increased by the length of the glue!
|
247 | - `autoPadding` (type: boolean) - add padding chars to formatted time and progress to force fixed width (default: false) - Note: handled standard format functions!
|
248 | - `padChar` (type: string) - the character sequence used for autoPadding (default: " ") - Note: due to performance optimizations this value requires a length of 3 identical chars
|
249 | - `barChars` (type: function) - a custom bar formatter function which renders the bar-element (default: [format-bar.js](lib/format-bar.js))
|
250 | - `formatTime` (type: function) - a custom timer formatter function which renders the formatted time elements like `eta_formatted` and `duration-formatted` (default: [format-time.js](lib/format-time.js))
|
251 | - `formatValue` (type: function) - a custom value formatter function which renders all other values (default: [format-value.js](lib/format-value.js))
|
252 |
|
253 | Events
|
254 | -----------------------------------
|
255 |
|
256 | The classes extends [EventEmitter](https://nodejs.org/api/events.html) which allows you to hook into different events.
|
257 |
|
258 | See [event docs](archive/docs/events.md) for detailed information + examples.
|
259 |
|
260 | Baro Formatting
|
261 | -----------------------------------
|
262 |
|
263 | The progressbar can be customized by using the following build-in placeholders. They can be combined in any order.
|
264 |
|
265 | - `{bar}` - the progress bar, customizable by the config **barSize**, **proChars** and **negChars**
|
266 | - `{progress}` - the current progress in percent (0-100)
|
267 | - `{total}` - the end value
|
268 | - `{value}` - the current value set by last `update()` call
|
269 | - `{eta}` - expected time of accomplishment in seconds (limited to 115days, otherwise INF is displayed)
|
270 | - `{duration}` - elapsed time in seconds
|
271 | - `{eta_formatted}` - expected time of accomplishment formatted into appropriate units
|
272 | - `{duration_formatted}` - elapsed time formatted into appropriate units
|
273 |
|
274 | ### Example ###
|
275 |
|
276 | ```js
|
277 | const opt = {
|
278 | format: 'progress [{bar}] {progress}% | ETA: {eta}s | {value}/{total}'
|
279 | }
|
280 | ```
|
281 |
|
282 | is rendered as
|
283 |
|
284 | ```
|
285 | progress [========================================] 100% | ETA: 0s | 200/200
|
286 | ```
|
287 |
|
288 | Custom formatters
|
289 | -----------------------------------
|
290 |
|
291 | Instead of a "static" format string it is also possible to pass a custom callback function as formatter.
|
292 | For a full example (including params) take a look on `lib/formatter.js`
|
293 |
|
294 | ### Example 1 ###
|
295 |
|
296 | ```js
|
297 | function formatter(config, params, payload){
|
298 |
|
299 | // bar grows dynamically by current progress - no whitespaces are added
|
300 | const bar = config.barCompleteString.substr(0, Math.round(params.progress*config.barSize));
|
301 |
|
302 | // end value reached ?
|
303 | // change color to green when finished
|
304 | if (params.value >= params.total){
|
305 | return '# ' + _colors.grey(payload.task) + ' ' + _colors.green(params.value + '/' + params.total) + ' --[' + bar + ']-- ';
|
306 | }else{
|
307 | return '# ' + payload.task + ' ' + _colors.yellow(params.value + '/' + params.total) + ' --[' + bar + ']-- ';
|
308 | }
|
309 | }
|
310 |
|
311 | const opt = {
|
312 | format: formatter
|
313 | }
|
314 | ```
|
315 |
|
316 | is rendered as
|
317 |
|
318 | ```
|
319 | # Task 1 0/200 --[]--
|
320 | # Task 1 98/200 --[████████████████████]--
|
321 | # Task 1 200/200 --[████████████████████████████████████████]--
|
322 | ```
|
323 |
|
324 |
|
325 | ### Example 2 ###
|
326 |
|
327 | You can also access the default format functions to use them within your formatter:
|
328 |
|
329 | ```js
|
330 | const {TimeFormat, ValueFormat, BarFormat, Formatter} = require('cli-progess').Format;
|
331 | ...
|
332 | ```
|
333 |
|
334 | Examples
|
335 | ---------------------------------------------
|
336 |
|
337 | ### Example 1 - Set Options ###
|
338 |
|
339 | ```js
|
340 | // change the progress characters
|
341 | // set fps limit to 5
|
342 | // change the output stream and barSize
|
343 | const bar = new _progress.Baro({
|
344 | proChar: '#',
|
345 | negChar: '.',
|
346 | fps: 5,
|
347 | stream: process.stdout,
|
348 | barSize: 65,
|
349 | position: 'center'
|
350 | });
|
351 | ```
|
352 |
|
353 | ### Example 2 - Change Styles defined by Layout ###
|
354 |
|
355 | ```js
|
356 | // uee shades preset
|
357 | // change the barSize
|
358 | const bar = new _progress.Baro({
|
359 | barSize: 65,
|
360 | position: 'right'
|
361 | }, _progress.Layouts.shades_grey);
|
362 | ```
|
363 |
|
364 | ### Example 3 - Custom Payload ###
|
365 |
|
366 | The payload object keys should only contain keys matching standard `\w+` regex!
|
367 |
|
368 | ```js
|
369 | // create new progress bar with custom token "speed"
|
370 | const bar = new _progress.Baro({
|
371 | format: 'progress [{bar}] {progress}% | ETA: {eta}s | {value}/{total} | Speed: {speed} kbit'
|
372 | });
|
373 |
|
374 | // initialize the bar - set payload token "speed" with the default value "N/A"
|
375 | bar.init(200, 0, {
|
376 | speed: "N/A"
|
377 | });
|
378 |
|
379 | // some code/update loop
|
380 | // ...
|
381 |
|
382 | // update bar value. set custom token "speed" to 125
|
383 | bar.update(5, {
|
384 | speed: '125'
|
385 | });
|
386 |
|
387 | // process finished
|
388 | bar.stop();
|
389 | ```
|
390 |
|
391 | ### Example 4 - Custom Layouts ###
|
392 |
|
393 | **File** `myPreset.js`
|
394 |
|
395 | ```js
|
396 | const _colors = require('colors');
|
397 |
|
398 | module.exports = {
|
399 | format: _colors.red(' {bar}') + ' {progress}% | ETA: {eta}s | {value}/{total} | Speed: {speed} kbit',
|
400 | proChar: '\u2588',
|
401 | negChar: '\u2591'
|
402 | };
|
403 | ```
|
404 |
|
405 | **Application**
|
406 |
|
407 | ```js
|
408 | const myPreset = require('./myPreset.js');
|
409 |
|
410 | const bar = new _progress.Baro({
|
411 | barSize: 65
|
412 | }, myPreset);
|
413 | ```
|
414 |
|
415 |
|
416 | Layouts/Themes
|
417 | ---------------------------------------------
|
418 |
|
419 | Need a more modern appearance ? **baro** supports predefined themes via presets. You are welcome to add your custom one :)
|
420 |
|
421 | But keep in mind that a lot of the "special-chars" rely on Unicode - it might not work as expected on legacy systems.
|
422 |
|
423 | ### Default Layouts ###
|
424 |
|
425 | The following presets are included by default
|
426 |
|
427 | * **legacy** - Styles as of baro v1.3.0
|
428 | * **shades-classic** - Unicode background shades are used for the bar
|
429 | * **shades-grey** - Unicode background shades with grey bar
|
430 | * **rect** - Unicode Rectangles
|
431 |
|
432 |
|
433 | Compatibility
|
434 | ---------------------------------------------
|
435 |
|
436 | **baro** is designed for linux/macOS/container applications which mostly providing standard compliant tty terminals/shells. In non-tty mode it is suitable to be used with logging daemons (cyclic output).
|
437 |
|
438 | It also works with PowerShell on Windows 10 - the legacy command prompt on outdated Windows versions won't work as expected and is not supported!
|
439 |
|
440 | Any Questions ? Report a Bug ? Enhancements ?
|
441 | ---------------------------------------------
|
442 | Please open a new issue on [GitHub](https://github.com/npkgz/baro/issues)
|
443 |
|
444 | License
|
445 | -------
|
446 | baro is OpenSource and licensed under the Terms of [The MIT License (X11)](http://opensource.org/licenses/MIT). You're welcome to [contribute](https://github.com/npkgz/baro/blob/master/CONTRIBUTE.md)!
|