feat: update deno-commons-mod.ts

This commit is contained in:
2025-01-19 16:22:50 +08:00
parent 4b2a88d1cc
commit 4d61625252

View File

@@ -136,6 +136,100 @@ export async function printLastLine(line: string) {
);
}
class Term {
constructor() {
}
blink(message: string): string {
return `\x1b[5m${message}\x1b[0m`;
}
bold(message: string): string {
return `\x1b[1m${message}\x1b[0m`;
}
red(message: string): string {
return `\x1b[31m${message}\x1b[0m`;
}
green(message: string): string {
return `\x1b[32m${message}\x1b[0m`;
}
yellow(message: string): string {
return `\x1b[33m${message}\x1b[0m`;
}
}
export const term = new Term();
function pad(message: string, length: number): string {
if (message.length >= length) {
return message;
}
return message + " ".repeat(length - message.length);
}
const LOGGER_PREFIX_LEN: number = 8;
class Logger {
constructor() {
}
// deno-lint-ignore no-explicit-any
success(...data: any[]) {
this.log(
term.bold(term.green(`[${pad("SUCCESS", LOGGER_PREFIX_LEN)}]`)),
data,
);
}
// deno-lint-ignore no-explicit-any
error(...data: any[]) {
this.log(
term.bold(term.red(`[${pad("ERROR", LOGGER_PREFIX_LEN)}]`)),
data,
);
}
// deno-lint-ignore no-explicit-any
warn(...data: any[]) {
this.log(
term.bold(term.yellow(`[${pad("WARN", LOGGER_PREFIX_LEN)}]`)),
data,
);
}
// deno-lint-ignore no-explicit-any
warning(...data: any[]) {
this.log(
term.blink(
term.bold(term.yellow(`[${pad("WARN", LOGGER_PREFIX_LEN)}]`)),
),
data,
);
}
// deno-lint-ignore no-explicit-any
info(...data: any[]) {
this.log(term.bold(`[${pad("INFO", LOGGER_PREFIX_LEN)}]`), data);
}
// deno-lint-ignore no-explicit-any
debug(...data: any[]) {
this.log(`[${pad("DEBUG", LOGGER_PREFIX_LEN)}]`, data);
}
// deno-lint-ignore no-explicit-any
log(prefix: string, data: any[]) {
const args = [prefix];
for (let i = 0; i < data.length; i++) {
args.push(data[i]);
}
console.log.apply(console, args);
}
}
export const log = new Logger();
Deno.test("isOn", () => {
assertEquals(false, isOn(undefined));
assertEquals(false, isOn(""));