2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-25 13:32:34 -04:00
|
|
|
/** A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
|
2019-08-24 13:38:18 -04:00
|
|
|
* 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.
|
2020-05-25 13:32:34 -04:00
|
|
|
*
|
|
|
|
* This module is browser compatible. */
|
|
|
|
|
|
|
|
const noColor = globalThis.Deno?.noColor ?? true;
|
2019-01-19 05:09:35 -05:00
|
|
|
|
|
|
|
interface Code {
|
|
|
|
open: string;
|
|
|
|
close: string;
|
|
|
|
regexp: RegExp;
|
|
|
|
}
|
|
|
|
|
2020-05-09 06:29:44 -04:00
|
|
|
/** RGB 8-bits per channel. Each in range `0->255` or `0x00->0xff` */
|
|
|
|
interface Rgb {
|
|
|
|
r: number;
|
|
|
|
g: number;
|
|
|
|
b: number;
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:13:44 -05:00
|
|
|
let enabled = !noColor;
|
2019-01-19 05:09:35 -05:00
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set changing text color to enabled or disabled */
|
2019-12-20 15:21:30 -05:00
|
|
|
export function setColorEnabled(value: boolean): void {
|
2019-02-09 19:13:44 -05:00
|
|
|
if (noColor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-19 05:09:35 -05:00
|
|
|
enabled = value;
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Get wheather text color change is enabled or disabled. */
|
2019-12-20 15:21:30 -05:00
|
|
|
export function getColorEnabled(): boolean {
|
2019-01-19 05:09:35 -05:00
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2020-05-09 06:29:44 -04:00
|
|
|
function code(open: number[], close: number): Code {
|
2019-01-19 05:09:35 -05:00
|
|
|
return {
|
2020-05-09 06:29:44 -04:00
|
|
|
open: `\x1b[${open.join(";")}m`,
|
2019-01-19 05:09:35 -05:00
|
|
|
close: `\x1b[${close}m`,
|
2020-03-28 13:03:49 -04:00
|
|
|
regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
|
2019-01-19 05:09:35 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
function run(str: string, code: Code): string {
|
2019-01-19 05:09:35 -05:00
|
|
|
return enabled
|
|
|
|
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
|
|
|
|
: str;
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Reset the text modified */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function reset(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([0], 0));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Make the text bold. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function bold(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([1], 22));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** The text emits only a small amount of light. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function dim(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([2], 22));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Make the text italic. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function italic(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([3], 23));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Make the text underline. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function underline(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([4], 24));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Invert background color and text color. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function inverse(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([7], 27));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Make the text hidden. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function hidden(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([8], 28));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Put horizontal line throught the center of the text. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function strikethrough(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([9], 29));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to black. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function black(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([30], 39));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to red. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function red(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([31], 39));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to green. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function green(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([32], 39));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to yellow. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function yellow(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([33], 39));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to blue. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function blue(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([34], 39));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to magenta. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function magenta(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([35], 39));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to cyan. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function cyan(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([36], 39));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to white. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function white(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([37], 39));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to gray. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function gray(str: string): string {
|
2020-08-29 04:29:41 -04:00
|
|
|
return brightBlack(str);
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to bright black. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function brightBlack(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([90], 39));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to bright red. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function brightRed(str: string): string {
|
|
|
|
return run(str, code([91], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to bright green. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function brightGreen(str: string): string {
|
|
|
|
return run(str, code([92], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to bright yellow. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function brightYellow(str: string): string {
|
|
|
|
return run(str, code([93], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to bright blue. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function brightBlue(str: string): string {
|
|
|
|
return run(str, code([94], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to bright magenta. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function brightMagenta(str: string): string {
|
|
|
|
return run(str, code([95], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to bright cyan. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function brightCyan(str: string): string {
|
|
|
|
return run(str, code([96], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set text color to bright white. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function brightWhite(str: string): string {
|
|
|
|
return run(str, code([97], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to black. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function bgBlack(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([40], 49));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to red. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function bgRed(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([41], 49));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to green. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function bgGreen(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([42], 49));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to yellow. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function bgYellow(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([43], 49));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to blue. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function bgBlue(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([44], 49));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to magenta. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function bgMagenta(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([45], 49));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to cyan. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function bgCyan(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([46], 49));
|
2019-01-19 05:09:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to white. */
|
2019-01-19 05:09:35 -05:00
|
|
|
export function bgWhite(str: string): string {
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(str, code([47], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to bright black. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function bgBrightBlack(str: string): string {
|
|
|
|
return run(str, code([100], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to bright red. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function bgBrightRed(str: string): string {
|
|
|
|
return run(str, code([101], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to bright green. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function bgBrightGreen(str: string): string {
|
|
|
|
return run(str, code([102], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to bright yellow. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function bgBrightYellow(str: string): string {
|
|
|
|
return run(str, code([103], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to bright blue. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function bgBrightBlue(str: string): string {
|
|
|
|
return run(str, code([104], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to bright magenta. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function bgBrightMagenta(str: string): string {
|
|
|
|
return run(str, code([105], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to bright cyan. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function bgBrightCyan(str: string): string {
|
|
|
|
return run(str, code([106], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Set background color to bright white. */
|
2020-08-29 04:29:41 -04:00
|
|
|
export function bgBrightWhite(str: string): string {
|
|
|
|
return run(str, code([107], 49));
|
|
|
|
}
|
|
|
|
|
2020-05-09 06:29:44 -04:00
|
|
|
/* Special Color Sequences */
|
|
|
|
|
|
|
|
function clampAndTruncate(n: number, max = 255, min = 0): number {
|
|
|
|
return Math.trunc(Math.max(Math.min(n, max), min));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set text color using paletted 8bit colors.
|
|
|
|
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
|
|
|
|
export function rgb8(str: string, color: number): string {
|
|
|
|
return run(str, code([38, 5, clampAndTruncate(color)], 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set background color using paletted 8bit colors.
|
|
|
|
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
|
|
|
|
export function bgRgb8(str: string, color: number): string {
|
|
|
|
return run(str, code([48, 5, clampAndTruncate(color)], 49));
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:29:59 -04:00
|
|
|
/** Set text color using 24bit rgb.
|
|
|
|
* `color` can be a number in range `0x000000` to `0xffffff` or
|
|
|
|
* an `Rgb`.
|
|
|
|
*
|
|
|
|
* To produce the color magenta:
|
|
|
|
*
|
|
|
|
* rgba24("foo", 0xff00ff);
|
|
|
|
* rgba24("foo", {r: 255, g: 0, b: 255});
|
|
|
|
*/
|
|
|
|
export function rgb24(str: string, color: number | Rgb): string {
|
|
|
|
if (typeof color === "number") {
|
|
|
|
return run(
|
|
|
|
str,
|
2020-07-14 15:24:17 -04:00
|
|
|
code(
|
|
|
|
[38, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff],
|
|
|
|
39,
|
|
|
|
),
|
2020-05-20 10:29:59 -04:00
|
|
|
);
|
|
|
|
}
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(
|
|
|
|
str,
|
|
|
|
code(
|
|
|
|
[
|
|
|
|
38,
|
|
|
|
2,
|
|
|
|
clampAndTruncate(color.r),
|
|
|
|
clampAndTruncate(color.g),
|
|
|
|
clampAndTruncate(color.b),
|
|
|
|
],
|
2020-07-14 15:24:17 -04:00
|
|
|
39,
|
|
|
|
),
|
2020-05-09 06:29:44 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:29:59 -04:00
|
|
|
/** Set background color using 24bit rgb.
|
|
|
|
* `color` can be a number in range `0x000000` to `0xffffff` or
|
|
|
|
* an `Rgb`.
|
|
|
|
*
|
|
|
|
* To produce the color magenta:
|
|
|
|
*
|
|
|
|
* bgRgba24("foo", 0xff00ff);
|
|
|
|
* bgRgba24("foo", {r: 255, g: 0, b: 255});
|
|
|
|
*/
|
|
|
|
export function bgRgb24(str: string, color: number | Rgb): string {
|
|
|
|
if (typeof color === "number") {
|
|
|
|
return run(
|
|
|
|
str,
|
2020-07-14 15:24:17 -04:00
|
|
|
code(
|
|
|
|
[48, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff],
|
|
|
|
49,
|
|
|
|
),
|
2020-05-20 10:29:59 -04:00
|
|
|
);
|
|
|
|
}
|
2020-05-09 06:29:44 -04:00
|
|
|
return run(
|
|
|
|
str,
|
|
|
|
code(
|
|
|
|
[
|
|
|
|
48,
|
|
|
|
2,
|
|
|
|
clampAndTruncate(color.r),
|
|
|
|
clampAndTruncate(color.g),
|
|
|
|
clampAndTruncate(color.b),
|
|
|
|
],
|
2020-07-14 15:24:17 -04:00
|
|
|
49,
|
|
|
|
),
|
2020-05-09 06:29:44 -04:00
|
|
|
);
|
2018-12-18 23:30:44 -05:00
|
|
|
}
|
2020-05-19 14:19:26 -04:00
|
|
|
|
|
|
|
// https://github.com/chalk/ansi-regex/blob/2b56fb0c7a07108e5b54241e8faec160d393aedb/index.js
|
|
|
|
const ANSI_PATTERN = new RegExp(
|
|
|
|
[
|
|
|
|
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
|
|
|
|
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
|
|
|
|
].join("|"),
|
2020-07-14 15:24:17 -04:00
|
|
|
"g",
|
2020-05-19 14:19:26 -04:00
|
|
|
);
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Remove ANSI escape codes from the string. */
|
2020-05-19 14:19:26 -04:00
|
|
|
export function stripColor(string: string): string {
|
|
|
|
return string.replace(ANSI_PATTERN, "");
|
|
|
|
}
|