adds flag coalescing to argparser

adds handling of inlined args to call tools
fixes terminal layout clearing troubles
This commit is contained in:
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() {
for (const name of this.layoutOrder) {
this.blocks[name].clear();
}
Deno.stdout.writeSync(
new TextEncoder().encode(
TerminalLayout.ALT_BUFFER_DISABLE,
),
);
Cursor.show();
for (const name of this.layoutOrder) {
this.blocks[name].clear();
}
}
clear() {
@@ -223,7 +223,6 @@ export class TerminalBlock {
for (let i = 0; i < this.renderedLineCount; i++) {
Deno.stdout.writeSync(new TextEncoder().encode(`\x1b[2K\x1b[1E`));
}
this.renderedLineCount = 0;
}
clearAll() {

View File

@@ -1,5 +1,6 @@
export class ArgParser {
export class ArgParser<T extends Record<string, string[]>> {
private args: string[];
private flags: Map<keyof T, boolean> = new Map();
constructor(args: string[]) {
this.args = args;
@@ -11,7 +12,22 @@ export class ArgParser {
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("-"));
}
@@ -26,6 +42,9 @@ export class ArgParser {
get task() {
return this.nonFlags[0];
}
get taskArgs() {
return this.nonFlags.slice(1);
}
static parse(args: string[]) {
return new ArgParser(args);

View File

@@ -9,8 +9,11 @@ import { cliAlert, cliLog } from "./prompts.ts";
export class PdfToolsCli {
private tools: Map<string, ITool> = new Map();
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) {
tools = tools?.replace(/\/$/, "").replace(/^\.?\//, "") || "tools";
@@ -51,17 +54,22 @@ export class PdfToolsCli {
this.terminalLayout.register("title", titleBlock);
const bodyBlock = new TerminalBlock();
this.terminalLayout.register("body", bodyBlock);
this.embiggenHeader();
if (Deno.args.length === 0) {
// console.log(
// colorize("No tool specified. Importing all tools...", "gray"),
// );
if (this.args.getFlag("help") && !this.args.task) {
await this.help();
return;
} else if (this.args.nonFlags.length === 0 || !this.args.task) {
this.embiggenHeader();
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 {
this.terminalLayout.clearAll();
Deno.stdin.setRaw(false);
if (this.closeMessage) console.log(this.closeMessage);
}
}
@@ -95,9 +103,15 @@ export class PdfToolsCli {
const bodyBlock = this.terminalLayout.getBlock("body");
bodyBlock.clearAll();
tool.setBlock?.(bodyBlock);
await tool.run();
await tool.done?.();
this.embiggenHeader();
if (this.args.getFlag("help")) {
await tool.help?.();
} 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 { ScrollManager } from "./scrollManager.ts";
import { colorize } from "./style.ts";
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
@@ -11,11 +12,8 @@ export async function cliPrompt(
await Deno.stdin.setRaw(true);
let cursorVisible = true;
if (!block) {
cursorVisible = Cursor["visible"];
Cursor.show();
}
const cursorVisible = Cursor["visible"];
Cursor.show();
let range: [number, number] = [0, 1];
if (block) {
@@ -54,7 +52,7 @@ export async function cliPrompt(
}
await Deno.stdin.setRaw(false);
if (!block && !cursorVisible) {
if (!cursorVisible) {
Cursor.hide();
}
Deno.stdout.writeSync(encoder.encode("\n"));
@@ -86,10 +84,12 @@ export function cliLog(message: string, block?: TerminalBlock) {
}
if (import.meta.main) {
Cursor.hide();
const layout = new TerminalLayout();
const title = new TerminalBlock();
const block = new TerminalBlock();
block.setPreserveHistory(true);
// ScrollManager.enable(block);
title.setLines(["Hello, World!"]);
title.setFixedHeight(1);
@@ -105,6 +105,7 @@ if (import.meta.main) {
cliLog(`Hello, ${name}!`, block);
const single = await cliConfirm("Are you single?", block);
cliLog(single ? "Do you want to go out with me?" : "Okay", block);
// ScrollManager.enable(block);
const loopingConvo = [
"No response?",
"I guess that's okay",