-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ansi): add chainable ansi colors module (#128)
- Loading branch information
Showing
12 changed files
with
350 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { assertEquals, bold, red } from "../dev_deps.ts"; | ||
import { underline } from "../prompt/deps.ts"; | ||
import { ansi } from "./ansi.ts"; | ||
import { colors } from "./colors.ts"; | ||
|
||
Deno.test({ | ||
name: "test colors", | ||
fn() { | ||
assertEquals( | ||
ansi.cursorTo(3, 2).eraseDown.cursorHide(), | ||
"\x1B[2;3H\x1B[0J\x1B[?25l", | ||
); | ||
}, | ||
}); |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as stdColors from "https://deno.land/std@0.82.0/fmt/colors.ts"; | ||
|
||
type ExcludedColorMethods = "setColorEnabled" | "getColorEnabled"; | ||
type PropertyNames = keyof typeof stdColors; | ||
type ColorMethod = (str: string, ...args: Array<unknown>) => string; | ||
type ColorMethods = Exclude<PropertyNames, ExcludedColorMethods>; | ||
type Chainable<T, E extends keyof T | null = null> = { | ||
[P in keyof T]: P extends E ? T[P] : Chainable<T, E> & T[P]; | ||
}; | ||
|
||
/** Chainable colors instance returned by all ansi escape properties. */ | ||
export type ColorsChain = | ||
& Chainable<typeof stdColors, ExcludedColorMethods> | ||
& { _stack: Array<ColorMethods> }; | ||
|
||
/** Create new `Colors` instance. */ | ||
export type ColorsFactory = () => Colors; | ||
|
||
/** | ||
* Chainable colors module. | ||
* If invoked as method, a new `Colors` instance will be returned. | ||
*/ | ||
export type Colors = ColorsFactory & ColorsChain; | ||
|
||
const proto = Object.create(null); | ||
const methodNames = Object.keys(stdColors) as Array<PropertyNames>; | ||
for (const name of methodNames) { | ||
if (name === "setColorEnabled" || name === "getColorEnabled") { | ||
continue; | ||
} | ||
Object.defineProperty(proto, name, { | ||
get(this: ColorsChain) { | ||
return factory([...this._stack, name]); | ||
}, | ||
}); | ||
} | ||
|
||
export const colors: Colors = factory(); | ||
|
||
/** | ||
* Chainable colors module. | ||
* ``` | ||
* console.log(colors.blue.bgRed.bold('Welcome to Deno.Land!')); | ||
* ``` | ||
* If invoked as method, a new Ansi instance will be returned. | ||
* ``` | ||
* const myColors: Colors = colors(); | ||
* console.log(myColors.blue.bgRed.bold('Welcome to Deno.Land!')); | ||
* ``` | ||
*/ | ||
function factory(stack: Array<ColorMethods> = []): Colors { | ||
const colors: Colors = function ( | ||
this: ColorsChain | undefined, | ||
str?: string, | ||
...args: Array<unknown> | ||
): string | ColorsChain { | ||
if (str) { | ||
const lastIndex = stack.length - 1; | ||
return stack.reduce( | ||
(str: string, name: PropertyNames, index: number) => | ||
index === lastIndex | ||
? (stdColors[name] as ColorMethod)(str, ...args) | ||
: (stdColors[name] as ColorMethod)(str), | ||
str, | ||
); | ||
} | ||
const tmp = stack.slice(); | ||
stack = []; | ||
return factory(tmp); | ||
} as Colors; | ||
|
||
Object.setPrototypeOf(colors, proto); | ||
colors._stack = stack; | ||
return colors; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { assertEquals, bold, red } from "../dev_deps.ts"; | ||
import { underline } from "../prompt/deps.ts"; | ||
import { colors } from "./colors.ts"; | ||
|
||
Deno.test({ | ||
name: "test colors", | ||
fn() { | ||
assertEquals( | ||
colors.red.underline.bold("test"), | ||
bold(underline(red("test"))), | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "test color theme", | ||
fn() { | ||
const theme = colors.red.underline; | ||
assertEquals( | ||
theme.bold("test"), | ||
bold(underline(red("test"))), | ||
); | ||
assertEquals( | ||
theme("test"), | ||
underline(red("test")), | ||
); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./ansi.ts"; | ||
export * from "./ansi_escapes.ts"; | ||
export * from "./chain.ts"; | ||
export * from "./colors.ts"; | ||
export * from "./cursor_position.ts"; | ||
export * from "./tty.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { colors } from "../../ansi/colors.ts"; | ||
|
||
// Define theme colors. | ||
const error = colors.bold.red; | ||
const warn = colors.bold.yellow; | ||
const info = colors.bold.blue; | ||
|
||
// Use theme colors. | ||
console.log(error("[ERROR]"), "Some error!"); | ||
console.log(warn("[WARN]"), "Some warning!"); | ||
console.log(info("[INFO]"), "Some information!"); | ||
|
||
// Override theme colors. | ||
console.log(error.underline("[ERROR]"), "Some error!"); | ||
console.log(warn.underline("[WARN]"), "Some warning!"); | ||
console.log(info.underline("[INFO]"), "Some information!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { colors } from "../../ansi/colors.ts"; | ||
|
||
console.log( | ||
colors.bold.underline.rgb24("Welcome to Deno.Land!", 0xff3333), | ||
); |
Oops, something went wrong.