console-toolkit 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +34 -0
- package/README.md +92 -0
- package/package.json +59 -0
- package/src/alphanumeric/arrows.js +31 -0
- package/src/alphanumeric/fractions.js +82 -0
- package/src/alphanumeric/number-formatters.js +131 -0
- package/src/alphanumeric/roman.js +86 -0
- package/src/alphanumeric/unicode-cultural-numbers.js +68 -0
- package/src/alphanumeric/unicode-letters.js +63 -0
- package/src/alphanumeric/unicode-numbers.js +75 -0
- package/src/alphanumeric/utils.js +44 -0
- package/src/ansi/csi.js +67 -0
- package/src/ansi/index.js +20 -0
- package/src/ansi/sgr-constants.js +99 -0
- package/src/ansi/sgr-state.js +398 -0
- package/src/ansi/sgr.js +214 -0
- package/src/box.js +253 -0
- package/src/charts/bars/block-frac-grouped.js +6 -0
- package/src/charts/bars/block-frac.js +36 -0
- package/src/charts/bars/block-grouped.js +6 -0
- package/src/charts/bars/block.js +43 -0
- package/src/charts/bars/draw-grouped.js +39 -0
- package/src/charts/bars/draw-stacked.js +24 -0
- package/src/charts/bars/frac-grouped.js +33 -0
- package/src/charts/bars/plain-grouped.js +6 -0
- package/src/charts/bars/plain.js +63 -0
- package/src/charts/columns/block-frac-grouped.js +6 -0
- package/src/charts/columns/block-frac.js +30 -0
- package/src/charts/columns/block-grouped.js +6 -0
- package/src/charts/columns/block.js +37 -0
- package/src/charts/columns/draw-grouped.js +48 -0
- package/src/charts/columns/draw-stacked.js +31 -0
- package/src/charts/columns/frac-grouped.js +27 -0
- package/src/charts/columns/plain-grouped.js +6 -0
- package/src/charts/columns/plain.js +39 -0
- package/src/charts/themes/default.js +12 -0
- package/src/charts/themes/rainbow-reversed.js +5 -0
- package/src/charts/themes/rainbow.js +8 -0
- package/src/charts/utils.js +75 -0
- package/src/draw-block-frac.js +33 -0
- package/src/draw-block.js +55 -0
- package/src/meta.js +41 -0
- package/src/output/show.js +40 -0
- package/src/output/updater.js +82 -0
- package/src/output/writer.js +131 -0
- package/src/panel.js +748 -0
- package/src/plot/bitmap.js +108 -0
- package/src/plot/draw-line.js +26 -0
- package/src/plot/draw-rect.js +216 -0
- package/src/plot/index.js +24 -0
- package/src/plot/to-quads.js +32 -0
- package/src/spinner/index.js +8 -0
- package/src/spinner/spin.js +51 -0
- package/src/spinner/spinner.js +75 -0
- package/src/spinner/spinners.js +65 -0
- package/src/strings.js +72 -0
- package/src/style.js +620 -0
- package/src/symbols.js +131 -0
- package/src/table/draw-borders.js +87 -0
- package/src/table/index.js +7 -0
- package/src/table/table.js +330 -0
- package/src/themes/blocks/unicode-half.js +9 -0
- package/src/themes/blocks/unicode-thin.js +9 -0
- package/src/themes/lines/ascii-compact.js +11 -0
- package/src/themes/lines/ascii-dots.js +9 -0
- package/src/themes/lines/ascii-girder.js +9 -0
- package/src/themes/lines/ascii-github.js +11 -0
- package/src/themes/lines/ascii-reddit.js +11 -0
- package/src/themes/lines/ascii-rounded.js +11 -0
- package/src/themes/lines/ascii.js +11 -0
- package/src/themes/lines/unicode-bold.js +15 -0
- package/src/themes/lines/unicode-rounded.js +15 -0
- package/src/themes/lines/unicode.js +15 -0
- package/src/themes/utils.js +38 -0
- package/src/turtle/draw-line-art.js +46 -0
- package/src/turtle/draw-unicode.js +33 -0
- package/src/turtle/index.js +12 -0
- package/src/turtle/turtle.js +286 -0
package/src/style.js
ADDED
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
import * as sgr from './ansi/sgr.js';
|
|
2
|
+
import {
|
|
3
|
+
RESET_STATE,
|
|
4
|
+
combineStates,
|
|
5
|
+
addCommandsToState,
|
|
6
|
+
stateTransition,
|
|
7
|
+
stateReverseTransition,
|
|
8
|
+
stringifyCommands,
|
|
9
|
+
toState,
|
|
10
|
+
optimize,
|
|
11
|
+
extractState
|
|
12
|
+
} from './ansi/sgr-state.js';
|
|
13
|
+
import {matchCsi} from './ansi/csi.js';
|
|
14
|
+
import {capitalize, toCamelCase, fromSnakeCase, addGetter, addAliases, addGetters} from './meta.js';
|
|
15
|
+
|
|
16
|
+
export {RESET_STATE};
|
|
17
|
+
|
|
18
|
+
const styleSymbol = Symbol('styleObject'),
|
|
19
|
+
isBrightSymbol = Symbol('isBright'),
|
|
20
|
+
initStateSymbol = Symbol('initState'),
|
|
21
|
+
stateSymbol = Symbol('currentState'),
|
|
22
|
+
colorDepthSymbol = Symbol('colorDepth'),
|
|
23
|
+
optionsSymbol = Symbol('options');
|
|
24
|
+
|
|
25
|
+
class ExtendedColor {
|
|
26
|
+
constructor(styleObject, options, isBright) {
|
|
27
|
+
this[styleSymbol] = styleObject;
|
|
28
|
+
this[optionsSymbol] = options;
|
|
29
|
+
this[isBrightSymbol] = isBright;
|
|
30
|
+
}
|
|
31
|
+
make(newCommands) {
|
|
32
|
+
if (Array.isArray(newCommands)) newCommands[0] = this[optionsSymbol].extended;
|
|
33
|
+
return this[styleSymbol].make(newCommands);
|
|
34
|
+
}
|
|
35
|
+
// options: bright
|
|
36
|
+
get bright() {
|
|
37
|
+
return new ExtendedColor(this[styleSymbol], this[optionsSymbol], true);
|
|
38
|
+
}
|
|
39
|
+
get dark() {
|
|
40
|
+
return new ExtendedColor(this[styleSymbol], this[optionsSymbol], false);
|
|
41
|
+
}
|
|
42
|
+
get default() {
|
|
43
|
+
return this.make(this[optionsSymbol].default);
|
|
44
|
+
}
|
|
45
|
+
// standard colors: defined externally
|
|
46
|
+
// get red() {
|
|
47
|
+
// return this.make([this[optionsSymbol].base, sgr.ColorFormat.COLOR_256, (this[isBrightSymbol] ? 8 : 0) + sgr.Colors.RED]);
|
|
48
|
+
// }
|
|
49
|
+
// get brightRed() {
|
|
50
|
+
// return this.make([this[optionsSymbol].base, sgr.ColorFormat.COLOR_256, 8 + sgr.Colors.RED]);
|
|
51
|
+
// }
|
|
52
|
+
// 256 colors
|
|
53
|
+
color(c) {
|
|
54
|
+
return this.make(sgr.getRawColor256(c));
|
|
55
|
+
}
|
|
56
|
+
stdRgb256(r, g, b) {
|
|
57
|
+
return this.make(sgr.getRawColor256((this[isBrightSymbol] ? 8 : 0) + sgr.colorStdRgb(r, g, b)));
|
|
58
|
+
}
|
|
59
|
+
brightStdRgb256(r, g, b) {
|
|
60
|
+
return this.make(sgr.getRawColor256(8 + sgr.colorStdRgb(r, g, b)));
|
|
61
|
+
}
|
|
62
|
+
darkStdRgb256(r, g, b) {
|
|
63
|
+
return this.make(sgr.getRawColor256(sgr.colorStdRgb(r, g, b)));
|
|
64
|
+
}
|
|
65
|
+
rgb256(r, g, b) {
|
|
66
|
+
return this.make(sgr.getColor256(r, g, b));
|
|
67
|
+
}
|
|
68
|
+
hex256(hex) {
|
|
69
|
+
return this.make(sgr.getHexColor256(hex));
|
|
70
|
+
}
|
|
71
|
+
rgb6(r, g, b) {
|
|
72
|
+
return this.make(sgr.getColor6(r, g, b));
|
|
73
|
+
}
|
|
74
|
+
grayscale256(i) {
|
|
75
|
+
return this.make(sgr.getGrayColor256(i));
|
|
76
|
+
}
|
|
77
|
+
grayscale24(i) {
|
|
78
|
+
return this.make(sgr.getGrayColor24(i));
|
|
79
|
+
}
|
|
80
|
+
// true colors
|
|
81
|
+
trueColor(r, g, b) {
|
|
82
|
+
return this.make(sgr.getTrueColor(r, g, b));
|
|
83
|
+
}
|
|
84
|
+
trueGrayscale(i) {
|
|
85
|
+
return this.make(sgr.getTrueColor(i, i, i));
|
|
86
|
+
}
|
|
87
|
+
hexTrueColor(hex) {
|
|
88
|
+
return this.make(sgr.getHexTrueColor(hex));
|
|
89
|
+
}
|
|
90
|
+
// composite
|
|
91
|
+
rgb(r, g, b) {
|
|
92
|
+
return this[styleSymbol][colorDepthSymbol] > 8 ? this.trueColor(r, g, b) : this.rgb256(r, g, b);
|
|
93
|
+
}
|
|
94
|
+
grayscale(i) {
|
|
95
|
+
return this[styleSymbol][colorDepthSymbol] > 8 ? this.trueGrayscale(i) : this.grayscale256(i);
|
|
96
|
+
}
|
|
97
|
+
hex(hex) {
|
|
98
|
+
return this[styleSymbol][colorDepthSymbol] > 8 ? this.hexTrueColor(hex) : this.hex256(hex);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
addAliases(ExtendedColor, {stdRgb: 'stdRgb256', brightStdRgb: 'brightStdRgb256', darkStdRgb: 'darkStdRgb256'});
|
|
102
|
+
|
|
103
|
+
class Color extends ExtendedColor {
|
|
104
|
+
// options: bright
|
|
105
|
+
get bright() {
|
|
106
|
+
return new Color(this[styleSymbol], this[optionsSymbol], true);
|
|
107
|
+
}
|
|
108
|
+
get dark() {
|
|
109
|
+
return new Color(this[styleSymbol], this[optionsSymbol], false);
|
|
110
|
+
}
|
|
111
|
+
// standard colors: defined externally
|
|
112
|
+
// get red() {
|
|
113
|
+
// return this.make((this[isBrightSymbol] ? this[optionsSymbol].brightBase : this[optionsSymbol].base) + sgr.Colors.RED);
|
|
114
|
+
// }
|
|
115
|
+
// get brightRed() {
|
|
116
|
+
// return this.make(this[optionsSymbol].brightBase + sgr.Colors.RED);
|
|
117
|
+
// }
|
|
118
|
+
stdRgb(r, g, b) {
|
|
119
|
+
return this.make(
|
|
120
|
+
(this[isBrightSymbol] ? this[optionsSymbol].brightBase : this[optionsSymbol].base) + sgr.colorStdRgb(r, g, b)
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
brightStdRgb(r, g, b) {
|
|
124
|
+
return this.make(this[optionsSymbol].brightBase + sgr.colorStdRgb(r, g, b));
|
|
125
|
+
}
|
|
126
|
+
darkStdRgb(r, g, b) {
|
|
127
|
+
return this.make(this[optionsSymbol].base + sgr.colorStdRgb(r, g, b));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
class Bright {
|
|
132
|
+
constructor(styleObject, isBright) {
|
|
133
|
+
this[styleSymbol] = styleObject;
|
|
134
|
+
this[isBrightSymbol] = isBright;
|
|
135
|
+
}
|
|
136
|
+
make(newCommands) {
|
|
137
|
+
return this[styleSymbol].make(newCommands);
|
|
138
|
+
}
|
|
139
|
+
// options: bright
|
|
140
|
+
get bright() {
|
|
141
|
+
return new Bright(this[styleSymbol], true);
|
|
142
|
+
}
|
|
143
|
+
get dark() {
|
|
144
|
+
return new Bright(this[styleSymbol], false);
|
|
145
|
+
}
|
|
146
|
+
// standard colors: defined externally
|
|
147
|
+
// get red() {
|
|
148
|
+
// return this.make(this[isBrightSymbol] ? sgr.getBrightColor(sgr.Colors.RED) : sgr.getColor(sgr.Colors.RED));
|
|
149
|
+
// }
|
|
150
|
+
stdRgb(r, g, b) {
|
|
151
|
+
return this.make(this[isBrightSymbol] ? sgr.getBrightStdRgb(r, g, b) : sgr.getStdRgb(r, g, b));
|
|
152
|
+
}
|
|
153
|
+
brightStdRgb(r, g, b) {
|
|
154
|
+
return this.make(sgr.getBrightStdRgb(r, g, b));
|
|
155
|
+
}
|
|
156
|
+
darkStdRgb(r, g, b) {
|
|
157
|
+
return this.make(sgr.getStdRgb(r, g, b));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
class Reset {
|
|
162
|
+
constructor(styleObject) {
|
|
163
|
+
this[styleSymbol] = styleObject;
|
|
164
|
+
}
|
|
165
|
+
make(newCommands) {
|
|
166
|
+
return this[styleSymbol].make(newCommands);
|
|
167
|
+
}
|
|
168
|
+
// resettable properties: defined externally
|
|
169
|
+
get all() {
|
|
170
|
+
return this.make('');
|
|
171
|
+
}
|
|
172
|
+
get bold() {
|
|
173
|
+
return this[styleSymbol].addState({bold: null});
|
|
174
|
+
}
|
|
175
|
+
get dim() {
|
|
176
|
+
return this[styleSymbol].addState({dim: null});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export class Style {
|
|
181
|
+
constructor(initState, currentState, colorDepth = 24) {
|
|
182
|
+
this[initStateSymbol] = toState(initState);
|
|
183
|
+
this[stateSymbol] = currentState ? toState(currentState) : this[initStateSymbol];
|
|
184
|
+
this[colorDepthSymbol] = colorDepth;
|
|
185
|
+
}
|
|
186
|
+
make(newCommands = []) {
|
|
187
|
+
if (!Array.isArray(newCommands)) newCommands = [newCommands];
|
|
188
|
+
return new Style(this[initStateSymbol], addCommandsToState(this[stateSymbol], newCommands), this[colorDepthSymbol]);
|
|
189
|
+
}
|
|
190
|
+
add(commandSequence) {
|
|
191
|
+
const state = extractState(String(commandSequence), this[stateSymbol]);
|
|
192
|
+
return state === this[stateSymbol] ? this : new Style(this[initStateSymbol], state, this[colorDepthSymbol]);
|
|
193
|
+
}
|
|
194
|
+
addState(state) {
|
|
195
|
+
return new Style(this[initStateSymbol], combineStates(this[stateSymbol], toState(state)), this[colorDepthSymbol]);
|
|
196
|
+
}
|
|
197
|
+
mark(fn) {
|
|
198
|
+
const newStyle = new Style(this[stateSymbol], null, this[colorDepthSymbol]);
|
|
199
|
+
if (typeof fn != 'function') return newStyle;
|
|
200
|
+
fn(newStyle);
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
getInitialState(fn) {
|
|
204
|
+
if (typeof fn != 'function') return this[initStateSymbol];
|
|
205
|
+
fn(this[initStateSymbol]);
|
|
206
|
+
return this;
|
|
207
|
+
}
|
|
208
|
+
getState(fn) {
|
|
209
|
+
if (typeof fn != 'function') return this[stateSymbol];
|
|
210
|
+
fn(this[stateSymbol]);
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
// color depth
|
|
214
|
+
get colorDepth() {
|
|
215
|
+
return this[colorDepthSymbol]; // 1, 4, 8, 24
|
|
216
|
+
}
|
|
217
|
+
setColorDepth(colorDepth) {
|
|
218
|
+
return new Style(this[initStateSymbol], this[stateSymbol], colorDepth);
|
|
219
|
+
}
|
|
220
|
+
// fg, bg, decoration, reset, bright
|
|
221
|
+
get fg() {
|
|
222
|
+
return new Color(this, sgr.FgColorOptions);
|
|
223
|
+
}
|
|
224
|
+
get bg() {
|
|
225
|
+
return new Color(this, sgr.BgColorOptions);
|
|
226
|
+
}
|
|
227
|
+
get colorDecoration() {
|
|
228
|
+
return new ExtendedColor(this, sgr.DecorationColorOptions);
|
|
229
|
+
}
|
|
230
|
+
get reset() {
|
|
231
|
+
return new Reset(this);
|
|
232
|
+
}
|
|
233
|
+
get bright() {
|
|
234
|
+
return new Bright(this, true);
|
|
235
|
+
}
|
|
236
|
+
get dark() {
|
|
237
|
+
return new Bright(this, false);
|
|
238
|
+
}
|
|
239
|
+
// general commands: defined externally
|
|
240
|
+
get resetAll() {
|
|
241
|
+
return this.make('');
|
|
242
|
+
}
|
|
243
|
+
get resetBold() {
|
|
244
|
+
return this.addState({bold: null});
|
|
245
|
+
}
|
|
246
|
+
get resetDim() {
|
|
247
|
+
return this.addState({dim: null});
|
|
248
|
+
}
|
|
249
|
+
// color commands: defined externally
|
|
250
|
+
stdRgb(r, g, b) {
|
|
251
|
+
return this.make(sgr.getStdRgb(r, g, b));
|
|
252
|
+
}
|
|
253
|
+
brightStdRgb(r, g, b) {
|
|
254
|
+
return this.make(sgr.getBrightStdRgb(r, g, b));
|
|
255
|
+
}
|
|
256
|
+
color(c) {
|
|
257
|
+
return this.make(sgr.getRawColor256(c));
|
|
258
|
+
}
|
|
259
|
+
rgb256(r, g, b) {
|
|
260
|
+
return this.make(sgr.getColor256(r, g, b));
|
|
261
|
+
}
|
|
262
|
+
hex256(hex) {
|
|
263
|
+
return this.make(sgr.getHexColor256(hex));
|
|
264
|
+
}
|
|
265
|
+
rgb6(r, g, b) {
|
|
266
|
+
return this.make(sgr.getColor6(r, g, b));
|
|
267
|
+
}
|
|
268
|
+
grayscale256(i) {
|
|
269
|
+
return this.make(sgr.getGrayColor256(i));
|
|
270
|
+
}
|
|
271
|
+
grayscale24(i) {
|
|
272
|
+
return this.make(sgr.getGrayColor24(i));
|
|
273
|
+
}
|
|
274
|
+
trueColor(r, g, b) {
|
|
275
|
+
return this.make(sgr.getTrueColor(r, g, b));
|
|
276
|
+
}
|
|
277
|
+
trueGrayscale(i) {
|
|
278
|
+
return this.make(sgr.getTrueColor(i, i, i));
|
|
279
|
+
}
|
|
280
|
+
hexTrueColor(hex) {
|
|
281
|
+
return this.make(sgr.getHexTrueColor(hex));
|
|
282
|
+
}
|
|
283
|
+
rgb(r, g, b) {
|
|
284
|
+
return this[colorDepthSymbol] > 8 ? this.trueColor(r, g, b) : this.rgb256(r, g, b);
|
|
285
|
+
}
|
|
286
|
+
grayscale(i) {
|
|
287
|
+
return this[colorDepthSymbol] > 8 ? this.trueGrayscale(i) : this.grayscale256(i);
|
|
288
|
+
}
|
|
289
|
+
hex(hex) {
|
|
290
|
+
return this[colorDepthSymbol] > 8 ? this.hexTrueColor(hex) : this.hex256(hex);
|
|
291
|
+
}
|
|
292
|
+
bgStdRgb(r, g, b) {
|
|
293
|
+
return this.make(getBgStdRgb(r, g, b));
|
|
294
|
+
}
|
|
295
|
+
bgBrightStdRgb(r, g, b) {
|
|
296
|
+
return this.make(sgr.getBgBrightStdRgb(r, g, b));
|
|
297
|
+
}
|
|
298
|
+
bgColor(c) {
|
|
299
|
+
return this.make(sgr.getBgRawColor256(c));
|
|
300
|
+
}
|
|
301
|
+
bgRgb256(r, g, b) {
|
|
302
|
+
return this.make(sgr.getBgColor256(r, g, b));
|
|
303
|
+
}
|
|
304
|
+
bgHex256(hex) {
|
|
305
|
+
return this.make(sgr.getBgHexColor256(hex));
|
|
306
|
+
}
|
|
307
|
+
bgRgb6(r, g, b) {
|
|
308
|
+
return this.make(sgr.getBgColor6(r, g, b));
|
|
309
|
+
}
|
|
310
|
+
bgGrayscale256(i) {
|
|
311
|
+
return this.make(sgr.getBgGrayColor256(i));
|
|
312
|
+
}
|
|
313
|
+
bgGrayscale24(i) {
|
|
314
|
+
return this.make(sgr.getBgGrayColor24(i));
|
|
315
|
+
}
|
|
316
|
+
bgTrueColor(r, g, b) {
|
|
317
|
+
return this.make(sgr.getBgTrueColor(r, g, b));
|
|
318
|
+
}
|
|
319
|
+
bgTrueGrayscale(i) {
|
|
320
|
+
return this.make(sgr.getBgTrueColor(i, i, i));
|
|
321
|
+
}
|
|
322
|
+
bgHexTrueColor(hex) {
|
|
323
|
+
return this.make(sgr.getBgHexTrueColor(hex));
|
|
324
|
+
}
|
|
325
|
+
bgRgb(r, g, b) {
|
|
326
|
+
return this[colorDepthSymbol] > 8 ? this.bgTrueColor(r, g, b) : this.bgRgb256(r, g, b);
|
|
327
|
+
}
|
|
328
|
+
bgGrayscale(i) {
|
|
329
|
+
return this[colorDepthSymbol] > 8 ? this.bgTrueGrayscale(i) : this.bgGrayscale256(i);
|
|
330
|
+
}
|
|
331
|
+
bgHex(hex) {
|
|
332
|
+
return this[colorDepthSymbol] > 8 ? this.bgHexTrueColor(hex) : this.bgHex256(hex);
|
|
333
|
+
}
|
|
334
|
+
decorationStdRgb256(r, g, b) {
|
|
335
|
+
return this.make(sgr.getDecorationStdColor256(r, g, b));
|
|
336
|
+
}
|
|
337
|
+
decorationBrightStdRgb256(r, g, b) {
|
|
338
|
+
return this.make(getDecorationBrightStdColor256(r, g, b));
|
|
339
|
+
}
|
|
340
|
+
decorationColor(c) {
|
|
341
|
+
return this.make(sgr.getDecorationRawColor256(c));
|
|
342
|
+
}
|
|
343
|
+
decorationRgb256(r, g, b) {
|
|
344
|
+
return this.make(sgr.getDecorationColor256(r, g, b));
|
|
345
|
+
}
|
|
346
|
+
decorationHex256(hex) {
|
|
347
|
+
return this.make(sgr.getDecorationHexColor256(hex));
|
|
348
|
+
}
|
|
349
|
+
decorationRgb6(r, g, b) {
|
|
350
|
+
return this.make(sgr.getDecorationColor6(r, g, b));
|
|
351
|
+
}
|
|
352
|
+
decorationGrayscale256(i) {
|
|
353
|
+
return this.make(sgr.getDecorationGrayColor256(i));
|
|
354
|
+
}
|
|
355
|
+
decorationGrayscale24(i) {
|
|
356
|
+
return this.make(sgr.getDecorationGrayColor24(i));
|
|
357
|
+
}
|
|
358
|
+
decorationTrueColor(r, g, b) {
|
|
359
|
+
return this.make(sgr.getDecorationTrueColor(r, g, b));
|
|
360
|
+
}
|
|
361
|
+
decorationTrueGrayscale(i) {
|
|
362
|
+
return this.make(sgr.getDecorationTrueColor(i, i, i));
|
|
363
|
+
}
|
|
364
|
+
decorationHexTrueColor(hex) {
|
|
365
|
+
return this.make(sgr.getDecorationHexTrueColor(hex));
|
|
366
|
+
}
|
|
367
|
+
decorationRgb(r, g, b) {
|
|
368
|
+
return this[colorDepthSymbol] > 8 ? this.decorationTrueColor(r, g, b) : this.decorationRgb256(r, g, b);
|
|
369
|
+
}
|
|
370
|
+
decorationGrayscale(i) {
|
|
371
|
+
return this[colorDepthSymbol] > 8 ? this.decorationTrueGrayscale(i) : this.decorationGrayscale256(i);
|
|
372
|
+
}
|
|
373
|
+
decorationHex(hex) {
|
|
374
|
+
return this[colorDepthSymbol] > 8 ? this.decorationHexTrueColor(hex) : this.decorationHex256(hex);
|
|
375
|
+
}
|
|
376
|
+
// wrap a string
|
|
377
|
+
text(s) {
|
|
378
|
+
if (Array.isArray(s)) return s.map(s => this.text(s));
|
|
379
|
+
s = String(s);
|
|
380
|
+
|
|
381
|
+
let state = this[stateSymbol];
|
|
382
|
+
const initialCommands = stateTransition(this[initStateSymbol], state);
|
|
383
|
+
matchCsi.lastIndex = 0;
|
|
384
|
+
for (const match of s.matchAll(matchCsi)) {
|
|
385
|
+
if (match[3] !== 'm') continue;
|
|
386
|
+
state = addCommandsToState(state, match[1].split(';'));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const cleanupCommands = stateReverseTransition(this[initStateSymbol], state);
|
|
390
|
+
return stringifyCommands(initialCommands) + s + stringifyCommands(cleanupCommands);
|
|
391
|
+
}
|
|
392
|
+
// convert to string
|
|
393
|
+
toString() {
|
|
394
|
+
const initialCommands = stateTransition(this[initStateSymbol], this[stateSymbol]);
|
|
395
|
+
return stringifyCommands(initialCommands);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// add color names to ExtendedColor, Bright and Style
|
|
400
|
+
|
|
401
|
+
const make = value =>
|
|
402
|
+
function () {
|
|
403
|
+
return this.make(value);
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
for (const [name, value] of Object.entries(sgr.Colors)) {
|
|
407
|
+
const nameLower = name.toLowerCase(),
|
|
408
|
+
nameCap = capitalize(name);
|
|
409
|
+
|
|
410
|
+
addGetters(ExtendedColor, {
|
|
411
|
+
[nameLower]: function () {
|
|
412
|
+
return this.make([
|
|
413
|
+
this[optionsSymbol].extended,
|
|
414
|
+
sgr.ColorFormat.COLOR_256,
|
|
415
|
+
(this[isBrightSymbol] ? 8 : 0) + value
|
|
416
|
+
]);
|
|
417
|
+
},
|
|
418
|
+
['bright' + nameCap]: function () {
|
|
419
|
+
return this.make([this[optionsSymbol].extended, sgr.ColorFormat.COLOR_256, 8 + value]);
|
|
420
|
+
},
|
|
421
|
+
['dark' + nameCap]: function () {
|
|
422
|
+
return this.make([this[optionsSymbol].extended, sgr.ColorFormat.COLOR_256, value]);
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
addGetters(Color, {
|
|
427
|
+
[nameLower]: function () {
|
|
428
|
+
return this.make(
|
|
429
|
+
(this[isBrightSymbol] ? this[optionsSymbol].brightBase : this[optionsSymbol].base) + sgr.colorNumber(value)
|
|
430
|
+
);
|
|
431
|
+
},
|
|
432
|
+
['bright' + nameCap]: function () {
|
|
433
|
+
return this.make(this[optionsSymbol].brightBase + sgr.colorNumber(value));
|
|
434
|
+
},
|
|
435
|
+
['dark' + nameCap]: function () {
|
|
436
|
+
return this.make(this[optionsSymbol].base + sgr.colorNumber(value));
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
addGetters(Bright, {
|
|
441
|
+
[nameLower]: function () {
|
|
442
|
+
return this.make(this[isBrightSymbol] ? sgr.getBrightColor(value) : sgr.getColor(value));
|
|
443
|
+
},
|
|
444
|
+
['bright' + nameCap]: make(sgr.getBrightColor(value)),
|
|
445
|
+
['dark' + nameCap]: make(sgr.getColor(value))
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
addGetters(Style, {
|
|
449
|
+
[nameLower]: make(sgr.getColor(value)),
|
|
450
|
+
['bright' + nameCap]: make(sgr.getBrightColor(value)),
|
|
451
|
+
['bg' + nameCap]: make(sgr.getBgColor(value)),
|
|
452
|
+
['bgBright' + nameCap]: make(sgr.getBgBrightColor(value))
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
addAliases(Style, {['dark' + nameCap]: nameLower, ['bgDark' + nameCap]: 'bg' + nameCap});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
addAliases(Style, {
|
|
459
|
+
// method aliases
|
|
460
|
+
addCommands: 'make',
|
|
461
|
+
decoration: 'colorDecoration',
|
|
462
|
+
foreground: 'fg',
|
|
463
|
+
background: 'bg',
|
|
464
|
+
|
|
465
|
+
// color aliases
|
|
466
|
+
gray: 'brightBlack',
|
|
467
|
+
bgGray: 'bgBrightBlack',
|
|
468
|
+
|
|
469
|
+
// alias "gray" to "grey"
|
|
470
|
+
grey: 'brightBlack',
|
|
471
|
+
bgGrey: 'bgBrightBlack',
|
|
472
|
+
greyscale: 'grayscale',
|
|
473
|
+
greyscale24: 'grayscale24',
|
|
474
|
+
greyscale256: 'grayscale256',
|
|
475
|
+
trueGreyscale: 'trueGrayscale',
|
|
476
|
+
bgGreyscale: 'bgGrayscale',
|
|
477
|
+
bgGreyscale24: 'bgGrayscale24',
|
|
478
|
+
bgGreyscale256: 'bgGrayscale256',
|
|
479
|
+
bgTrueGreyscale: 'bgTrueGrayscale',
|
|
480
|
+
decorationGreyscale: 'decorationGrayscale',
|
|
481
|
+
decorationGreyscale24: 'decorationGrayscale24',
|
|
482
|
+
decorationGreyscale256: 'decorationGrayscale256',
|
|
483
|
+
decorationTrueGreyscale: 'decorationTrueGrayscale'
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
addAliases(ExtendedColor, {
|
|
487
|
+
// color aliases
|
|
488
|
+
gray: 'brightBlack',
|
|
489
|
+
|
|
490
|
+
// alias "gray" to "grey"
|
|
491
|
+
grey: 'brightBlack',
|
|
492
|
+
greyscale: 'grayscale',
|
|
493
|
+
greyscale24: 'grayscale24',
|
|
494
|
+
greyscale256: 'grayscale256',
|
|
495
|
+
trueGreyscale: 'trueGrayscale'
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
addAliases(Color, {
|
|
499
|
+
// color aliases
|
|
500
|
+
gray: 'brightBlack',
|
|
501
|
+
|
|
502
|
+
// alias "gray" to "grey"
|
|
503
|
+
grey: 'brightBlack'
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
// add commands to Reset, Style
|
|
507
|
+
|
|
508
|
+
const skipCommands = {EXTENDED_COLOR: 1, BG_EXTENDED_COLOR: 1, DECORATION_COLOR: 1};
|
|
509
|
+
|
|
510
|
+
for (const [name, value] of Object.entries(sgr.Commands)) {
|
|
511
|
+
if (name.startsWith('RESET_')) {
|
|
512
|
+
addGetter(Reset, toCamelCase(fromSnakeCase(name).slice(1)), make(value));
|
|
513
|
+
}
|
|
514
|
+
if (skipCommands[name] !== 1) {
|
|
515
|
+
addGetter(Style, toCamelCase(fromSnakeCase(name)), make(value));
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// the back quote function
|
|
520
|
+
|
|
521
|
+
const matchOps = /\{\{([\.\w]+)\}\}/g;
|
|
522
|
+
|
|
523
|
+
const processPart = s => {
|
|
524
|
+
s = String(s);
|
|
525
|
+
const result = [];
|
|
526
|
+
let start = (matchOps.lastIndex = 0);
|
|
527
|
+
for (const match of s.matchAll(matchOps)) {
|
|
528
|
+
result.push(s.substring(start, match.index), match[1].split('.'));
|
|
529
|
+
start = match.index + match[0].length;
|
|
530
|
+
}
|
|
531
|
+
if (start < s.length) result.push(s.substring(start));
|
|
532
|
+
return result;
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
const processStringConstant = (strings, i, result, stack, style) => {
|
|
536
|
+
const pos = () => (i < strings.length ? 'string before argument #' + i : 'string after the last argument');
|
|
537
|
+
for (const input of processPart(strings[i])) {
|
|
538
|
+
if (typeof input == 'string') {
|
|
539
|
+
// process a string
|
|
540
|
+
style = style.add(input);
|
|
541
|
+
result += style + input;
|
|
542
|
+
style = style.mark();
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
// process commands
|
|
546
|
+
for (const command of input) {
|
|
547
|
+
switch (command) {
|
|
548
|
+
case 'save':
|
|
549
|
+
const setupCommands = stateTransition(style[initStateSymbol], style[stateSymbol]);
|
|
550
|
+
result += stringifyCommands(setupCommands);
|
|
551
|
+
stack.push(style);
|
|
552
|
+
style = style.mark();
|
|
553
|
+
continue;
|
|
554
|
+
case 'restore':
|
|
555
|
+
{
|
|
556
|
+
const newStyle = style;
|
|
557
|
+
style = stack.pop();
|
|
558
|
+
if (!style) throw new ReferenceError(`Unmatched restore (${pos()})`);
|
|
559
|
+
const cleanupCommands = stateReverseTransition(style[stateSymbol], newStyle[stateSymbol]);
|
|
560
|
+
result += stringifyCommands(cleanupCommands);
|
|
561
|
+
style = style.mark();
|
|
562
|
+
}
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
style = style[command];
|
|
566
|
+
if (!style) throw new TypeError(`Wrong style property: "${command}" (${pos()})`);
|
|
567
|
+
}
|
|
568
|
+
if (!(style instanceof Style)) throw new TypeError(`The final object is not Style (${pos()})`);
|
|
569
|
+
result += style;
|
|
570
|
+
style = style.mark();
|
|
571
|
+
}
|
|
572
|
+
return {result, style};
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
const makeBq =
|
|
576
|
+
clear =>
|
|
577
|
+
(strings, ...args) => {
|
|
578
|
+
const callAsFunction = !Array.isArray(strings),
|
|
579
|
+
states = callAsFunction && strings;
|
|
580
|
+
|
|
581
|
+
const bq = (strings, ...args) => {
|
|
582
|
+
const stack = [];
|
|
583
|
+
let style = new Style(states?.initState, states?.setState),
|
|
584
|
+
result = '';
|
|
585
|
+
for (let i = 0; i < args.length; ++i) {
|
|
586
|
+
// process a string constant
|
|
587
|
+
({result, style} = processStringConstant(strings, i, result, stack, style));
|
|
588
|
+
// process an argument
|
|
589
|
+
const arg = args[i];
|
|
590
|
+
if (typeof arg == 'function') {
|
|
591
|
+
style = arg(style);
|
|
592
|
+
if (!(style instanceof Style)) throw new TypeError(`The returned object is not Style (argument #${i})`);
|
|
593
|
+
result += style;
|
|
594
|
+
style = style.mark();
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
const input = String(arg);
|
|
598
|
+
style = style.add(input);
|
|
599
|
+
result += input;
|
|
600
|
+
style = style.mark();
|
|
601
|
+
}
|
|
602
|
+
({result, style} = processStringConstant(strings, strings.length - 1, result, stack, style));
|
|
603
|
+
if (clear) {
|
|
604
|
+
const cleanupCommands = stateReverseTransition(states?.initState, style[stateSymbol]);
|
|
605
|
+
result += stringifyCommands(cleanupCommands);
|
|
606
|
+
}
|
|
607
|
+
return optimize(result, states?.initState);
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
if (callAsFunction) return bq;
|
|
611
|
+
return bq(strings, ...args);
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
export const s = makeBq(false);
|
|
615
|
+
export const c = makeBq(true);
|
|
616
|
+
|
|
617
|
+
// singleton
|
|
618
|
+
export const style = new Style({});
|
|
619
|
+
|
|
620
|
+
export default style;
|