From 2634f40f2b223050788247b72f7049d3dc8ba404 Mon Sep 17 00:00:00 2001 From: Emma Date: Thu, 24 Apr 2025 21:40:59 -0600 Subject: [PATCH] struggle --- cli/TerminalLayout.ts | 4 +++ cli/index.ts | 57 +++++++++++++++++++++++++++++------------- deno.json | 5 ++-- tools/fieldRename.ts | 16 ++++++++++++ types.ts | 1 + util/call.ts | 6 ++++- util/caseManagement.ts | 2 -- 7 files changed, 68 insertions(+), 23 deletions(-) diff --git a/cli/TerminalLayout.ts b/cli/TerminalLayout.ts index 0703fd4..5cf9e34 100644 --- a/cli/TerminalLayout.ts +++ b/cli/TerminalLayout.ts @@ -27,6 +27,10 @@ export class TerminalLayout { } } + getBlock(name: string) { + return this.blocks[name]; + } + requestRender() { if (this.debounceTimer !== null) { clearTimeout(this.debounceTimer); diff --git a/cli/index.ts b/cli/index.ts index 814fa2a..b89fc78 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -3,9 +3,13 @@ import { toCase } from "util/caseManagement.ts"; import { ArgParser } from "./argParser.ts"; import { colorize } from "./colorize.ts"; import { selectMenuInteractive } from "./selectMenu.ts"; +import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts"; export class PdfToolsCli { private tools: Map = new Map(); + private terminalLayout = new TerminalLayout(); + + private args = ArgParser.parse(Deno.args); async importTools(tools?: string) { tools = tools?.replace(/\/$/, "").replace(/^\.?\//, "") || "tools"; @@ -34,31 +38,48 @@ export class PdfToolsCli { } public async run() { - console.clear(); - let lineCount = 0; - for (const t of ["bearmetal:porple", "pdftools:cyan"]) { - const [name, color] = t.split(":"); - const asciiArt = await getAsciiArt(name); - console.log(colorize(asciiArt, color)); - lineCount += asciiArt.split("\n").length; + try { + const lines: string[] = []; + for (const t of ["bearmetal:porple", "pdftools:cyan"]) { + const [name, color] = t.split(":"); + const asciiArt = await getAsciiArt(name); + lines.push(...colorize(asciiArt, color).split("\n")); + } + const titleBlock = new TerminalBlock(); + this.terminalLayout.register("title", titleBlock); + const bodyBlock = new TerminalBlock(); + this.terminalLayout.register("body", bodyBlock); + titleBlock.setFixedHeight(lines.length); + titleBlock.setLines(lines); + if (Deno.args.length === 0) { + // console.log( + // colorize("No tool specified. Importing all tools...", "gray"), + // ); + await this.importTools(); + } + this.toolMenu(); + } finally { + this.terminalLayout.clearAll(); } - if (Deno.args.length === 0) { - console.log( - colorize("No tool specified. Importing all tools...", "gray"), - ); - await this.importTools(); - } - const args = ArgParser.parse(Deno.args); - this.toolMenu(Deno.consoleSize().rows - lineCount); } - private async toolMenu(space?: number) { + private async toolMenu() { const tools = this.tools.keys().toArray(); - const selected = await selectMenuInteractive("Choose a tool", tools); + const bodyBlock = this.terminalLayout.getBlock("body"); + const selected = await selectMenuInteractive("Choose a tool", tools, { + terminalBlock: bodyBlock, + }); + bodyBlock.clear(); if (!selected) return; - const tool = this.tools.get(toCase(selected, "camel")); + await this.runTool(selected); + this.toolMenu(); + } + + private async runTool(toolName: string) { + const tool = this.tools.get(toolName); if (tool) { await tool.run(); + await tool.done?.(); } } } diff --git a/deno.json b/deno.json index a58620e..4dbb894 100644 --- a/deno.json +++ b/deno.json @@ -1,9 +1,10 @@ { "name": "@bearmetal/pdf-tools", "tasks": { - "dev": "deno run -A --watch main.ts", + "dev": "deno run -A --env-file=.env --watch main.ts", "compile": "deno compile -o compare-form-fields.exe --target x86_64-pc-windows-msvc -R ./main.ts", - "install": "deno install -fgq --import-map ./deno.json -n checkfields -R ./main.ts" + "install": "deno install -fgq --import-map ./deno.json -n checkfields -R ./main.ts", + "debug": "deno run -A --env-file=.env --inspect-wait --watch main.ts" }, "imports": { "@std/assert": "jsr:@std/assert@1", diff --git a/tools/fieldRename.ts b/tools/fieldRename.ts index 633074d..9fbe024 100644 --- a/tools/fieldRename.ts +++ b/tools/fieldRename.ts @@ -80,6 +80,22 @@ async function renameFields( } } +class RenameFields implements ITool { + name = "renamefields"; + description = "Renames fields in a PDF form"; + help() { + console.log("Usage: renamefields "); + } + async run(pdfPath: string = "", pattern: string = "", change: string = "") { + await callWithArgPrompt(renameFields, [ + ["Please provide path to PDF:", (p) => !!p && p.endsWith(".pdf")], + "Please provide search string:", + "Please provide requested change:", + ], [pdfPath, pattern, change]); + } +} +export default new RenameFields(); + if (import.meta.main) { // await call(renameFields) // while (!path || !path.endsWith('.pdf')) path = prompt("Please provide path to PDF:") || ''; diff --git a/types.ts b/types.ts index 413e1d3..aa54300 100644 --- a/types.ts +++ b/types.ts @@ -5,5 +5,6 @@ declare global { description: string; run: ToolFunc; help?: () => Promise | void; + done?: () => Promise | void; } } diff --git a/util/call.ts b/util/call.ts index 2197a5c..0d7cadb 100644 --- a/util/call.ts +++ b/util/call.ts @@ -15,7 +15,11 @@ export async function call( Object.assign(config, conf); } - const args = config.args || Deno.args; + let args = config.args || Deno.args; + if (!args.length && transforms.length) { + config.multiTransform ? args = transforms.map(() => "") : args = [""]; + } + const shouldPair = transforms.length === args.length; const multiTransform = config.multiTransform || !shouldPair && transforms.length > 1; diff --git a/util/caseManagement.ts b/util/caseManagement.ts index db0369e..d538c68 100644 --- a/util/caseManagement.ts +++ b/util/caseManagement.ts @@ -99,10 +99,8 @@ function coerceCaseToLower(str: string, caseType: CaseType) { export function toCase(str: string, toCase: CaseType) { const caseType = parseCase(str) || ""; - console.log(caseType); if (caseType === toCase) return str; const lowerStr = coerceCaseToLower(str, caseType); - console.log(lowerStr); switch (toCase) { case "pascal": return lowerToPascalCase(lowerStr);