From d1c5db0e0755a32cf23b678ad3235578d26c09a5 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 8 Feb 2026 14:26:24 +0800 Subject: [PATCH] updates ssh.ts --- libraries/deno-commons-mod.ts | 60 +++++++++++++++++++++++++++++++++-- single-scripts/ssh.ts | 39 +++++++++++++---------- 2 files changed, 81 insertions(+), 18 deletions(-) diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index bfb34de..538dbc5 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -252,6 +252,7 @@ function parseColorTokens(message: string, renderColor: boolean): ColorToken[] { if (message) { let inColorStart = false; let inColorEnd = false; + let noEscape = false; let chars: string[] = []; let startedColors: string[] = []; const messageLength = message.length; @@ -260,6 +261,19 @@ function parseColorTokens(message: string, renderColor: boolean): ColorToken[] { const nextC = (i + 1) < messageLength ? message.charAt(i + 1) : null; + const nextNexC = (i + 2) < messageLength + ? message.charAt(i + 2) + : null; + if (noEscape) { + if (c === "]" && nextC === "]" && nextNexC === "]") { + // end no escape + noEscape = false; + i += 2; + } else { + chars.push(c); + } + continue; + } switch (c) { case "\\": if (nextC === null) { @@ -276,6 +290,11 @@ function parseColorTokens(message: string, renderColor: boolean): ColorToken[] { } else if (nextC == "/") { inColorEnd = true; i++; + } else if (nextC == "[" && nextNexC == "[") { + // now no escape + noEscape = true; + i += 2; + break; } else { inColorStart = true; } @@ -337,14 +356,51 @@ const COLOR_MAP: Record = { blink: "5", bold: "1", under: "4", + + black: "30", red: "31", green: "32", yellow: "33", blue: "34", pink: "35", cyan: "36", + white: "37", + + bg_black: "40", + bg_red: "41", + bg_green: "42", + bg_yellow: "43", + bg_blue: "44", + bg_pink: "45", + bg_cyan: "46", + bg_white: "47", + + black_bright: "90", + red_bright: "91", + green_bright: "92", + yellow_bright: "93", + blue_bright: "94", + pink_bright: "95", + cyan_bright: "96", + white_bright: "97", + + bg_black_bright: "100", + bg_red_bright: "101", + bg_green_bright: "102", + bg_yellow_bright: "103", + bg_blue_bright: "104", + bg_pink_bright: "105", + bg_cyan_bright: "106", + bg_white_bright: "107", }; +function getColorCode(color: string): string { + if (color.startsWith("#")) { + return color.substring(1); + } + return COLOR_MAP[color]; +} + function renderColorTokens(tokens: ColorToken[]): string { const text: string[] = []; const colorMapStack = new Map(); @@ -352,7 +408,7 @@ function renderColorTokens(tokens: ColorToken[]): string { if (token.type === "color") { const color = token.color; if (color) { - const colorCode = COLOR_MAP[color]; + const colorCode = getColorCode(color); if (!colorCode) { text.push(`[${token.colorStart ? "" : "/"}${color}]`); continue; @@ -370,7 +426,7 @@ function renderColorTokens(tokens: ColorToken[]): string { const colors: string[] = []; for (const [color, colorStack] of colorMapStack) { if (colorStack.length > 0) { - const currentColorCode = COLOR_MAP[color]; + const currentColorCode = getColorCode(color); if (currentColorCode) { colors.push(currentColorCode); } diff --git a/single-scripts/ssh.ts b/single-scripts/ssh.ts index 07711e1..4aea4ad 100755 --- a/single-scripts/ssh.ts +++ b/single-scripts/ssh.ts @@ -4,7 +4,7 @@ import {parseArgs} from "node:util"; -import {execCommandShell, log, readFileToString, term,} from "https://script.hatter.ink/@29/deno-commons-mod.ts"; +import {execCommandShell, log, readFileToString, term,} from "https://script.hatter.ink/@32/deno-commons-mod.ts"; import JSON5 from "npm:json5"; class SshTsArgs { @@ -49,7 +49,11 @@ function printSshConfig(sshConfig: SshConfig) { maxProfileHostLength = sshProfile.host.length; } } - console.log(term.auto("[green][OK ][/green] Total 10 server(s):")); + console.log( + term.auto( + `[green]\\[OK ][/] Total [bold]${allProfiles.length}[/] server(s):`, + ), + ); allProfiles.sort((a, b) => a.localeCompare(b)); for (let i = 0; i < allProfiles.length; i++) { const k = allProfiles[i]; @@ -57,7 +61,7 @@ function printSshConfig(sshConfig: SshConfig) { const features = []; if (sshProfile.proxy) features.push("proxy"); if (sshProfile.forward_agent) features.push("forward_agent"); - const nameWithPad = `${k}${ + const nameWithPad = `[bold]${k}[/]${ " ".repeat(maxProfileNameLength - k.length) }`; const hostWithPad = `${sshProfile.host}${ @@ -69,7 +73,7 @@ function printSshConfig(sshConfig: SshConfig) { : "aliases" }: [${(sshProfile.alias && sshProfile.alias.join(", ")) || ""}]`; console.log(term.auto( - `- ${nameWithPad} : [blue]${hostWithPad}[/blue] [yellow]${alias}[/yellow] # ${sshProfile.comment}${ + `- ${nameWithPad} : [blue][[[${hostWithPad}]]][/] [yellow][[[${alias}]]][/] # ${sshProfile.comment}${ (features.length > 0) ? (" ;[" + features.join(" ") + "]") : "" }`, )); @@ -80,7 +84,9 @@ function printSshConfig(sshConfig: SshConfig) { if (sshConfig.default_proxy) features.push("proxy"); if (sshConfig.default_forward_agent) features.push("forward_agent"); console.log(); - log.info(`Global default features: [${features.join(" ")}]`); + log.info( + term.auto(`Global default features: [[[[${features.join(" ")}]]]]`), + ); } } @@ -194,20 +200,21 @@ async function main() { sshConfig.default_username || "root"; sshArgs.push(`${sshUsername}@${sshProfile.host}`); + const joinedSshArgs = sshArgs.map((arg) => { + if (arg.includes(" ")) { + return `"${arg}"`; + } else { + return arg; + } + }).join(" "); console.log( - term.auto(`[green][OK ][/green] ${sshCommand} ${ - sshArgs.map((arg) => { - if (arg.includes(" ")) { - return `"${arg}"`; - } else { - return arg; - } - }).join(" ") - }`), + term.auto( + `[green]\\[OK ][/] [bold][blue][[[${sshCommand}]]][/][/] [yellow][[[${joinedSshArgs}]]][/]`, + ), ); await execCommandShell(sshCommand, sshArgs); } await main(); -// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260207T015534+08:00.MEYCIQDrK2IaMQnSL1d0Z+jd -// G9+7qDQzyNxCR2N18b/+3lHjHQIhAM/Om/+Z6qoK2hiX84idYKeHc/JmizWUi/yqxI+xcvWl +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260208T142616+08:00.MEYCIQDb99eHtM0E2g2/SJc2 +// cLX9TpTZIY6WFl1hR0F9E3RmhwIhAJMoT77cJYjJwvLySwe8QYzGrpWHe4ROBIdRXiQERNMT