pdf-tools/cli/index.ts

65 lines
1.8 KiB
TypeScript

import { getAsciiArt } from "../util/asciiArt.ts";
import { toCase } from "util/caseManagement.ts";
import { ArgParser } from "./argParser.ts";
import { colorize } from "./colorize.ts";
import { selectMenuInteractive } from "./selectMenu.ts";
export class PdfToolsCli {
private tools: Map<string, ITool> = new Map();
async importTools(tools?: string) {
tools = tools?.replace(/\/$/, "").replace(/^\.?\//, "") || "tools";
for (const toolfile of Deno.readDirSync(tools)) {
if (toolfile.isFile) {
const tool = await import(
Deno.cwd() + "/" + tools + "/" + toolfile.name
);
if (tool.default) {
this.tools.set(
toCase(toolfile.name.replace(".ts", ""), "title"),
tool.default,
);
}
}
}
}
private async banana() {
const asciiArt = await getAsciiArt("banana");
console.log(colorize(asciiArt, "yellow"));
}
private help() {
console.log("BearMetal PDF CLI");
}
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;
}
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) {
const tools = this.tools.keys().toArray();
const selected = await selectMenuInteractive("Choose a tool", tools);
if (!selected) return;
const tool = this.tools.get(toCase(selected, "camel"));
if (tool) {
await tool.run();
}
}
}