This commit is contained in:
2026-02-08 15:47:10 +08:00
parent fa361f0809
commit 4428a5e3fc
3 changed files with 34 additions and 18 deletions

View File

@@ -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;

View File

@@ -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",

View File

@@ -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=