From 4d61625252962343f503b995f9d6cbd7f012a8f4 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 19 Jan 2025 16:22:50 +0800 Subject: [PATCH] feat: update deno-commons-mod.ts --- libraries/deno-commons-mod.ts | 96 ++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index a481d20..5a1fed1 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -1,7 +1,7 @@ // Reference: // - https://docs.deno.com/runtime/fundamentals/testing/ -import {assertEquals} from "jsr:@std/assert"; +import { assertEquals } from "jsr:@std/assert"; export function compareVersion(ver1: string, ver2: string): 0 | 1 | -1 { if (ver1 === ver2) return 0; @@ -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(""));