-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add src/common/utils/colors.ts (#782)
This is a thin wrapper around the 'ansi-colors' module, which will be used if it can be found. This interface may also support HTML colorization in the future. Will be used to make the mental parsing of expectation failures easier.
- Loading branch information
1 parent
81dc07e
commit 6e1b3a2
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,123 @@ | ||
/** | ||
* The interface used for formatting strings to contain color metadata. | ||
* | ||
* Use the interface properties to construct a style, then use the | ||
* `(s: string): string` function to format the provided string with the given | ||
* style. | ||
*/ | ||
export interface Colors { | ||
// Returns the string formatted to contain the specified color or style. | ||
(s: string): string; | ||
|
||
// modifiers | ||
reset: Colors; | ||
bold: Colors; | ||
dim: Colors; | ||
italic: Colors; | ||
underline: Colors; | ||
inverse: Colors; | ||
hidden: Colors; | ||
strikethrough: Colors; | ||
|
||
// colors | ||
black: Colors; | ||
red: Colors; | ||
green: Colors; | ||
yellow: Colors; | ||
blue: Colors; | ||
magenta: Colors; | ||
cyan: Colors; | ||
white: Colors; | ||
gray: Colors; | ||
grey: Colors; | ||
|
||
// bright colors | ||
blackBright: Colors; | ||
redBright: Colors; | ||
greenBright: Colors; | ||
yellowBright: Colors; | ||
blueBright: Colors; | ||
magentaBright: Colors; | ||
cyanBright: Colors; | ||
whiteBright: Colors; | ||
|
||
// background colors | ||
bgBlack: Colors; | ||
bgRed: Colors; | ||
bgGreen: Colors; | ||
bgYellow: Colors; | ||
bgBlue: Colors; | ||
bgMagenta: Colors; | ||
bgCyan: Colors; | ||
bgWhite: Colors; | ||
|
||
// bright background colors | ||
bgBlackBright: Colors; | ||
bgRedBright: Colors; | ||
bgGreenBright: Colors; | ||
bgYellowBright: Colors; | ||
bgBlueBright: Colors; | ||
bgMagentaBright: Colors; | ||
bgCyanBright: Colors; | ||
bgWhiteBright: Colors; | ||
} | ||
|
||
/** | ||
* The interface used for formatting strings with color metadata. | ||
* | ||
* Currently Colors will use the 'ansi-colors' module if it can be loaded. | ||
* If it cannot be loaded, then the Colors implementation is a straight pass-through. | ||
* | ||
* Colors may also be a no-op if the current environment does not support colors. | ||
*/ | ||
export let Colors: Colors; | ||
|
||
try { | ||
/* eslint-disable-next-line node/no-unpublished-require */ | ||
Colors = require('ansi-colors') as Colors; | ||
} catch { | ||
const passthrough = ((s: string) => s) as Colors; | ||
passthrough.reset = passthrough; | ||
passthrough.bold = passthrough; | ||
passthrough.dim = passthrough; | ||
passthrough.italic = passthrough; | ||
passthrough.underline = passthrough; | ||
passthrough.inverse = passthrough; | ||
passthrough.hidden = passthrough; | ||
passthrough.strikethrough = passthrough; | ||
passthrough.black = passthrough; | ||
passthrough.red = passthrough; | ||
passthrough.green = passthrough; | ||
passthrough.yellow = passthrough; | ||
passthrough.blue = passthrough; | ||
passthrough.magenta = passthrough; | ||
passthrough.cyan = passthrough; | ||
passthrough.white = passthrough; | ||
passthrough.gray = passthrough; | ||
passthrough.grey = passthrough; | ||
passthrough.blackBright = passthrough; | ||
passthrough.redBright = passthrough; | ||
passthrough.greenBright = passthrough; | ||
passthrough.yellowBright = passthrough; | ||
passthrough.blueBright = passthrough; | ||
passthrough.magentaBright = passthrough; | ||
passthrough.cyanBright = passthrough; | ||
passthrough.whiteBright = passthrough; | ||
passthrough.bgBlack = passthrough; | ||
passthrough.bgRed = passthrough; | ||
passthrough.bgGreen = passthrough; | ||
passthrough.bgYellow = passthrough; | ||
passthrough.bgBlue = passthrough; | ||
passthrough.bgMagenta = passthrough; | ||
passthrough.bgCyan = passthrough; | ||
passthrough.bgWhite = passthrough; | ||
passthrough.bgBlackBright = passthrough; | ||
passthrough.bgRedBright = passthrough; | ||
passthrough.bgGreenBright = passthrough; | ||
passthrough.bgYellowBright = passthrough; | ||
passthrough.bgBlueBright = passthrough; | ||
passthrough.bgMagentaBright = passthrough; | ||
passthrough.bgCyanBright = passthrough; | ||
passthrough.bgWhiteBright = passthrough; | ||
Colors = passthrough; | ||
} |