summaryrefslogtreecommitdiff
path: root/colors/styles.ts
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2018-12-19 15:30:44 +1100
committerRyan Dahl <ry@tinyclouds.org>2018-12-18 23:30:44 -0500
commita3d164df917536c702775af1937569825fc5ceaf (patch)
tree13442b0e8d95049d9220c4477e3da7a941768f1a /colors/styles.ts
parent2d58da520fffaeaee1bceeb33b6e3dc339ea68a3 (diff)
Add colors module (denoland/deno_std#30)
Original: https://github.com/denoland/deno_std/commit/54787f172c62df7c98d8e9e534c40e317825e614
Diffstat (limited to 'colors/styles.ts')
-rw-r--r--colors/styles.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/colors/styles.ts b/colors/styles.ts
new file mode 100644
index 000000000..74f609b83
--- /dev/null
+++ b/colors/styles.ts
@@ -0,0 +1,66 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+const matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
+function escapeStringRegexp(str: string): string {
+ return str.replace(matchOperatorsRe, "\\$&");
+}
+
+const codes = {
+ reset: [0, 0],
+ bold: [1, 22],
+ dim: [2, 22],
+ italic: [3, 23],
+ underline: [4, 24],
+ inverse: [7, 27],
+ hidden: [8, 28],
+ strikethrough: [9, 29],
+
+ black: [30, 39],
+ red: [31, 39],
+ green: [32, 39],
+ yellow: [33, 39],
+ blue: [34, 39],
+ magenta: [35, 39],
+ cyan: [36, 39],
+ white: [37, 39],
+
+ blackBright: [90, 39],
+ redBright: [91, 39],
+ greenBright: [92, 39],
+ yellowBright: [93, 39],
+ blueBright: [94, 39],
+ magentaBright: [95, 39],
+ cyanBright: [96, 39],
+ whiteBright: [97, 39],
+
+ bgBlack: [40, 49],
+ bgRed: [41, 49],
+ bgGreen: [42, 49],
+ bgYellow: [43, 49],
+ bgBlue: [44, 49],
+ bgMagenta: [45, 49],
+ bgCyan: [46, 49],
+ bgWhite: [47, 49],
+
+ bgBlackBright: [100, 49],
+ bgRedBright: [101, 49],
+ bgGreenBright: [102, 49],
+ bgYellowBright: [103, 49],
+ bgBlueBright: [104, 49],
+ bgMagentaBright: [105, 49],
+ bgCyanBright: [106, 49],
+ bgWhiteBright: [107, 49]
+};
+
+type Styles<T> = {
+ [S in keyof T]: { open: string; close: string; closeRe: RegExp }
+};
+
+export const styles: Styles<typeof codes> = {} as any;
+
+for (const [style, [open, close]] of Object.entries(codes)) {
+ styles[style] = {
+ open: `\u001b[${open}m`,
+ close: `\u001b[${close}m`,
+ closeRe: new RegExp(escapeStringRegexp(`\u001b[${close}m`), "g")
+ };
+}