From 6f5dbfe10234060828670c8848eb7e42ded1c4cd Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Fri, 11 Jan 2019 12:18:21 +1100 Subject: Reorg colors (denoland/deno_std#96) Original: https://github.com/denoland/deno_std/commit/92bbca8166d6566011aff0ef467a83433747a937 --- colors/README.md | 2 +- colors/example.ts | 2 +- colors/main.ts | 33 --------------------------------- colors/main_test.ts | 11 ----------- colors/mod.ts | 33 +++++++++++++++++++++++++++++++++ colors/test.ts | 11 +++++++++++ 6 files changed, 46 insertions(+), 46 deletions(-) delete mode 100644 colors/main.ts delete mode 100644 colors/main_test.ts create mode 100644 colors/mod.ts create mode 100644 colors/test.ts (limited to 'colors') diff --git a/colors/README.md b/colors/README.md index eafdbb9c1..3148b7cab 100644 --- a/colors/README.md +++ b/colors/README.md @@ -10,7 +10,7 @@ The main modules exports a single function name `color` which is a function that provides chaining to stack colors. Basic usage looks like this: ```ts -import { color } from "https://deno.land/x/colors/main.ts"; +import { color } from "https://deno.land/x/std/colors/mod.ts"; console.log(color.bgBlue.red.bold("Hello world!")); ``` diff --git a/colors/example.ts b/colors/example.ts index d60ecc31c..e98a32ec9 100644 --- a/colors/example.ts +++ b/colors/example.ts @@ -1,3 +1,3 @@ -import { color } from "main.ts"; +import { color } from "./mod.ts"; console.log(color.bgBlue.red.bold("Hello world!")); diff --git a/colors/main.ts b/colors/main.ts deleted file mode 100644 index 8316060db..000000000 --- a/colors/main.ts +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { styles } from "./styles.ts"; - -type Styles = { readonly [S in keyof typeof styles]: Color }; - -type Color = Styles & { - (str: string): string; -}; - -const styleStack: string[] = []; - -export const color = function color(str: string): string { - styleStack.reverse(); - while (styleStack.length) { - const style = styleStack.pop(); - const code = styles[style]; - str = `${code.open}${str.replace(code.closeRe, code.open)}${ - code.close - }`.replace(/\r?\n/g, `${code.close}$&${code.open}`); - } - return str; -} as Color; - -for (const style of Object.keys(styles)) { - Object.defineProperty(color, style, { - get() { - styleStack.push(style); - return color; - }, - enumerable: true, - configurable: false - }); -} diff --git a/colors/main_test.ts b/colors/main_test.ts deleted file mode 100644 index 2073c7c78..000000000 --- a/colors/main_test.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { assertEqual, test } from "../testing/mod.ts"; -import { color } from "main.ts"; -import "example.ts"; - -test(function singleColor() { - assertEqual(color.red("Hello world"), "Hello world"); -}); - -test(function doubleColor() { - assertEqual(color.red.bgBlue("Hello world"), "Hello world"); -}); diff --git a/colors/mod.ts b/colors/mod.ts new file mode 100644 index 000000000..8316060db --- /dev/null +++ b/colors/mod.ts @@ -0,0 +1,33 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { styles } from "./styles.ts"; + +type Styles = { readonly [S in keyof typeof styles]: Color }; + +type Color = Styles & { + (str: string): string; +}; + +const styleStack: string[] = []; + +export const color = function color(str: string): string { + styleStack.reverse(); + while (styleStack.length) { + const style = styleStack.pop(); + const code = styles[style]; + str = `${code.open}${str.replace(code.closeRe, code.open)}${ + code.close + }`.replace(/\r?\n/g, `${code.close}$&${code.open}`); + } + return str; +} as Color; + +for (const style of Object.keys(styles)) { + Object.defineProperty(color, style, { + get() { + styleStack.push(style); + return color; + }, + enumerable: true, + configurable: false + }); +} diff --git a/colors/test.ts b/colors/test.ts new file mode 100644 index 000000000..1acc30072 --- /dev/null +++ b/colors/test.ts @@ -0,0 +1,11 @@ +import { assertEqual, test } from "../testing/mod.ts"; +import { color } from "./mod.ts"; +import "./example.ts"; + +test(function singleColor() { + assertEqual(color.red("Hello world"), "Hello world"); +}); + +test(function doubleColor() { + assertEqual(color.red.bgBlue("Hello world"), "Hello world"); +}); -- cgit v1.2.3