2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2021-01-02 15:17:01 +00:00
|
|
|
// A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
|
|
|
|
// on npm.
|
2021-09-12 15:14:54 +00:00
|
|
|
|
|
|
|
/**
|
2022-06-14 09:20:56 +00:00
|
|
|
* String formatters and utilities for dealing with ANSI color codes.
|
|
|
|
*
|
2024-07-29 11:31:14 +00:00
|
|
|
* > [!IMPORTANT]
|
2024-07-10 05:26:41 +00:00
|
|
|
* > If printing directly to the console, it's recommended to style console
|
|
|
|
* > output using CSS (guide
|
|
|
|
* > {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/console#styling_console_output | here}).
|
|
|
|
*
|
2021-09-12 15:14:54 +00:00
|
|
|
* This module supports `NO_COLOR` environmental variable disabling any coloring
|
|
|
|
* if `NO_COLOR` is set.
|
|
|
|
*
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2022-11-25 11:40:23 +00:00
|
|
|
* import {
|
|
|
|
* bgBlue,
|
|
|
|
* bgRgb24,
|
|
|
|
* bgRgb8,
|
|
|
|
* bold,
|
|
|
|
* italic,
|
|
|
|
* red,
|
|
|
|
* rgb24,
|
|
|
|
* rgb8,
|
2024-04-29 02:57:30 +00:00
|
|
|
* } from "@std/fmt/colors";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* console.log(bgBlue(italic(red(bold("Hello, World!")))));
|
|
|
|
*
|
|
|
|
* // also supports 8bit colors
|
|
|
|
*
|
|
|
|
* console.log(rgb8("Hello, World!", 42));
|
|
|
|
*
|
|
|
|
* console.log(bgRgb8("Hello, World!", 42));
|
|
|
|
*
|
|
|
|
* // and 24bit rgb
|
|
|
|
*
|
|
|
|
* console.log(rgb24("Hello, World!", {
|
|
|
|
* r: 41,
|
|
|
|
* g: 42,
|
|
|
|
* b: 43,
|
|
|
|
* }));
|
|
|
|
*
|
|
|
|
* console.log(bgRgb24("Hello, World!", {
|
|
|
|
* r: 41,
|
|
|
|
* g: 42,
|
|
|
|
* b: 43,
|
|
|
|
* }));
|
|
|
|
* ```
|
|
|
|
*
|
2021-09-12 15:14:54 +00:00
|
|
|
* @module
|
|
|
|
*/
|
2020-05-25 17:32:34 +00:00
|
|
|
|
2021-07-06 02:15:37 +00:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { Deno } = globalThis as any;
|
|
|
|
const noColor = typeof Deno?.noColor === "boolean"
|
|
|
|
? Deno.noColor as boolean
|
2023-07-13 07:02:03 +00:00
|
|
|
: false;
|
2019-01-19 10:09:35 +00:00
|
|
|
|
|
|
|
interface Code {
|
|
|
|
open: string;
|
|
|
|
close: string;
|
|
|
|
regexp: RegExp;
|
|
|
|
}
|
|
|
|
|
2020-05-09 10:29:44 +00:00
|
|
|
/** RGB 8-bits per channel. Each in range `0->255` or `0x00->0xff` */
|
2024-01-11 06:57:29 +00:00
|
|
|
export interface Rgb {
|
|
|
|
/** Red component value */
|
2020-05-09 10:29:44 +00:00
|
|
|
r: number;
|
2024-01-11 06:57:29 +00:00
|
|
|
/** Green component value */
|
2020-05-09 10:29:44 +00:00
|
|
|
g: number;
|
2024-01-11 06:57:29 +00:00
|
|
|
/** Blue component value */
|
2020-05-09 10:29:44 +00:00
|
|
|
b: number;
|
|
|
|
}
|
|
|
|
|
2019-02-10 00:13:44 +00:00
|
|
|
let enabled = !noColor;
|
2019-01-19 10:09:35 +00:00
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
2024-05-28 06:14:34 +00:00
|
|
|
* Enable or disable text color when styling.
|
|
|
|
*
|
|
|
|
* `@std/fmt/colors` automatically detects NO_COLOR environmental variable
|
|
|
|
* and disables text color. Use this API only when the automatic detection
|
|
|
|
* doesn't work.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { setColorEnabled } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* // Disable text color
|
|
|
|
* setColorEnabled(false);
|
|
|
|
*
|
|
|
|
* // Enable text color
|
|
|
|
* setColorEnabled(true);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param value The boolean value to enable or disable text color
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2022-08-24 01:21:57 +00:00
|
|
|
export function setColorEnabled(value: boolean) {
|
2023-07-13 07:02:03 +00:00
|
|
|
if (Deno?.noColor) {
|
2019-02-10 00:13:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-19 10:09:35 +00:00
|
|
|
enabled = value;
|
|
|
|
}
|
|
|
|
|
2024-05-28 06:14:34 +00:00
|
|
|
/**
|
|
|
|
* Get whether text color change is enabled or disabled.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { getColorEnabled } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(getColorEnabled()); // true if enabled, false if disabled
|
|
|
|
* ```
|
|
|
|
* @returns `true` if text color is enabled, `false` otherwise
|
|
|
|
*/
|
2019-12-20 20:21:30 +00:00
|
|
|
export function getColorEnabled(): boolean {
|
2019-01-19 10:09:35 +00:00
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Builds color code
|
|
|
|
* @param open
|
|
|
|
* @param close
|
|
|
|
*/
|
2020-05-09 10:29:44 +00:00
|
|
|
function code(open: number[], close: number): Code {
|
2019-01-19 10:09:35 +00:00
|
|
|
return {
|
2020-05-09 10:29:44 +00:00
|
|
|
open: `\x1b[${open.join(";")}m`,
|
2019-01-19 10:09:35 +00:00
|
|
|
close: `\x1b[${close}m`,
|
2020-03-28 17:03:49 +00:00
|
|
|
regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
|
2019-01-19 10:09:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Applies color and background based on color code and its associated text
|
2024-05-28 06:14:34 +00:00
|
|
|
* @param str The text to apply color settings to
|
|
|
|
* @param code The color code to apply
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-03-05 00:53:35 +00:00
|
|
|
function run(str: string, code: Code): string {
|
2019-01-19 10:09:35 +00:00
|
|
|
return enabled
|
|
|
|
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
|
|
|
|
: str;
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
2024-01-11 06:57:29 +00:00
|
|
|
* Reset the text modified.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { reset } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(reset("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to reset
|
|
|
|
* @returns The text with reset color
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function reset(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([0], 0));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Make the text bold.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bold } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bold("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make bold
|
|
|
|
* @returns The bold text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function bold(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([1], 22));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* The text emits only a small amount of light.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { dim } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(dim("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to dim
|
|
|
|
* @returns The dimmed text
|
2024-02-12 01:15:58 +00:00
|
|
|
*
|
|
|
|
* Warning: Not all terminal emulators support `dim`.
|
|
|
|
* For compatibility across all terminals, use {@linkcode gray} or {@linkcode brightBlack} instead.
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function dim(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([2], 22));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Make the text italic.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { italic } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(italic("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make italic
|
|
|
|
* @returns The italic text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function italic(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([3], 23));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Make the text underline.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { underline } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(underline("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to underline
|
|
|
|
* @returns The underlined text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function underline(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([4], 24));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Invert background color and text color.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { inverse } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(inverse("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to invert its color
|
|
|
|
* @returns The inverted text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function inverse(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([7], 27));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Make the text hidden.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { hidden } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(hidden("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to hide
|
|
|
|
* @returns The hidden text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function hidden(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([8], 28));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Put horizontal line through the center of the text.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { strikethrough } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(strikethrough("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to strike through
|
|
|
|
* @returns The text with horizontal line through the center
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function strikethrough(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([9], 29));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to black.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { black } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(black("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make black
|
|
|
|
* @returns The black text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function black(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([30], 39));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to red.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { red } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(red("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make red
|
|
|
|
* @returns The red text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function red(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([31], 39));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to green.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { green } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(green("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make green
|
|
|
|
* @returns The green text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function green(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([32], 39));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to yellow.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { yellow } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(yellow("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make yellow
|
|
|
|
* @returns The yellow text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function yellow(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([33], 39));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to blue.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { blue } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(blue("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make blue
|
|
|
|
* @returns The blue text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function blue(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([34], 39));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to magenta.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { magenta } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(magenta("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make magenta
|
|
|
|
* @returns The magenta text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function magenta(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([35], 39));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to cyan.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { cyan } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(cyan("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make cyan
|
|
|
|
* @returns The cyan text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function cyan(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([36], 39));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to white.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { white } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(white("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make white
|
|
|
|
* @returns The white text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function white(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([37], 39));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to gray.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { gray } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(gray("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make gray
|
|
|
|
* @returns The gray text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function gray(str: string): string {
|
2020-08-29 08:29:41 +00:00
|
|
|
return brightBlack(str);
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to bright black.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { brightBlack } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(brightBlack("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make bright black
|
|
|
|
* @returns The bright black text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function brightBlack(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([90], 39));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to bright red.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { brightRed } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(brightRed("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make bright red
|
|
|
|
* @returns The bright red text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function brightRed(str: string): string {
|
|
|
|
return run(str, code([91], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to bright green.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { brightGreen } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(brightGreen("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make bright green
|
|
|
|
* @returns The bright green text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function brightGreen(str: string): string {
|
|
|
|
return run(str, code([92], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to bright yellow.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { brightYellow } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(brightYellow("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make bright yellow
|
|
|
|
* @returns The bright yellow text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function brightYellow(str: string): string {
|
|
|
|
return run(str, code([93], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to bright blue.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { brightBlue } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(brightBlue("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make bright blue
|
|
|
|
* @returns The bright blue text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function brightBlue(str: string): string {
|
|
|
|
return run(str, code([94], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to bright magenta.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { brightMagenta } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(brightMagenta("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make bright magenta
|
|
|
|
* @returns The bright magenta text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function brightMagenta(str: string): string {
|
|
|
|
return run(str, code([95], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to bright cyan.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { brightCyan } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(brightCyan("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make bright cyan
|
|
|
|
* @returns The bright cyan text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function brightCyan(str: string): string {
|
|
|
|
return run(str, code([96], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color to bright white.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { brightWhite } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(brightWhite("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make bright white
|
|
|
|
* @returns The bright white text
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function brightWhite(str: string): string {
|
|
|
|
return run(str, code([97], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to black.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBlack } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBlack("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background black
|
|
|
|
* @returns The text with black background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function bgBlack(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([40], 49));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to red.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgRed } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgRed("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background red
|
|
|
|
* @returns The text with red background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function bgRed(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([41], 49));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to green.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgGreen } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgGreen("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background green
|
|
|
|
* @returns The text with green background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function bgGreen(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([42], 49));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to yellow.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgYellow } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgYellow("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background yellow
|
|
|
|
* @returns The text with yellow background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function bgYellow(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([43], 49));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to blue.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBlue } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBlue("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background blue
|
|
|
|
* @returns The text with blue background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function bgBlue(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([44], 49));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to magenta.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgMagenta } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgMagenta("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background magenta
|
|
|
|
* @returns The text with magenta background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function bgMagenta(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([45], 49));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to cyan.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgCyan } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgCyan("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background cyan
|
|
|
|
* @returns The text with cyan background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function bgCyan(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([46], 49));
|
2019-01-19 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to white.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgWhite } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgWhite("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background white
|
|
|
|
* @returns The text with white background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2019-01-19 10:09:35 +00:00
|
|
|
export function bgWhite(str: string): string {
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(str, code([47], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to bright black.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBrightBlack } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBrightBlack("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background bright black
|
|
|
|
* @returns The text with bright black background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function bgBrightBlack(str: string): string {
|
|
|
|
return run(str, code([100], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to bright red.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBrightRed } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBrightRed("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background bright red
|
|
|
|
* @returns The text with bright red background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function bgBrightRed(str: string): string {
|
|
|
|
return run(str, code([101], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to bright green.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBrightGreen } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBrightGreen("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background bright green
|
|
|
|
* @returns The text with bright green background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function bgBrightGreen(str: string): string {
|
|
|
|
return run(str, code([102], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to bright yellow.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBrightYellow } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBrightYellow("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background bright yellow
|
|
|
|
* @returns The text with bright yellow background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function bgBrightYellow(str: string): string {
|
|
|
|
return run(str, code([103], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to bright blue.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBrightBlue } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBrightBlue("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background bright blue
|
|
|
|
* @returns The text with bright blue background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function bgBrightBlue(str: string): string {
|
|
|
|
return run(str, code([104], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to bright magenta.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBrightMagenta } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBrightMagenta("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background bright magenta
|
|
|
|
* @returns The text with bright magenta background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function bgBrightMagenta(str: string): string {
|
|
|
|
return run(str, code([105], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to bright cyan.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBrightCyan } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBrightCyan("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background bright cyan
|
|
|
|
* @returns The text with bright cyan background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function bgBrightCyan(str: string): string {
|
|
|
|
return run(str, code([106], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color to bright white.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgBrightWhite } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgBrightWhite("Hello, world!"));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text to make its background bright white
|
|
|
|
* @returns The text with bright white background
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-08-29 08:29:41 +00:00
|
|
|
export function bgBrightWhite(str: string): string {
|
|
|
|
return run(str, code([107], 49));
|
|
|
|
}
|
|
|
|
|
2020-05-09 10:29:44 +00:00
|
|
|
/* Special Color Sequences */
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Clam and truncate color codes
|
2024-05-28 06:14:34 +00:00
|
|
|
* @param n The input number
|
|
|
|
* @param max The number to truncate to
|
|
|
|
* @param min The number to truncate from
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-05-09 10:29:44 +00:00
|
|
|
function clampAndTruncate(n: number, max = 255, min = 0): number {
|
|
|
|
return Math.trunc(Math.max(Math.min(n, max), min));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color using paletted 8bit colors.
|
|
|
|
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { rgb8 } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(rgb8("Hello, world!", 42));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text color to apply paletted 8bit colors to
|
|
|
|
* @param color The color code
|
|
|
|
* @returns The text with paletted 8bit color
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-05-09 10:29:44 +00:00
|
|
|
export function rgb8(str: string, color: number): string {
|
|
|
|
return run(str, code([38, 5, clampAndTruncate(color)], 39));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color using paletted 8bit colors.
|
|
|
|
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { bgRgb8 } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(bgRgb8("Hello, world!", 42));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param str The text color to apply paletted 8bit background colors to
|
2020-11-07 14:05:37 +00:00
|
|
|
* @param color code
|
2024-05-28 06:14:34 +00:00
|
|
|
* @returns The text with paletted 8bit background color
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2020-05-09 10:29:44 +00:00
|
|
|
export function bgRgb8(str: string, color: number): string {
|
|
|
|
return run(str, code([48, 5, clampAndTruncate(color)], 49));
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set text color using 24bit rgb.
|
2020-05-20 14:29:59 +00:00
|
|
|
* `color` can be a number in range `0x000000` to `0xffffff` or
|
|
|
|
* an `Rgb`.
|
|
|
|
*
|
2024-05-28 06:14:34 +00:00
|
|
|
* @example To produce the color magenta:
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { rgb24 } from "@std/fmt/colors";
|
2024-01-11 06:57:29 +00:00
|
|
|
*
|
|
|
|
* rgb24("foo", 0xff00ff);
|
|
|
|
* rgb24("foo", {r: 255, g: 0, b: 255});
|
2021-09-12 15:14:54 +00:00
|
|
|
* ```
|
2024-05-28 06:14:34 +00:00
|
|
|
* @param str The text color to apply 24bit rgb to
|
|
|
|
* @param color The color code
|
|
|
|
* @returns The text with 24bit rgb color
|
2020-05-20 14:29:59 +00:00
|
|
|
*/
|
|
|
|
export function rgb24(str: string, color: number | Rgb): string {
|
|
|
|
if (typeof color === "number") {
|
|
|
|
return run(
|
|
|
|
str,
|
2020-07-14 19:24:17 +00:00
|
|
|
code(
|
|
|
|
[38, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff],
|
|
|
|
39,
|
|
|
|
),
|
2020-05-20 14:29:59 +00:00
|
|
|
);
|
|
|
|
}
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(
|
|
|
|
str,
|
|
|
|
code(
|
|
|
|
[
|
|
|
|
38,
|
|
|
|
2,
|
|
|
|
clampAndTruncate(color.r),
|
|
|
|
clampAndTruncate(color.g),
|
|
|
|
clampAndTruncate(color.b),
|
|
|
|
],
|
2020-07-14 19:24:17 +00:00
|
|
|
39,
|
|
|
|
),
|
2020-05-09 10:29:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Set background color using 24bit rgb.
|
2020-05-20 14:29:59 +00:00
|
|
|
* `color` can be a number in range `0x000000` to `0xffffff` or
|
|
|
|
* an `Rgb`.
|
|
|
|
*
|
2024-05-28 06:14:34 +00:00
|
|
|
* @example To produce the color magenta:
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { bgRgb24 } from "@std/fmt/colors";
|
2024-01-11 06:57:29 +00:00
|
|
|
*
|
|
|
|
* bgRgb24("foo", 0xff00ff);
|
|
|
|
* bgRgb24("foo", {r: 255, g: 0, b: 255});
|
2021-09-12 15:14:54 +00:00
|
|
|
* ```
|
2024-05-28 06:14:34 +00:00
|
|
|
* @param str The text color to apply 24bit rgb to
|
|
|
|
* @param color The color code
|
|
|
|
* @returns The text with 24bit rgb color
|
2020-05-20 14:29:59 +00:00
|
|
|
*/
|
|
|
|
export function bgRgb24(str: string, color: number | Rgb): string {
|
|
|
|
if (typeof color === "number") {
|
|
|
|
return run(
|
|
|
|
str,
|
2020-07-14 19:24:17 +00:00
|
|
|
code(
|
|
|
|
[48, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff],
|
|
|
|
49,
|
|
|
|
),
|
2020-05-20 14:29:59 +00:00
|
|
|
);
|
|
|
|
}
|
2020-05-09 10:29:44 +00:00
|
|
|
return run(
|
|
|
|
str,
|
|
|
|
code(
|
|
|
|
[
|
|
|
|
48,
|
|
|
|
2,
|
|
|
|
clampAndTruncate(color.r),
|
|
|
|
clampAndTruncate(color.g),
|
|
|
|
clampAndTruncate(color.b),
|
|
|
|
],
|
2020-07-14 19:24:17 +00:00
|
|
|
49,
|
|
|
|
),
|
2020-05-09 10:29:44 +00:00
|
|
|
);
|
2018-12-19 04:30:44 +00:00
|
|
|
}
|
2020-05-19 18:19:26 +00:00
|
|
|
|
2022-03-06 11:45:51 +00:00
|
|
|
// https://github.com/chalk/ansi-regex/blob/02fa893d619d3da85411acc8fd4e2eea0e95a9d9/index.js
|
2020-05-19 18:19:26 +00:00
|
|
|
const ANSI_PATTERN = new RegExp(
|
|
|
|
[
|
2022-03-06 11:45:51 +00:00
|
|
|
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
|
2023-09-02 06:03:59 +00:00
|
|
|
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TXZcf-nq-uy=><~]))",
|
2020-05-19 18:19:26 +00:00
|
|
|
].join("|"),
|
2020-07-14 19:24:17 +00:00
|
|
|
"g",
|
2020-05-19 18:19:26 +00:00
|
|
|
);
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Remove ANSI escape codes from the string.
|
2024-01-11 06:57:29 +00:00
|
|
|
*
|
2024-05-28 06:14:34 +00:00
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { stripAnsiCode, red } from "@std/fmt/colors";
|
|
|
|
*
|
|
|
|
* console.log(stripAnsiCode(red("Hello, world!")));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param string The text to remove ANSI escape codes from
|
|
|
|
* @returns The text without ANSI escape codes
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2023-08-31 05:22:19 +00:00
|
|
|
export function stripAnsiCode(string: string): string {
|
2020-05-19 18:19:26 +00:00
|
|
|
return string.replace(ANSI_PATTERN, "");
|
|
|
|
}
|