2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-04-29 10:58:31 -04:00
|
|
|
|
|
|
|
interface Code {
|
|
|
|
open: string;
|
|
|
|
close: string;
|
|
|
|
regexp: RegExp;
|
|
|
|
}
|
|
|
|
|
|
|
|
function code(open: number, close: number): Code {
|
|
|
|
return {
|
|
|
|
open: `\x1b[${open}m`,
|
|
|
|
close: `\x1b[${close}m`,
|
2020-03-28 13:03:49 -04:00
|
|
|
regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
|
2019-04-29 10:58:31 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function run(str: string, code: Code): string {
|
2020-05-19 14:19:26 -04:00
|
|
|
return !globalThis || !globalThis.Deno || globalThis.Deno.noColor
|
|
|
|
? str
|
|
|
|
: `${code.open}${str.replace(code.regexp, code.open)}${code.close}`;
|
2019-04-29 10:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function bold(str: string): string {
|
|
|
|
return run(str, code(1, 22));
|
|
|
|
}
|
|
|
|
|
2020-02-11 06:01:56 -05:00
|
|
|
export function italic(str: string): string {
|
|
|
|
return run(str, code(3, 23));
|
|
|
|
}
|
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
export function yellow(str: string): string {
|
|
|
|
return run(str, code(33, 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cyan(str: string): string {
|
|
|
|
return run(str, code(36, 39));
|
|
|
|
}
|
2020-02-11 06:01:56 -05:00
|
|
|
|
|
|
|
export function red(str: string): string {
|
|
|
|
return run(str, code(31, 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function green(str: string): string {
|
|
|
|
return run(str, code(32, 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function bgRed(str: string): string {
|
|
|
|
return run(str, code(41, 49));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function white(str: string): string {
|
|
|
|
return run(str, code(37, 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function gray(str: string): string {
|
|
|
|
return run(str, code(90, 39));
|
|
|
|
}
|
2020-04-11 02:08:11 -04:00
|
|
|
|
2020-05-19 14:19:26 -04:00
|
|
|
export function magenta(str: string): string {
|
|
|
|
return run(str, code(35, 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function dim(str: string): string {
|
|
|
|
return run(str, code(2, 22));
|
|
|
|
}
|
|
|
|
|
2020-04-11 02:08:11 -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("|"),
|
|
|
|
"g"
|
|
|
|
);
|
|
|
|
|
|
|
|
export function stripColor(string: string): string {
|
|
|
|
return string.replace(ANSI_PATTERN, "");
|
|
|
|
}
|