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
|
|
|
|
|
|
|
// TODO(kitsonk) Replace with `deno_std/colors/mod.ts` when we can load modules
|
|
|
|
// which end in `.ts`.
|
|
|
|
|
2019-09-02 17:07:11 -04:00
|
|
|
import { noColor } from "./deno.ts";
|
2019-04-29 10:58:31 -04:00
|
|
|
|
|
|
|
interface Code {
|
|
|
|
open: string;
|
|
|
|
close: string;
|
|
|
|
regexp: RegExp;
|
|
|
|
}
|
|
|
|
|
2019-09-07 12:27:18 -04:00
|
|
|
const enabled = !noColor;
|
2019-04-29 10:58:31 -04:00
|
|
|
|
|
|
|
function code(open: number, close: number): Code {
|
|
|
|
return {
|
|
|
|
open: `\x1b[${open}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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function bold(str: string): string {
|
|
|
|
return run(str, code(1, 22));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function yellow(str: string): string {
|
|
|
|
return run(str, code(33, 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cyan(str: string): string {
|
|
|
|
return run(str, code(36, 39));
|
|
|
|
}
|