forked from likun7981/hlink
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogress.ts
233 lines (206 loc) · 6.2 KB
/
progress.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import ansiEscapes from 'ansi-escapes'
import wrapAnsi from 'wrap-ansi'
import { log } from './utils.js'
type TokenType = Record<string, any>
type OptionsType = {
stream?: NodeJS.WriteStream
curr?: number
total: number
complete?: string
incomplete?: string
head?: string
renderThrottle?: number
callback?: Function
clear?: boolean
width?: number
}
class ProgressBar {
/** from option */
private stream: NodeJS.WriteStream
private fmt: string
private curr: number
private clear: boolean
private lines: number
private chars: {
complete: string
incomplete: string
head: string
}
private total: number
private width: number
private renderThrottle: number
private callback: Function
/** from option */
complete: boolean
private tokens: TokenType
private lastRender: number
private lastDraw: string
private start: number
private columns: number
constructor(fmt: string, options: OptionsType | number) {
if (typeof options === 'number') {
const total = options
options = {
total: total
}
} else {
options = options || {}
if (typeof fmt !== 'string') throw new Error('format required')
if (typeof options.total !== 'number') throw new Error('total required')
}
const {
stream = process.stderr,
curr = 0,
total,
width,
clear = false,
complete,
incomplete,
head,
renderThrottle,
callback
} = options
this.stream = stream
this.fmt = fmt
this.curr = curr || 0
this.total = total
this.columns = (process.stderr.columns || 80) - 10
this.width = width || this.columns - 38 * 2;
this.clear = clear
this.lines = fmt.split('\n').length
this.chars = {
complete: complete || '=',
incomplete: incomplete || '-',
head: head || complete || '='
}
this.complete = false
this.renderThrottle = renderThrottle !== 0 ? renderThrottle || 16 : 0
this.lastRender = -Infinity
this.callback = callback || function() {}
this.tokens = {}
this.start = Date.now()
this.lastDraw = ''
}
tick = (len: number, tokens: TokenType) => {
if (len !== 0) len = len || 1
// swap tokens
if ('object' == typeof len) (tokens = len), (len = 1)
if (tokens) this.tokens = tokens
// start time for eta
this.curr += len
if (0 == this.curr) this.start = Date.now()
// try to render
this.render()
// progress complete
if (this.curr >= this.total) {
this.render(true)
this.complete = true
this.terminate()
this.callback(this)
}
}
render = (force: boolean = false, tokens?: Record<string, any>) => {
if (tokens) this.tokens = tokens
if (!this.stream.isTTY) return
const now = Date.now()
const delta = now - this.lastRender
if (!force && delta < this.renderThrottle) {
return
} else {
this.lastRender = now
}
let ratio = this.curr / this.total
ratio = Math.min(Math.max(ratio, 0), 1)
const percent = Math.floor(ratio * 100)
let incomplete, complete, completeLength
const elapsed = Date.now() - this.start
const eta =
percent == 100 ? 0 : (elapsed / this.curr) * (this.total - this.curr)
const rate = this.curr / (elapsed / 1000)
/* populate the bar template with percentages and timestamps */
let str = this.fmt
.replace(':current', String(this.curr))
.replace(':total', String(this.total))
.replace(':elapsed', isNaN(elapsed) ? '0.0' : (elapsed / 1000).toFixed(1))
.replace(
':eta',
isNaN(eta) || !isFinite(eta) ? '0.0' : (eta / 1000).toFixed(1)
)
.replace(':percent', percent.toFixed(0) + '%')
.replace(':rate', String(Math.round(rate)))
/* compute the available space (non-zero) for the bar */
let availableSpace = Math.max(
0,
this.stream.columns - str.replace(':bar', '').length
)
if (availableSpace && process.platform === 'win32') {
availableSpace = availableSpace - 1
}
const width = Math.min(this.width, availableSpace)
/* TODO: the following assumes the user has one ':bar' token */
completeLength = Math.round(width * ratio)
complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete)
incomplete = Array(Math.max(0, width - completeLength + 1)).join(
this.chars.incomplete
)
/* add head to the complete string */
if (completeLength > 0) complete = complete.slice(0, -1) + this.chars.head
/* fill in the actual progress bar */
str = str.replace(':bar', complete + incomplete)
/* replace the extra tokens */
if (this.tokens)
for (const key in this.tokens) {
const wrappedLines = wrapAnsi(this.tokens[key], this.columns, {
trim: false,
hard: true,
wordWrap: false
})
str = str.replace(':' + key, wrappedLines)
}
if (this.lastDraw !== str) {
if (this.lastDraw) this.stream.write(ansiEscapes.eraseLines(this.lines))
this.lines = str.split('\n').length
this.stream.write(str)
this.lastDraw = str
}
}
update = (ratio: number, tokens: TokenType) => {
const goal = Math.floor(ratio * this.total)
const delta = goal - this.curr
this.tick(delta, tokens)
}
interrupt = (message: string) => {
// clear the current line
this.stream.clearLine(1)
// move the cursor to the start of the line
this.stream.cursorTo(0)
// write the message text
this.stream.write(message)
// terminate the line after writing the message
this.stream.write('\n')
// re-display the progress bar with its lastDraw
this.stream.write(this.lastDraw)
}
terminate = () => {
if (this.clear) {
this.stream.write(ansiEscapes.eraseLines(this.lines))
this.stream.cursorTo(0)
} else {
this.stream.write('\n')
}
}
}
export class SimpleProgressBar {
private total: number
private current: number;
constructor(total: number) {
log.info('如果你看到这个消息,说明你的bash不支持格式化输入')
this.total = total
this.current = 0
}
tick = (count: number) => {
this.current += count
log.info(`执行中,当前进度${this.current}/${this.total}`)
}
}
export default ProgressBar