refactor(assert): vendor code from fmt/colors.ts (#4716)

* refactor(assert): vendor code from `fmt/colors.ts`

* update

* tweak

* tweaks
This commit is contained in:
Asher Gomez 2024-05-15 15:00:04 +10:00 committed by GitHub
parent 656eddaf77
commit 0c44d968a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 249 additions and 7 deletions

View File

@ -1,6 +1,13 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, AssertionError, assertThrows } from "./mod.ts";
import { bold, gray, green, red, stripAnsiCode, yellow } from "@std/fmt/colors";
import {
bold,
gray,
green,
red,
stripAnsiCode,
yellow,
} from "@std/internal/styles";
const createHeader = (): string[] => [
"",

View File

@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { AssertionError } from "./assertion_error.ts";
import { stripAnsiCode } from "@std/fmt/colors";
import { stripAnsiCode } from "@std/internal/styles";
/**
* Make an assertion that `error` is an `Error`.

View File

@ -1,8 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { buildMessage, diff, diffstr, format } from "@std/internal";
import { buildMessage, diff, diffstr, format, red } from "@std/internal";
import { AssertionError } from "./assertion_error.ts";
import { red } from "@std/fmt/colors";
/**
* Make an assertion that `actual` and `expected` are equal using

View File

@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { bgGreen, bgRed, bold, gray, green, red, white } from "@std/fmt/colors";
import { bgGreen, bgRed, bold, gray, green, red, white } from "./styles.ts";
import type { DiffResult, DiffType } from "./_types.ts";
/**

View File

@ -6,6 +6,7 @@
"./build-message": "./build_message.ts",
"./diff-str": "./diff_str.ts",
"./diff": "./diff.ts",
"./format": "./format.ts"
"./format": "./format.ts",
"./styles": "./styles.ts"
}
}

View File

@ -1,5 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { green, red, stripAnsiCode } from "@std/fmt/colors";
import { green, red, stripAnsiCode } from "./styles.ts";
import { assertEquals, assertThrows } from "@std/assert";
import { format } from "./format.ts";

View File

@ -11,3 +11,4 @@ export * from "./build_message.ts";
export * from "./diff.ts";
export * from "./diff_str.ts";
export * from "./format.ts";
export * from "./styles.ts";

181
internal/styles.ts Normal file
View File

@ -0,0 +1,181 @@
// 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`.
/**
* String formatters and utilities for dealing with ANSI color codes.
*
* This module is browser compatible.
*
* This module supports `NO_COLOR` environmental variable disabling any coloring
* if `NO_COLOR` is set.
*
* @example
* ```ts
* import {
* bgBlue,
* bgRgb24,
* bgRgb8,
* bold,
* italic,
* red,
* rgb24,
* rgb8,
* } from "@std/fmt/colors";
*
* 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,
* }));
* ```
*
* @module
*/
// 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;
/**
* Builds color code
* @param open
* @param close
*/
function code(open: number[], close: number): Code {
return {
open: `\x1b[${open.join(";")}m`,
close: `\x1b[${close}m`,
regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
};
}
/**
* Applies color and background based on color code and its associated text
* @param str text to apply color settings to
* @param code color code to apply
*/
function run(str: string, code: Code): string {
return enabled
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
: str;
}
/**
* Make the text bold.
* @param str text to make bold
*/
export function bold(str: string): string {
return run(str, code([1], 22));
}
/**
* Set text color to red.
* @param str text to make red
*/
export function red(str: string): string {
return run(str, code([31], 39));
}
/**
* Set text color to green.
* @param str text to make green
*/
export function green(str: string): string {
return run(str, code([32], 39));
}
/**
* Set text color to yellow.
* @param str text to make yellow
*/
export function yellow(str: string): string {
return run(str, code([33], 39));
}
/**
* Set text color to white.
* @param str text to make white
*/
export function white(str: string): string {
return run(str, code([37], 39));
}
/**
* Set text color to gray.
* @param str text to make gray
*/
export function gray(str: string): string {
return brightBlack(str);
}
/**
* Set text color to bright black.
* @param str text to make bright-black
*/
function brightBlack(str: string): string {
return run(str, code([90], 39));
}
/**
* Set background color to red.
* @param str text to make its background red
*/
export function bgRed(str: string): string {
return run(str, code([41], 49));
}
/**
* Set background color to green.
* @param str text to make its background green
*/
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.
*
* @param string to remove ANSI escape codes from
*/
export function stripAnsiCode(string: string): string {
return string.replace(ANSI_PATTERN, "");
}

53
internal/styles_test.ts Normal file
View File

@ -0,0 +1,53 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@std/assert";
import * as c from "./styles.ts";
Deno.test("red() single color", function () {
assertEquals(c.red("foo bar"), "foo bar");
});
Deno.test("red() replaces close characters", function () {
assertEquals(c.red("Hello"), "Hello");
});
Deno.test("getColorEnabled() handles enabled colors", function () {
assertEquals(c.red("foo bar"), "foo bar");
});
Deno.test("bold()", function () {
assertEquals(c.bold("foo bar"), "foo bar");
});
Deno.test("red()", function () {
assertEquals(c.red("foo bar"), "foo bar");
});
Deno.test("green()", function () {
assertEquals(c.green("foo bar"), "foo bar");
});
Deno.test("white()", function () {
assertEquals(c.white("foo bar"), "foo bar");
});
Deno.test("gray()", function () {
assertEquals(c.gray("foo bar"), "foo bar");
});
Deno.test("bgRed()", function () {
assertEquals(c.bgRed("foo bar"), "foo bar");
});
Deno.test("bgGreen()", function () {
assertEquals(c.bgGreen("foo bar"), "foo bar");
});
// https://github.com/chalk/strip-ansi/blob/2b8c961e75760059699373f9a69101065c3ded3a/test.js#L4-L6
Deno.test("stripAnsiCode()", function () {
assertEquals(
c.stripAnsiCode(
"\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m",
),
"foofoo",
);
});