From 4428a5e3fc2902b7652e214030b6cc81c62ff0b1 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 8 Feb 2026 15:47:10 +0800 Subject: [PATCH] term --- libraries/deno-commons-mod.ts | 36 +++++++++++++++++++++++++---------- script-meta-v2.json | 6 +++--- single-scripts/term-color.ts | 10 +++++----- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index 73030f8..d8f6a28 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -256,6 +256,17 @@ function parseColorTokens(message: string, renderColor: boolean): ColorToken[] { let chars: string[] = []; let startedColors: string[] = []; const messageLength = message.length; + const _isAllSlashOrEmpty = function (chars: string): { + result: boolean; + count: number; + } { + for (const char of chars) { + if (char !== "/") { + return { result: false, count: -1 }; + } + } + return { result: true, count: chars.length }; + }; for (let i = 0; i < messageLength; i++) { const c = message.charAt(i); const nextC = (i + 1) < messageLength @@ -308,7 +319,21 @@ function parseColorTokens(message: string, renderColor: boolean): ColorToken[] { break; case "]": if (inColorStart || inColorEnd) { - if (chars.length > 0) { + const isAllSlashOrEmpty = _isAllSlashOrEmpty(chars); + if (isAllSlashOrEmpty.result && inColorEnd) { + const popCount = isAllSlashOrEmpty.count + 1; + for (let _i = 0; _i < popCount; _i++) { + const poppedColor = startedColors.pop(); + if (poppedColor) { + tokens.push({ + type: "color", + colorStart: false, + color: poppedColor, + }); + } + } + chars = []; + } else if (chars.length > 0) { tokens.push({ type: "color", colorStart: inColorStart, @@ -320,15 +345,6 @@ function parseColorTokens(message: string, renderColor: boolean): ColorToken[] { startedColors.pop(); } chars = []; - } else if (chars.length === 0 && inColorEnd) { - const poppedColor = startedColors.pop(); - if (poppedColor) { - tokens.push({ - type: "color", - colorStart: false, - color: poppedColor, - }); - } } inColorStart = false; inColorEnd = false; diff --git a/script-meta-v2.json b/script-meta-v2.json index 478874a..70ac94f 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -256,12 +256,12 @@ }, "term-color.ts": { "script_name": "term-color.ts", - "script_length": 1456, - "script_sha256": "ab27f067d4289e80e66ac248aa665a6cf7daee0a1c3305a6f8fa3310561d6073", + "script_length": 1472, + "script_sha256": "64523ef7952cc9e189b04605677e2e03193b9435b9d171423d590fd6a37280e3", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/term-color.ts", "single_script_file": true, "publish_time": 1770533214194, - "update_time": 1770533506887 + "update_time": 1770536826085 }, "tree.ts": { "script_name": "tree.ts", diff --git a/single-scripts/term-color.ts b/single-scripts/term-color.ts index 6f895c2..7564021 100755 --- a/single-scripts/term-color.ts +++ b/single-scripts/term-color.ts @@ -1,6 +1,6 @@ #!/usr/bin/env runts -- --allow-import -import {term} from "https://script.hatter.ink/@32/deno-commons-mod.ts"; +import {term} from "https://script.hatter.ink/@33/deno-commons-mod.ts"; function printColors(colors: string[], prefix: string, suffix: string) { const termOutputs: string[] = []; @@ -35,16 +35,16 @@ async function main() { console.log(); console.log( term.auto( - "[bg_black][white][bold][[[[bg_black][white][bold]Bold, white and black background [/][/][/]]]][/][/][/]", + "[bg_black][white][bold][[[[bg_black][white][bold]Bold, white and black background [/][/][/]]]][/][/][/] plaintext", ), ); console.log( term.auto( - "[black][bg_white][bold][[[[black][bg_white][bold]Bold, black and white background [/][/][/]]]][/][/][/]", + "[black][bg_white][bold][[[[black][bg_white][bold]Bold, black and white background [///]]]][///] plaintext", ), ); } await main(); -// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260208T145142+08:00.MEUCIEk9mPBqcyKRIOymv3+v -// dGH7SuuTE4RuUg966B2noRjUAiEAgpQ3vd8PN23J6XUTgPbGtdiNHXSqtJyvDOev3LxuNcQ= +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260208T154653+08:00.MEUCIQCt2qj/3lPKwhX8n5ID +// jvkItXyST6mG1/j3fcUHybaePQIgAM4ErXInKntZU3DvmuX8szEFXIcFN1W3+zYLzPuYywk=