summaryrefslogtreecommitdiff
path: root/fmt
diff options
context:
space:
mode:
Diffstat (limited to 'fmt')
-rw-r--r--fmt/colors.ts148
-rw-r--r--fmt/colors_test.ts121
2 files changed, 269 insertions, 0 deletions
diff --git a/fmt/colors.ts b/fmt/colors.ts
new file mode 100644
index 000000000..52ba22487
--- /dev/null
+++ b/fmt/colors.ts
@@ -0,0 +1,148 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+/**
+ * A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
+ * on npm.
+ *
+ * ```
+ * import { bgBlue, red, bold } from "https://deno.land/std/fmt/colors.ts";
+ * console.log(bgBlue(red(bold("Hello world!"))));
+ * ```
+ *
+ * This module supports `NO_COLOR` environmental variable disabling any coloring
+ * if `NO_COLOR` is set.
+ */
+const { noColor } = Deno;
+
+interface Code {
+ open: string;
+ close: string;
+ regexp: RegExp;
+}
+
+let enabled = !noColor;
+
+export function setEnabled(value: boolean): void {
+ if (noColor) {
+ return;
+ }
+
+ enabled = value;
+}
+
+export function getEnabled(): boolean {
+ return enabled;
+}
+
+function code(open: number, close: number): Code {
+ return {
+ open: `\x1b[${open}m`,
+ close: `\x1b[${close}m`,
+ regexp: new RegExp(`\\x1b\\[${close}m`, "g")
+ };
+}
+
+function run(str: string, code: Code): string {
+ return enabled
+ ? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
+ : str;
+}
+
+export function reset(str: string): string {
+ return run(str, code(0, 0));
+}
+
+export function bold(str: string): string {
+ return run(str, code(1, 22));
+}
+
+export function dim(str: string): string {
+ return run(str, code(2, 22));
+}
+
+export function italic(str: string): string {
+ return run(str, code(3, 23));
+}
+
+export function underline(str: string): string {
+ return run(str, code(4, 24));
+}
+
+export function inverse(str: string): string {
+ return run(str, code(7, 27));
+}
+
+export function hidden(str: string): string {
+ return run(str, code(8, 28));
+}
+
+export function strikethrough(str: string): string {
+ return run(str, code(9, 29));
+}
+
+export function black(str: string): string {
+ return run(str, code(30, 39));
+}
+
+export function red(str: string): string {
+ return run(str, code(31, 39));
+}
+
+export function green(str: string): string {
+ return run(str, code(32, 39));
+}
+
+export function yellow(str: string): string {
+ return run(str, code(33, 39));
+}
+
+export function blue(str: string): string {
+ return run(str, code(34, 39));
+}
+
+export function magenta(str: string): string {
+ return run(str, code(35, 39));
+}
+
+export function cyan(str: string): string {
+ return run(str, code(36, 39));
+}
+
+export function white(str: string): string {
+ return run(str, code(37, 39));
+}
+
+export function gray(str: string): string {
+ return run(str, code(90, 39));
+}
+
+export function bgBlack(str: string): string {
+ return run(str, code(40, 49));
+}
+
+export function bgRed(str: string): string {
+ return run(str, code(41, 49));
+}
+
+export function bgGreen(str: string): string {
+ return run(str, code(42, 49));
+}
+
+export function bgYellow(str: string): string {
+ return run(str, code(43, 49));
+}
+
+export function bgBlue(str: string): string {
+ return run(str, code(44, 49));
+}
+
+export function bgMagenta(str: string): string {
+ return run(str, code(45, 49));
+}
+
+export function bgCyan(str: string): string {
+ return run(str, code(46, 49));
+}
+
+export function bgWhite(str: string): string {
+ return run(str, code(47, 49));
+}
diff --git a/fmt/colors_test.ts b/fmt/colors_test.ts
new file mode 100644
index 000000000..949bc34e7
--- /dev/null
+++ b/fmt/colors_test.ts
@@ -0,0 +1,121 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { test } from "../testing/mod.ts";
+import { assertEquals } from "../testing/asserts.ts";
+import * as c from "./colors.ts";
+import "../examples/colors.ts";
+
+test(function singleColor(): void {
+ assertEquals(c.red("foo bar"), "foo bar");
+});
+
+test(function doubleColor(): void {
+ assertEquals(c.bgBlue(c.red("foo bar")), "foo bar");
+});
+
+test(function replacesCloseCharacters(): void {
+ assertEquals(c.red("Hello"), "Hello");
+});
+
+test(function enablingColors(): void {
+ assertEquals(c.getEnabled(), true);
+ c.setEnabled(false);
+ assertEquals(c.bgBlue(c.red("foo bar")), "foo bar");
+ c.setEnabled(true);
+ assertEquals(c.red("foo bar"), "foo bar");
+});
+
+test(function testBold(): void {
+ assertEquals(c.bold("foo bar"), "foo bar");
+});
+
+test(function testDim(): void {
+ assertEquals(c.dim("foo bar"), "foo bar");
+});
+
+test(function testItalic(): void {
+ assertEquals(c.italic("foo bar"), "foo bar");
+});
+
+test(function testUnderline(): void {
+ assertEquals(c.underline("foo bar"), "foo bar");
+});
+
+test(function testInverse(): void {
+ assertEquals(c.inverse("foo bar"), "foo bar");
+});
+
+test(function testHidden(): void {
+ assertEquals(c.hidden("foo bar"), "foo bar");
+});
+
+test(function testStrikethrough(): void {
+ assertEquals(c.strikethrough("foo bar"), "foo bar");
+});
+
+test(function testBlack(): void {
+ assertEquals(c.black("foo bar"), "foo bar");
+});
+
+test(function testRed(): void {
+ assertEquals(c.red("foo bar"), "foo bar");
+});
+
+test(function testGreen(): void {
+ assertEquals(c.green("foo bar"), "foo bar");
+});
+
+test(function testYellow(): void {
+ assertEquals(c.yellow("foo bar"), "foo bar");
+});
+
+test(function testBlue(): void {
+ assertEquals(c.blue("foo bar"), "foo bar");
+});
+
+test(function testMagenta(): void {
+ assertEquals(c.magenta("foo bar"), "foo bar");
+});
+
+test(function testCyan(): void {
+ assertEquals(c.cyan("foo bar"), "foo bar");
+});
+
+test(function testWhite(): void {
+ assertEquals(c.white("foo bar"), "foo bar");
+});
+
+test(function testGray(): void {
+ assertEquals(c.gray("foo bar"), "foo bar");
+});
+
+test(function testBgBlack(): void {
+ assertEquals(c.bgBlack("foo bar"), "foo bar");
+});
+
+test(function testBgRed(): void {
+ assertEquals(c.bgRed("foo bar"), "foo bar");
+});
+
+test(function testBgGreen(): void {
+ assertEquals(c.bgGreen("foo bar"), "foo bar");
+});
+
+test(function testBgYellow(): void {
+ assertEquals(c.bgYellow("foo bar"), "foo bar");
+});
+
+test(function testBgBlue(): void {
+ assertEquals(c.bgBlue("foo bar"), "foo bar");
+});
+
+test(function testBgMagenta(): void {
+ assertEquals(c.bgMagenta("foo bar"), "foo bar");
+});
+
+test(function testBgCyan(): void {
+ assertEquals(c.bgCyan("foo bar"), "foo bar");
+});
+
+test(function testBgWhite(): void {
+ assertEquals(c.bgWhite("foo bar"), "foo bar");
+});