Compare commits
13 Commits
2634f40f2b
...
dev
Author | SHA1 | Date | |
---|---|---|---|
37f7a58f96 | |||
4691ddc745 | |||
53cb40ebe8 | |||
680aae8b4f | |||
673424d755 | |||
490b948576 | |||
91eb569d4b | |||
d1072d8a81 | |||
6346b28581 | |||
03a1e3ed21 | |||
26b7089cc2 | |||
65f0b4e0b7 | |||
9535222fb7 |
32
.gitea/workflows/build-and-release.yml
Normal file
32
.gitea/workflows/build-and-release.yml
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
name: Build and Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Build and release binaries
|
||||||
|
uses: https://git.cyborggrizzly.com/bearmetal/ci-actions/deno-publish@main
|
||||||
|
with:
|
||||||
|
entrypoint: main.ts
|
||||||
|
compile-flags: "--allow-read --allow-write --allow-env --allow-net"
|
||||||
|
env:
|
||||||
|
GITEA_TOKEN: ${{ secrets.GIT_PAT }}
|
||||||
|
|
||||||
|
publish:
|
||||||
|
needs: build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Set up Deno
|
||||||
|
uses: denoland/setup-deno@v1
|
||||||
|
with:
|
||||||
|
deno-version: 2.3.1
|
||||||
|
|
||||||
|
- name: Publish to JSR
|
||||||
|
run: deno publish --token ${{ secrets.JSR_TOKEN }}
|
13
.gitea/workflows/tag-cli.yml
Normal file
13
.gitea/workflows/tag-cli.yml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
name: Create Version Tag
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: https://git.cyborggrizzly.com/bearmetal/ci-actions/version-check@main
|
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,3 +1,6 @@
|
|||||||
*.exe
|
*.exe
|
||||||
|
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
log.txt
|
||||||
|
log
|
@@ -1,3 +1,5 @@
|
|||||||
|
import { Cursor } from "./cursor.ts";
|
||||||
|
|
||||||
export class TerminalLayout {
|
export class TerminalLayout {
|
||||||
private static ALT_BUFFER_ENABLE = "\x1b[?1049h";
|
private static ALT_BUFFER_ENABLE = "\x1b[?1049h";
|
||||||
private static ALT_BUFFER_DISABLE = "\x1b[?1049l";
|
private static ALT_BUFFER_DISABLE = "\x1b[?1049l";
|
||||||
@@ -12,10 +14,16 @@ export class TerminalLayout {
|
|||||||
constructor() {
|
constructor() {
|
||||||
Deno.stdout.writeSync(
|
Deno.stdout.writeSync(
|
||||||
new TextEncoder().encode(
|
new TextEncoder().encode(
|
||||||
TerminalLayout.ALT_BUFFER_ENABLE + TerminalLayout.CURSOR_HIDE,
|
TerminalLayout.ALT_BUFFER_ENABLE,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
Cursor.hide();
|
||||||
this.height = Deno.consoleSize().rows;
|
this.height = Deno.consoleSize().rows;
|
||||||
|
|
||||||
|
Deno.addSignalListener("SIGINT", () => {
|
||||||
|
this.clearAll();
|
||||||
|
Deno.exit(0);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
register(name: string, block: TerminalBlock, fixedHeight?: number) {
|
register(name: string, block: TerminalBlock, fixedHeight?: number) {
|
||||||
@@ -71,14 +79,21 @@ export class TerminalLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clearAll() {
|
clearAll() {
|
||||||
Deno.stdout.writeSync(
|
|
||||||
new TextEncoder().encode(
|
|
||||||
TerminalLayout.ALT_BUFFER_DISABLE + TerminalLayout.CURSOR_SHOW,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
for (const name of this.layoutOrder) {
|
for (const name of this.layoutOrder) {
|
||||||
this.blocks[name].clear();
|
this.blocks[name].clear();
|
||||||
}
|
}
|
||||||
|
Deno.stdout.writeSync(
|
||||||
|
new TextEncoder().encode(
|
||||||
|
TerminalLayout.ALT_BUFFER_DISABLE,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Cursor.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
for (let i = 0; i < this.height; i++) {
|
||||||
|
Deno.stdout.writeSync(new TextEncoder().encode("\x1b[2K\x1b[1E"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get availableHeight() {
|
get availableHeight() {
|
||||||
@@ -96,14 +111,24 @@ export class TerminalBlock {
|
|||||||
private renderHeight: number = 0;
|
private renderHeight: number = 0;
|
||||||
private lastRenderRow = 1;
|
private lastRenderRow = 1;
|
||||||
|
|
||||||
|
private preserveHistory = false;
|
||||||
|
|
||||||
constructor(private prepend: string = "") {}
|
constructor(private prepend: string = "") {}
|
||||||
|
|
||||||
|
setPreserveHistory(preserveHistory: boolean) {
|
||||||
|
this.preserveHistory = preserveHistory;
|
||||||
|
}
|
||||||
|
|
||||||
setLayout(layout: TerminalLayout) {
|
setLayout(layout: TerminalLayout) {
|
||||||
this.layout = layout;
|
this.layout = layout;
|
||||||
}
|
}
|
||||||
|
|
||||||
setLines(lines: string[]) {
|
setLines(lines: string[], range?: [number, number]) {
|
||||||
this.lines = lines;
|
if (range && this.preserveHistory) {
|
||||||
|
this.lines.splice(range[0], range[1], ...lines);
|
||||||
|
} else {
|
||||||
|
this.lines = this.preserveHistory ? this.lines.concat(lines) : lines;
|
||||||
|
}
|
||||||
if (this.scrollOffset > lines.length - 1) {
|
if (this.scrollOffset > lines.length - 1) {
|
||||||
this.scrollOffset = Math.max(0, lines.length - 1);
|
this.scrollOffset = Math.max(0, lines.length - 1);
|
||||||
}
|
}
|
||||||
@@ -115,6 +140,19 @@ export class TerminalBlock {
|
|||||||
);
|
);
|
||||||
this.renderInternal();
|
this.renderInternal();
|
||||||
}
|
}
|
||||||
|
range = [
|
||||||
|
range?.[0] ?? this.lines.length - lines.length,
|
||||||
|
range ? range[0] + lines.length : this.lines.length,
|
||||||
|
];
|
||||||
|
return range;
|
||||||
|
}
|
||||||
|
|
||||||
|
append(lines: string[]) {
|
||||||
|
this.lines.push(...lines);
|
||||||
|
this.scrollTo(this.lines.length - 1);
|
||||||
|
if (this.layout) {
|
||||||
|
this.layout.requestRender();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollTo(offset: number) {
|
scrollTo(offset: number) {
|
||||||
@@ -144,15 +182,6 @@ export class TerminalBlock {
|
|||||||
|
|
||||||
setRenderLines(lines: string[]) {
|
setRenderLines(lines: string[]) {
|
||||||
this.renderLines = lines;
|
this.renderLines = lines;
|
||||||
this.renderedLineCount = lines.reduce(
|
|
||||||
(count, line) =>
|
|
||||||
count +
|
|
||||||
Math.ceil(
|
|
||||||
(this.prepend.length + line.length) /
|
|
||||||
(Deno.consoleSize().columns || 80),
|
|
||||||
),
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setRenderHeight(height: number) {
|
setRenderHeight(height: number) {
|
||||||
@@ -165,15 +194,25 @@ export class TerminalBlock {
|
|||||||
|
|
||||||
renderInternal(startRow?: number) {
|
renderInternal(startRow?: number) {
|
||||||
this.lastRenderRow = startRow ?? this.lastRenderRow;
|
this.lastRenderRow = startRow ?? this.lastRenderRow;
|
||||||
this.clear();
|
this.clear(); // uses old renderedLineCount
|
||||||
let output = this.renderLines.map((line) => `${this.prepend}${line}\x1b[K`)
|
|
||||||
.join("\n");
|
const outputLines = this.renderLines.map((line) =>
|
||||||
|
`${this.prepend}${line}\x1b[K`
|
||||||
|
);
|
||||||
|
const output = outputLines.join("\n");
|
||||||
if (startRow !== undefined) {
|
if (startRow !== undefined) {
|
||||||
const moveCursor = `\x1b[${startRow};1H`;
|
const moveCursor = `\x1b[${startRow};1H`;
|
||||||
output = moveCursor + output;
|
Deno.stdout.writeSync(new TextEncoder().encode(moveCursor + output));
|
||||||
|
} else {
|
||||||
|
Deno.stdout.writeSync(new TextEncoder().encode(output));
|
||||||
}
|
}
|
||||||
Deno.stdout.writeSync(
|
|
||||||
new TextEncoder().encode(output),
|
// update rendered line count *after* rendering
|
||||||
|
this.renderedLineCount = outputLines.reduce(
|
||||||
|
(count, line) =>
|
||||||
|
count +
|
||||||
|
Math.ceil((line.length) / (Deno.consoleSize().columns || 80)),
|
||||||
|
0,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +223,11 @@ 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() {
|
||||||
|
this.clear();
|
||||||
|
this.lines = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
setFixedHeight(height: number) {
|
setFixedHeight(height: number) {
|
||||||
|
@@ -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);
|
||||||
|
29
cli/cursor.ts
Normal file
29
cli/cursor.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
export class Cursor {
|
||||||
|
private static visible = true;
|
||||||
|
|
||||||
|
static show() {
|
||||||
|
this.visible = true;
|
||||||
|
Deno.stdout.writeSync(new TextEncoder().encode("\x1b[?25h"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static hide() {
|
||||||
|
this.visible = false;
|
||||||
|
Deno.stdout.writeSync(new TextEncoder().encode("\x1b[?25l"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static restoreVisibility() {
|
||||||
|
if (this.visible) {
|
||||||
|
this.show();
|
||||||
|
} else {
|
||||||
|
this.hide();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static savePosition() {
|
||||||
|
Deno.stdout.writeSync(new TextEncoder().encode("\x1b7"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static restorePosition() {
|
||||||
|
Deno.stdout.writeSync(new TextEncoder().encode("\x1b8"));
|
||||||
|
}
|
||||||
|
}
|
29
cli/forceArgs.ts
Normal file
29
cli/forceArgs.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { cliPrompt } from "./prompts.ts";
|
||||||
|
import type { TerminalBlock } from "./TerminalLayout.ts";
|
||||||
|
|
||||||
|
type prompt = [string, (v?: string) => boolean] | string;
|
||||||
|
|
||||||
|
export async function forceArgs(
|
||||||
|
args: string[],
|
||||||
|
prompts: prompt[],
|
||||||
|
block?: TerminalBlock,
|
||||||
|
) {
|
||||||
|
const newArgs: string[] = [];
|
||||||
|
for (const [i, arg] of args.entries()) {
|
||||||
|
if (typeof prompts[i] === "string") {
|
||||||
|
let val = arg;
|
||||||
|
while (!val) {
|
||||||
|
val = await cliPrompt(prompts[i], block) || "";
|
||||||
|
}
|
||||||
|
newArgs.push(val);
|
||||||
|
} else {
|
||||||
|
const [promptText, validation] = prompts[i];
|
||||||
|
let val = arg;
|
||||||
|
while (!validation(val)) {
|
||||||
|
val = await cliPrompt(promptText, block) || "";
|
||||||
|
}
|
||||||
|
newArgs.push(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newArgs;
|
||||||
|
}
|
137
cli/index.ts
137
cli/index.ts
@@ -1,85 +1,150 @@
|
|||||||
import { getAsciiArt } from "../util/asciiArt.ts";
|
import { getAsciiArt } from "../util/asciiArt.ts";
|
||||||
import { toCase } from "util/caseManagement.ts";
|
import { toCase } from "util/caseManagement.ts";
|
||||||
import { ArgParser } from "./argParser.ts";
|
import { ArgParser } from "./argParser.ts";
|
||||||
import { colorize } from "./colorize.ts";
|
import { colorize } from "./style.ts";
|
||||||
import { selectMenuInteractive } from "./selectMenu.ts";
|
import { selectMenuInteractive } from "./selectMenu.ts";
|
||||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||||
|
import { cliAlert, cliLog } from "./prompts.ts";
|
||||||
|
import type { ITool } from "../types.ts";
|
||||||
|
import { join, toFileUrl } from "@std/path";
|
||||||
|
import { log } from "util/logfile.ts";
|
||||||
|
|
||||||
|
// Register tools here (filename, no extension)
|
||||||
|
const toolRegistry: [string, Promise<{ default: ITool }>][] = [
|
||||||
|
["checkCode", import("../tools/checkCode.ts")],
|
||||||
|
["fieldRename", import("../tools/fieldRename.ts")],
|
||||||
|
["listFormFields", import("../tools/listFormFields.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 = tools?.replace(/\/$/, "").replace(/^\.?\//, "") || "tools";
|
for (const [name, toolfile] of toolRegistry) {
|
||||||
for (const toolfile of Deno.readDirSync(tools)) {
|
const t = await toolfile;
|
||||||
if (toolfile.isFile) {
|
try {
|
||||||
const tool = await import(
|
if (t.default) {
|
||||||
Deno.cwd() + "/" + tools + "/" + toolfile.name
|
|
||||||
);
|
|
||||||
if (tool.default) {
|
|
||||||
this.tools.set(
|
this.tools.set(
|
||||||
toCase(toolfile.name.replace(".ts", ""), "title"),
|
toCase(name, "title"),
|
||||||
tool.default,
|
t.default,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
cliLog(e + "\n", this.terminalLayout.getBlock("body"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async banana() {
|
private async banana() {
|
||||||
const asciiArt = await getAsciiArt("banana");
|
const asciiArt = await getAsciiArt("banana");
|
||||||
console.log(colorize(asciiArt, "yellow"));
|
const body = this.terminalLayout.getBlock("body");
|
||||||
|
body.clearAll();
|
||||||
|
cliLog(colorize(asciiArt, "yellow"), body);
|
||||||
}
|
}
|
||||||
|
|
||||||
private help() {
|
private async help() {
|
||||||
console.log("BearMetal PDF CLI");
|
this.terminalLayout.clear();
|
||||||
|
this.ensmallenHeader("Help");
|
||||||
|
const bodyBlock = this.terminalLayout.getBlock("body");
|
||||||
|
bodyBlock.clearAll();
|
||||||
|
await cliAlert("BearMetal PDF CLI\n", bodyBlock);
|
||||||
|
await this.embiggenHeader();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async run() {
|
public async run() {
|
||||||
try {
|
try {
|
||||||
const lines: string[] = [];
|
await this.importTools();
|
||||||
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();
|
const titleBlock = new TerminalBlock();
|
||||||
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);
|
||||||
titleBlock.setFixedHeight(lines.length);
|
if (this.args.getFlag("help") && !this.args.task) {
|
||||||
titleBlock.setLines(lines);
|
await this.help();
|
||||||
if (Deno.args.length === 0) {
|
return;
|
||||||
// console.log(
|
} else if (this.args.nonFlags.length === 0 || !this.args.task) {
|
||||||
// colorize("No tool specified. Importing all tools...", "gray"),
|
this.embiggenHeader();
|
||||||
// );
|
await this.toolMenu();
|
||||||
await this.importTools();
|
} else {
|
||||||
|
const task = this.args.task;
|
||||||
|
await this.runTool(toCase(task, "title"));
|
||||||
}
|
}
|
||||||
this.toolMenu();
|
|
||||||
} finally {
|
} finally {
|
||||||
this.terminalLayout.clearAll();
|
this.terminalLayout.clearAll();
|
||||||
|
Deno.stdin.setRaw(false);
|
||||||
|
if (this.closeMessage) console.log(this.closeMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async toolMenu() {
|
private async toolMenu() {
|
||||||
const tools = this.tools.keys().toArray();
|
const tools = this.tools.keys().toArray();
|
||||||
const bodyBlock = this.terminalLayout.getBlock("body");
|
const bodyBlock = this.terminalLayout.getBlock("body");
|
||||||
const selected = await selectMenuInteractive("Choose a tool", tools, {
|
bodyBlock.clearAll();
|
||||||
terminalBlock: bodyBlock,
|
bodyBlock.setPreserveHistory(false);
|
||||||
});
|
const selected = await selectMenuInteractive(
|
||||||
bodyBlock.clear();
|
"Choose a tool",
|
||||||
|
tools.concat(["Help", "Exit"]),
|
||||||
|
{
|
||||||
|
terminalBlock: bodyBlock,
|
||||||
|
},
|
||||||
|
);
|
||||||
if (!selected) return;
|
if (!selected) return;
|
||||||
|
if (selected === "Exit") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
await this.runTool(selected);
|
await this.runTool(selected);
|
||||||
this.toolMenu();
|
await this.toolMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async runTool(toolName: string) {
|
private async runTool(toolName: string) {
|
||||||
|
if (toolName === "Help") {
|
||||||
|
return await this.help();
|
||||||
|
}
|
||||||
const tool = this.tools.get(toolName);
|
const tool = this.tools.get(toolName);
|
||||||
if (tool) {
|
if (tool) {
|
||||||
await tool.run();
|
this.ensmallenHeader(tool.name + " - " + tool.description);
|
||||||
await tool.done?.();
|
const bodyBlock = this.terminalLayout.getBlock("body");
|
||||||
|
bodyBlock.clearAll();
|
||||||
|
tool.setBlock?.(bodyBlock);
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ensmallenHeader(subtitle: string) {
|
||||||
|
this.terminalLayout.clear();
|
||||||
|
const titleBlock = this.terminalLayout.getBlock("title");
|
||||||
|
titleBlock.clear();
|
||||||
|
titleBlock.setFixedHeight(3);
|
||||||
|
titleBlock.setLines([
|
||||||
|
colorize("BearMetal PDF Tools", "porple"),
|
||||||
|
colorize(subtitle, "gray"),
|
||||||
|
"-=".repeat(Deno.consoleSize().columns / 2),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async embiggenHeader() {
|
||||||
|
const titleBlock = this.terminalLayout.getBlock("title");
|
||||||
|
titleBlock.clear();
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
titleBlock.setFixedHeight(lines.length);
|
||||||
|
titleBlock.setLines(lines);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
129
cli/prompts.ts
Normal file
129
cli/prompts.ts
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
// deno-lint-disable-must-await-calls
|
||||||
|
import { Cursor } from "./cursor.ts";
|
||||||
|
import { colorize } from "./style.ts";
|
||||||
|
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||||
|
|
||||||
|
export async function cliPrompt(
|
||||||
|
message: string,
|
||||||
|
block?: TerminalBlock,
|
||||||
|
): Promise<string> {
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const input: string[] = [];
|
||||||
|
|
||||||
|
await Deno.stdin.setRaw(true);
|
||||||
|
|
||||||
|
const cursorVisible = Cursor["visible"];
|
||||||
|
Cursor.show();
|
||||||
|
|
||||||
|
let range: [number, number] = [0, 1];
|
||||||
|
if (block) {
|
||||||
|
range = block.setLines([message + " "]);
|
||||||
|
} else {
|
||||||
|
Deno.stdout.writeSync(encoder.encode(message + " "));
|
||||||
|
}
|
||||||
|
|
||||||
|
const buf = new Uint8Array(1);
|
||||||
|
while (true) {
|
||||||
|
const n = await Deno.stdin.read(buf);
|
||||||
|
if (n === null) break;
|
||||||
|
const byte = buf[0];
|
||||||
|
|
||||||
|
if (byte === 3) { // Ctrl+C
|
||||||
|
Deno.stdin.setRaw(false);
|
||||||
|
block?.["layout"]?.clearAll();
|
||||||
|
block?.clear();
|
||||||
|
Deno.exit(130);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byte === 13) { // Enter
|
||||||
|
break;
|
||||||
|
} else if (byte === 127 || byte === 8) { // Backspace
|
||||||
|
input.pop();
|
||||||
|
} else if (byte >= 32 && byte <= 126) { // Printable chars
|
||||||
|
input.push(String.fromCharCode(byte));
|
||||||
|
}
|
||||||
|
|
||||||
|
const line = message + " " + input.join("");
|
||||||
|
if (block) {
|
||||||
|
range = block.setLines([line], range);
|
||||||
|
} else {
|
||||||
|
Deno.stdout.writeSync(encoder.encode("\r\x1b[K" + line));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await Deno.stdin.setRaw(false);
|
||||||
|
if (!cursorVisible) {
|
||||||
|
Cursor.hide();
|
||||||
|
}
|
||||||
|
Deno.stdout.writeSync(encoder.encode("\n"));
|
||||||
|
|
||||||
|
return input.join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cliConfirm(message: string, block?: TerminalBlock) {
|
||||||
|
return cliPrompt(message + " (y/n)", block).then((v) =>
|
||||||
|
v.toLowerCase() === "y"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cliAlert(message: string, block?: TerminalBlock) {
|
||||||
|
return cliPrompt(
|
||||||
|
message + colorize(" Press Enter to continue", "gray"),
|
||||||
|
block,
|
||||||
|
).then((v) => {
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cliLog(
|
||||||
|
message: string | object | Array<unknown>,
|
||||||
|
block?: TerminalBlock,
|
||||||
|
) {
|
||||||
|
if (!block) {
|
||||||
|
console.log(message);
|
||||||
|
} else {
|
||||||
|
if (typeof message === "object") message = Deno.inspect(message);
|
||||||
|
block.setLines(message.split("\n"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
layout.register("title", title);
|
||||||
|
layout.register("block", block);
|
||||||
|
|
||||||
|
Deno.addSignalListener("SIGINT", () => {
|
||||||
|
layout.clearAll();
|
||||||
|
// console.clear();
|
||||||
|
Deno.exit(0);
|
||||||
|
});
|
||||||
|
const name = await cliPrompt("Enter your name:", block);
|
||||||
|
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",
|
||||||
|
"Maybe I'll see you next week?",
|
||||||
|
"Wow, really not going to say anything to me?",
|
||||||
|
"Well, if that's how you feel",
|
||||||
|
];
|
||||||
|
let convo = 0;
|
||||||
|
setInterval(() => {
|
||||||
|
cliLog(loopingConvo[convo % loopingConvo.length], block);
|
||||||
|
convo++;
|
||||||
|
}, 2000);
|
||||||
|
// setTimeout(async () => {
|
||||||
|
// await cliAlert("Well, if that's that...", block);
|
||||||
|
// Deno.exit(0);
|
||||||
|
// }, 10000);
|
||||||
|
}
|
@@ -1,9 +1,11 @@
|
|||||||
import { colorize } from "./colorize.ts";
|
import type { callback } from "../types.ts";
|
||||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
import { colorize } from "./style.ts";
|
||||||
|
import { TerminalBlock } from "./TerminalLayout.ts";
|
||||||
|
|
||||||
interface ISelectMenuConfig {
|
interface ISelectMenuConfig {
|
||||||
multiSelect?: boolean;
|
|
||||||
terminalBlock?: TerminalBlock;
|
terminalBlock?: TerminalBlock;
|
||||||
|
initialSelection?: number;
|
||||||
|
initialSelections?: number[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function selectMenu(items: string[]) {
|
export function selectMenu(items: string[]) {
|
||||||
@@ -21,18 +23,12 @@ export async function selectMenuInteractive(
|
|||||||
Deno.stdin.setRaw(true);
|
Deno.stdin.setRaw(true);
|
||||||
let selected = 0;
|
let selected = 0;
|
||||||
|
|
||||||
if (config?.multiSelect) {
|
|
||||||
console.warn("Multi-select not implemented yet");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const terminalBlock = config?.terminalBlock || new TerminalBlock();
|
const terminalBlock = config?.terminalBlock || new TerminalBlock();
|
||||||
if (!config?.terminalBlock) {
|
if (!config?.terminalBlock) {
|
||||||
terminalBlock.setRenderHeight(Deno.consoleSize().rows);
|
terminalBlock.setRenderHeight(Deno.consoleSize().rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(terminalBlock.getRenderHeight());
|
let range: [number, number] = [terminalBlock.lineCount, 1];
|
||||||
|
|
||||||
function renderMenu() {
|
function renderMenu() {
|
||||||
const { rows } = Deno.consoleSize();
|
const { rows } = Deno.consoleSize();
|
||||||
const terminalHeight = terminalBlock.getRenderHeight() || rows;
|
const terminalHeight = terminalBlock.getRenderHeight() || rows;
|
||||||
@@ -54,7 +50,7 @@ export async function selectMenuInteractive(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
terminalBlock.setLines(lines);
|
range = terminalBlock.setLines(lines, range);
|
||||||
}
|
}
|
||||||
|
|
||||||
function numberAndPadding(i: number, prefix?: string) {
|
function numberAndPadding(i: number, prefix?: string) {
|
||||||
@@ -78,7 +74,7 @@ export async function selectMenuInteractive(
|
|||||||
|
|
||||||
if (a === 3) {
|
if (a === 3) {
|
||||||
Deno.stdin.setRaw(false);
|
Deno.stdin.setRaw(false);
|
||||||
console.log("\nInterrupted\n");
|
terminalBlock?.["layout"]?.clearAll();
|
||||||
Deno.exit(130);
|
Deno.exit(130);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,13 +101,112 @@ export async function selectMenuInteractive(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
terminalBlock.clear();
|
|
||||||
Deno.stdin.setRaw(false);
|
Deno.stdin.setRaw(false);
|
||||||
return options[selected];
|
return options[selected];
|
||||||
}
|
}
|
||||||
|
terminalBlock.setLines(["Selected: " + options[selected]], range);
|
||||||
return await handleInput();
|
return await handleInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function multiSelectMenuInteractive(
|
||||||
|
q: string,
|
||||||
|
options: string[] | [string, callback][],
|
||||||
|
config?: ISelectMenuConfig,
|
||||||
|
): Promise<string[] | null> {
|
||||||
|
Deno.stdin.setRaw(true);
|
||||||
|
let selected = 0;
|
||||||
|
let selectedOptions: number[] = config?.initialSelections || [];
|
||||||
|
|
||||||
|
const rawValues = new Set(
|
||||||
|
options.map((i) => typeof i === "string" ? i : i[0]),
|
||||||
|
).values().toArray();
|
||||||
|
|
||||||
|
if (rawValues.length !== options.length) {
|
||||||
|
throw new Error("Duplicate options in multi-select menu");
|
||||||
|
}
|
||||||
|
|
||||||
|
const terminalBlock = config?.terminalBlock || new TerminalBlock();
|
||||||
|
if (!config?.terminalBlock) {
|
||||||
|
terminalBlock.setRenderHeight(Deno.consoleSize().rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
let range: [number, number] = [terminalBlock.lineCount, 1];
|
||||||
|
function renderMenu() {
|
||||||
|
const { rows } = Deno.consoleSize();
|
||||||
|
const terminalHeight = terminalBlock.getRenderHeight() || rows;
|
||||||
|
const maxHeight = Math.min(terminalHeight - 1, options.length);
|
||||||
|
let startPoint = Math.max(0, selected - Math.floor(maxHeight / 2));
|
||||||
|
const endPoint = Math.min(options.length, startPoint + maxHeight);
|
||||||
|
if (endPoint - startPoint < maxHeight) {
|
||||||
|
startPoint = Math.max(0, options.length - maxHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
const lines: string[] = [];
|
||||||
|
lines.push(colorize(q, "green"));
|
||||||
|
for (let i = startPoint; i < endPoint; i++) {
|
||||||
|
const option = rawValues[i];
|
||||||
|
const checkbox = selectedOptions.includes(i)
|
||||||
|
? colorize("◼", "green")
|
||||||
|
: "◻";
|
||||||
|
if (i === selected) {
|
||||||
|
lines.push(`> ${checkbox} ${colorize(option, "porple")}`);
|
||||||
|
} else {
|
||||||
|
lines.push(` ${checkbox} ${option}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
range = terminalBlock.setLines(lines, range);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to handle input
|
||||||
|
async function handleInput() {
|
||||||
|
const buf = new Uint8Array(3); // arrow keys send 3 bytes
|
||||||
|
while (true) {
|
||||||
|
renderMenu();
|
||||||
|
const n = await Deno.stdin.read(buf);
|
||||||
|
if (n === null) break;
|
||||||
|
|
||||||
|
const [a, b, c] = buf;
|
||||||
|
|
||||||
|
if (a === 3) {
|
||||||
|
Deno.stdin.setRaw(false);
|
||||||
|
terminalBlock?.["layout"]?.clearAll();
|
||||||
|
Deno.exit(130);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a === 13) { // Enter key
|
||||||
|
break;
|
||||||
|
} else if (a === 27 && b === 91) { // Arrow keys
|
||||||
|
if (c === 65) { // Up
|
||||||
|
selected = (selected - 1 + options.length) % options.length;
|
||||||
|
} else if (c === 66) { // Down
|
||||||
|
selected = (selected + 1) % options.length;
|
||||||
|
}
|
||||||
|
} else if (a === 32) { // Space
|
||||||
|
Deno.stdout.writeSync(new TextEncoder().encode("\x07"));
|
||||||
|
if (selectedOptions.includes(selected)) {
|
||||||
|
selectedOptions = selectedOptions.filter((i) => i !== selected);
|
||||||
|
} else {
|
||||||
|
selectedOptions.push(selected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Deno.stdin.setRaw(false);
|
||||||
|
return selectedOptions;
|
||||||
|
}
|
||||||
|
const selections = await handleInput();
|
||||||
|
for (const optionI of selections) {
|
||||||
|
const option = options[optionI];
|
||||||
|
if (Array.isArray(option)) {
|
||||||
|
await option[1](option[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const final = selectedOptions.map((i) => rawValues[i]);
|
||||||
|
terminalBlock.setLines(["Selected: " + final.join(", ")], range);
|
||||||
|
return final;
|
||||||
|
}
|
||||||
|
|
||||||
if (import.meta.main) {
|
if (import.meta.main) {
|
||||||
// const layout = new TerminalLayout();
|
// const layout = new TerminalLayout();
|
||||||
// const block = new TerminalBlock();
|
// const block = new TerminalBlock();
|
||||||
@@ -154,7 +249,7 @@ if (import.meta.main) {
|
|||||||
// layout.clearAll();
|
// layout.clearAll();
|
||||||
// console.log(val);
|
// console.log(val);
|
||||||
|
|
||||||
const val = await selectMenuInteractive("choose a fruit", [
|
const val = await multiSelectMenuInteractive("choose a fruit", [
|
||||||
"apple",
|
"apple",
|
||||||
"banana",
|
"banana",
|
||||||
"cherry",
|
"cherry",
|
||||||
@@ -183,4 +278,6 @@ if (import.meta.main) {
|
|||||||
"zucchini",
|
"zucchini",
|
||||||
]);
|
]);
|
||||||
console.log(val);
|
console.log(val);
|
||||||
|
|
||||||
|
// Deno.stdout.writeSync(new TextEncoder().encode("\x07"));
|
||||||
}
|
}
|
||||||
|
21
deno.json
21
deno.json
@@ -1,5 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@bearmetal/pdf-tools",
|
"name": "@bearmetal/pdf-tools",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"license": "GPL 3.0",
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"dev": "deno run -A --env-file=.env --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",
|
"compile": "deno compile -o compare-form-fields.exe --target x86_64-pc-windows-msvc -R ./main.ts",
|
||||||
@@ -8,8 +10,25 @@
|
|||||||
},
|
},
|
||||||
"imports": {
|
"imports": {
|
||||||
"@std/assert": "jsr:@std/assert@1",
|
"@std/assert": "jsr:@std/assert@1",
|
||||||
|
"@std/path": "jsr:@std/path@^1.0.9",
|
||||||
"pdf-lib": "npm:pdf-lib@^1.17.1",
|
"pdf-lib": "npm:pdf-lib@^1.17.1",
|
||||||
"util/": "./util/"
|
"util/": "./util/"
|
||||||
},
|
},
|
||||||
"exports": {}
|
"exports": {
|
||||||
|
".": "./main.ts"
|
||||||
|
},
|
||||||
|
"lint": {
|
||||||
|
"rules": {
|
||||||
|
"exclude": [
|
||||||
|
"no-explicit-any"
|
||||||
|
],
|
||||||
|
"include": [
|
||||||
|
"require-await"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"plugins": [
|
||||||
|
"./no-log.ts",
|
||||||
|
"./must_await_cli_prompts.ts"
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
7
deno.lock
generated
7
deno.lock
generated
@@ -1,8 +1,9 @@
|
|||||||
{
|
{
|
||||||
"version": "4",
|
"version": "5",
|
||||||
"specifiers": {
|
"specifiers": {
|
||||||
"jsr:@std/assert@1": "1.0.12",
|
"jsr:@std/assert@1": "1.0.12",
|
||||||
"jsr:@std/internal@^1.0.6": "1.0.6",
|
"jsr:@std/internal@^1.0.6": "1.0.6",
|
||||||
|
"jsr:@std/path@^1.0.9": "1.0.9",
|
||||||
"npm:pdf-lib@^1.17.1": "1.17.1"
|
"npm:pdf-lib@^1.17.1": "1.17.1"
|
||||||
},
|
},
|
||||||
"jsr": {
|
"jsr": {
|
||||||
@@ -14,6 +15,9 @@
|
|||||||
},
|
},
|
||||||
"@std/internal@1.0.6": {
|
"@std/internal@1.0.6": {
|
||||||
"integrity": "9533b128f230f73bd209408bb07a4b12f8d4255ab2a4d22a1fd6d87304aca9a4"
|
"integrity": "9533b128f230f73bd209408bb07a4b12f8d4255ab2a4d22a1fd6d87304aca9a4"
|
||||||
|
},
|
||||||
|
"@std/path@1.0.9": {
|
||||||
|
"integrity": "260a49f11edd3db93dd38350bf9cd1b4d1366afa98e81b86167b4e3dd750129e"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"npm": {
|
"npm": {
|
||||||
@@ -48,6 +52,7 @@
|
|||||||
"workspace": {
|
"workspace": {
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"jsr:@std/assert@1",
|
"jsr:@std/assert@1",
|
||||||
|
"jsr:@std/path@^1.0.9",
|
||||||
"npm:pdf-lib@^1.17.1"
|
"npm:pdf-lib@^1.17.1"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
1
main.ts
1
main.ts
@@ -1,3 +1,4 @@
|
|||||||
|
/// <reference types="./types.ts" />
|
||||||
import { PdfToolsCli } from "./cli/index.ts";
|
import { PdfToolsCli } from "./cli/index.ts";
|
||||||
|
|
||||||
const app = new PdfToolsCli();
|
const app = new PdfToolsCli();
|
||||||
|
45
must_await_cli_prompts.ts
Normal file
45
must_await_cli_prompts.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
const TARGET_FUNCTIONS = new Set(["cliAlert", "cliPrompt", "cliConfirm"]);
|
||||||
|
|
||||||
|
const plugin: Deno.lint.Plugin = {
|
||||||
|
name: "must-await-calls",
|
||||||
|
rules: {
|
||||||
|
"must-await-calls": {
|
||||||
|
create(context) {
|
||||||
|
return {
|
||||||
|
CallExpression(node) {
|
||||||
|
if (
|
||||||
|
node.callee.type !== "Identifier" ||
|
||||||
|
!TARGET_FUNCTIONS.has(node.callee.name)
|
||||||
|
) return;
|
||||||
|
|
||||||
|
const parent = node.parent;
|
||||||
|
|
||||||
|
// Allow `await fetchData()`
|
||||||
|
if (parent?.type === "AwaitExpression") return;
|
||||||
|
|
||||||
|
// Allow `return fetchData()` or `return await fetchData()`
|
||||||
|
if (parent?.type === "ReturnStatement") return;
|
||||||
|
|
||||||
|
// Allow `fetchData().then(...)`
|
||||||
|
if (
|
||||||
|
parent?.type === "MemberExpression" &&
|
||||||
|
parent.property.type === "Identifier" &&
|
||||||
|
parent.property.name === "then"
|
||||||
|
) return;
|
||||||
|
|
||||||
|
context.report({
|
||||||
|
node,
|
||||||
|
message:
|
||||||
|
`Call to "${node.callee.name}" must be awaited, returned, or .then-chained.`,
|
||||||
|
fix(fixer) {
|
||||||
|
return fixer.insertTextBefore(node, "await ");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default plugin;
|
26
no-log.ts
Normal file
26
no-log.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
const plugin: Deno.lint.Plugin = {
|
||||||
|
name: "no-log",
|
||||||
|
rules: {
|
||||||
|
"no-log": {
|
||||||
|
create(context) {
|
||||||
|
return {
|
||||||
|
// Identifier(node) {
|
||||||
|
// if (node.name === "log") {
|
||||||
|
// context.report({
|
||||||
|
// node,
|
||||||
|
// message: "Do not use log",
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
'ExpressionStatement > CallExpression[callee.name="log"]'(node) {
|
||||||
|
context.report({
|
||||||
|
node,
|
||||||
|
message: "Clean up log statements",
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
export default plugin;
|
@@ -0,0 +1,7 @@
|
|||||||
|
const thing: string = "";
|
||||||
|
switch (thing) {
|
||||||
|
case "Text1":
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
@@ -1,58 +1,75 @@
|
|||||||
import { PDFDocument } from "pdf-lib";
|
import { forceArgs } from "../cli/forceArgs.ts";
|
||||||
|
import { cliAlert, cliLog } from "../cli/prompts.ts";
|
||||||
|
import { colorize } from "../cli/style.ts";
|
||||||
|
import type { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||||
|
import type { ITool } from "../types.ts";
|
||||||
import { loadPdfForm } from "../util/saveLoadPdf.ts";
|
import { loadPdfForm } from "../util/saveLoadPdf.ts";
|
||||||
import { callWithArgPrompt } from "util/call.ts";
|
|
||||||
|
|
||||||
export async function checkFile(pdfPath: string, csPath: string) {
|
function getCaseSyntaxPatternByFileExtension(
|
||||||
while (!pdfPath || !pdfPath.endsWith(".pdf")) {
|
extenstion: string,
|
||||||
pdfPath = prompt("Please provide path to PDF file:") || "";
|
field: string,
|
||||||
}
|
) {
|
||||||
while (!csPath || !csPath.endsWith(".cs")) {
|
switch (extenstion.trim().toLowerCase().replace(".", "")) {
|
||||||
csPath = prompt("Please provide path to CS class file:") || "";
|
case "cs":
|
||||||
}
|
case "js":
|
||||||
|
case "ts":
|
||||||
const form = await loadPdfForm(pdfPath);
|
default:
|
||||||
|
return `(?<!//\s?)case ?"${field.replace(/\[\d\]/, `\\[\\?|\\d+\\]`)}"`;
|
||||||
const fields = form.getFields();
|
|
||||||
const csFiles = await Promise.all(
|
|
||||||
csPath.split(",").map((c) => Deno.readTextFile(c.trim())),
|
|
||||||
);
|
|
||||||
|
|
||||||
const fieldNames: string[] = fields.map((f) => f.getName())
|
|
||||||
.filter((f) => {
|
|
||||||
const rx = new RegExp(
|
|
||||||
`(?<!//\s?)case ?"${f.replace(/\[\d\]/, "\\[\\?\\]")}"`,
|
|
||||||
);
|
|
||||||
return !csFiles.some((c) => rx.test(c));
|
|
||||||
})
|
|
||||||
.filter((f) => !f.toLowerCase().includes("signature"));
|
|
||||||
|
|
||||||
if (fieldNames.length) {
|
|
||||||
console.log(
|
|
||||||
"%cThe following field names are not present in the CS code",
|
|
||||||
"color: red",
|
|
||||||
);
|
|
||||||
console.log(fieldNames);
|
|
||||||
alert("Your princess is in another castle...");
|
|
||||||
} else {
|
|
||||||
console.log("%cAll form fields present", "color: lime");
|
|
||||||
alert("Ok!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CheckCode implements ITool {
|
class CheckCode implements ITool {
|
||||||
name = "checkcode";
|
name = "checkcode";
|
||||||
description = "Checks if form fields are present in CS code";
|
description = "Checks if form fields are present in a given code file";
|
||||||
help() {
|
private block?: TerminalBlock;
|
||||||
console.log("Usage: checkcode <pdfPath> <csPath>");
|
setBlock(block: TerminalBlock) {
|
||||||
|
this.block = block;
|
||||||
|
this.block.setPreserveHistory(true);
|
||||||
}
|
}
|
||||||
async run(...args: string[]) {
|
async help() {
|
||||||
await callWithArgPrompt(checkFile, [
|
cliLog("Usage: checkcode <pdfPath> <csPath>", this.block);
|
||||||
["Please provide path to PDF file:", (p) => !!p && p.endsWith(".pdf")],
|
await cliAlert("", this.block);
|
||||||
[
|
}
|
||||||
"Please provide path to CS file (comma separated for multiple):",
|
async run(pdfPath: string, codePaths: string) {
|
||||||
(p) => !!p && p.endsWith(".cs"),
|
[pdfPath, codePaths] = await forceArgs([pdfPath, codePaths], [
|
||||||
],
|
"Please provide path to PDF file:",
|
||||||
], args);
|
"Please provide path(s) to code file(s) (comma separated for multiple):",
|
||||||
|
], this.block);
|
||||||
|
|
||||||
|
const form = await loadPdfForm(pdfPath);
|
||||||
|
|
||||||
|
const fields = form.getFields();
|
||||||
|
const codeFiles: [string, string][] = codePaths.split(",").map((
|
||||||
|
c,
|
||||||
|
) => [c, Deno.readTextFileSync(c.trim())]);
|
||||||
|
|
||||||
|
const fieldNames: string[] = fields.map((f) => f.getName())
|
||||||
|
.filter((f) => !f.toLowerCase().includes("signature"));
|
||||||
|
let unfound = fieldNames.slice();
|
||||||
|
|
||||||
|
for (const [path, content] of codeFiles) {
|
||||||
|
unfound = unfound.filter((f) => {
|
||||||
|
const rx = new RegExp(
|
||||||
|
getCaseSyntaxPatternByFileExtension(path.split(".").at(-1) ?? "", f),
|
||||||
|
);
|
||||||
|
return rx.test(content);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unfound.length) {
|
||||||
|
cliLog(
|
||||||
|
colorize(
|
||||||
|
"The following field names are not present in the CS code",
|
||||||
|
"red",
|
||||||
|
),
|
||||||
|
this.block,
|
||||||
|
);
|
||||||
|
cliLog(unfound, this.block);
|
||||||
|
await cliAlert("Your princess is in another castle...", this.block);
|
||||||
|
} else {
|
||||||
|
cliLog(colorize("All form fields present", "green"), this.block);
|
||||||
|
await cliAlert("Ok!", this.block);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,29 +1,12 @@
|
|||||||
import {
|
import { type PDFAcroField, type PDFField, PDFName, PDFString } from "pdf-lib";
|
||||||
PDFAcroField,
|
import { loadPdf, loadPdfForm, savePdf } from "util/saveLoadPdf.ts";
|
||||||
PDFHexString,
|
import { callWithArgPrompt } from "util/call.ts";
|
||||||
PDFName,
|
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||||
PDFString,
|
import { forceArgs } from "../cli/forceArgs.ts";
|
||||||
toHexString,
|
import { colorize } from "../cli/style.ts";
|
||||||
} from "pdf-lib";
|
import { cliAlert, cliLog, cliPrompt } from "../cli/prompts.ts";
|
||||||
import { loadPdfForm, savePdf } from "util/saveLoadPdf.ts";
|
import { multiSelectMenuInteractive } from "../cli/selectMenu.ts";
|
||||||
import { PDFDocument } from "pdf-lib";
|
import type { callback, ITool } from "../types.ts";
|
||||||
import { call, callWithArgPrompt } from "util/call.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,
|
||||||
@@ -50,29 +33,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) {
|
||||||
@@ -80,18 +44,108 @@ async function renameFields(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function applyRename(
|
||||||
|
field: PDFField,
|
||||||
|
name: string,
|
||||||
|
pattern: RegExp,
|
||||||
|
change: string,
|
||||||
|
) {
|
||||||
|
const segments = name.split(".");
|
||||||
|
const matchingSegments = segments.filter((s) => pattern.test(s));
|
||||||
|
let cField: PDFAcroField | undefined = field.acroField;
|
||||||
|
while (cField) {
|
||||||
|
if (
|
||||||
|
cField.getPartialName() &&
|
||||||
|
matchingSegments.includes(cField.getPartialName()!)
|
||||||
|
) {
|
||||||
|
const mName = cField.getPartialName()?.replace(pattern, change);
|
||||||
|
if (mName) {
|
||||||
|
cField.dict.set(PDFName.of("T"), PDFString.of(mName));
|
||||||
|
// console.log(cField.getPartialName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cField = cField.getParent();
|
||||||
|
// console.log(cField?.getPartialName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function evaluateChange(change: string, match: RegExpExecArray) {
|
||||||
|
return change.replace(
|
||||||
|
/\$(\d+)(i?)/g,
|
||||||
|
(_, i, indexed) =>
|
||||||
|
indexed
|
||||||
|
? (parseInt(match[i]) ? (parseInt(match[i]) - 1).toString() : match[i])
|
||||||
|
: match[i],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
class RenameFields implements ITool {
|
class RenameFields implements ITool {
|
||||||
name = "renamefields";
|
name = "renamefields";
|
||||||
description = "Renames fields in a PDF form";
|
description = "Renames fields in a PDF form";
|
||||||
help() {
|
block: TerminalBlock | undefined;
|
||||||
console.log("Usage: renamefields <pdfPath> <pattern> <change>");
|
|
||||||
|
setBlock(block: TerminalBlock) {
|
||||||
|
this.block = block;
|
||||||
|
}
|
||||||
|
|
||||||
|
async help(standalone = false) {
|
||||||
|
await cliAlert(
|
||||||
|
"Usage: rename-fields <pdfPath> <pattern> <change>\n",
|
||||||
|
standalone ? undefined : this.block,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
async run(pdfPath: string = "", pattern: string = "", change: string = "") {
|
async run(pdfPath: string = "", pattern: string = "", change: string = "") {
|
||||||
await callWithArgPrompt(renameFields, [
|
if (!this.block) {
|
||||||
["Please provide path to PDF:", (p) => !!p && p.endsWith(".pdf")],
|
this.block = new TerminalBlock();
|
||||||
"Please provide search string:",
|
}
|
||||||
"Please provide requested change:",
|
this.block.setPreserveHistory(true);
|
||||||
], [pdfPath, pattern, change]);
|
|
||||||
|
[pdfPath, pattern, change] = await forceArgs(
|
||||||
|
[pdfPath, pattern, change],
|
||||||
|
[
|
||||||
|
["Please provide path to PDF:", (p) => !!p && p.endsWith(".pdf")],
|
||||||
|
"Please provide search string:",
|
||||||
|
"Please provide requested change:",
|
||||||
|
],
|
||||||
|
this.block,
|
||||||
|
);
|
||||||
|
|
||||||
|
const patternRegex = new RegExp(pattern);
|
||||||
|
|
||||||
|
const pdf = await loadPdf(pdfPath);
|
||||||
|
const form = pdf.getForm();
|
||||||
|
const fields = form.getFields();
|
||||||
|
|
||||||
|
const foundUpdates: [string, callback][] = [];
|
||||||
|
|
||||||
|
for (const field of fields) {
|
||||||
|
const name = field.getName();
|
||||||
|
const match = patternRegex.exec(name);
|
||||||
|
if (match) {
|
||||||
|
const toChange = evaluateChange(change, match);
|
||||||
|
foundUpdates.push([
|
||||||
|
`${colorize(name, "red")} -> ${colorize(toChange, "green")}`,
|
||||||
|
() => {
|
||||||
|
applyRename(field, name, patternRegex, toChange);
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (foundUpdates.length) {
|
||||||
|
cliLog("Found updates:", this.block);
|
||||||
|
await multiSelectMenuInteractive(
|
||||||
|
"Please select an option to apply",
|
||||||
|
foundUpdates,
|
||||||
|
{ terminalBlock: this.block },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = await cliPrompt(
|
||||||
|
"Save to path (or hit enter to keep current):",
|
||||||
|
this.block,
|
||||||
|
);
|
||||||
|
await savePdf(pdf, path || pdfPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export default new RenameFields();
|
export default new RenameFields();
|
||||||
|
57
tools/listFormFields.ts
Normal file
57
tools/listFormFields.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import { forceArgs } from "../cli/forceArgs.ts";
|
||||||
|
import { cliAlert } from "../cli/prompts.ts";
|
||||||
|
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||||
|
import { loadPdfForm } from "util/saveLoadPdf.ts";
|
||||||
|
import type { ITool } from "../types.ts";
|
||||||
|
|
||||||
|
export class ListFormFields implements ITool {
|
||||||
|
name = "listformfields";
|
||||||
|
description = "Lists fields in a PDF form";
|
||||||
|
block?: TerminalBlock;
|
||||||
|
async run(pdfPath: string = "") {
|
||||||
|
if (!this.block) {
|
||||||
|
this.block = new TerminalBlock();
|
||||||
|
}
|
||||||
|
this.block.setPreserveHistory(true);
|
||||||
|
[pdfPath] = await forceArgs([pdfPath], [[
|
||||||
|
"Please provide path to PDF:",
|
||||||
|
(p) => !!p && p.endsWith(".pdf"),
|
||||||
|
]], this.block);
|
||||||
|
|
||||||
|
const form = await loadPdfForm(pdfPath);
|
||||||
|
const fields = form.getFields();
|
||||||
|
const height = this.block.getRenderHeight() - 1;
|
||||||
|
const fieldNames = fields.sort((a, b) => {
|
||||||
|
const aRect = a.acroField.getWidgets().find((e) => e.Rect())?.Rect()
|
||||||
|
?.asRectangle();
|
||||||
|
const bRect = b.acroField.getWidgets().find((e) => e.Rect())?.Rect()
|
||||||
|
?.asRectangle();
|
||||||
|
|
||||||
|
if (aRect && bRect) {
|
||||||
|
if (aRect.x !== bRect.x) {
|
||||||
|
return aRect.x - bRect.x; // Sort left to right
|
||||||
|
} else {
|
||||||
|
return bRect.y - aRect.y; // If x is equal, sort top to bottom
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return a.getName().localeCompare(b.getName());
|
||||||
|
}).map((f) => f.getName());
|
||||||
|
const maxLength = Math.max(...fieldNames.map((f) => f.length)) + 4;
|
||||||
|
const lines = [];
|
||||||
|
for (let i = 0; i < height; i++) {
|
||||||
|
let line = "";
|
||||||
|
for (let j = 0; j < fieldNames.length; j += height) {
|
||||||
|
const fieldName = fieldNames[i + j] ?? "";
|
||||||
|
line += fieldName.padEnd(maxLength, " ");
|
||||||
|
}
|
||||||
|
lines.push(line);
|
||||||
|
}
|
||||||
|
this.block.setLines(lines, [0, 1]);
|
||||||
|
await cliAlert("", this.block);
|
||||||
|
}
|
||||||
|
setBlock(terminalBlock: TerminalBlock) {
|
||||||
|
this.block = terminalBlock;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new ListFormFields();
|
21
types.ts
21
types.ts
@@ -1,10 +1,13 @@
|
|||||||
declare global {
|
import type { TerminalBlock } from "./cli/TerminalLayout.ts";
|
||||||
type ToolFunc<T extends unknown[]> = (...args: T) => Promise<void>;
|
|
||||||
interface ITool {
|
export type ToolFunc<T extends unknown[]> = (...args: T) => Promise<void>;
|
||||||
name: string;
|
export interface ITool {
|
||||||
description: string;
|
name: string;
|
||||||
run: ToolFunc<any[]>;
|
description: string;
|
||||||
help?: () => Promise<void> | void;
|
run: ToolFunc<any[]>;
|
||||||
done?: () => Promise<void> | void;
|
help?: () => Promise<void> | void;
|
||||||
}
|
done?: () => Promise<void> | void;
|
||||||
|
setBlock?: (block: TerminalBlock) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type callback = (...args: any[]) => any;
|
||||||
|
@@ -1,3 +1,5 @@
|
|||||||
|
import type { ToolFunc } from "../types.ts";
|
||||||
|
|
||||||
type transformer = (arg: string) => any;
|
type transformer = (arg: string) => any;
|
||||||
interface IConfig {
|
interface IConfig {
|
||||||
multiTransform?: boolean;
|
multiTransform?: boolean;
|
||||||
|
18
util/dedent.ts
Normal file
18
util/dedent.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
export function dedent(str: string) {
|
||||||
|
const lines = str.split("\n");
|
||||||
|
const indent = lines.reduce((count, line) => {
|
||||||
|
if (line.trim() === "") return count;
|
||||||
|
const match = line.match(/^(\s*)/);
|
||||||
|
return match ? Math.min(count, match[1].length) : count;
|
||||||
|
}, Infinity);
|
||||||
|
return lines.map((line) => line.slice(indent)).join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (import.meta.main) {
|
||||||
|
console.log(dedent(`
|
||||||
|
Hello, World!
|
||||||
|
This is a paragraph
|
||||||
|
that spans multiple lines.
|
||||||
|
And this is another paragraph.
|
||||||
|
`));
|
||||||
|
}
|
15
util/logfile.ts
Normal file
15
util/logfile.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
const logFile = Deno.openSync("./log.txt", {
|
||||||
|
create: true,
|
||||||
|
write: true,
|
||||||
|
read: true,
|
||||||
|
append: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
logFile.truncateSync(0);
|
||||||
|
|
||||||
|
export function log(message: any) {
|
||||||
|
if (typeof message === "object") {
|
||||||
|
message = JSON.stringify(message);
|
||||||
|
}
|
||||||
|
logFile.writeSync(new TextEncoder().encode(message + "\n"));
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
import { PDFDocument } from "pdf-lib";
|
import { PDFDocument, PDFTextField } from "pdf-lib";
|
||||||
|
|
||||||
export async function loadPdfForm(path: string) {
|
export async function loadPdfForm(path: string) {
|
||||||
const pdfDoc = await loadPdf(path);
|
const pdfDoc = await loadPdf(path);
|
||||||
@@ -13,6 +13,11 @@ export async function loadPdf(path: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function savePdf(doc: PDFDocument, path: string) {
|
export async function savePdf(doc: PDFDocument, path: string) {
|
||||||
|
doc.getForm().getFields().forEach((field) => {
|
||||||
|
if (field instanceof PDFTextField) {
|
||||||
|
field.disableRichFormatting();
|
||||||
|
}
|
||||||
|
});
|
||||||
const pdfBytes = await doc.save();
|
const pdfBytes = await doc.save();
|
||||||
if (Deno.env.get("DRYRUN") || path.includes("dryrun")) return;
|
if (Deno.env.get("DRYRUN") || path.includes("dryrun")) return;
|
||||||
await Deno.writeFile(path, pdfBytes);
|
await Deno.writeFile(path, pdfBytes);
|
||||||
|
Reference in New Issue
Block a user