-
Notifications
You must be signed in to change notification settings - Fork 4
/
mod.ts
243 lines (222 loc) · 7.32 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
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
import * as color from "https://deno.land/std@0.190.0/fmt/colors.ts";
/** @type Color is the sum type of all available colors */
export type Color =
| number
| "black"
| "red"
| "blue"
| "green"
| "yellow"
| "magenta"
| "cyan"
| "white"
| "brightBlack"
| "brightRed"
| "brightBlue"
| "brightGreen"
| "brightYellow"
| "brightMagenta"
| "brightCyan"
| "brightWhite";
/** @type Style is the sum type of all available styles */
export type Style =
| "italic"
| "underline"
| "bold"
| "inverse"
| "dim"
| "strike";
/** @type ColorType is a map of @type Color and `(str:string) => string` */
type ColorType = { [c in Color]: (str: string) => string };
/** @type StyleType is a map of @type Style and `(str:string) => string` */
type StyleType = { [s in Style]: (str: string) => string };
/**
* @interface BadgeOptions is an interface which is configuartion
* for the formation of the `badge`.
* @property msgBg - Bg of the msg. Default: `brightBlack`
* @property labelBg - Bg of the label. Default: `blue`
* @property msgColor - Text Color of the msg. Default: `white`
* @property labelColor - Text Color of the label. Default: `white`
* @property msgStyle - Style of the msg. Default: `null`
* @property labelStyle - Style of the label. Default: `null`
* @property msgWidth - Width of the msg. Default: `msg.length + 2`
* @property labelWidth - Width of the label. Default: `label.length + 2`
* @property is_8bit - Flag for allowing 8 bit Colors. Default: `false`
* @property hyper_link - Hyperlink of the badge. Default: `null`
*/
export interface BadgeOptions {
msgBg: Color;
labelBg: Color;
msgColor: Color;
labelColor: Color;
msgStyle?: Style;
labelStyle?: Style;
msgWidth?: number;
labelWidth?: number;
is_8bit?: boolean;
hyper_link?: string;
}
const colorBgTypes: ColorType = {
black: (str: string) => color.bgBlack(str),
red: (str: string) => color.bgRed(str),
blue: (str: string) => color.bgBlue(str),
green: (str: string) => color.bgGreen(str),
yellow: (str: string) => color.bgYellow(str),
magenta: (str: string) => color.bgMagenta(str),
cyan: (str: string) => color.bgCyan(str),
white: (str: string) => color.bgWhite(str),
brightBlack: (str: string) => color.bgBrightBlack(str),
brightRed: (str: string) => color.bgBrightRed(str),
brightBlue: (str: string) => color.bgBrightBlue(str),
brightGreen: (str: string) => color.bgBrightGreen(str),
brightYellow: (str: string) => color.bgBrightYellow(str),
brightMagenta: (str: string) => color.bgBrightMagenta(str),
brightCyan: (str: string) => color.bgBrightCyan(str),
brightWhite: (str: string) => color.bgBrightWhite(str),
};
const colorTypes: ColorType = {
black: (str: string) => color.black(str),
red: (str: string) => color.red(str),
blue: (str: string) => color.blue(str),
green: (str: string) => color.green(str),
yellow: (str: string) => color.yellow(str),
magenta: (str: string) => color.magenta(str),
cyan: (str: string) => color.cyan(str),
white: (str: string) => color.white(str),
brightBlack: (str: string) => color.brightBlack(str),
brightRed: (str: string) => color.brightRed(str),
brightBlue: (str: string) => color.brightBlue(str),
brightGreen: (str: string) => color.brightGreen(str),
brightYellow: (str: string) => color.brightYellow(str),
brightMagenta: (str: string) => color.brightMagenta(str),
brightCyan: (str: string) => color.brightCyan(str),
brightWhite: (str: string) => color.brightWhite(str),
};
const styleTypes: StyleType = {
bold: (str: string) => color.bold(str),
italic: (str: string) => color.italic(str),
inverse: (str: string) => color.inverse(str),
dim: (str: string) => color.dim(str),
strike: (str: string) => color.strikethrough(str),
underline: (str: string) => color.underline(str),
};
/** @function padd returns the padded `string` according to `width` provided */
function padd(str: string, width?: number): string {
if (!width) width = str.length + 2; // one space on each side
const halfWith = Math.ceil((width - str.length) / 2);
const paddStr = " ".repeat(halfWith);
return `${paddStr}${str}${paddStr}`;
}
/** @function getBgColor gets the Bg color according to @type Color */
function getBgColor(
colr?: Color,
is_8bit?: boolean,
): (str: string) => string {
if (!colr) {
return color.bgBrightBlack;
}
// needed for custom 24 bit and 8 bit RBG Colors
if (typeof colr === "number") {
// no limit check as color.bgRbg8 handles it
if (is_8bit) {
return (str: string) => color.bgRgb8(str, colr);
}
// no limit check as color.bgRbg24 handles it
return (str: string) => color.bgRgb24(str, colr);
}
return colorBgTypes[colr];
}
/** @function getTextColor gets the text color according to @type Color */
function getTextColor(
colr?: Color,
is_8bit?: boolean,
): (str: string) => string {
if (!colr) {
return color.white;
}
// needed for custom 24 bit and 8 bit RBG Colors
if (typeof colr === "number") {
// no limit check as color.rbg8 handles it
if (is_8bit) {
return (str: string) => color.rgb8(str, colr);
}
// no limit check as color.bgRbg24 handles it
return (str: string) => color.rgb24(str, colr);
}
return colorTypes[colr];
}
/** @function format formates the badge according to @type Style */
function format(
str: string,
StyleName?: Style,
): string {
if (!StyleName) return str;
const Styler = styleTypes[StyleName];
if (Styler) {
return Styler(str);
}
return str;
}
/** @function hyperlink makes a hyperlink */
function hyperlink(url: string, text: string): string {
// spaces because some terminals may mismatch the link
return `\u001B]8;; ${url} \u0007 ${text}\u001B]8;;\u0007`;
}
/** @var DEFAULT_OPTIONS are the default args passed to @function `badge`
* @property msgBg: `blue`
* @property labelBg: `brightBlack`
* @property msgColor: `white`
* @property labelColor: `white`
*/
export const DEFAULT_OPTIONS: Partial<BadgeOptions> = {
msgBg: "blue",
labelBg: "brightBlack",
msgColor: "white",
labelColor: "white",
};
/** @function badge returns the `string` repr of the `badge`
* @param label - label of the badge.
* @param msg - message of the badge.
* @param opts - options for the @interface BadgeOptions of the badge. Default: @var DEFAULT_OPTIONS
*/
export function badge(
label = "",
msg = "",
opts: Partial<BadgeOptions> = DEFAULT_OPTIONS,
) {
const {
labelBg,
labelColor,
labelWidth,
labelStyle,
msgWidth,
msgColor,
msgBg,
msgStyle,
is_8bit,
hyper_link,
} = opts;
const lblStr = padd(label, labelWidth);
const msgStr = padd(msg, msgWidth);
let msgColored: string, lblColored: string;
// needs to be checked for safety
if (!labelBg) {
lblColored = getTextColor(labelColor, is_8bit)(color.bgBrightBlack(lblStr));
} else {
lblColored = getTextColor(labelColor, is_8bit)(
getBgColor(labelBg, is_8bit)(lblStr),
);
}
if (!msgBg) {
msgColored = getTextColor(msgColor, is_8bit)(color.bgBlue(msgStr));
} else {
msgColored = getTextColor(msgColor, is_8bit)(
getBgColor(msgBg, is_8bit)(msgStr),
);
}
const label_Style = format(lblColored, labelStyle);
const msg_Style = format(msgColored, msgStyle);
const badge = `${label && label_Style}${msg && msg_Style} `;
// create hyperlink if provided
return hyper_link ? hyperlink(hyper_link, badge) : badge;
}