UNPKG

14 kBMarkdownView Raw
1[![Build State](https://travis-ci.org/npkgz/baro.svg?branch=master)](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
5baro
6============
7easy to use progress-bar for command-line/terminal applications
8
9![Demo](assets/baro.gif)
10
11![Demo](assets/presets.png)
12
13Install
14--------
15
16```bash
17$ yarn add baro
18$ npm install baro --save
19```
20
21Features
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
38Usage
39------------
40
41Multiple 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
44const cliProgress = require('baro');
45
46// create a new progress bar instance and use shades_classic theme
47const 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
50bar1.init(200, 0);
51
52// update the current value in your application..
53bar1.update(100);
54
55// stop the progress bar
56bar1.stop();
57```
58
59Single Baro Mode
60-----------------------------------
61
62![Demo](assets/presets.png)
63
64### Example ###
65
66```js
67const cliProgress = require('baro');
68
69// create new progress bar
70const 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"
78b1.init(200, 0, {
79 speed: "N/A"
80});
81
82// update values
83b1.increment();
84b1.update(20);
85
86// stop the bar
87b1.stop();
88```
89
90### Constructor ###
91
92Initialize a new Progress bar. An instance can be used **multiple** times! it's not required to re-create it!
93
94```js
95const cliProgress = require('baro');
96
97const <instance> = new cliProgress.SingleBar(config:object [, preset:object]);
98```
99
100#### Options ####
101
102
103### ::initialize() ###
104
105Starts 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
113Sets 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
124Increases 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
135Sets 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
143Stops the progress bar and go to next line
144
145```js
146<instance>.stop();
147```
148
149### ::updateETA() ###
150
151Force eta calculation update (long running processes) without altering the progress values.
152
153Note: 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
160Multi Baro Mode
161-----------------------------------
162
163![Demo](assets/multibar.png)
164
165### Example ###
166
167```js
168const cliProgress = require('baro');
169
170// create new container
171const multibar = new cliProgress.MultiBar({
172 autoClear: false,
173 hideCursor: true
174
175}, cliProgress.Layouts.shades_grey);
176
177// add bars
178const b1 = multibar.create(200, 0);
179const b2 = multibar.create(1000, 0);
180
181// control bars
182b1.increment();
183b2.update(20, {filename: "helloworld.txt"});
184
185// stop all bars
186multibar.stop();
187```
188
189### Constructor ###
190
191Initialize a new multiprogress container. Bars need to be added. The config/presets are used for each single bar!
192
193```js
194const cliProgress = require('baro');
195
196const <instance> = new cliProgress.MultiBar(config:object [, preset:object]);
197```
198
199### ::create() ###
200
201Adds a new progress bar to the container and starts the bar. Returns regular `SingleBar` object which can be individually controlled.
202
203```js
204const <barInstance> = <instance>.create(totalValue:int, startValue:int [, payload:object = {}]);
205```
206
207### ::remove() ###
208
209Removes an existing bar from the multi progress container.
210
211```js
212<instance>.remove(<barInstance>:object);
213```
214
215### ::stop() ###
216
217Stops the all progress bars
218
219```js
220<instance>.stop();
221```
222
223Options
224-----------------------------------
225
226The 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
253Events
254-----------------------------------
255
256The classes extends [EventEmitter](https://nodejs.org/api/events.html) which allows you to hook into different events.
257
258See [event docs](archive/docs/events.md) for detailed information + examples.
259
260Baro Formatting
261-----------------------------------
262
263The 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
277const opt = {
278 format: 'progress [{bar}] {progress}% | ETA: {eta}s | {value}/{total}'
279}
280```
281
282is rendered as
283
284```
285progress [========================================] 100% | ETA: 0s | 200/200
286```
287
288Custom formatters
289-----------------------------------
290
291Instead of a "static" format string it is also possible to pass a custom callback function as formatter.
292For a full example (including params) take a look on `lib/formatter.js`
293
294### Example 1 ###
295
296```js
297function 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
311const opt = {
312 format: formatter
313}
314```
315
316is 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
327You can also access the default format functions to use them within your formatter:
328
329```js
330const {TimeFormat, ValueFormat, BarFormat, Formatter} = require('cli-progess').Format;
331...
332```
333
334Examples
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
343const 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
358const bar = new _progress.Baro({
359 barSize: 65,
360 position: 'right'
361}, _progress.Layouts.shades_grey);
362```
363
364### Example 3 - Custom Payload ###
365
366The payload object keys should only contain keys matching standard `\w+` regex!
367
368```js
369// create new progress bar with custom token "speed"
370const 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"
375bar.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
383bar.update(5, {
384 speed: '125'
385});
386
387// process finished
388bar.stop();
389```
390
391### Example 4 - Custom Layouts ###
392
393**File** `myPreset.js`
394
395```js
396const _colors = require('colors');
397
398module.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
408const myPreset = require('./myPreset.js');
409
410const bar = new _progress.Baro({
411 barSize: 65
412}, myPreset);
413```
414
415
416Layouts/Themes
417---------------------------------------------
418
419Need a more modern appearance ? **baro** supports predefined themes via presets. You are welcome to add your custom one :)
420
421But 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
425The 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
433Compatibility
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
438It 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
440Any Questions ? Report a Bug ? Enhancements ?
441---------------------------------------------
442Please open a new issue on [GitHub](https://github.com/npkgz/baro/issues)
443
444License
445-------
446baro 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)!