2024-05-15 05:00:04 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// This module is browser compatible.
|
|
|
|
// A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
|
|
|
|
// on npm.
|
|
|
|
|
|
|
|
// This code is vendored from `fmt/colors.ts`.
|
|
|
|
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { Deno } = globalThis as any;
|
|
|
|
const noColor = typeof Deno?.noColor === "boolean"
|
|
|
|
? Deno.noColor as boolean
|
|
|
|
: false;
|
|
|
|
|
|
|
|
interface Code {
|
|
|
|
open: string;
|
|
|
|
close: string;
|
|
|
|
regexp: RegExp;
|
|
|
|
}
|
|
|
|
|
|
|
|
const enabled = !noColor;
|
|
|
|
|
|
|
|
function code(open: number[], close: number): Code {
|
|
|
|
return {
|
|
|
|
open: `\x1b[${open.join(";")}m`,
|
|
|
|
close: `\x1b[${close}m`,
|
|
|
|
regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function run(str: string, code: Code): string {
|
|
|
|
return enabled
|
|
|
|
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
|
|
|
|
: str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-15 07:57:20 +00:00
|
|
|
* Sets the style of text to be printed to bold.
|
|
|
|
*
|
|
|
|
* Disable by setting the `NO_COLOR` environmental variable.
|
|
|
|
*
|
|
|
|
* @param str Text to make bold
|
|
|
|
*
|
|
|
|
* @returns Bold text for printing
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { bold } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(bold("Hello, world!")); // Prints "Hello, world!" in bold
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
|
|
|
export function bold(str: string): string {
|
|
|
|
return run(str, code([1], 22));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-15 07:57:20 +00:00
|
|
|
* Sets the color of text to be printed to red.
|
|
|
|
*
|
|
|
|
* Disable by setting the `NO_COLOR` environmental variable.
|
|
|
|
*
|
|
|
|
* @param str Text to make red
|
|
|
|
*
|
|
|
|
* @returns Red text for printing
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { red } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(red("Hello, world!")); // Prints "Hello, world!" in red
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
|
|
|
export function red(str: string): string {
|
|
|
|
return run(str, code([31], 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-15 07:57:20 +00:00
|
|
|
* Sets the color of text to be printed to green.
|
|
|
|
*
|
|
|
|
* Disable by setting the `NO_COLOR` environmental variable.
|
|
|
|
*
|
|
|
|
* @param str Text to make green
|
|
|
|
*
|
|
|
|
* @returns Green text for print
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { green } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(green("Hello, world!")); // Prints "Hello, world!" in green
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
|
|
|
export function green(str: string): string {
|
|
|
|
return run(str, code([32], 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-15 07:57:20 +00:00
|
|
|
* Sets the color of text to be printed to yellow.
|
|
|
|
*
|
|
|
|
* Disable by setting the `NO_COLOR` environmental variable.
|
|
|
|
*
|
|
|
|
* @param str Text to make yellow
|
|
|
|
*
|
|
|
|
* @returns Yellow text for print
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { yellow } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(yellow("Hello, world!")); // Prints "Hello, world!" in yellow
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
|
|
|
export function yellow(str: string): string {
|
|
|
|
return run(str, code([33], 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-15 07:57:20 +00:00
|
|
|
* Sets the color of text to be printed to white.
|
|
|
|
*
|
|
|
|
* @param str Text to make white
|
|
|
|
*
|
|
|
|
* @returns White text for print
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { white } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(white("Hello, world!")); // Prints "Hello, world!" in white
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
|
|
|
export function white(str: string): string {
|
|
|
|
return run(str, code([37], 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-15 07:57:20 +00:00
|
|
|
* Sets the color of text to be printed to gray.
|
|
|
|
*
|
|
|
|
* @param str Text to make gray
|
|
|
|
*
|
|
|
|
* @returns Gray text for print
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { gray } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(gray("Hello, world!")); // Prints "Hello, world!" in gray
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
|
|
|
export function gray(str: string): string {
|
|
|
|
return brightBlack(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-15 07:57:20 +00:00
|
|
|
* Sets the color of text to be printed to bright-black.
|
|
|
|
*
|
|
|
|
* @param str Text to make bright-black
|
|
|
|
*
|
|
|
|
* @returns Bright-black text for print
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { brightBlack } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(brightBlack("Hello, world!")); // Prints "Hello, world!" in bright-black
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
2024-05-15 07:57:20 +00:00
|
|
|
export function brightBlack(str: string): string {
|
2024-05-15 05:00:04 +00:00
|
|
|
return run(str, code([90], 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-15 07:57:20 +00:00
|
|
|
* Sets the background color of text to be printed to red.
|
|
|
|
*
|
|
|
|
* @param str Text to make its background red
|
|
|
|
*
|
|
|
|
* @returns Red background text for print
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { bgRed } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(bgRed("Hello, world!")); // Prints "Hello, world!" with red background
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
|
|
|
export function bgRed(str: string): string {
|
|
|
|
return run(str, code([41], 49));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-15 07:57:20 +00:00
|
|
|
* Sets the background color of text to be printed to green.
|
|
|
|
*
|
|
|
|
* @param str Text to make its background green
|
|
|
|
*
|
|
|
|
* @returns Green background text for print
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { bgGreen } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(bgGreen("Hello, world!")); // Prints "Hello, world!" with green background
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
|
|
|
export function bgGreen(str: string): string {
|
|
|
|
return run(str, code([42], 49));
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/chalk/ansi-regex/blob/02fa893d619d3da85411acc8fd4e2eea0e95a9d9/index.js
|
|
|
|
const ANSI_PATTERN = new RegExp(
|
|
|
|
[
|
|
|
|
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
|
|
|
|
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TXZcf-nq-uy=><~]))",
|
|
|
|
].join("|"),
|
|
|
|
"g",
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove ANSI escape codes from the string.
|
|
|
|
*
|
2024-05-15 07:57:20 +00:00
|
|
|
* @param string Text to remove ANSI escape codes from
|
|
|
|
*
|
|
|
|
* @returns Text without ANSI escape codes
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { red, stripAnsiCode } from "@std/internal/styles";
|
|
|
|
*
|
|
|
|
* console.log(stripAnsiCode(red("Hello, world!"))); // Prints "Hello, world!"
|
|
|
|
* ```
|
2024-05-15 05:00:04 +00:00
|
|
|
*/
|
|
|
|
export function stripAnsiCode(string: string): string {
|
|
|
|
return string.replace(ANSI_PATTERN, "");
|
|
|
|
}
|