adds flag coalescing to argparser

adds handling of inlined args to call tools
fixes terminal layout clearing troubles
This commit is contained in:
Emmaline Autumn 2025-04-30 03:06:19 -06:00
parent 65f0b4e0b7
commit 26b7089cc2
5 changed files with 58 additions and 60 deletions

View File

@ -79,15 +79,15 @@ export class TerminalLayout {
} }
clearAll() { clearAll() {
for (const name of this.layoutOrder) {
this.blocks[name].clear();
}
Deno.stdout.writeSync( Deno.stdout.writeSync(
new TextEncoder().encode( new TextEncoder().encode(
TerminalLayout.ALT_BUFFER_DISABLE, TerminalLayout.ALT_BUFFER_DISABLE,
), ),
); );
Cursor.show(); Cursor.show();
for (const name of this.layoutOrder) {
this.blocks[name].clear();
}
} }
clear() { clear() {
@ -223,7 +223,6 @@ export class TerminalBlock {
for (let i = 0; i < this.renderedLineCount; i++) { for (let i = 0; i < this.renderedLineCount; i++) {
Deno.stdout.writeSync(new TextEncoder().encode(`\x1b[2K\x1b[1E`)); Deno.stdout.writeSync(new TextEncoder().encode(`\x1b[2K\x1b[1E`));
} }
this.renderedLineCount = 0;
} }
clearAll() { clearAll() {

View File

@ -1,5 +1,6 @@
export class ArgParser { export class ArgParser<T extends Record<string, string[]>> {
private args: string[]; private args: string[];
private flags: Map<keyof T, boolean> = new Map();
constructor(args: string[]) { constructor(args: string[]) {
this.args = args; this.args = args;
@ -11,7 +12,22 @@ export class ArgParser {
return this.args[index + 1]; return this.args[index + 1];
} }
get flags() { setFlagDefs(flagDefs: T) {
for (const [flag, defs] of Object.entries(flagDefs)) {
for (const def of defs) {
if (this.argFlags.includes(def)) {
this.flags.set(flag, true);
}
}
}
return this;
}
getFlag(flag: keyof T) {
return this.flags.get(flag);
}
get argFlags() {
return this.args.filter((arg) => arg.startsWith("-")); return this.args.filter((arg) => arg.startsWith("-"));
} }
@ -26,6 +42,9 @@ export class ArgParser {
get task() { get task() {
return this.nonFlags[0]; return this.nonFlags[0];
} }
get taskArgs() {
return this.nonFlags.slice(1);
}
static parse(args: string[]) { static parse(args: string[]) {
return new ArgParser(args); return new ArgParser(args);

View File

@ -9,8 +9,11 @@ import { cliAlert, cliLog } from "./prompts.ts";
export class PdfToolsCli { export class PdfToolsCli {
private tools: Map<string, ITool> = new Map(); private tools: Map<string, ITool> = new Map();
private terminalLayout = new TerminalLayout(); private terminalLayout = new TerminalLayout();
closeMessage?: string;
private args = ArgParser.parse(Deno.args); private args = ArgParser.parse(Deno.args).setFlagDefs({
help: ["-h", "--help"],
});
async importTools(tools?: string) { async importTools(tools?: string) {
tools = tools?.replace(/\/$/, "").replace(/^\.?\//, "") || "tools"; tools = tools?.replace(/\/$/, "").replace(/^\.?\//, "") || "tools";
@ -51,17 +54,22 @@ export class PdfToolsCli {
this.terminalLayout.register("title", titleBlock); this.terminalLayout.register("title", titleBlock);
const bodyBlock = new TerminalBlock(); const bodyBlock = new TerminalBlock();
this.terminalLayout.register("body", bodyBlock); this.terminalLayout.register("body", bodyBlock);
this.embiggenHeader(); if (this.args.getFlag("help") && !this.args.task) {
if (Deno.args.length === 0) { await this.help();
// console.log( return;
// colorize("No tool specified. Importing all tools...", "gray"), } else if (this.args.nonFlags.length === 0 || !this.args.task) {
// ); this.embiggenHeader();
await this.importTools(); await this.importTools();
await this.toolMenu();
} else {
await this.importTools();
const task = this.args.task;
await this.runTool(toCase(task, "title"));
} }
await this.toolMenu();
} finally { } finally {
this.terminalLayout.clearAll(); this.terminalLayout.clearAll();
Deno.stdin.setRaw(false); Deno.stdin.setRaw(false);
if (this.closeMessage) console.log(this.closeMessage);
} }
} }
@ -95,9 +103,15 @@ export class PdfToolsCli {
const bodyBlock = this.terminalLayout.getBlock("body"); const bodyBlock = this.terminalLayout.getBlock("body");
bodyBlock.clearAll(); bodyBlock.clearAll();
tool.setBlock?.(bodyBlock); tool.setBlock?.(bodyBlock);
await tool.run(); if (this.args.getFlag("help")) {
await tool.done?.(); await tool.help?.();
this.embiggenHeader(); } else {
await tool.run(...this.args.taskArgs);
await tool.done?.();
}
await this.embiggenHeader();
} else {
this.closeMessage = "No tool found for " + toolName;
} }
} }

View File

@ -1,4 +1,5 @@
import { Cursor } from "./cursor.ts"; import { Cursor } from "./cursor.ts";
import { ScrollManager } from "./scrollManager.ts";
import { colorize } from "./style.ts"; import { colorize } from "./style.ts";
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts"; import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
@ -11,11 +12,8 @@ export async function cliPrompt(
await Deno.stdin.setRaw(true); await Deno.stdin.setRaw(true);
let cursorVisible = true; const cursorVisible = Cursor["visible"];
if (!block) { Cursor.show();
cursorVisible = Cursor["visible"];
Cursor.show();
}
let range: [number, number] = [0, 1]; let range: [number, number] = [0, 1];
if (block) { if (block) {
@ -54,7 +52,7 @@ export async function cliPrompt(
} }
await Deno.stdin.setRaw(false); await Deno.stdin.setRaw(false);
if (!block && !cursorVisible) { if (!cursorVisible) {
Cursor.hide(); Cursor.hide();
} }
Deno.stdout.writeSync(encoder.encode("\n")); Deno.stdout.writeSync(encoder.encode("\n"));
@ -86,10 +84,12 @@ export function cliLog(message: string, block?: TerminalBlock) {
} }
if (import.meta.main) { if (import.meta.main) {
Cursor.hide();
const layout = new TerminalLayout(); const layout = new TerminalLayout();
const title = new TerminalBlock(); const title = new TerminalBlock();
const block = new TerminalBlock(); const block = new TerminalBlock();
block.setPreserveHistory(true); block.setPreserveHistory(true);
// ScrollManager.enable(block);
title.setLines(["Hello, World!"]); title.setLines(["Hello, World!"]);
title.setFixedHeight(1); title.setFixedHeight(1);
@ -105,6 +105,7 @@ if (import.meta.main) {
cliLog(`Hello, ${name}!`, block); cliLog(`Hello, ${name}!`, block);
const single = await cliConfirm("Are you single?", block); const single = await cliConfirm("Are you single?", block);
cliLog(single ? "Do you want to go out with me?" : "Okay", block); cliLog(single ? "Do you want to go out with me?" : "Okay", block);
// ScrollManager.enable(block);
const loopingConvo = [ const loopingConvo = [
"No response?", "No response?",
"I guess that's okay", "I guess that's okay",

View File

@ -4,25 +4,9 @@ import { callWithArgPrompt } from "util/call.ts";
import { TerminalBlock } from "../cli/TerminalLayout.ts"; import { TerminalBlock } from "../cli/TerminalLayout.ts";
import { forceArgs } from "../cli/forceArgs.ts"; import { forceArgs } from "../cli/forceArgs.ts";
import { colorize } from "../cli/style.ts"; import { colorize } from "../cli/style.ts";
import { cliLog, cliPrompt } from "../cli/prompts.ts"; import { cliAlert, cliLog, cliPrompt } from "../cli/prompts.ts";
import { multiSelectMenuInteractive } from "../cli/selectMenu.ts"; import { multiSelectMenuInteractive } from "../cli/selectMenu.ts";
// const thing = PDFAcroField.prototype.getFullyQualifiedName;
// PDFAcroField.prototype.getFullyQualifiedName = function () {
// const name = thing.call(this)
// // if (name?.includes('langauge'))
// console.log(name)
// return name;
// }
// const thing = PDFHexString.prototype.copyBytesInto
// PDFHexString.prototype.copyBytesInto = function (buffer: Uint8Array, offset: number) {
// console.log((this as any).value)
// const result = thing.call(this, buffer, offset)
// return result;
// }
async function renameFields( async function renameFields(
path: string, path: string,
pattern: string | RegExp, pattern: string | RegExp,
@ -48,29 +32,10 @@ async function renameFields(
if (mName) { if (mName) {
changesMade = true; changesMade = true;
cField.dict.set(PDFName.of("T"), PDFString.of(mName)); cField.dict.set(PDFName.of("T"), PDFString.of(mName));
// console.log(cField.getPartialName())
} }
} }
cField = cField.getParent(); cField = cField.getParent();
// console.log(cField?.getPartialName())
} }
console.log(field.getName());
// const newName = name.replace(pattern, change);
// console.log("Change to: %c" + newName, "color: yellow");
// if (confirm('Ok?')) {
// let parent = field.acroField.getParent();
// field.acroField.setPartialName(segments.pop())
// while (parent && segments.length) {
// console.log(parent.getPartialName())
// parent.setPartialName(segments.pop())
// parent = parent.getParent();
// }
// changesMade = true;
// console.log(field.getName())
// // dict.set(PDFName.of("T"), PDFHexString.fromText(newName))
// console.log("%cDone!", "color: lime")
// }
// break;
} }
} }
if (changesMade) { if (changesMade) {
@ -123,8 +88,8 @@ class RenameFields implements ITool {
} }
help(standalone = false) { help(standalone = false) {
cliLog( cliAlert(
"Usage: renamefields <pdfPath> <pattern> <change>", "Usage: rename-fields <pdfPath> <pattern> <change>\n",
standalone ? undefined : this.block, standalone ? undefined : this.block,
); );
} }