/
StyleMe.js
331 lines (299 loc) · 9.53 KB
/
StyleMe.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* eslint-disable no-extend-native */
const STYLES = require('../styles/Styles.js')
let IS_EXTENDED = false
/**
* StyleMe
*
* Static class that is used to easily add colors to console outputs.
*/
class StyleMe {
/**
* Styles the given string according to the given style codes.
* @param {String} str - String to style
* @param {String|Array} styleCodes - Comma seperated list (or array) of style codes.
* @returns {String} Styled output
*
* @throws Will throw error if an invalid style code is provided.
*/
static style (str, styleCodes) {
const output = []
const styles = Array.isArray(styleCodes) ? styleCodes : styleCodes.split(',')
const specials = []
styles.forEach((styleCode) => {
styleCode = styleCode.trim()
if (Object.hasOwnProperty.call(STYLES, styleCode)) {
const styleArray = Array.isArray(STYLES[styleCode]) ? STYLES[styleCode] : [STYLES[styleCode]]
styleArray.forEach((style) => {
switch (typeof style) {
case 'function':
specials.push(style)
break
case 'string':
output.push(style)
}
})
} else {
throw Error(`Style code '${styleCode}' not found!`)
}
})
if (specials.length !== 0) {
for (let i = 0; i < str.length; i++) {
let char = str.charAt(i)
specials.forEach((styleFn) => {
char = styleFn(char, i, STYLES)
})
output.push(char)
}
} else {
output.push(str)
}
output.push('\x1b[0m')
return output.join('')
}
/**
* Styles a string with inline style markers
* @param {String} str - String to style with inline style markers
* @returns {String} Returns the styled string
*
* @throws Will give error if braces are mismatched.
*/
static styleInline (str) {
const codeStack = []
const output = []
let currentStyle = ''
let outputIndex = 0
const currentSpecials = []
function updateCurrent () {
currentSpecials.length = 0
let top = 0
const flattenedArray = []
const styleStrings = []
codeStack.forEach((styles) => {
styles.forEach((style) => {
const index = flattenedArray.indexOf(style)
if (index !== -1) {
flattenedArray.splice(index, 1)
}
flattenedArray.push(style)
})
})
for (let i = flattenedArray.length - 1; i >= 0; i--) {
if (typeof flattenedArray[i] !== 'function') {
top = i
break
}
}
flattenedArray.forEach((style, i) => {
if (typeof style === 'function') {
if (i >= top) {
currentSpecials.push(style)
}
} else {
styleStrings.push(style)
}
})
currentStyle = styleStrings.join('')
}
for (let i = 0; i < str.length; i++) {
let char = str.charAt(i)
let isEscaped = i !== 0 && str.charAt(i - 1) === '\\'
const isOpening = char === '{'
const isClosing = char === '}'
if (isEscaped && (isOpening || isClosing)) {
isEscaped = i < 2 || str.charAt(i - 2) !== '\\'
output.pop()
outputIndex--
}
if (!isEscaped && isOpening && i >= 3) {
const bracketClose = str.lastIndexOf(']', i - 1)
const bracketOpen = str.lastIndexOf('[', i - 1)
if (bracketOpen === -1) {
throw new Error(`Opening square bracket not found! Index: ${i}`)
}
if (bracketClose === -1) {
throw new Error(`Closing square bracket not found! Index: ${i}`)
}
if (bracketClose <= bracketOpen) {
throw new Error(`Invalid brackets at ${bracketOpen}-${bracketClose}`)
}
if (/\S/.test(str.substring(bracketClose + 1, i))) {
throw new Error(`Expected only whitespace at ${bracketClose + 1} but found '${str.substring(bracketClose + 1, i)}'`)
}
const codes = str.substring(bracketOpen + 1, bracketClose).split(',').map(c => c.trim())
const stylesArray = []
codes.forEach((code) => {
if (!Object.hasOwnProperty.call(STYLES, code)) {
throw new Error(`Style code '${code}' not found! Index: ${i}`)
}
if (Array.isArray(STYLES[code])) {
STYLES[code].forEach((style) => {
stylesArray.push(style)
})
} else {
stylesArray.push(STYLES[code])
}
})
codeStack.push(stylesArray)
output.length = output.length - (i - bracketOpen)
outputIndex = outputIndex - (i - bracketOpen)
char = stylesArray.filter(style => typeof style === 'string').join('')
updateCurrent()
} else if (!isEscaped && isClosing) {
if (codeStack.length === 0) {
throw new Error(`Extra closing brace found at position ${i}`)
}
codeStack.pop()
updateCurrent()
char = '\x1b[0m' + currentStyle
} else {
const index = outputIndex++
currentSpecials.forEach((specialFn) => {
char = specialFn(char, index, STYLES)
})
}
output.push(char)
}
if (codeStack.length !== 0) {
throw new Error('Missing a closing brace!')
}
return output.join('')
}
/**
* Styles the given string according to the given style codes if given and uses inline style markers otherwise.
* @param {String} str - String to style
* @param {String|Array=} styleCodes - Optional comma seperated list (or array) of style codes.
* @returns {String} - Styled output
*
* @throws Will throw error if an invalid style code is provided or if braces are mismatched.
*/
static styleMe (str, styleCodes) {
if (styleCodes !== undefined) {
return this.style(str, styleCodes)
} else {
return this.styleInline(str)
}
}
/**
* Extends String.prototype with helper methods that can be used to easily style a string
*/
static extend () {
IS_EXTENDED = true
for (const name in STYLES) {
((code) => {
/**
* Styles the string according to the method's code
* @returns {String}
*/
String.prototype[code] = function () {
return StyleMe.style(this, code)
}
})(name)
}
/**
* Styles the string according to the given style codes if given and uses inline style markers otherwise.
* @param {String|Array=} styleCodes
* @returns {String} Styled string
*
* @throws Will throw error if an invalid style code is provided or if braces are mismatched.
*/
String.prototype.styleMe = function (styleCodes) {
return StyleMe.styleMe(this, styleCodes)
}
}
/**
* Flattens an array style into a single array
* @param {Array|String|Function} style
* @param {Array=} arr
* @returns {Array} Flattened array
* @throws Error if style code not found or if invalid
*/
static flattenStyle (style, arr) {
if (arr === undefined) {
arr = []
}
if (Array.isArray(style)) {
style.forEach((styleCode) => {
if (typeof styleCode === 'string') {
if (!Object.hasOwnProperty.call(STYLES, styleCode)) {
throw new Error(`Style code '${styleCode}' not found`)
}
this.flattenStyle(STYLES[styleCode], arr)
} else if (typeof styleCode === 'function') {
arr.push(styleCode)
} else if (Array.isArray(styleCode)) {
this.flattenStyle(styleCode, arr)
} else {
throw new Error('Style must be a string code, a function, or an array!')
}
})
} else {
arr.push(style)
}
return arr
}
/**
* A function that adds styling to individual characters
* @callback SpecialStyleFn
* @param {char} char - Character involved
* @param {number} index - Index of the character
* @param {Object} styles - Object containing the styles as properties.
* @returns Should return the styled character.
*/
/**
* Adds a style
* @param {String} styleCode - style code string (alphabetical string, no spaces)
* @param {String|SpecialStyleFn} style - Style string or function
*/
static addStyle (styleCode, style) {
if (styleCode.length === 0 || !/^[a-zA-Z]+$/.test(styleCode)) {
throw Error('Style code must be an alphabetical string with no spaces')
}
if (Object.hasOwnProperty.call(STYLES, styleCode)) {
throw Error(`Style with style code '${styleCode}' already exists!`)
}
STYLES[styleCode] = Array.isArray(style) ? this.flattenStyle(style) : style
if (IS_EXTENDED) {
/**
* Styles the string according to the method's code
* @returns {String}
*/
String.prototype[styleCode] = function () {
return StyleMe.style(this, styleCode)
}
}
}
/**
* Sets a theme
* @param {Object} theme
*/
static setTheme (theme) {
for (const styleCode in theme) {
this.addStyle(styleCode, [theme[styleCode]])
}
}
}
/**
* Wraps objects with a Proxy for code chaining
* @param {Object} target
* @param {Array} styles
* @returns
*/
function recursiveProxy (target, styles) {
if (styles === undefined) styles = []
return new Proxy(target, {
get: (target, prop, receiver) => {
if (Object.hasOwnProperty.call(target, prop)) {
return target[prop]
}
if (STYLES[prop] !== undefined) {
const newstyle = styles.slice(0)
newstyle.push(prop)
return recursiveProxy((str) => {
return StyleMe.style(str, newstyle)
}, newstyle)
}
}
})
}
const proxy = recursiveProxy(StyleMe)
module.exports = proxy