From a42633f3b743dac2dcd836fec025205365d72ed7 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 25 Jan 2026 13:38:41 +0800 Subject: [PATCH] update deno-commons-mode.ts --- libraries/deno-commons-mod.ts | 43 +++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index f1573c1..f3d3d90 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -1,10 +1,10 @@ // Reference: // - https://docs.deno.com/runtime/fundamentals/testing/ -import { assert } from "jsr:@std/assert/assert"; -import { assertEquals } from "jsr:@std/assert"; -import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64"; -import { dirname } from "https://deno.land/std@0.208.0/path/mod.ts"; +import {assert} from "jsr:@std/assert/assert"; +import {assertEquals} from "jsr:@std/assert"; +import {decodeBase64, encodeBase64} from "jsr:@std/encoding/base64"; +import {dirname} from "https://deno.land/std@0.208.0/path/mod.ts"; // reference: https://docs.deno.com/examples/hex_base64_encoding/ // import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64"; @@ -439,6 +439,41 @@ export function setKeyRingPassword( return; } +export class ProcessBar { + interval?: number; + message: string; + constructor(message?: string) { + this.message = message || "Processing"; + } + async call(cb: () => Promise, clearLine?: boolean): Promise { + this.start(); + try { + return await cb(); + } finally { + this.stop(clearLine); + } + } + start(): void { + const startMs = new Date().getTime(); + let count = 0; + this.interval = setInterval(() => { + const dots = ".".repeat(((count++) % 10) + 1); + const costMs = new Date().getTime() - startMs; + let time = `${costMs}ms`; + if (costMs > 1000) { + time = `${Math.floor(costMs / 1000)}s`; + } + process.stderr.write(`\r${this.message} ${time} ${dots}\x1b[K`); + }, 200); + } + stop(clearLine?: boolean): void { + if (this.interval) { + clearInterval(this.interval); + process.stderr.write(clearLine ? "\r" : "\n"); + } + } +} + Deno.test("isOn", () => { assertEquals(false, isOn(undefined)); assertEquals(false, isOn(""));