-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
295 lines (272 loc) · 7.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* ─────────────╮
│ SGR Composer │ Convert RGB values to SGR TTY codes
╰──────────────┴─────────────────────────────────────────────────────────────── */
import assert from 'assert'
import converter from 'color-convert'
const _SGRparts = {
start: '\u001B[',
fg: [38, 39],
bg: [48, 49],
reset: {in: 0, out: ''},
end: 'm'
}
const _styles = {
bold: [1, 22],
dim: [2, 22],
italic: [3, 23],
underline: [4, 24],
blink: [5, 25],
invert: [7, 27]
}
/**
* Parse a color array.
* @private
* @param {string | string[]} color_ - The color to parse.
* @param {number} depth_ - 0: no color, 1: 16 colors, 2: 256 colors, 3: 16m colors
* @param {boolean} bg_ - Is this a background color
* @return {SGRColor} The color as an SGR pair.
*/
function parseColor(color_, depth_, bg_) {
if (['reset', 'normal'].includes(color_)) {
return _SGRparts.reset
}
assert(Array.isArray(color_) && color_.length === 3, `provided RGB value needs to be an array, i.e [R, G, B], not ${color_}.`)
return (() => {
const color = (() => {
switch (depth_) {
case 3:
return color_.join(';')
case 2:
return converter.rgb.ansi256(color_)
case 1:
return converter.rgb.ansi16(color_)
default:
return ''
}
})()
const mode = {
in: (() => {
const fgBg = (bg_) ? _SGRparts.bg[0] : _SGRparts.fg[0]
switch (depth_) {
case 3:
return `${fgBg};2;`
case 2:
return `${fgBg};5;`
case 1:
return ''
default:
return ''
}
})(),
out: (bg_) ? _SGRparts.bg[1] : _SGRparts.fg[1]
}
/**
* A representation of an in/out SGR code pair.
* @namespace {object} SGRColor
* @property {string} in - The opening SGR color code.
* @property {string} out - The closing SGR code.
*/
return {
in: `${mode.in}${color}`,
out: `${mode.out}`
}
})()
}
/**
* Parse a style object
* @private
* @param {style} styles_ The style object to parse
* @return {style} The parsed style.
*/
function parseStyles(styles_) {
/**
* Style declaration
* @namespace {object} style
* @property {boolean} background - Set background color
* @property {boolean} bold - Set bold
* @property {boolean} dim - Set dim mode
* @property {boolean} italic - Set italics
* @property {boolean} underline - Set underline
* @property {boolean} blink - Set blink
* @property {boolean} invert - Set inverted video
*/
const styles = {
background: false,
bold: false,
dim: false,
italic: false,
underline: false,
blink: false,
invert: false
}
switch (true) {
case styles_ === 'reset':
return styles
case Array.isArray(styles_):
Object.keys(styles).forEach(key_ => {
if (styles_.includes(key_)) {
styles[key_] = true
}
})
return styles
case (typeof styles_ === 'object'):
return Object.assign(styles, styles_)
default:
return styles
}
}
/**
* Set a style object/
* @param {style} styles - The style object to set
* @param {style} excluded_ - Styles to exclude.
* @returns {Object} - In and Out SGR parts
*/
function setStyles(styles, excluded_) {
const excluded = (excluded_ === undefined) ? {} : excluded_
const sgrIn = []
const sgrOut = []
Object.keys(_styles).forEach(key_ => {
if (styles[key_] && (!excluded[key_])) {
if (!sgrIn.includes(_styles[key_][0])) {
sgrIn.push(_styles[key_][0])
}
if (!sgrOut.includes(_styles[key_][1])) {
sgrOut.unshift(_styles[key_][1])
}
}
})
return {
in: sgrIn.join(';'),
out: sgrOut.join(';')
}
}
/**
* A Class for composing various SGR codes.
* @type {SGRcomposer}
*/
export default class SGRcomposer {
/**
* A Class for composing various SGR codes.
* @param {number|string} targetDepth - The target color depth.
* @param {style} styles - Additional SGR style codes.
*/
constructor(targetDepth, styles) {
this._depth = (depth_ => {
switch (true) {
case [3, '16m', 'millions'].includes(depth_):
return 3
case [2, 256, '256', 'hundreds'].includes(depth_):
return 2
case [1, 8, '8', 16, '16', 'ansi', 'color'].includes(depth_):
return 1
default:
return 0
}
})(targetDepth)
this.colorSGR = {in: '', out: ''}
this.style = styles
}
/**
* Get the current color depth.
* @return {number} 0: no color, 1: 16 colors, 2: 256 colors, 3: 16m colors
*/
get depth() {
return this._depth
}
/**
* Get the raw RGB color as an array.
* @return {number[]} An array of RGB elements
*/
get color() {
return this._color
}
/**
* Set the current color
* @param {number[]} color RGB as an array.
*/
set color(color) {
this.colorSGR = parseColor(color, this._depth, false)
this._color = color
}
/**
* Get the raw color as a hex string.
* @return {string} Color as hex string: #RRGGBB
*/
get hex() {
return converter.rgb.hex(this._color)
}
/**
* Get the red component.
* @return {number} Red component [0-255]
*/
get red() {
return this._color[0]
}
/**
* Get the green component.
* @return {number} Green component [0-255]
*/
get green() {
return this._color[1]
}
/**
* Get the blue component.
* @return {number} Blue component [0-255]
*/
get blue() {
return this._color[2]
}
/**
* Get the current style object
* @return {style} The currently set styles
*/
get style() {
let styles = ''
Object.keys(this.styles).forEach(key_ => {
if (this.styles[key_]) {
const space = (styles === '') ? '' : ' '
styles += (key_ === 'color') ? '' : `${space}${key_}`
}
})
return (styles === '') ? undefined : styles
}
/**
* Set the current style
* @param {style | array} styles The desired styles as an object or array of keys.
*/
set style(styles) {
this.styles = parseStyles(styles)
this.colorSGR = ('color' in this.styles) ?
parseColor(this.styles.color, this._depth, this.styles.background) :
this.colorSGR
this.styleSGR = setStyles(this.styles)
this._color = ('color' in this.styles) ? this.styles.color : this._color
}
/**
* Get the current style as an array of keys.
* @return {string[]} An array of style keys.
*/
get styleArray() {
const styles = []
Object.keys(this.styles).forEach(key_ => (this.styles[key_] === true) && styles.push(key_))
return styles
}
/**
* Render and SGRColor object.
* @param {object} exclusions - Styles to exclude from render.
* @return {SGRColor} The rendered SGRColor object.
*/
sgr(exclusions) {
const styleSGRtemp = (exclusions === undefined) ?
this.styleSGR :
setStyles(this.styles, parseStyles(exclusions))
const inJoin = (this.colorSGR.in !== '' && styleSGRtemp.in !== '') ? ';' : ''
const outJoin = (this.colorSGR.out !== '' && styleSGRtemp.out !== '') ? ';' : ''
const output = {
in: `${_SGRparts.start}${this.colorSGR.in}${inJoin}${styleSGRtemp.in}${_SGRparts.end}`,
out: `${_SGRparts.start}${styleSGRtemp.out}${outJoin}${this.colorSGR.out}${_SGRparts.end}`
}
Object.defineProperty(output, 'toString', {value: () => output.in})
return output
}
}