-
Notifications
You must be signed in to change notification settings - Fork 1
/
progress-bar.ts
208 lines (179 loc) · 6.44 KB
/
progress-bar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { Wrapper } from 'stdout-update/lib/wrapper';
import * as Figures from 'figures';
import { Theme, IndicationType } from './theme';
import { TaskStatus } from './task';
export enum Progress {
Default = -1,
Start = 0,
End = 100,
}
export enum TemplateToken {
// the progress bar itself
Bar = ':bar',
// current tick number
Current = ':current',
// total ticks
Total = ':total',
// time elapsed in seconds
Elapsed = ':elapsed',
// completion percentage
Percent = ':percent',
// estimated completion time in seconds
ETA = ':eta',
// rate of ticks per second
Rate = ':rate',
}
export interface ProgressBarOptions {
// current completed index
current?: number;
// total number of ticks to complete
total?: number;
// the displayed width of the progress bar defaulting to total
width?: number;
// completion character
complete?: string;
// incomplete character
incomplete?: string;
// option to clear the bar on completion
clear?: boolean;
// option to add badge
badges?: boolean;
// option to add gradient to pending bar
gradient?: boolean;
}
export interface ProgressBarToken {
[key: string]: string;
}
export class ProgressBar {
public static TICK = 1;
public static TIME_DIMENSION = 1000;
public static MAX_POINT_POSITION = 1;
public static MIN_POINT_POSITION = 0;
public static MIN_PERCENT = 0;
public static MAX_PERCENT = 100;
public static MIN_RATIO = 0;
public static MAX_RATIO = 1;
public readonly total = Progress.End;
public readonly completeBlock = Figures.square;
public readonly incompleteBlock = Figures.square;
public readonly width: number = 20;
public readonly clear: boolean = false;
public readonly badges: boolean = true;
public readonly gradient: boolean = true;
public readonly template: string;
private current = Progress.Start;
private start = new Date().getTime();
private end: number | undefined;
private status = TaskStatus.Pending;
private tokens: Map<TemplateToken | string, string> = new Map();
public constructor(template?: string, options?: ProgressBarOptions) {
this.template =
template ||
Theme.join(
Wrapper.SPACE,
TemplateToken.Bar,
`${TemplateToken.Rate}/bps`,
TemplateToken.Percent,
`${TemplateToken.ETA}s`
);
if (typeof options === 'object') {
if (options.current) this.current = options.current;
if (options.total) this.total = options.total;
if (options.width) this.width = options.width;
if (options.complete) this.completeBlock = options.complete;
if (options.incomplete) this.incompleteBlock = options.incomplete;
this.clear = !!options.clear;
}
}
public getRatio(): number {
return Math.min(Math.max(this.current / this.total, ProgressBar.MIN_RATIO), ProgressBar.MAX_RATIO);
}
public getPercent(): number {
return Math.floor(this.getRatio() * ProgressBar.MAX_PERCENT);
}
public getElapsed(): number {
return (this.end || new Date().getTime()) - this.start;
}
public getRate(): number {
return this.current / (this.getElapsed() / ProgressBar.TIME_DIMENSION);
}
public getETA(): number {
return this.getPercent() === ProgressBar.MAX_PERCENT
? ProgressBar.MIN_PERCENT
: this.getElapsed() * (this.total / this.current - ProgressBar.MAX_RATIO);
}
public getStart(): number {
return this.start;
}
public getEnd(): number | undefined {
return this.end;
}
public isCompleted(): boolean {
return this.current >= this.total || !!this.end;
}
public tick(step?: number, tokens?: ProgressBarToken): ProgressBar {
this.current = Math.min(this.total, this.current + (step || ProgressBar.TICK));
this.tokens = typeof tokens === 'object' ? new Map(Object.entries(tokens)) : new Map();
if (this.isCompleted()) this.complete();
return this;
}
public complete(): void {
this.current = this.total;
this.status = TaskStatus.Completed;
this.end = new Date().getTime();
}
public skip(): void {
if (!this.isCompleted()) {
this.status = TaskStatus.Skipped;
this.end = new Date().getTime();
}
}
public fail(): void {
if (!this.isCompleted()) {
this.status = TaskStatus.Failed;
this.end = new Date().getTime();
}
}
public render(theme: Theme): string {
const length = Math.round(this.width * this.getRatio());
const type = Theme.type(this.status);
let blocks = Wrapper.EMPTY.padStart(length, this.completeBlock);
if (!this.isCompleted() && this.gradient) {
blocks = theme.gradient(blocks, {
position: this.getRatio(),
begin: Theme.type(this.status),
end: IndicationType.Success,
});
} else {
blocks = theme.paint(blocks, type);
}
let result = this.template
.replace(TemplateToken.Current, this.current.toString())
.replace(TemplateToken.Total, this.total.toString())
.replace(TemplateToken.Percent, `${this.getPercent().toFixed(ProgressBar.MIN_POINT_POSITION)}%`)
.replace(
TemplateToken.ETA,
(this.getETA() / ProgressBar.TIME_DIMENSION).toFixed(ProgressBar.MAX_POINT_POSITION)
)
.replace(TemplateToken.Rate, Math.round(this.getRate()).toString())
.replace(
TemplateToken.Elapsed,
(this.getElapsed() / ProgressBar.TIME_DIMENSION).toFixed(ProgressBar.MAX_POINT_POSITION)
)
.replace(
TemplateToken.Bar,
Theme.join(
Wrapper.EMPTY,
blocks,
theme.paint(
Wrapper.EMPTY.padStart(this.width - length, this.incompleteBlock),
IndicationType.Subtask
)
)
);
this.tokens.forEach((value: string, key: string): void => {
result = result.replace(`:${key}`, value);
});
return this.badges ? Theme.join(Wrapper.SPACE, result, theme.badge(type)) : result;
}
}