1 | const _GenericBar = require('./generic-bar');
|
2 | const _options = require('./options');
|
3 |
|
4 |
|
5 | module.exports = class SingleBar extends _GenericBar{
|
6 |
|
7 | constructor(options, preset){
|
8 | super(_options.parse(options, preset));
|
9 |
|
10 |
|
11 | this.timer = null;
|
12 |
|
13 |
|
14 | if (this.options.noTTYOutput && this.terminal.isTTY() === false){
|
15 | this.options.synchronousUpdate = false;
|
16 | }
|
17 |
|
18 |
|
19 | this.schedulingRate = (this.terminal.isTTY() ? this.options.throttleTime : this.options.notTTYSchedule);
|
20 | }
|
21 |
|
22 |
|
23 | render(){
|
24 |
|
25 | if (this.timer){
|
26 | clearTimeout(this.timer);
|
27 | this.timer = null;
|
28 | }
|
29 |
|
30 |
|
31 | super.render();
|
32 |
|
33 |
|
34 | if (this.options.noTTYOutput && this.terminal.isTTY() === false){
|
35 | this.terminal.newline();
|
36 | }
|
37 |
|
38 |
|
39 | this.timer = setTimeout(this.render.bind(this), this.schedulingRate);
|
40 | }
|
41 |
|
42 | update(current, payload){
|
43 |
|
44 | if (!this.timer) {
|
45 | return;
|
46 | }
|
47 |
|
48 | super.update(current, payload);
|
49 |
|
50 |
|
51 |
|
52 | if (this.options.synchronousUpdate && (this.lastRedraw + this.options.throttleTime*2) < Date.now()){
|
53 |
|
54 | this.render();
|
55 | }
|
56 | }
|
57 |
|
58 |
|
59 | start(total, startValue, payload){
|
60 |
|
61 | if (this.options.noTTYOutput === false && this.terminal.isTTY() === false){
|
62 | return;
|
63 | }
|
64 |
|
65 |
|
66 | this.terminal.cursorSave();
|
67 |
|
68 |
|
69 | if (this.options.hideCursor === true){
|
70 | this.terminal.cursor(false);
|
71 | }
|
72 |
|
73 |
|
74 | if (this.options.linewrap === false){
|
75 | this.terminal.lineWrapping(false);
|
76 | }
|
77 |
|
78 |
|
79 | super.start(total, startValue, payload);
|
80 |
|
81 |
|
82 | this.render();
|
83 | }
|
84 |
|
85 |
|
86 | stop(){
|
87 |
|
88 | if (!this.timer) {
|
89 | return;
|
90 | }
|
91 |
|
92 |
|
93 | this.render();
|
94 |
|
95 |
|
96 | super.stop();
|
97 |
|
98 |
|
99 | clearTimeout(this.timer);
|
100 | this.timer = null;
|
101 |
|
102 |
|
103 | if (this.options.hideCursor === true){
|
104 | this.terminal.cursor(true);
|
105 | }
|
106 |
|
107 |
|
108 | if (this.options.linewrap === false){
|
109 | this.terminal.lineWrapping(true);
|
110 | }
|
111 |
|
112 |
|
113 | this.terminal.cursorRestore();
|
114 |
|
115 |
|
116 | if (this.options.clearOnComplete){
|
117 | this.terminal.cursorTo(0, null);
|
118 | this.terminal.clearLine();
|
119 | }else{
|
120 |
|
121 | this.terminal.newline();
|
122 | }
|
123 | }
|
124 | } |
\ | No newline at end of file |