update deno commons

This commit is contained in:
2026-02-06 23:03:20 +08:00
parent 141c04ba7f
commit ca3dc2377f

View File

@@ -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(),
);
}
}