-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
214 lines (207 loc) · 5.91 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
/**
* Tagged template literal for styling `console.log` statements.
* @see [Styling console output on MDN](https://developer.mozilla.org/en-US/docs/Web/API/console#styling_console_output)
* @example
* // Basic
* console.log(
* ...sttyl`Print ${"bold"}.b/ and ${"italic red"}.i.red/.`,
* );
* // Dynamic Styles
* const style1 = ".b.cyan.u/";
* console.log(
* ...sttyl`Some ${"dynamically styled text"}${style1}.`,
* );
* // Custom Styles
* const styles = {
* s1: `color: magenta; font-weight: bold`,
* s2: `color: orange; font-style: italic`,
* };
* const myStyle = sttyl.with(styles);
* console.log(
* ...myStyle`${"And"}.s1/ ${"custom styles"}.s2.u/.`,
* );
* // Default Styles
* {
* // Colors
* black: "color: black",
* blue: "color: blue",
* cyan: "color: cyan",
* gray: "color: gray",
* green: "color: green",
* grey: "color: grey",
* magenta: "color: magenta",
* red: "color: red",
* white: "color: white",
* yellow: "color: yellow",
* // Formatting
* b: "font-weight: bold",
* bold: "font-weight: bold",
* i: "font-style: italic",
* italic: "font-style: italic",
* s: "text-decoration: line-through",
* strike: "text-decoration: line-through",
* u: "text-decoration: underline",
* underline: "text-decoration: underline",
* }
*/
export interface sttyl {
(strings: TemplateStringsArray, ...args: unknown[]): string[];
/**
* Creates a sttyl function that mixes your custom styles in with the default
* styles. See main `sttyl` docs for a list of default styles.
* @example
* const styles = {
* s1: `color: magenta; font-weight: bold`,
* s2: `color: orange; font-style: italic`,
* };
* const myStyle = sttyl.with(styles);
* console.log(
* ...myStyle`Log ${"some"}.s1/ ${"custom styles"}.s2.u/.`,
* );
*/
with: typeof styledWith;
}
const defaultStyles: Record<string, string> = {
// #region Colors
black: "color: black",
blue: "color: blue",
cyan: "color: cyan",
gray: "color: gray",
green: "color: green",
grey: "color: grey",
magenta: "color: magenta",
red: "color: red",
white: "color: white",
yellow: "color: yellow",
// #endregion
// #region Formatting
b: "font-weight: bold",
bold: "font-weight: bold",
i: "font-style: italic",
italic: "font-style: italic",
s: "text-decoration: line-through",
strike: "text-decoration: line-through",
u: "text-decoration: underline",
underline: "text-decoration: underline",
// #endregion
};
function styledWith(styles: Record<string, string>) {
if (styles !== defaultStyles) {
styles = {
...defaultStyles,
...styles,
};
}
function styledTag(strings: TemplateStringsArray, ...args: unknown[]) {
/** Message parts to be joined for 1st string argument of `console.log`. */
const msg: string[] = [];
/** CSS style arguments for `console.log`. */
const css: string[] = [];
const { length: stringsLen } = strings;
for (let strIdx = 0; strIdx < stringsLen; strIdx++) {
const str = parseStyleCodes(strings[strIdx]);
let arg = args[strIdx];
if (typeof arg === "string") {
arg = parseStyleCodes(arg);
} else {
arg = arg ?? "";
}
msg.push(str);
msg.push(arg as string);
}
/** Return array for spreading into a call to `console.log(...)`. */
const rv = [msg.join(""), ...css];
// console.log("STYLED", rv);
return rv;
function parseStyleCodes(str: string) {
if (str.startsWith(".")) {
//
// Match a period, followed by non-whitespace up to the first "/".
//
const match = str.match(/^.[^\s/]+\/{1}/);
if (!match) {
return str;
}
const frame = match[0].toString();
const code = frame.substring(1, frame.length - 1);
// console.log("MATCH: frame", frame, "code", code);
// If it's more than just the period...
if (code) {
// Split the code into keys (keyof styles), skipping the first dot.
const keys = code.split(".");
const { length: keysLen } = keys;
let style = "";
for (let keyIdx = 0; keyIdx < keysLen; keyIdx++) {
const key = keys[keyIdx];
// Join all keys found in styles into one style string.
if (key in styles) {
style += styles[key] + ";";
}
}
if (style) {
// We only apply the `code` found above if it produced a style.
// Otherwise, it's displayed as part of `msg` (below).
const prev = msg.length - 1;
msg[prev] = "%c" + msg[prev] + "%c";
str = str.substring(frame.length);
css.push(style);
css.push("");
}
}
}
return str;
}
}
return styledTag;
}
export const sttyl = styledWith(defaultStyles) as sttyl;
sttyl.with = styledWith;
export default sttyl;
// #region Testing
// console.log("");
// console.log(
// "Testing %csttyl%c...",
// `${defaultStyles.b};${defaultStyles.u};${defaultStyles.cyan}`,
// "",
// );
// console.log("");
// //
// // Basic
// //
// console.log(
// ...sttyl`Hello, here is some ${"RED"}.b.i.red.u//&${"YELLOW"}.yellow/ text.`,
// {
// and: "some args",
// more: "and some more args",
// },
// );
// console.log("");
// //
// // Dynamic Styles
// //
// const style1 = ".b.i.cyan.u/";
// const style2 = ".b.green/";
// console.log(
// ...sttyl`...and some ${"dynamically"}${style1} ${"styled"}${style2} text.`,
// );
// console.log("");
// //
// // Custom Styles
// //
// const styles = {
// s1: `color: magenta; font-weight: bold`,
// s2: `color: orange; font-style: italic`,
// };
// const myStyle = sttyl.with(styles);
// console.log(
// ...myStyle`...and ${"text"}.s1/ ${"with custom style names"}.s2.u/.`,
// );
// console.log("");
// //
// // Done
// //
// console.log(
// ...sttyl`Testing ${"sttyl"}.cyan.b.u/ completed.`,
// );
// console.log("");
// #endregion