UNPKG

3.36 kBJavaScriptView Raw
1// global options storage
2const _options = {};
3
4// utility to merge defaults
5function mergeOption(v, defaultValue){
6 if (typeof v === 'undefined' || v === null){
7 return defaultValue;
8 }else{
9 return v;
10 }
11}
12
13module.exports = {
14 // set global options
15 parse: function parse(rawOptions, preset){
16
17 // merge preset
18 const opt = Object.assign({}, preset, rawOptions);
19
20 // the max update rate in fps (redraw will only triggered on value change)
21 _options.throttleTime = 1000 / (mergeOption(opt.fps, 10));
22
23 // the output stream to write on
24 _options.stream = mergeOption(opt.stream, process.stderr);
25
26 // external terminal provided ?
27 _options.terminal = mergeOption(opt.terminal, null);
28
29 // clear on finish ?
30 _options.clearOnComplete = mergeOption(opt.clearOnComplete, false);
31
32 // stop on finish ?
33 _options.stopOnComplete = mergeOption(opt.stopOnComplete, false);
34
35 // size of the progressbar in chars
36 _options.barsize = mergeOption(opt.barsize, 40);
37
38 // position of the progress bar - 'left' (default), 'right' or 'center'
39 _options.align = mergeOption(opt.align, 'left');
40
41 // hide the cursor ?
42 _options.hideCursor = mergeOption(opt.hideCursor, false);
43
44 // disable linewrapping ?
45 _options.linewrap = mergeOption(opt.linewrap, false);
46
47 // pre-render bar strings (performance)
48 _options.barCompleteString = (new Array(_options.barsize + 1 ).join(opt.barCompleteChar || '='));
49 _options.barIncompleteString = (new Array(_options.barsize + 1 ).join(opt.barIncompleteChar || '-'));
50
51 // glue sequence (control chars) between bar elements ?
52 _options.barGlue = mergeOption(opt.barGlue, '');
53
54 // the bar format
55 _options.format = mergeOption(opt.format, 'progress [{bar}] {percentage}% | ETA: {eta}s | {value}/{total}');
56
57 // external time-format provided ?
58 _options.formatTime = mergeOption(opt.formatTime, null);
59
60 // external value-format provided ?
61 _options.formatValue = mergeOption(opt.formatValue, null);
62
63 // external bar-format provided ?
64 _options.formatBar = mergeOption(opt.formatBar, null);
65
66 // the number of results to average ETA over
67 _options.etaBufferLength = mergeOption(opt.etaBuffer, 10);
68
69 // allow synchronous updates ?
70 _options.synchronousUpdate = mergeOption(opt.synchronousUpdate, true);
71
72 // notty mode
73 _options.noTTYOutput = mergeOption(opt.noTTYOutput, false);
74
75 // schedule - 2s
76 _options.notTTYSchedule = mergeOption(opt.notTTYSchedule, 2000);
77
78 // emptyOnZero - false
79 _options.emptyOnZero = mergeOption(opt.emptyOnZero, false);
80
81 // force bar redraw even if progress did not change
82 _options.forceRedraw = mergeOption(opt.forceRedraw, false);
83
84 // automated padding to fixed width ?
85 _options.autopadding = mergeOption(opt.autopadding, false);
86
87 // autopadding character - empty in case autopadding is disabled
88 _options.autopaddingChar = _options.autopadding ? mergeOption(opt.autopaddingChar, ' ') : '';
89
90 return _options;
91 },
92
93 // fetch all options
94 getOptions: function getOptions(){
95 return _options;
96 }
97};
\No newline at end of file