diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index c8a6a9d..1470129 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -1,8 +1,8 @@ // Reference: // - https://docs.deno.com/runtime/fundamentals/testing/ -import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64"; -import { dirname, fromFileUrl } from "https://deno.land/std/path/mod.ts"; +import {decodeBase64, encodeBase64} from "jsr:@std/encoding/base64"; +import {dirname, fromFileUrl} from "https://deno.land/std/path/mod.ts"; // reference: https://docs.deno.com/examples/hex_base64_encoding/ // import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64"; @@ -247,7 +247,7 @@ interface ColorToken { color?: string; } -function parseColorTokens(message: string): ColorToken[] { +function parseColorTokens(message: string, renderColor: boolean): ColorToken[] { const tokens: ColorToken[] = []; if (message) { let inColorStart = false; @@ -372,6 +372,21 @@ function renderColorTokens(tokens: ColorToken[]): string { return text.join(""); } +export function supportColor(): boolean { + try { + if (process.env.FORCE_COLOR !== undefined) { + return process.env.FORCE_COLOR !== "0"; + } + if (process.env.NO_COLOR !== undefined) { + return false; + } + return process.stdout.isTTY && process.stderr.isTTY; + } catch (e) { + // check color support failed, default false + return false; + } +} + class Term { constructor() { } @@ -412,8 +427,11 @@ class Term { return `\x1b[36m${message}\x1b[0m`; } - auto(message: string): string { - return renderColorTokens(parseColorTokens(message)); + auto(message: string, renderColor?: boolean): string { + return renderColorTokens( + parseColorTokens(message), + renderColor ?? supportColor(), + ); } }