-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoxyColor.js
More file actions
127 lines (107 loc) · 3.75 KB
/
foxyColor.js
File metadata and controls
127 lines (107 loc) · 3.75 KB
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
/**
* @summary A class for styling console text with ANSI escape codes
* @description Implement terminal color based on :
* - ECMA-48, 5th Edition (Juin 1991), Section 8.3.117
* - AIXterm bright colors extension (codes 90-97, 100-107), an IBM extension that became a de facto standard
* @see https://ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf
* @property {Object} ANSI The ANSI escape codes for styling text
*/
export default class FoxyColor {
static ANSI = {
reset: "\x1b[0m",
STYLE: {
bold: "\x1b[1m",
faint: "\x1b[2m",
italic: "\x1b[3m",
underline: "\x1b[4m",
slowBlink: "\x1b[5m",
rapidBlink: "\x1b[6m", // ⬅ rarely supported
reverse: "\x1b[7m",
concealed: "\x1b[8m",
strike: "\x1b[9m"
},
foreground: {
classic:{
black: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
white: "\x1b[37m"
},
bright: {
black: "\x1b[90m",
red: "\x1b[91m",
green: "\x1b[92m",
yellow: "\x1b[93m",
blue: "\x1b[94m",
magenta: "\x1b[95m",
cyan: "\x1b[96m",
white: "\x1b[97m"
}
},
background: {
classic:{
black: "\x1b[40m",
red: "\x1b[41m",
green: "\x1b[42m",
yellow: "\x1b[43m",
blue: "\x1b[44m",
magenta: "\x1b[45m",
cyan: "\x1b[46m",
white: "\x1b[47m"
},
bright: {
black: "\x1b[100m",
red: "\x1b[101m",
green: "\x1b[102m",
yellow: "\x1b[103m",
blue: "\x1b[104m",
magenta: "\x1b[105m",
cyan: "\x1b[106m",
white: "\x1b[107m"
}
}
}
/**
* @summary Styles the given text with the given style options
* @description Styles the given text according to the given style options with ANSI escape codes.
* Then, it resets the style at the end of the text.
* @see FoxyColor.ANSI for available styles, foreground colors and background colors.
* @param {String} text The text to style
* @param {FoxyStyleOptions} styleOptions The style options
* @returns {String} The styled text
* @throws {Error} If styleOptions is not provided
* @see FoxyColor.ANSI
*/
static style(text,styleOptions) {
if(!styleOptions) throw new Error("styleOptions is required");
const defaultStyleOptions = {
foregroundColor: "",
backgroundColor: "",
style: ""
};
styleOptions = {
...defaultStyleOptions,
...styleOptions
};
const outputMessageString = `${styleOptions.style}${styleOptions.backgroundColor}${styleOptions.foregroundColor}${text}${FoxyColor.ANSI.reset}`;
return outputMessageString;
}
}
/**
* @typedef FoxyStyleOptions
* @property {String} foregroundColor The foreground color (one of {@link FoxyColor.ANSI.foreground})
* @property {String} backgroundColor The background color (one of {@link FoxyColor.ANSI.background})
* @property {String} style The style (one of {@link FoxyColor.ANSI.style})
*/
/**
* @see FoxyColor.style
* @param {FoxyStyleOptions} styleOptions
* @returns
*/
String.prototype.foxyStyle = function(styleOptions) {
return FoxyColor.style(this,styleOptions);
};