From 4d997d4e2239adb0d982245271e43938443e871f Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 24 Aug 2019 10:38:18 -0700 Subject: Move colors to fmt (denoland/deno_std#571) Original: https://github.com/denoland/deno_std/commit/28e77389ff409814c9da81d767458b36534f095a --- .ci/check_source_file_changes.ts | 2 +- colors/README.md | 31 -------- colors/mod.ts | 136 ----------------------------------- colors/test.ts | 121 -------------------------------- examples/catj.ts | 2 +- examples/colors.ts | 2 +- fmt/colors.ts | 148 +++++++++++++++++++++++++++++++++++++++ fmt/colors_test.ts | 121 ++++++++++++++++++++++++++++++++ log/handlers.ts | 2 +- testing/asserts.ts | 2 +- testing/asserts_test.ts | 2 +- testing/mod.ts | 2 +- ws/README.md | 2 +- ws/example_client.ts | 2 +- 14 files changed, 278 insertions(+), 297 deletions(-) delete mode 100644 colors/README.md delete mode 100644 colors/mod.ts delete mode 100644 colors/test.ts create mode 100644 fmt/colors.ts create mode 100644 fmt/colors_test.ts diff --git a/.ci/check_source_file_changes.ts b/.ci/check_source_file_changes.ts index e96a16b62..6aac5d645 100644 --- a/.ci/check_source_file_changes.ts +++ b/.ci/check_source_file_changes.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { xrun } from "../prettier/util.ts"; -import { red, green } from "../colors/mod.ts"; +import { red, green } from "../fmt/colors.ts"; /** * Checks whether any source file is changed since the given start time. diff --git a/colors/README.md b/colors/README.md deleted file mode 100644 index 7a02d9efb..000000000 --- a/colors/README.md +++ /dev/null @@ -1,31 +0,0 @@ -# colors - -Is a basic console color module intended for [Deno](https://deno.land/). It is -inspired by [chalk](https://www.npmjs.com/package/chalk), -[kleur](https://www.npmjs.com/package/kleur), and -[colors](https://www.npmjs.com/package/colors) on npm. - -## Usage - -The main modules exports several functions which can color the output to the -console: - -```ts -import { bgBlue, red, bold } from "https://deno.land/std/colors/mod.ts"; - -console.log(bgBlue(red(bold("Hello world!")))); -``` - -This module supports `NO_COLOR` environmental variable disabling any coloring if `NO_COLOR` is set. - -## TODO - -- Currently, it just assumes it is running in an environment that supports ANSI - escape code terminal coloring. It should actually detect, specifically windows - and adjust properly. - -- Test coverage is very basic at the moment. - ---- - -Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. diff --git a/colors/mod.ts b/colors/mod.ts deleted file mode 100644 index d0b4bc320..000000000 --- a/colors/mod.ts +++ /dev/null @@ -1,136 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -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/colors/test.ts b/colors/test.ts deleted file mode 100644 index dec246fb0..000000000 --- a/colors/test.ts +++ /dev/null @@ -1,121 +0,0 @@ -// 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 "./mod.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"); -}); diff --git a/examples/catj.ts b/examples/catj.ts index 79a129780..cbe06d4b8 100644 --- a/examples/catj.ts +++ b/examples/catj.ts @@ -8,7 +8,7 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ import { parse } from "../flags/mod.ts"; -import * as colors from "../colors/mod.ts"; +import * as colors from "../fmt/colors.ts"; const decoder = new TextDecoder(); diff --git a/examples/colors.ts b/examples/colors.ts index 9468e8768..6f03355c6 100644 --- a/examples/colors.ts +++ b/examples/colors.ts @@ -1,4 +1,4 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { bgBlue, red, bold, italic } from "../colors/mod.ts"; +import { bgBlue, red, bold, italic } from "../fmt/colors.ts"; console.log(bgBlue(italic(red(bold("Hello world!"))))); 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"); +}); diff --git a/log/handlers.ts b/log/handlers.ts index 1f68e9794..5dfd0caa4 100644 --- a/log/handlers.ts +++ b/log/handlers.ts @@ -4,7 +4,7 @@ type File = Deno.File; type Writer = Deno.Writer; import { getLevelByName, LogLevel } from "./levels.ts"; import { LogRecord } from "./logger.ts"; -import { red, yellow, blue, bold } from "../colors/mod.ts"; +import { red, yellow, blue, bold } from "../fmt/colors.ts"; const DEFAULT_FORMATTER = "{levelName} {msg}"; type FormatterFunction = (logRecord: LogRecord) => string; diff --git a/testing/asserts.ts b/testing/asserts.ts index 56480c95b..adf77f3a7 100644 --- a/testing/asserts.ts +++ b/testing/asserts.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { red, green, white, gray, bold } from "../colors/mod.ts"; +import { red, green, white, gray, bold } from "../fmt/colors.ts"; import diff, { DiffType, DiffResult } from "./diff.ts"; import { format } from "./format.ts"; diff --git a/testing/asserts_test.ts b/testing/asserts_test.ts index ba07b3a83..b480fe7c9 100644 --- a/testing/asserts_test.ts +++ b/testing/asserts_test.ts @@ -15,7 +15,7 @@ import { unreachable } from "./asserts.ts"; import { test } from "./mod.ts"; -import { red, green, white, gray, bold } from "../colors/mod.ts"; +import { red, green, white, gray, bold } from "../fmt/colors.ts"; test(function testingEqual(): void { assert(equal("world", "world")); diff --git a/testing/mod.ts b/testing/mod.ts index b237a0d85..c68528fbd 100644 --- a/testing/mod.ts +++ b/testing/mod.ts @@ -9,7 +9,7 @@ import { gray, yellow, italic -} from "../colors/mod.ts"; +} from "../fmt/colors.ts"; export type TestFunction = () => void | Promise; export interface TestDefinition { diff --git a/ws/README.md b/ws/README.md index 3b44b6eea..fe5bae983 100644 --- a/ws/README.md +++ b/ws/README.md @@ -85,7 +85,7 @@ import { import { encode } from "https://deno.land/std/strings/mod.ts"; import { BufReader } from "https://deno.land/std/io/bufio.ts"; import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts"; -import { blue, green, red, yellow } from "https://deno.land/std/colors/mod.ts"; +import { blue, green, red, yellow } from "https://deno.land/std/fmt/colors.ts"; const endpoint = Deno.args[1] || "ws://127.0.0.1:8080"; async function main(): Promise { diff --git a/ws/example_client.ts b/ws/example_client.ts index cdb482410..665c4cd8c 100644 --- a/ws/example_client.ts +++ b/ws/example_client.ts @@ -7,7 +7,7 @@ import { import { encode } from "../strings/mod.ts"; import { BufReader } from "../io/bufio.ts"; import { TextProtoReader } from "../textproto/mod.ts"; -import { blue, green, red, yellow } from "../colors/mod.ts"; +import { blue, green, red, yellow } from "../fmt/colors.ts"; const endpoint = Deno.args[1] || "ws://127.0.0.1:8080"; /** simple websocket cli */ -- cgit v1.2.3