-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
234 lines (193 loc) · 4.77 KB
/
mod.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
234
import { wcswidth } from 'https://deno.land/x/tty/wcwidth.ts';
import { colors, cursor, clearAnsi } from './ansi.ts';
// Loading spinner
// from https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json
const dots = {
"interval": 80,
"frames": [
"⠋",
"⠙",
"⠹",
"⠸",
"⠼",
"⠴",
"⠦",
"⠧",
"⠇",
"⠏"
]
}
// Default symbols
// from https://github.com/sindresorhus/log-symbols.git
const symbols = {
info: 'ℹ',
success: '✔',
warning: '⚠',
error: '✖'
}
/* ------ Dora based on Ora ------*/
class Dora {
#stream;
#frameIndex;
#spinner;
#interval;
#text;
#options;
#color;
#showCursor;
#lines2clear = 0;
#lineCount = 0;
#columns;
// options { message, color }
constructor (options) {
if (typeof options === 'string') {
options = {
text: options,
}
}
this.#options = {
color: 'cyan',
stream: Deno.stderr,
showCursor: false,
width: 80, // Manually provide columns console
spinner: dots.frames,
...options,
};
this.#spinner = this.#options.spinner;
this.#frameIndex = 0;
this.#color = this.#options.color;
this.#stream = this.#options.stream;
this.text = this.#options.text;
this.#showCursor = this.#options.showCursor;
this.#columns = this.#options.width;
}
// TODO List
// - [ ] Get columns of console
updateLineCount() {
this.#lineCount = 0;
for (const line of clearAnsi(this.#text).split('\n')) {
this.#lineCount += Math.max(1, Math.ceil(wcswidth(line) / this.#columns));
}
}
get text() {
return this.#text;
}
set text(message) {
this.#text = message || '';
this.updateLineCount();
}
get color() {
return this.#color;
}
set color(newColor: string) {
this.#color = colors[newColor] ? newColor : 'cyan';
}
set spinner(newSpinner: Array) {
this.#spinner = newSpinner;
}
frame() {
const frames = this.#spinner;
let frame = colors[this.#color](frames[this.#frameIndex]);
this.#frameIndex = ++this.#frameIndex % frames.length;
const suffix = ' ' + this.#text;
return frame + suffix;
}
// TODO
// - [ ] Indent
clear() {
// Move cursor to the beginning of the line
cursor.toHorizontal(this.#stream, 1);
for (let i=0; i<this.#lines2clear; i++) {
if (i > 0) {
cursor.up(this.#stream);
}
cursor.clearLine(this.#stream);
}
this.#lines2clear = 0;
}
render() {
this.clear();
this.#stream.write(new TextEncoder().encode(this.frame()));
this.#lines2clear = this.#lineCount;
}
start(text: string) {
if (text) {
this.text = text;
}
if (!this.#showCursor) {
cursor.hide(this.#stream);
}
this.#interval = setInterval(this.render.bind(this), 80);
}
stop() {
clearInterval(this.#interval);
this.#interval = undefined;
this.#frameIndex = 0;
this.clear();
if (!this.#showCursor) {
cursor.show(this.#stream);
}
}
succeed(options) {
if (typeof options === 'string') {
options = {
text: options,
}
}
const finalOptions = {
icon: colors['green'](symbols.success),
text: this.#text,
...options
}
this.stopAndPersist(finalOptions.icon, finalOptions.text);
}
fail(options) {
if (typeof options === 'string') {
options = {
text: options,
}
}
const finalOptions = {
icon: colors['red'](symbols.error),
text: this.#text,
...options
}
this.stopAndPersist(finalOptions.icon, finalOptions.text);
}
warn(options) {
if (typeof options === 'string') {
options = {
text: options,
}
}
const finalOptions = {
icon: colors['yellow'](symbols.warning),
text: this.#text,
...options
}
this.stopAndPersist(finalOptions.icon, finalOptions.text);
}
info(options) {
if (typeof options === 'string') {
options = {
text: options,
}
}
const finalOptions = {
icon: colors['blue'](symbols.info),
text: this.#text,
...options
}
this.stopAndPersist(finalOptions.icon, finalOptions.text);
}
stopAndPersist(icon: string = ' ', text: string) {
this.stop();
if (!text) {
text = this.#text;
}
this.#stream.write(new TextEncoder().encode(icon + ' ' + text + '\n'));
}
}
export default function dora(message) {
return new Dora(message);
}