-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
200 lines (171 loc) · 6.12 KB
/
index.js
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
/**
* @author OA Wu <comdan66@gmail.com>
* @copyright Copyright (c) 2015 - 2021, @oawu/cli-progress
* @license http://opensource.org/licenses/MIT MIT License
* @link https://www.ioa.tw/
*/
const Xterm = require('@oawu/xterm')
const Progress = {
lines: [],
preLines: [],
timer: null,
finish: null,
option: {
space: 3, color: false,
$: {
title: { value: '', color: text => text },
subtitle: { value: '', color: text => text },
percent: { value: '', color: text => text },
header: { value: '◉', color: text => Xterm.purple(text) },
newline: { value: '↳', color: text => Xterm.purple(text).dim() },
dash: { value: '─', color: text => Xterm.dim(text) },
dot: { value: '…', color: text => Xterm.lightBlack(text).dim() },
loading: { _value: '⠦⠧⠇⠏⠉⠙⠹⠸⠼⠴⠤⠦', _index: 0, _length: 12, get value () { return this._value[this._index++ % this._length] }, set value (val) { return this._index = 0, this._length = val.length, this._value = val }, color: text => Xterm.yellow(text) },
done: { value: '完成', color: text => Xterm.green(text) },
fail: { value: '錯誤', color: text => Xterm.red(text) },
index: { value: '', color: text => Xterm.dim(text) }
},
},
percent: {
index: null, total: null, text: '',
toString (percent) {
if (this.index !== null && this.total !== null) {
percent = Math.ceil(this.index * 100) / this.total
Progress.option.index = `(${this.index}/${this.total})`
return [
Progress.option.index,
(Progress.option.percent = `${parseInt(percent <= 100
? percent >= 0
? percent
: 0
: 100, 10)}%`),
this.text].filter(t => t !== '').join(` ${Progress.option.dash} `)
}
return this.text !== ''
? ` ${Progress.option.dash} ${this.text}`
: ''
},
appendTo(lines) {
const s = ' '.repeat(Progress.option.space)
if (!lines.length)
return `${s}${this}`
const header = (index, space) => index
? `${space} ${Progress.option.newline}`
: Progress.option.header
const title = (index, str) => index
? (Progress.option.subtitle = str, Progress.option.subtitle)
: (Progress.option.title = str, Progress.option.title)
const percent = index => index
? ''
: this
return [...lines].map(({ index, space, str }) => `\x1b[K${s}${header(index, space)} ${title(index, str)}${percent(index)}`).join("\n")
}
},
set advance (val) {
Progress.percent.index += val
if (Progress.percent.index > Progress.percent.total)
Progress.percent.index = Progress.percent.total
return Progress
},
get advance () {
Progress.advance = 1
return Progress
},
get clean () {
return Progress.option.$.loading._index
? Progress.lines.length > 1
? `\x1b[${Progress.lines.length - 1}A`
: "\r"
: ''
},
print: (...strs) => process.stdout.write(`\r${strs.join('')}`),
title (...strs) {
if (Progress.timer) return Progress
Progress.option.$.loading._index = 0
Progress.lines = []
Progress.appendTitle(...strs)
Progress.timer = setInterval(Progress._timer.bind(Progress, false), 85, Progress._timer(false))
return Progress
},
_timer (fromStop) {
if (!fromStop && Progress.finish)
return Progress.stop()
if (Progress.lines.length > 1)
Progress.print(Progress.clean)
if (Progress.preLines.length) {
Progress.lines = Progress.lines
.concat(Progress.preLines)
.map(({ space, str }, index) => ({ space, str, index }))
Progress.preLines = []
}
fromStop
? Progress.print(`${Progress.percent.appendTo(Progress.lines)}\n`)
: Progress.print(`${Progress.percent.appendTo(Progress.lines)}${Progress.option.dot} ${Progress.option.loading} `)
},
appendTitle(...strs) {
Progress.preLines = Progress.preLines.concat(strs.map(line => {
const match = /(?<space>^\s*)(?<str>.*)/gm.exec(line)
return match !== null ? { ...match.groups } : null
})
.filter(line => line !== null))
return Progress
},
total (total) {
Progress.percent.total = total
Progress.percent.index = 0
return Progress
},
stop () {
if (Progress.timer === null)
return Progress
else
clearInterval(Progress.timer)
Progress._timer(true)
Progress.lines = []
Progress.preLines = []
Progress.percent.index = null
Progress.percent.total = null
Progress.percent.text = ''
Progress.option.$.loading._index = 0
Progress.finish()
Progress.finish = null
Progress.timer = null
return Progress
},
done (message = '完成') {
Progress.percent.index = Progress.percent.total
Progress.option.done = message
Progress.percent.text = Progress.option.done
Progress.stop(Progress.finish = _ => {})
return Progress
},
fail (message = '錯誤', ...errors) {
Progress.option.fail = message === null || message === undefined
? '錯誤'
: message
Progress.percent.text = Progress.option.fail
Progress.stop(Progress.finish = _ => Progress.error(...errors))
return Progress
},
error (...errors) {
if (errors.length <= 0)
return Progress
Progress.print(
`${Progress.option.color
? "\n 【錯誤訊息】\n".red
: "\n 【錯誤訊息】\n"}${errors.map(
error => `${' '.repeat(Progress.option.space)}${Progress.option.header} ${error instanceof Error
? error.stack
: error}\n`).join('')}\n`)
process.emit('SIGINT')
return Progress
},
}
Object.keys(Progress.option.$).forEach(key => Object.defineProperty(Progress.option, key, {
set: val => Progress.option.$[key].value = val,
get: _ => {
const f = function() {}
return f.toString = _ => Progress.option.color ? Progress.option.$[key].color(Progress.option.$[key].value).toString() : Progress.option.$[key].value, Object.defineProperty(f, 'color', { set: func => Progress.option.$[key].color = func }), f
}
}))
module.exports = Progress