diff --git a/script-meta-v2.json b/script-meta-v2.json index 8594690..dcadeb7 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -153,12 +153,12 @@ }, "howto.ts": { "script_name": "howto.ts", - "script_length": 2337, - "script_sha256": "778267bdb0d5005951090f8e3dc608da507343b06f4c1cc507b9ea797bed64ea", + "script_length": 3597, + "script_sha256": "741b5f399a75438f7f50f4a8118b8f3ab11a2e752d5056ca4d659f7a26b3333b", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/howto.ts", "single_script_file": true, "publish_time": 1775973075081, - "update_time": 1775981177228 + "update_time": 1776004257105 }, "init-cheatsheet.ts": { "script_name": "init-cheatsheet.ts", diff --git a/single-scripts/howto.ts b/single-scripts/howto.ts index 303c336..630c0ec 100755 --- a/single-scripts/howto.ts +++ b/single-scripts/howto.ts @@ -38,18 +38,47 @@ howto.ts 'MESSAGE' - generate command line for MESSAGE if (!noExec) { try { - const commandLine = extractCommand(summary); + const commandLines = extractCommand(summary); console.log( - "Found command line: ", - term.auto(`[green][[[${commandLine}]]][/]`), + "Found command lines: ", + term.auto(`[green][[[${commandLines}]]][/]`), ); - if (confirm("Execute this command?")) { - const status = await execCommandShell("sh", [ - "-c", - commandLine, - ]); - exit(status); + if (commandLines.length == 1) { + if (confirm("Execute this command?")) { + exit( + await execCommandShell("sh", [ + "-c", + commandLines[0], + ]), + ); + } + } else { + for (const [i, command] of commandLines.entries()) { + console.log(`# ${i}: ${command}`); + } + const selectCommandLineIndex = parseInt( + prompt( + "Please select the command line: ", + ), + 10, + ); + if ( + isNaN(selectCommandLineIndex) || + selectCommandLineIndex < 0 || + selectCommandLineIndex >= commandLines.length + ) { + log.error( + `Bad command line index: ${selectCommandLineIndex}`, + ); + } else { + exit( + await execCommandShell("sh", [ + "-c", + commandLines[0], + ]), + ); + } } } catch (e) { log.error("Extract command line failed", e); @@ -57,20 +86,26 @@ howto.ts 'MESSAGE' - generate command line for MESSAGE } } -function extractCommand(summary: string): string { +function extractCommand(summary: string): string[] { const lines = summary.split(/\r?\n/); - const sb = []; + const codeBlocks: string[] = []; + const sb: string[] = []; let inCode = false; for (const line of lines) { if (inCode) { if (line.startsWith("```")) { - return sb.join("\n"); + codeBlocks.push(sb.join("\n")); + inCode = false; + } else { + sb.push(line); } - sb.push(line); } else if (line.startsWith("```")) { inCode = true; } } + if (codeBlocks.length > 0) { + return codeBlocks; + } throw new Error("Command line not found"); } @@ -79,5 +114,5 @@ main().catch((err) => { process.exit(0); }).then(() => process.exit(0)); -// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260412T160614+08:00.MEUCIEasEUKkR/P0ESG/IpIB -// 9turT2yM/EoOpSEwQdlz277qAiEAxCYPwvP563coKn1Ov3wI1icQR3Tesg2Hta0tdIn8f+8= +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260412T223039+08:00.MEQCID73a9pHazHm+d5lLNqX +// w4YNafpy3qBHxKky88N9b4mCAiAnbuECrSICNjIK/onLSpWLc0MHjpdQyqGgL0B6pU24RQ==