Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
7eb7197a1c | |||
04d5044c43 | |||
7c19ada88b | |||
7a3b3f2161 | |||
65743d8562 | |||
0f9c377853 | |||
7a394c642a | |||
569c67583d |
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,3 +4,5 @@
|
||||
|
||||
log.txt
|
||||
log
|
||||
|
||||
test2.pdf
|
323
cli/InputManager.ts
Normal file
323
cli/InputManager.ts
Normal file
@ -0,0 +1,323 @@
|
||||
interface EventMap {
|
||||
keypress: CLIKeypressEvent;
|
||||
char: CLICharEvent;
|
||||
activate: Event;
|
||||
deactivate: Event;
|
||||
exit: Event;
|
||||
enter: Event;
|
||||
backspace: Event;
|
||||
escape: Event;
|
||||
delete: Event;
|
||||
"arrow-left": Event;
|
||||
"arrow-right": Event;
|
||||
"arrow-up": Event;
|
||||
"arrow-down": Event;
|
||||
// [key: string]: Event;
|
||||
}
|
||||
|
||||
interface EventDetailMap {
|
||||
keypress: {
|
||||
key: number;
|
||||
sequence?: Uint8Array;
|
||||
};
|
||||
char: EventDetailMap["keypress"] & {
|
||||
char: string;
|
||||
};
|
||||
}
|
||||
|
||||
export type TypedEventTarget<EventMap extends object> = {
|
||||
new (): IntermediateEventTarget<EventMap>;
|
||||
};
|
||||
|
||||
export interface IntermediateEventTarget<EventMap> extends EventTarget {
|
||||
addEventListener<K extends keyof EventMap>(
|
||||
type: K,
|
||||
listener: (
|
||||
event: EventMap[K] extends Event ? EventMap[K] : Event,
|
||||
) => EventMap[K] extends Event ? void : never,
|
||||
options?: boolean | AddEventListenerOptions,
|
||||
): void;
|
||||
|
||||
addEventListener(
|
||||
type: string,
|
||||
listener: EventListenerOrEventListenerObject,
|
||||
options?: boolean | AddEventListenerOptions,
|
||||
): void;
|
||||
|
||||
removeEventListener<K extends keyof EventMap>(
|
||||
type: K,
|
||||
listener: (
|
||||
event: EventMap[K] extends Event ? EventMap[K] : Event,
|
||||
) => EventMap[K] extends Event ? void : never,
|
||||
options?: boolean | AddEventListenerOptions,
|
||||
): void;
|
||||
|
||||
removeEventListener(
|
||||
type: string,
|
||||
listener: EventListenerOrEventListenerObject,
|
||||
options?: boolean | AddEventListenerOptions,
|
||||
): void;
|
||||
}
|
||||
|
||||
const ManagerEventTarget = EventTarget as TypedEventTarget<EventMap>;
|
||||
|
||||
export class CLICharEvent extends CustomEvent<EventDetailMap["char"]> {
|
||||
constructor(detail: EventDetailMap["char"]) {
|
||||
super("char", { detail, cancelable: true });
|
||||
}
|
||||
}
|
||||
export class CLIKeypressEvent extends CustomEvent<EventDetailMap["keypress"]> {
|
||||
constructor(detail: EventDetailMap["keypress"]) {
|
||||
super("keypress", { detail, cancelable: true });
|
||||
}
|
||||
}
|
||||
|
||||
type bounds = {
|
||||
top?: number;
|
||||
left?: number;
|
||||
right?: number;
|
||||
bottom?: number;
|
||||
boundMode?: "relative" | "absolute";
|
||||
};
|
||||
|
||||
export class InputManager extends ManagerEventTarget {
|
||||
private static instance = new InputManager();
|
||||
private active = false;
|
||||
|
||||
static getInstance(): InputManager {
|
||||
return this.instance ??= new InputManager();
|
||||
}
|
||||
|
||||
static addEventListener = InputManager.prototype.addEventListener.bind(
|
||||
this.instance,
|
||||
);
|
||||
static removeEventListener = InputManager.prototype.removeEventListener.bind(
|
||||
this.instance,
|
||||
);
|
||||
static dispatchEvent = InputManager.prototype.dispatchEvent.bind(
|
||||
this.instance,
|
||||
);
|
||||
|
||||
activate({ raw = true }: { raw?: boolean } = {}) {
|
||||
if (this.active) return;
|
||||
this.active = true;
|
||||
this.dispatchEvent(new Event("activate"));
|
||||
this.listen(raw);
|
||||
}
|
||||
|
||||
deactivate({ dactivateRaw = true }: { dactivateRaw?: boolean } = {}) {
|
||||
if (!this.active) return;
|
||||
this.active = false;
|
||||
this.dispatchEvent(new Event("deactivate"));
|
||||
if (dactivateRaw) Deno.stdin.setRaw(false);
|
||||
}
|
||||
|
||||
once<T extends string>(type: T): Promise<Event> {
|
||||
return new Promise((resolve) => {
|
||||
const handler = (event: Event) => {
|
||||
this.removeEventListener(type, handler);
|
||||
resolve(event);
|
||||
};
|
||||
this.addEventListener(type, handler);
|
||||
});
|
||||
}
|
||||
|
||||
private async listen(raw: boolean) {
|
||||
if (raw) await Deno.stdin.setRaw(true);
|
||||
const buf = new Uint8Array(64);
|
||||
|
||||
while (this.active) {
|
||||
const n = await Deno.stdin.read(buf);
|
||||
if (n === null) break;
|
||||
|
||||
let i = 0;
|
||||
while (i < n) {
|
||||
const byte = buf[i];
|
||||
|
||||
// Ctrl+C
|
||||
if (byte === 3) {
|
||||
this.dispatchEvent(new Event("exit"));
|
||||
await Deno.stdin.setRaw(false);
|
||||
Deno.exit(130);
|
||||
}
|
||||
|
||||
// Enter
|
||||
if (byte === 13) {
|
||||
this.dispatchEvent(new Event("enter"));
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Backspace
|
||||
if (byte === 127 || byte === 8) {
|
||||
this.dispatchEvent(new Event("backspace"));
|
||||
i++;
|
||||
this.moveCursor(-1, 0);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (byte === 27 && i + 1 >= n) {
|
||||
this.dispatchEvent(new Event("escape"));
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Escape sequences
|
||||
if (byte === 27 && i + 1 < n && buf[i + 1] === 91) {
|
||||
const code = buf[i + 2];
|
||||
switch (code) {
|
||||
case 65: // Up
|
||||
this.moveCursor(0, -1);
|
||||
this.dispatchEvent(new Event("arrow-up"));
|
||||
break;
|
||||
case 66: // Down
|
||||
this.moveCursor(0, 1);
|
||||
this.dispatchEvent(new Event("arrow-down"));
|
||||
break;
|
||||
case 67: // Right
|
||||
this.moveCursor(1, 0);
|
||||
this.dispatchEvent(new Event("arrow-right"));
|
||||
break;
|
||||
case 68: // Left
|
||||
this.moveCursor(-1, 0);
|
||||
this.dispatchEvent(new Event("arrow-left"));
|
||||
break;
|
||||
|
||||
case 51:
|
||||
if (i + 3 < n && buf[i + 3] === 126) {
|
||||
this.dispatchEvent(new Event("delete"));
|
||||
i += 4;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
i += 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Printable ASCII
|
||||
if (byte >= 32 && byte <= 126) {
|
||||
this.dispatchEvent(
|
||||
new CLICharEvent({ key: byte, char: String.fromCharCode(byte) }),
|
||||
);
|
||||
this.moveCursor(1, 0);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Unknown
|
||||
this.dispatchEvent(
|
||||
new CLIKeypressEvent({ key: byte, sequence: buf.slice(i, i + 1) }),
|
||||
);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
if (raw) await Deno.stdin.setRaw(false);
|
||||
}
|
||||
|
||||
dispatchKey(key: string) {
|
||||
switch (key) {
|
||||
case "enter":
|
||||
case "backspace":
|
||||
case "arrow-up":
|
||||
case "arrow-down":
|
||||
case "arrow-right":
|
||||
case "arrow-left":
|
||||
case "delete":
|
||||
case "escape":
|
||||
this.dispatchEvent(new Event(key));
|
||||
break;
|
||||
default:
|
||||
this.dispatchEvent(
|
||||
new CLICharEvent({ key: key.charCodeAt(0), char: key }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Cursor management
|
||||
private cursor = { row: 0, col: 0 };
|
||||
private bounds: bounds = {};
|
||||
|
||||
getCursor() {
|
||||
return { ...this.cursor };
|
||||
}
|
||||
|
||||
getRelativeCursor() {
|
||||
const boundStart = (this.bounds.top ?? 0) * Deno.consoleSize().columns +
|
||||
(this.bounds.left ?? 0);
|
||||
const cursorPos = (this.cursor.row * Deno.consoleSize().columns) +
|
||||
this.cursor.col;
|
||||
return cursorPos - boundStart;
|
||||
}
|
||||
|
||||
setCursor(row: number, col: number) {
|
||||
const { columns, rows } = Deno.consoleSize();
|
||||
const { top, bottom, left, right, boundMode } = {
|
||||
top: 0,
|
||||
bottom: rows - 1,
|
||||
left: 0,
|
||||
right: columns,
|
||||
boundMode: "relative",
|
||||
...this.bounds,
|
||||
} as bounds;
|
||||
|
||||
switch (boundMode) {
|
||||
case "absolute":
|
||||
this.cursor.row = Math.max(
|
||||
top ?? -Infinity,
|
||||
Math.min(bottom ?? Infinity, row),
|
||||
);
|
||||
this.cursor.col = Math.max(
|
||||
left ?? -Infinity,
|
||||
Math.min(right ?? Infinity, col),
|
||||
);
|
||||
break;
|
||||
case "relative": {
|
||||
const boundStart = (top! * columns) + left!;
|
||||
const boundEnd = (bottom! * columns) + right!;
|
||||
let proposedPosition = (row * columns) + col;
|
||||
if (proposedPosition < boundStart) proposedPosition = boundStart;
|
||||
if (proposedPosition > boundEnd) proposedPosition = boundEnd;
|
||||
col = proposedPosition % columns;
|
||||
row = (proposedPosition - col) / columns;
|
||||
this.cursor.row = row;
|
||||
this.cursor.col = col;
|
||||
}
|
||||
}
|
||||
|
||||
this.applyCursor();
|
||||
}
|
||||
|
||||
moveCursor(dx: number, dy: number) {
|
||||
this.setCursor(this.cursor.row + dy, this.cursor.col + dx);
|
||||
}
|
||||
|
||||
setBounds(
|
||||
bounds: { top?: number; left?: number; right?: number; bottom?: number },
|
||||
) {
|
||||
this.bounds = bounds;
|
||||
this.setCursor(this.cursor.row, this.cursor.col); // enforce bounds immediately
|
||||
}
|
||||
getBounds() {
|
||||
return { ...this.bounds };
|
||||
}
|
||||
updateBounds(
|
||||
bounds: { top?: number; left?: number; right?: number; bottom?: number },
|
||||
) {
|
||||
this.bounds = { ...this.bounds, ...bounds };
|
||||
this.setCursor(this.cursor.row, this.cursor.col);
|
||||
}
|
||||
resetBounds() {
|
||||
this.bounds = {};
|
||||
this.setCursor(this.cursor.row, this.cursor.col);
|
||||
}
|
||||
|
||||
private applyCursor() {
|
||||
Deno.stdout.writeSync(
|
||||
new TextEncoder().encode(
|
||||
`\x1b[${this.cursor.row + 1};${this.cursor.col + 1}H`,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import { Cursor } from "./cursor.ts";
|
||||
import { InputManager } from "./InputManager.ts";
|
||||
|
||||
export class TerminalLayout {
|
||||
private static ALT_BUFFER_ENABLE = "\x1b[?1049h";
|
||||
@ -22,7 +23,7 @@ export class TerminalLayout {
|
||||
|
||||
Deno.addSignalListener("SIGINT", () => {
|
||||
this.clearAll();
|
||||
Deno.exit(0);
|
||||
// Deno.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
@ -121,7 +122,8 @@ export class TerminalBlock {
|
||||
|
||||
private preserveHistory = false;
|
||||
|
||||
constructor(private prepend: string = "") {}
|
||||
constructor(private prepend: string = "") {
|
||||
}
|
||||
|
||||
setPreserveHistory(preserveHistory: boolean) {
|
||||
this.preserveHistory = preserveHistory;
|
||||
@ -131,12 +133,8 @@ export class TerminalBlock {
|
||||
this.layout = layout;
|
||||
}
|
||||
|
||||
setLines(lines: string[], range?: [number, number]) {
|
||||
if (range && this.preserveHistory) {
|
||||
this.lines.splice(range[0], range[1], ...lines);
|
||||
} else {
|
||||
this.lines = this.preserveHistory ? this.lines.concat(lines) : lines;
|
||||
}
|
||||
setLines(lines: string[]) {
|
||||
this.lines = lines;
|
||||
if (this.scrollOffset > lines.length - 1) {
|
||||
this.scrollOffset = Math.max(0, lines.length - 1);
|
||||
}
|
||||
@ -148,11 +146,47 @@ export class TerminalBlock {
|
||||
);
|
||||
this.renderInternal();
|
||||
}
|
||||
range = [
|
||||
range?.[0] ?? this.lines.length - lines.length,
|
||||
range ? range[0] + lines.length : this.lines.length,
|
||||
];
|
||||
return range;
|
||||
}
|
||||
|
||||
wrapLines(maxWidth: number): string[] {
|
||||
const wrapped: string[] = [];
|
||||
const inputManager = InputManager.getInstance();
|
||||
const cursor = inputManager.getCursor();
|
||||
const bounds = inputManager.getBounds();
|
||||
|
||||
const blockStart = this.lastRenderRow;
|
||||
let visualRow = blockStart;
|
||||
|
||||
let maxCursorRow = cursor.row;
|
||||
|
||||
for (const line of this.lines) {
|
||||
const chunks: string[] = [];
|
||||
|
||||
for (let start = 0; start < line.length; start += maxWidth) {
|
||||
const chunk = line.slice(start, start + maxWidth);
|
||||
chunks.push(chunk);
|
||||
wrapped.push(this.prepend + chunk);
|
||||
}
|
||||
|
||||
const visualLines = chunks.length;
|
||||
const visualEnd = visualRow + visualLines - 1;
|
||||
|
||||
// Check if the cursor is within this wrapped line’s visual range
|
||||
if (cursor.row >= visualRow && cursor.row <= visualEnd) {
|
||||
maxCursorRow = visualEnd; // this becomes the new bottom bound
|
||||
}
|
||||
|
||||
visualRow = visualEnd + 1;
|
||||
}
|
||||
|
||||
if (maxCursorRow !== cursor.row) {
|
||||
inputManager.setBounds({
|
||||
...bounds,
|
||||
bottom: maxCursorRow - blockStart,
|
||||
});
|
||||
}
|
||||
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
append(lines: string[]) {
|
||||
@ -185,7 +219,16 @@ export class TerminalBlock {
|
||||
}
|
||||
|
||||
getRenderedLines(maxHeight: number): string[] {
|
||||
return this.lines.slice(this.scrollOffset, this.scrollOffset + maxHeight);
|
||||
const width = Deno.consoleSize().columns - this.prepend.length;
|
||||
const wrapped = this.wrapLines(width);
|
||||
return wrapped.slice(this.scrollOffset, this.scrollOffset + maxHeight);
|
||||
}
|
||||
getStartRow(): number {
|
||||
return this.lastRenderRow;
|
||||
}
|
||||
|
||||
getEndRow(): number {
|
||||
return this.lastRenderRow + this.renderedLineCount - 1;
|
||||
}
|
||||
|
||||
setRenderLines(lines: string[]) {
|
||||
@ -261,12 +304,21 @@ export class TerminalBlock {
|
||||
getFixedHeight(): number {
|
||||
return this.fixedHeight ?? 0;
|
||||
}
|
||||
requestCursorAt(
|
||||
lineOffsetFromStart = 0,
|
||||
col = 0,
|
||||
): [row: number, col: number] {
|
||||
return [this.lastRenderRow + lineOffsetFromStart, col];
|
||||
}
|
||||
|
||||
private _postRenderAction?: () => void;
|
||||
setPostRenderAction(action: (this: TerminalBlock) => void) {
|
||||
this._postRenderAction = action;
|
||||
}
|
||||
runPostRenderAction() {
|
||||
const im = InputManager.getInstance();
|
||||
im.moveCursor(0, 0);
|
||||
|
||||
if (this._postRenderAction) {
|
||||
this._postRenderAction.call(this);
|
||||
this._postRenderAction = undefined;
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { cliPrompt } from "./prompts.ts";
|
||||
import type { TerminalBlock } from "./TerminalLayout.ts";
|
||||
|
||||
type prompt = [string, (v?: string) => boolean] | string;
|
||||
type prompt = [string, (v?: string) => boolean | undefined] | string;
|
||||
|
||||
export async function forceArgs(
|
||||
args: string[],
|
||||
|
11
cli/index.ts
11
cli/index.ts
@ -6,12 +6,15 @@ import { selectMenuInteractive } from "./selectMenu.ts";
|
||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||
import { cliAlert, cliLog } from "./prompts.ts";
|
||||
import type { ITool } from "../types.ts";
|
||||
import { InputManager } from "./InputManager.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")],
|
||||
["deleteFields", import("../tools/deleteFields.ts")],
|
||||
["fieldVisibility", import("../tools/fieldVisibility.ts")],
|
||||
];
|
||||
|
||||
export class PdfToolsCli {
|
||||
@ -55,6 +58,12 @@ export class PdfToolsCli {
|
||||
}
|
||||
|
||||
public async run() {
|
||||
const im = InputManager.getInstance();
|
||||
im.activate();
|
||||
im.addEventListener("exit", () => {
|
||||
this.closeMessage = "Exiting...";
|
||||
this.cleanup();
|
||||
});
|
||||
try {
|
||||
await this.importTools();
|
||||
const titleBlock = new TerminalBlock();
|
||||
@ -78,11 +87,13 @@ export class PdfToolsCli {
|
||||
}
|
||||
} finally {
|
||||
this.cleanup();
|
||||
Deno.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
private cleanup() {
|
||||
this.terminalLayout.clearAll();
|
||||
InputManager.getInstance().deactivate();
|
||||
Deno.stdin.setRaw(false);
|
||||
if (this.closeMessage) console.log(this.closeMessage);
|
||||
}
|
||||
|
195
cli/prompts.ts
195
cli/prompts.ts
@ -1,8 +1,8 @@
|
||||
// deno-lint-disable-must-await-calls
|
||||
import { log } from "util/logfile.ts";
|
||||
import { Cursor } from "./cursor.ts";
|
||||
import { colorize } from "./style.ts";
|
||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||
import { type CLICharEvent, InputManager } from "./InputManager.ts";
|
||||
|
||||
export async function cliPrompt(
|
||||
message: string,
|
||||
@ -12,120 +12,143 @@ export async function cliPrompt(
|
||||
const input: string[] = [];
|
||||
let cursorPos = 0;
|
||||
|
||||
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 im = InputManager.getInstance();
|
||||
im.activate();
|
||||
|
||||
const render = () => {
|
||||
const line = message + " " + input.join("");
|
||||
const moveTo = `\x1b[${message.length + 2 + cursorPos}G`;
|
||||
|
||||
if (block) {
|
||||
block.setPostRenderAction(function () {
|
||||
Deno.stdout.writeSync(
|
||||
encoder.encode(`\x1b[${this["lastRenderRow"]};1H`),
|
||||
);
|
||||
Deno.stdout.writeSync(encoder.encode(moveTo));
|
||||
});
|
||||
range = block.setLines([line], range);
|
||||
block.setLines([line]);
|
||||
} else {
|
||||
Deno.stdout.writeSync(encoder.encode("\x1b[K" + line + moveTo));
|
||||
Deno.stdout.writeSync(encoder.encode("\r\x1b[K" + line + moveTo));
|
||||
}
|
||||
};
|
||||
|
||||
render();
|
||||
const cPos = block?.requestCursorAt(0, message.length + 1);
|
||||
if (cPos) {
|
||||
const [row, column] = cPos;
|
||||
|
||||
const buf = new Uint8Array(64); // large enough for most pastes
|
||||
inputLoop:
|
||||
while (true) {
|
||||
const n = await Deno.stdin.read(buf);
|
||||
if (n === null) break;
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
const byte = buf[i];
|
||||
|
||||
// Ctrl+C
|
||||
if (byte === 3) {
|
||||
block?.clear();
|
||||
block?.["layout"]?.clearAll();
|
||||
await Deno.stdin.setRaw(false);
|
||||
Deno.exit(130);
|
||||
im.setCursor(row, column);
|
||||
im.setBounds({ top: row, left: column, right: column, bottom: row });
|
||||
}
|
||||
|
||||
if (byte === 13) { // Enter
|
||||
break inputLoop;
|
||||
}
|
||||
const exit = () => {
|
||||
im.removeEventListener("enter", onEnter);
|
||||
im.removeEventListener("backspace", onBackspace);
|
||||
im.removeEventListener("delete", onDelete);
|
||||
im.removeEventListener("arrow-left", onLeft);
|
||||
im.removeEventListener("arrow-right", onRight);
|
||||
im.removeEventListener("char", onKey);
|
||||
Cursor.hide();
|
||||
};
|
||||
|
||||
// Escape sequence?
|
||||
if (byte === 27 && i + 1 < n && buf[i + 1] === 91) {
|
||||
const third = buf[i + 2];
|
||||
if (third === 68 && cursorPos > 0) cursorPos--; // Left
|
||||
else if (third === 67 && cursorPos < input.length) cursorPos++; // Right
|
||||
else if (third === 51 && i + 3 < n && buf[i + 3] === 126) { // Delete
|
||||
if (cursorPos < input.length) input.splice(cursorPos, 1);
|
||||
i += 1; // consume tilde
|
||||
}
|
||||
i += 2; // consume ESC [ X
|
||||
continue;
|
||||
}
|
||||
let resolve: null | ((value: string) => void) = null;
|
||||
|
||||
// Backspace
|
||||
if (byte === 127 || byte === 8) {
|
||||
const onEnter = () => {
|
||||
exit();
|
||||
resolve?.(input.join(""));
|
||||
};
|
||||
|
||||
const onBackspace = () => {
|
||||
if (cursorPos > 0) {
|
||||
input.splice(cursorPos - 1, 1);
|
||||
cursorPos--;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Delete (ASCII 46)
|
||||
if (byte === 46 && cursorPos < input.length) {
|
||||
input.splice(cursorPos, 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Printable
|
||||
if (byte >= 32 && byte <= 126) {
|
||||
input.splice(cursorPos, 0, String.fromCharCode(byte));
|
||||
cursorPos++;
|
||||
}
|
||||
|
||||
// Other cases: ignore
|
||||
}
|
||||
|
||||
render();
|
||||
}
|
||||
};
|
||||
|
||||
await Deno.stdin.setRaw(false);
|
||||
if (!cursorVisible) {
|
||||
Cursor.hide();
|
||||
const onDelete = () => {
|
||||
if (cursorPos < input.length) {
|
||||
input.splice(cursorPos, 1);
|
||||
render();
|
||||
}
|
||||
Deno.stdout.writeSync(encoder.encode("\n"));
|
||||
};
|
||||
|
||||
return input.join("");
|
||||
const onLeft = () => {
|
||||
if (cursorPos > 0) {
|
||||
cursorPos--;
|
||||
render();
|
||||
}
|
||||
};
|
||||
|
||||
const onRight = () => {
|
||||
if (cursorPos < input.length) {
|
||||
cursorPos++;
|
||||
render();
|
||||
}
|
||||
};
|
||||
|
||||
const onKey = (e: Event) => {
|
||||
const ke = (e as CLICharEvent).detail;
|
||||
cursorPos = im.getRelativeCursor();
|
||||
input.splice(cursorPos, 0, ke.char);
|
||||
im.updateBounds({ right: input.length + message.length + 1 });
|
||||
render();
|
||||
};
|
||||
|
||||
return await new Promise<string>((res) => {
|
||||
resolve = res;
|
||||
im.addEventListener("enter", onEnter);
|
||||
im.addEventListener("backspace", onBackspace);
|
||||
im.addEventListener("delete", onDelete);
|
||||
im.addEventListener("arrow-left", onLeft);
|
||||
im.addEventListener("arrow-right", onRight);
|
||||
im.addEventListener("char", onKey);
|
||||
});
|
||||
}
|
||||
|
||||
export function cliConfirm(message: string, block?: TerminalBlock) {
|
||||
return cliPrompt(message + " (y/n)", block).then((v) =>
|
||||
v.toLowerCase() === "y"
|
||||
export async function cliConfirm(message: string, block?: TerminalBlock) {
|
||||
const im = InputManager.getInstance();
|
||||
let inpout = "";
|
||||
function isValidInput(input: string) {
|
||||
switch (input) {
|
||||
case "y":
|
||||
case "n":
|
||||
return inpout.length === 0;
|
||||
case "e":
|
||||
return inpout === "y";
|
||||
case "s":
|
||||
return inpout === "ye";
|
||||
case "o":
|
||||
return inpout === "n";
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function onKey(e: CLICharEvent) {
|
||||
const ke = e.detail;
|
||||
const char = String.fromCharCode(ke.key);
|
||||
if (isValidInput(char)) {
|
||||
inpout += char;
|
||||
} else {
|
||||
e.stopImmediatePropagation();
|
||||
}
|
||||
}
|
||||
im.addEventListener("char", onKey);
|
||||
const value = await cliPrompt(message + " (y/n)", block).then((v) =>
|
||||
v.charAt(0).toLowerCase() === "y"
|
||||
);
|
||||
im.removeEventListener("char", onKey);
|
||||
return value;
|
||||
}
|
||||
|
||||
export function cliAlert(message: string, block?: TerminalBlock) {
|
||||
return cliPrompt(
|
||||
export async function cliAlert(message: string, block?: TerminalBlock) {
|
||||
const im = InputManager.getInstance();
|
||||
const onKey = (e: CLICharEvent) => {
|
||||
e.stopImmediatePropagation();
|
||||
};
|
||||
im.addEventListener("char", onKey);
|
||||
await cliPrompt(
|
||||
message + colorize(" Press Enter to continue", "gray"),
|
||||
block,
|
||||
).then((v) => {
|
||||
return v;
|
||||
});
|
||||
);
|
||||
im.removeEventListener("char", onKey);
|
||||
}
|
||||
|
||||
export function cliLog(
|
||||
@ -158,11 +181,17 @@ if (import.meta.main) {
|
||||
layout.register("block", block);
|
||||
layout.register("footer", footer);
|
||||
|
||||
Deno.addSignalListener("SIGINT", () => {
|
||||
InputManager.addEventListener("exit", () => {
|
||||
layout.clearAll();
|
||||
// console.clear();
|
||||
Deno.exit(0);
|
||||
});
|
||||
|
||||
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);
|
||||
|
@ -1,6 +1,8 @@
|
||||
import type { callback } from "../types.ts";
|
||||
import { type CLICharEvent, InputManager } from "./InputManager.ts";
|
||||
import { cliLog } from "./prompts.ts";
|
||||
import { colorize } from "./style.ts";
|
||||
import { TerminalBlock } from "./TerminalLayout.ts";
|
||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||
|
||||
interface ISelectMenuConfig {
|
||||
terminalBlock?: TerminalBlock;
|
||||
@ -28,7 +30,6 @@ export async function selectMenuInteractive(
|
||||
terminalBlock.setRenderHeight(Deno.consoleSize().rows);
|
||||
}
|
||||
|
||||
let range: [number, number] = [terminalBlock.lineCount, 1];
|
||||
function renderMenu() {
|
||||
const { rows } = Deno.consoleSize();
|
||||
const terminalHeight = terminalBlock.getRenderHeight() || rows;
|
||||
@ -50,7 +51,7 @@ export async function selectMenuInteractive(
|
||||
}
|
||||
}
|
||||
|
||||
range = terminalBlock.setLines(lines, range);
|
||||
terminalBlock.setLines(lines);
|
||||
}
|
||||
|
||||
function numberAndPadding(i: number, prefix?: string) {
|
||||
@ -62,23 +63,47 @@ export async function selectMenuInteractive(
|
||||
|
||||
let inputBuffer = "";
|
||||
|
||||
// Function to handle input
|
||||
async function handleInput() {
|
||||
const buf = new Uint8Array(3); // arrow keys send 3 bytes
|
||||
while (true) {
|
||||
const im = InputManager.getInstance();
|
||||
im.activate();
|
||||
|
||||
const onUp = (e: Event) => {
|
||||
e.stopImmediatePropagation();
|
||||
selected = (selected - 1 + options.length) % options.length;
|
||||
renderMenu();
|
||||
const n = await Deno.stdin.read(buf);
|
||||
if (n === null) break;
|
||||
};
|
||||
|
||||
const [a, b, c] = buf;
|
||||
const onDown = (e: Event) => {
|
||||
e.stopImmediatePropagation();
|
||||
selected = (selected + 1) % options.length;
|
||||
renderMenu();
|
||||
};
|
||||
|
||||
if (a === 3) {
|
||||
Deno.stdin.setRaw(false);
|
||||
terminalBlock?.["layout"]?.clearAll();
|
||||
Deno.exit(130);
|
||||
}
|
||||
const onKey = (e: CLICharEvent) => {
|
||||
e.stopImmediatePropagation();
|
||||
const ke = e.detail;
|
||||
const char = String.fromCharCode(ke.key);
|
||||
inputBuffer += char;
|
||||
};
|
||||
|
||||
if (a === 13) { // Enter key
|
||||
const onBackspace = (e: Event) => {
|
||||
e.stopImmediatePropagation();
|
||||
inputBuffer = inputBuffer.slice(0, -1);
|
||||
};
|
||||
|
||||
let resolve: null | ((value: string | null) => void) = null;
|
||||
|
||||
const onEscape = () => {
|
||||
im.removeEventListener("arrow-up", onUp);
|
||||
im.removeEventListener("arrow-down", onDown);
|
||||
im.removeEventListener("char", onKey);
|
||||
im.removeEventListener("backspace", onBackspace);
|
||||
im.removeEventListener("enter", onEnter);
|
||||
im.removeEventListener("escape", onEscape);
|
||||
resolve?.(null);
|
||||
};
|
||||
|
||||
const onEnter = (e: Event) => {
|
||||
e.stopImmediatePropagation();
|
||||
if (inputBuffer) {
|
||||
const parsed = parseInt(inputBuffer);
|
||||
if (!isNaN(parsed)) {
|
||||
@ -86,40 +111,44 @@ export async function selectMenuInteractive(
|
||||
}
|
||||
inputBuffer = "";
|
||||
}
|
||||
break;
|
||||
} else if (a === 27 && b === 91) { // Arrow keys
|
||||
inputBuffer = "";
|
||||
if (c === 65) { // Up
|
||||
selected = (selected - 1 + options.length) % options.length;
|
||||
} else if (c === 66) { // Down
|
||||
selected = (selected + 1) % options.length;
|
||||
}
|
||||
} else if (a >= 48 && a <= 57) {
|
||||
inputBuffer += String.fromCharCode(a);
|
||||
} else if (a === 8) {
|
||||
inputBuffer = inputBuffer.slice(0, -1);
|
||||
}
|
||||
}
|
||||
im.removeEventListener("arrow-up", onUp);
|
||||
im.removeEventListener("arrow-down", onDown);
|
||||
im.removeEventListener("char", onKey);
|
||||
im.removeEventListener("backspace", onBackspace);
|
||||
im.removeEventListener("enter", onEnter);
|
||||
im.removeEventListener("escape", onEscape);
|
||||
resolve?.(options[selected]);
|
||||
};
|
||||
|
||||
Deno.stdin.setRaw(false);
|
||||
return options[selected];
|
||||
}
|
||||
terminalBlock.setLines(["Selected: " + options[selected]], range);
|
||||
return await handleInput();
|
||||
renderMenu();
|
||||
const final = await new Promise<string | null>((res) => {
|
||||
resolve = res;
|
||||
im.addEventListener("char", onKey);
|
||||
im.addEventListener("backspace", onBackspace);
|
||||
im.addEventListener("enter", onEnter);
|
||||
im.addEventListener("arrow-up", onUp);
|
||||
im.addEventListener("arrow-down", onDown);
|
||||
im.addEventListener("escape", onEscape);
|
||||
});
|
||||
|
||||
// terminalBlock.setLines(["Selected: " + final], range);
|
||||
|
||||
return final;
|
||||
}
|
||||
|
||||
export async function multiSelectMenuInteractive(
|
||||
q: string,
|
||||
options: string[] | [string, callback][],
|
||||
config?: ISelectMenuConfig,
|
||||
options: (string | [string, callback])[],
|
||||
config?: ISelectMenuConfig & { allOption?: boolean },
|
||||
): 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 (config?.allOption) {
|
||||
options.unshift("Select All");
|
||||
}
|
||||
const rawValues = options.map((i) => typeof i === "string" ? i : i[0]);
|
||||
|
||||
if (rawValues.length !== options.length) {
|
||||
throw new Error("Duplicate options in multi-select menu");
|
||||
@ -130,7 +159,19 @@ export async function multiSelectMenuInteractive(
|
||||
terminalBlock.setRenderHeight(Deno.consoleSize().rows);
|
||||
}
|
||||
|
||||
let range: [number, number] = [terminalBlock.lineCount, 1];
|
||||
const checkSelectAll = () => {
|
||||
if (selectedOptions.includes(0)) {
|
||||
selectedOptions = [];
|
||||
} else {
|
||||
selectedOptions = Array.from(options).map((_, i) => i);
|
||||
}
|
||||
};
|
||||
|
||||
const validateSelectAll = () => {
|
||||
const allPresent = selectedOptions.length == options.length;
|
||||
if (!allPresent) selectedOptions = selectedOptions.filter((e) => e != 0);
|
||||
};
|
||||
|
||||
function renderMenu() {
|
||||
const { rows } = Deno.consoleSize();
|
||||
const terminalHeight = terminalBlock.getRenderHeight() || rows;
|
||||
@ -155,47 +196,70 @@ export async function multiSelectMenuInteractive(
|
||||
}
|
||||
}
|
||||
|
||||
range = terminalBlock.setLines(lines, range);
|
||||
terminalBlock.setLines(lines);
|
||||
}
|
||||
|
||||
// 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 im = InputManager.getInstance();
|
||||
im.activate();
|
||||
|
||||
const [a, b, c] = buf;
|
||||
let resolve = null as null | ((value: number[] | null) => void);
|
||||
|
||||
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
|
||||
const onUp = (e: Event) => {
|
||||
e.stopImmediatePropagation();
|
||||
selected = (selected - 1 + options.length) % options.length;
|
||||
} else if (c === 66) { // Down
|
||||
renderMenu();
|
||||
};
|
||||
|
||||
const onDown = (e: Event) => {
|
||||
e.stopImmediatePropagation();
|
||||
selected = (selected + 1) % options.length;
|
||||
}
|
||||
} else if (a === 32) { // Space
|
||||
Deno.stdout.writeSync(new TextEncoder().encode("\x07"));
|
||||
if (selectedOptions.includes(selected)) {
|
||||
renderMenu();
|
||||
};
|
||||
|
||||
const onSpace = (e: CLICharEvent) => {
|
||||
if (e.detail.char !== " ") return;
|
||||
e.stopImmediatePropagation();
|
||||
if (config?.allOption && selected === 0) {
|
||||
checkSelectAll();
|
||||
} else if (selectedOptions.includes(selected)) {
|
||||
selectedOptions = selectedOptions.filter((i) => i !== selected);
|
||||
} else {
|
||||
selectedOptions.push(selected);
|
||||
}
|
||||
}
|
||||
}
|
||||
validateSelectAll();
|
||||
renderMenu();
|
||||
};
|
||||
|
||||
Deno.stdin.setRaw(false);
|
||||
return selectedOptions;
|
||||
}
|
||||
const selections = await handleInput();
|
||||
const onEscape = () => {
|
||||
im.removeEventListener("arrow-up", onUp);
|
||||
im.removeEventListener("arrow-down", onDown);
|
||||
im.removeEventListener("char", onSpace);
|
||||
im.removeEventListener("enter", onEnter);
|
||||
im.removeEventListener("escape", onEscape);
|
||||
resolve?.(null);
|
||||
};
|
||||
|
||||
const onEnter = (e: Event) => {
|
||||
e.stopImmediatePropagation();
|
||||
im.removeEventListener("arrow-up", onUp);
|
||||
im.removeEventListener("arrow-down", onDown);
|
||||
im.removeEventListener("char", onSpace);
|
||||
im.removeEventListener("enter", onEnter);
|
||||
im.removeEventListener("escape", onEscape);
|
||||
resolve?.(selectedOptions);
|
||||
};
|
||||
|
||||
renderMenu();
|
||||
|
||||
const selections = await new Promise<number[] | null>((res) => {
|
||||
resolve = res;
|
||||
im.addEventListener("arrow-up", onUp);
|
||||
im.addEventListener("arrow-down", onDown);
|
||||
im.addEventListener("char", onSpace);
|
||||
im.addEventListener("enter", onEnter);
|
||||
im.addEventListener("escape", onEscape);
|
||||
});
|
||||
if (!selections) return null;
|
||||
for (const optionI of selections) {
|
||||
const option = options[optionI];
|
||||
if (Array.isArray(option)) {
|
||||
@ -203,22 +267,22 @@ export async function multiSelectMenuInteractive(
|
||||
}
|
||||
}
|
||||
const final = selectedOptions.map((i) => rawValues[i]);
|
||||
terminalBlock.setLines(["Selected: " + final.join(", ")], range);
|
||||
terminalBlock.setLines(["Selected: " + final.join(", ")]);
|
||||
return final;
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
// const layout = new TerminalLayout();
|
||||
// const block = new TerminalBlock();
|
||||
// const titleBlock = new TerminalBlock();
|
||||
// const postBlock = new TerminalBlock();
|
||||
// titleBlock.setLines(["An incredible fruit menu!"]);
|
||||
// postBlock.setLines(["I'm here too!"]);
|
||||
// titleBlock.setFixedHeight(1);
|
||||
// postBlock.setFixedHeight(1);
|
||||
// layout.register("title", titleBlock);
|
||||
// layout.register("block", block);
|
||||
// layout.register("post", postBlock);
|
||||
const layout = new TerminalLayout();
|
||||
const block = new TerminalBlock();
|
||||
const titleBlock = new TerminalBlock();
|
||||
const postBlock = new TerminalBlock();
|
||||
InputManager.addEventListener("exit", () => layout.clearAll());
|
||||
titleBlock.setLines(["An incredible fruit menu!"]);
|
||||
postBlock.setLines(["I'm here too!"]);
|
||||
titleBlock.setFixedHeight(1);
|
||||
postBlock.setFixedHeight(1);
|
||||
layout.register("title", titleBlock);
|
||||
layout.register("block", block);
|
||||
|
||||
// const val = await selectMenuInteractive("choose a fruit", [
|
||||
// "apple",
|
||||
@ -276,8 +340,8 @@ if (import.meta.main) {
|
||||
"ximenia",
|
||||
"yuzu",
|
||||
"zucchini",
|
||||
]);
|
||||
console.log(val);
|
||||
], { terminalBlock: block, allOption: true });
|
||||
cliLog(val || "No value", block);
|
||||
|
||||
// Deno.stdout.writeSync(new TextEncoder().encode("\x07"));
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
{
|
||||
"name": "@bearmetal/pdf-tools",
|
||||
"version": "1.0.7",
|
||||
"version": "1.0.8-p",
|
||||
"license": "GPL 3.0",
|
||||
"tasks": {
|
||||
"dev": "deno run -A --env-file=.env main.ts",
|
||||
"dev": "deno run -A main.ts",
|
||||
"compile": "deno compile -o pdf-tools.exe --target x86_64-pc-windows-msvc --include ./asciiart.txt -A ./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"
|
||||
|
5
main.ts
5
main.ts
@ -1,5 +1,10 @@
|
||||
/// <reference types="./types.ts" />
|
||||
import { PdfToolsCli } from "./cli/index.ts";
|
||||
// import { log } from "util/logfile.ts";
|
||||
|
||||
// try {
|
||||
const app = new PdfToolsCli();
|
||||
app.run();
|
||||
// } catch (e) {
|
||||
// // log(e);
|
||||
// }
|
||||
|
BIN
testing/test.pdf
BIN
testing/test.pdf
Binary file not shown.
49
tools/deleteFields.ts
Normal file
49
tools/deleteFields.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { forceArgs } from "../cli/forceArgs.ts";
|
||||
import { cliPrompt } from "../cli/prompts.ts";
|
||||
import { multiSelectMenuInteractive } from "../cli/selectMenu.ts";
|
||||
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||
import type { callback, ITool } from "../types.ts";
|
||||
import { loadPdf, savePdf } from "util/saveLoadPdf.ts";
|
||||
|
||||
export class DeleteFormFields implements ITool {
|
||||
name = "deleteFormFields";
|
||||
description = "delete multiple form fields from a PDF";
|
||||
block?: TerminalBlock;
|
||||
|
||||
async run(pdfPath: string = "") {
|
||||
if (!this.block) this.block = new TerminalBlock();
|
||||
[pdfPath] = await forceArgs([pdfPath], [[
|
||||
"Please provide path to PDF",
|
||||
(d) => d?.endsWith(".pdf"),
|
||||
]], this.block);
|
||||
|
||||
const pdf = await loadPdf(pdfPath);
|
||||
const form = pdf.getForm();
|
||||
const fields = form.getFields();
|
||||
let updatesMade = false;
|
||||
await multiSelectMenuInteractive(
|
||||
`${pdfPath}\nSelect fields to delete:`,
|
||||
fields.map<[string, callback]>((
|
||||
f,
|
||||
) => [f.getName(), () => {
|
||||
while (f.acroField.getWidgets().length) {
|
||||
f.acroField.removeWidget(0);
|
||||
}
|
||||
form.removeField(f);
|
||||
updatesMade = true;
|
||||
}]),
|
||||
);
|
||||
if (!updatesMade) return;
|
||||
const path = await cliPrompt(
|
||||
"Save to path (or hit enter to keep current):",
|
||||
this.block,
|
||||
) || pdfPath;
|
||||
await savePdf(pdf, path);
|
||||
}
|
||||
help?: (() => Promise<void> | void) | undefined;
|
||||
done?: (() => Promise<void> | void) | undefined;
|
||||
setBlock(block: TerminalBlock) {
|
||||
this.block = block;
|
||||
}
|
||||
}
|
||||
export default new DeleteFormFields();
|
@ -1,6 +1,18 @@
|
||||
import { type PDFAcroField, type PDFField, PDFName, PDFString } from "pdf-lib";
|
||||
import { loadPdf, loadPdfForm, savePdf } from "util/saveLoadPdf.ts";
|
||||
import { callWithArgPrompt } from "util/call.ts";
|
||||
import {
|
||||
type PDFAcroField,
|
||||
PDFArray,
|
||||
PDFCheckBox,
|
||||
type PDFDocument,
|
||||
type PDFField,
|
||||
PDFName,
|
||||
PDFNumber,
|
||||
PDFRadioGroup,
|
||||
type PDFRef,
|
||||
PDFString,
|
||||
PDFTextField,
|
||||
type PDFWidgetAnnotation,
|
||||
} from "pdf-lib";
|
||||
import { loadPdf, savePdf } from "util/saveLoadPdf.ts";
|
||||
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||
import { forceArgs } from "../cli/forceArgs.ts";
|
||||
import { colorize } from "../cli/style.ts";
|
||||
@ -9,42 +21,6 @@ import { multiSelectMenuInteractive } from "../cli/selectMenu.ts";
|
||||
import type { callback, ITool } from "../types.ts";
|
||||
import { toCase } from "util/caseManagement.ts";
|
||||
|
||||
async function renameFields(
|
||||
path: string,
|
||||
pattern: string | RegExp,
|
||||
change: string,
|
||||
) {
|
||||
if (typeof pattern === "string") pattern = new RegExp(pattern);
|
||||
const form = await loadPdfForm(path);
|
||||
const fields = form.getFields();
|
||||
let changesMade = false;
|
||||
for (const field of fields) {
|
||||
const name = field.getName();
|
||||
if (pattern.test(name)) {
|
||||
console.log(name + " %cfound", "color: red");
|
||||
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) {
|
||||
changesMade = true;
|
||||
cField.dict.set(PDFName.of("T"), PDFString.of(mName));
|
||||
}
|
||||
}
|
||||
cField = cField.getParent();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changesMade) {
|
||||
savePdf(form.doc, path);
|
||||
}
|
||||
}
|
||||
|
||||
function applyRename(
|
||||
field: PDFField,
|
||||
name: string,
|
||||
@ -70,6 +46,294 @@ function applyRename(
|
||||
}
|
||||
}
|
||||
|
||||
// function applyWidgetRename(
|
||||
// doc: PDFDocument,
|
||||
// field: PDFField,
|
||||
// widget: PDFWidgetAnnotation,
|
||||
// name: string,
|
||||
// pattern: RegExp,
|
||||
// change: string,
|
||||
// ) {
|
||||
// if (field.acroField.getWidgets().length > 1) {
|
||||
// const widgets = field.acroField.getWidgets();
|
||||
// const widgetIndex = widgets.indexOf(widget);
|
||||
// widgets.splice(widgetIndex, 1);
|
||||
|
||||
// const pdfDocContext = doc.context;
|
||||
|
||||
// const originalRef = field.acroField.ref;
|
||||
// const originalFieldDict = pdfDocContext.lookup(originalRef);
|
||||
// if (!originalFieldDict) return;
|
||||
|
||||
// const newFieldDict = pdfDocContext.obj({
|
||||
// ...originalFieldDict,
|
||||
// T: PDFString.of(name.replace(pattern, change)),
|
||||
// Kids: [getWidgetRef(widget, doc.getPages())],
|
||||
// });
|
||||
// const newField = pdfDocContext.register(newFieldDict);
|
||||
|
||||
// const acroForm = doc.catalog.lookup(PDFName.of("AcroForm"), PDFDict);
|
||||
// const fields = acroForm.lookup(PDFName.of("Fields"), PDFArray);
|
||||
// fields.push(newField);
|
||||
// }
|
||||
// }
|
||||
function findPageForWidget(
|
||||
doc: PDFDocument,
|
||||
widget: PDFWidgetAnnotation,
|
||||
) {
|
||||
const pages = doc.getPages();
|
||||
for (const page of pages) {
|
||||
const annots = page.node.Annots();
|
||||
if (!annots) continue;
|
||||
|
||||
const annotRefs = annots.asArray();
|
||||
for (const ref of annotRefs) {
|
||||
const annot = doc.context.lookup(ref);
|
||||
if (annot === widget.dict) {
|
||||
return page;
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function detectFieldType(field: PDFField): string | undefined {
|
||||
const ft = field.acroField.dict.get(PDFName.of("FT"));
|
||||
return ft instanceof PDFName ? ft.asString() : undefined;
|
||||
}
|
||||
|
||||
function getFlag(field: PDFField, bit: number): boolean {
|
||||
const ff = field.acroField.dict.get(PDFName.of("Ff"));
|
||||
return ff instanceof PDFNumber ? (ff.asNumber() & (1 << bit)) !== 0 : false;
|
||||
}
|
||||
|
||||
function getWidgetRef(
|
||||
widget: PDFWidgetAnnotation,
|
||||
doc: PDFDocument,
|
||||
): PDFRef | undefined {
|
||||
for (const page of doc.getPages()) {
|
||||
const annots = page.node.Annots()?.asArray() ?? [];
|
||||
for (const ref of annots) {
|
||||
const maybeDict = doc.context.lookup(ref);
|
||||
if (maybeDict === widget.dict) {
|
||||
return ref as PDFRef;
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function applyWidgetRename(
|
||||
doc: PDFDocument,
|
||||
field: PDFField,
|
||||
widget: PDFWidgetAnnotation,
|
||||
newName: string,
|
||||
pattern: RegExp,
|
||||
change: string,
|
||||
) {
|
||||
try {
|
||||
const form = doc.getForm();
|
||||
const widgets = field.acroField.getWidgets();
|
||||
|
||||
if (widgets.length <= 1) return;
|
||||
const widgetDict = widget.dict;
|
||||
const widgetIndex = widgets.findIndex((w) => w.dict === widgetDict);
|
||||
if (widgetIndex === -1) return;
|
||||
|
||||
widgets.splice(widgetIndex, 1);
|
||||
|
||||
const kids = field.acroField.dict.lookup(PDFName.of("Kids"), PDFArray);
|
||||
if (kids) {
|
||||
const updatedKids = kids.asArray().filter((ref) => {
|
||||
const maybeDict = doc.context.lookup(ref);
|
||||
return maybeDict !== widget.dict;
|
||||
});
|
||||
field.acroField.dict.set(
|
||||
PDFName.of("Kids"),
|
||||
doc.context.obj(updatedKids),
|
||||
);
|
||||
}
|
||||
|
||||
const page = findPageForWidget(doc, widget);
|
||||
if (!page) throw new Error("Widget page not found");
|
||||
|
||||
const rect = widget.getRectangle();
|
||||
if (!rect) throw new Error("Widget has no rectangle");
|
||||
|
||||
const finalName = newName.replace(pattern, change);
|
||||
|
||||
// Try to get existing field with the new name
|
||||
let targetField: PDFField | undefined;
|
||||
|
||||
try {
|
||||
targetField = form.getField(finalName);
|
||||
} catch {
|
||||
// Field doesn't exist — that's fine
|
||||
}
|
||||
|
||||
// Compare field types if field exists
|
||||
if (targetField) {
|
||||
const sourceType = detectFieldType(field);
|
||||
const targetType = detectFieldType(targetField);
|
||||
|
||||
if (sourceType !== targetType) {
|
||||
throw new Error(
|
||||
`Field "${finalName}" already exists with a different type (${targetType} vs ${sourceType})`,
|
||||
);
|
||||
}
|
||||
|
||||
// ✅ Same type — attach widget to the existing field
|
||||
// const targetFieldWidgets = targetField.acroField.getWidgets();
|
||||
const targetKidsArray = targetField.acroField.dict.lookup(
|
||||
PDFName.of("Kids"),
|
||||
PDFArray,
|
||||
);
|
||||
|
||||
// Set /Parent on the widget to point to the existing field
|
||||
widget.dict.set(PDFName.of("Parent"), targetField.acroField.ref);
|
||||
|
||||
// Add the widget to the field's /Kids array
|
||||
const widgetRef = getWidgetRef(widget, doc);
|
||||
if (!widgetRef) throw new Error("Widget ref not found");
|
||||
if (targetKidsArray) {
|
||||
targetKidsArray.push(widgetRef);
|
||||
} else {
|
||||
targetField.acroField.dict.set(
|
||||
PDFName.of("Kids"),
|
||||
doc.context.obj([widgetRef]),
|
||||
);
|
||||
}
|
||||
|
||||
// Also ensure widget is attached to a page
|
||||
const page = findPageForWidget(doc, widget);
|
||||
if (!page) throw new Error("Widget's page not found");
|
||||
|
||||
const pageAnnots = page.node.Annots();
|
||||
const refs = pageAnnots?.asArray() ?? [];
|
||||
if (!refs.includes(widgetRef)) {
|
||||
refs.push(widgetRef);
|
||||
page.node.set(PDFName.of("Annots"), doc.context.obj(refs));
|
||||
}
|
||||
|
||||
return; // Done
|
||||
}
|
||||
removeWidgetFromPage(widget, doc);
|
||||
|
||||
const fieldType = detectFieldType(field);
|
||||
|
||||
let newField: PDFField;
|
||||
|
||||
switch (fieldType) {
|
||||
case "/Tx": {
|
||||
const tf = form.createTextField(finalName);
|
||||
if (field instanceof PDFTextField) {
|
||||
const val = field.getText();
|
||||
if (val) tf.setText(val);
|
||||
}
|
||||
newField = tf;
|
||||
break;
|
||||
}
|
||||
|
||||
case "/Btn": {
|
||||
const isRadio = getFlag(field, 15);
|
||||
if (isRadio) {
|
||||
const rf = form.createRadioGroup(finalName);
|
||||
rf.addOptionToPage(finalName, page, {
|
||||
x: rect.x,
|
||||
y: rect.y,
|
||||
width: rect.width,
|
||||
height: rect.height,
|
||||
});
|
||||
if (field instanceof PDFRadioGroup) {
|
||||
const selected = field.getSelected();
|
||||
if (selected) rf.select(selected);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
const cb = form.createCheckBox(finalName);
|
||||
cb.addToPage(page, {
|
||||
x: rect.x,
|
||||
y: rect.y,
|
||||
width: rect.width,
|
||||
height: rect.height,
|
||||
});
|
||||
if (field instanceof PDFCheckBox && field.isChecked()) {
|
||||
cb.check();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error(`Unsupported field type: ${fieldType}`);
|
||||
}
|
||||
|
||||
// Attach the new field to the page if necessary
|
||||
if (
|
||||
newField instanceof PDFTextField ||
|
||||
newField instanceof PDFCheckBox
|
||||
) {
|
||||
newField.addToPage(page, {
|
||||
x: rect.x,
|
||||
y: rect.y,
|
||||
width: rect.width,
|
||||
height: rect.height,
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// log(e);
|
||||
}
|
||||
}
|
||||
|
||||
function removeWidgetFromPage(widget: PDFWidgetAnnotation, doc: PDFDocument) {
|
||||
const pages = doc.getPages();
|
||||
|
||||
for (const page of pages) {
|
||||
const annotsArray = page.node.Annots();
|
||||
if (!annotsArray) continue;
|
||||
|
||||
const refs = annotsArray.asArray();
|
||||
const newRefs = refs.filter((ref) => {
|
||||
const maybeDict = doc.context.lookup(ref);
|
||||
return maybeDict !== widget.dict;
|
||||
});
|
||||
|
||||
// Replace /Annots with updated array
|
||||
if (newRefs.length === refs.length) continue;
|
||||
page.node.set(PDFName.of("Annots"), doc.context.obj(newRefs));
|
||||
}
|
||||
}
|
||||
|
||||
// function getWidgetRef(
|
||||
// widget: PDFWidgetAnnotation,
|
||||
// pages: PDFPage[],
|
||||
// ): PDFRef | undefined {
|
||||
// const widgetRect = (widget?.dict?.get(PDFName.of("Rect")) as PDFArray)
|
||||
// ?.asArray();
|
||||
// const widgetFT = (widget?.dict?.get(PDFName.of("FT")) as PDFString)
|
||||
// ?.["value"];
|
||||
|
||||
// for (const page of pages) {
|
||||
// const annotsArray = page.node.Annots()?.asArray();
|
||||
// if (!annotsArray) continue;
|
||||
|
||||
// for (const annotRef of annotsArray) {
|
||||
// const annotDict = page.doc.context.lookup(annotRef);
|
||||
// if (!annotDict) continue;
|
||||
// if (!(annotDict instanceof PDFDict)) continue;
|
||||
// const rect = (annotDict.get(PDFName.of("Rect")) as PDFArray)?.asArray();
|
||||
// const ft = (annotDict.get(PDFName.of("FT")) as PDFString)?.["value"];
|
||||
|
||||
// // rudimentary match (you can add more checks like /T, /Subtype, etc.)
|
||||
// if (rect?.toString() === widgetRect?.toString() && ft === widgetFT) {
|
||||
// return annotRef as PDFRef;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// return undefined;
|
||||
// }
|
||||
|
||||
/***
|
||||
* Evaluates the change string with the match array
|
||||
*
|
||||
@ -83,7 +347,7 @@ function applyRename(
|
||||
* - $<int>u - capture groups, indexed from 1, transforming a string to upper case
|
||||
* - $<int>t - capture groups, indexed from 1, transforming a string to title case
|
||||
*/
|
||||
function evaluateChange(change: string, match: RegExpExecArray) {
|
||||
function evaluateChange(change: string, match: RegExpExecArray, index: number) {
|
||||
return change.replace(
|
||||
/\$(\d+)([icslut]?)/g,
|
||||
(_, i, indexed) => {
|
||||
@ -106,6 +370,18 @@ function evaluateChange(change: string, match: RegExpExecArray) {
|
||||
return match[i];
|
||||
}
|
||||
},
|
||||
)
|
||||
.replace(
|
||||
/\$I{((\w+,?)+)}/,
|
||||
(_, offset) => {
|
||||
const options = offset.split(",");
|
||||
return options[index % options.length];
|
||||
},
|
||||
)
|
||||
.replace(
|
||||
/\$I(-?\d+)?/,
|
||||
(_, offset) =>
|
||||
(parseInt(offset) ? index + parseInt(offset) : index).toString(),
|
||||
);
|
||||
}
|
||||
|
||||
@ -128,7 +404,7 @@ class RenameFields implements ITool {
|
||||
if (!this.block) {
|
||||
this.block = new TerminalBlock();
|
||||
}
|
||||
this.block.setPreserveHistory(true);
|
||||
this.block.setPreserveHistory(false);
|
||||
|
||||
[pdfPath, pattern, change] = await forceArgs(
|
||||
[pdfPath, pattern, change],
|
||||
@ -150,29 +426,92 @@ class RenameFields implements ITool {
|
||||
|
||||
const pdf = await loadPdf(pdfPath);
|
||||
const form = pdf.getForm();
|
||||
const fields = form.getFields();
|
||||
const fields = form.getFields().sort((a, b) => {
|
||||
const aWidgets = a.acroField.getWidgets();
|
||||
const bWidgets = b.acroField.getWidgets();
|
||||
const aWidget = aWidgets[0];
|
||||
const bWidget = bWidgets[0];
|
||||
|
||||
const aPage = a.doc.findPageForAnnotationRef(a.acroField.ref);
|
||||
const bPage = b.doc.findPageForAnnotationRef(b.acroField.ref);
|
||||
|
||||
if (aPage && bPage && aPage !== bPage) {
|
||||
const pages = a.doc.getPages();
|
||||
const aPageIndex = pages.indexOf(aPage);
|
||||
const bPageIndex = pages.indexOf(bPage);
|
||||
|
||||
if (aPageIndex !== bPageIndex) return aPageIndex - bPageIndex;
|
||||
}
|
||||
|
||||
const aRect = aWidget.Rect()?.asRectangle();
|
||||
const bRect = bWidget.Rect()?.asRectangle();
|
||||
|
||||
if (aRect && bRect) {
|
||||
const dy = bRect.y - aRect.y;
|
||||
if (Math.abs(dy) > 5) return dy;
|
||||
|
||||
return aRect.x - bRect.x;
|
||||
}
|
||||
|
||||
return a.getName().localeCompare(b.getName());
|
||||
});
|
||||
let badFields = 0;
|
||||
|
||||
for (const field of fields) {
|
||||
if (field.acroField.getWidgets().length > 1) {
|
||||
badFields++;
|
||||
}
|
||||
}
|
||||
|
||||
badFields && await cliLog(
|
||||
colorize(
|
||||
`Warning, ${badFields} fields with shared widgets found`,
|
||||
"yellow",
|
||||
),
|
||||
this.block,
|
||||
);
|
||||
|
||||
const foundUpdates: [string, callback][] = [];
|
||||
let changesMade = false;
|
||||
|
||||
let i = 0;
|
||||
for (const field of fields) {
|
||||
const name = field.getName();
|
||||
const match = patternRegex.exec(name);
|
||||
if (match) {
|
||||
const toChange = evaluateChange(change, match);
|
||||
const preview = name.replace(new RegExp(patternRegex), toChange);
|
||||
foundUpdates.push([
|
||||
foundUpdates.push(
|
||||
...field.acroField.getWidgets()?.map<[string, callback]>((
|
||||
widget,
|
||||
) => {
|
||||
const toChange = evaluateChange(change, match, i);
|
||||
const preview = name.replace(
|
||||
new RegExp(patternRegex),
|
||||
toChange,
|
||||
);
|
||||
i++;
|
||||
return [
|
||||
`${colorize(name, "red")} -> ${colorize(preview, "green")}`,
|
||||
() => {
|
||||
applyRename(field, name, patternRegex, toChange);
|
||||
field.acroField.getWidgets().length > 1
|
||||
? applyWidgetRename(
|
||||
pdf,
|
||||
field,
|
||||
widget,
|
||||
name,
|
||||
new RegExp(patternRegex),
|
||||
toChange,
|
||||
)
|
||||
: applyRename(field, name, patternRegex, toChange);
|
||||
changesMade = true;
|
||||
},
|
||||
]);
|
||||
];
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (foundUpdates.length) {
|
||||
cliLog("Found updates:", this.block);
|
||||
await cliLog("Found updates:", this.block);
|
||||
await multiSelectMenuInteractive(
|
||||
"Please select an option to apply",
|
||||
foundUpdates,
|
||||
@ -185,7 +524,11 @@ class RenameFields implements ITool {
|
||||
"Save to path (or hit enter to keep current):",
|
||||
this.block,
|
||||
);
|
||||
try {
|
||||
await savePdf(pdf, path || pdfPath);
|
||||
} catch {
|
||||
// log(e);
|
||||
}
|
||||
} else {
|
||||
cliLog("No changes made, skipping", this.block);
|
||||
}
|
||||
@ -194,14 +537,14 @@ class RenameFields implements ITool {
|
||||
}
|
||||
export default new RenameFields();
|
||||
|
||||
if (import.meta.main) {
|
||||
// await call(renameFields)
|
||||
// while (!path || !path.endsWith('.pdf')) path = prompt("Please provide path to PDF:") || '';
|
||||
// while (!pattern) pattern = prompt("Please provide search string:") || '';
|
||||
// while (!change) change = prompt("Please provide requested change:") || '';
|
||||
await callWithArgPrompt(renameFields, [
|
||||
["Please provide path to PDF:", (p) => !!p && p.endsWith(".pdf")],
|
||||
"Please provide search string:",
|
||||
"Please provide requested change:",
|
||||
]);
|
||||
}
|
||||
// if (import.meta.main) {
|
||||
// // await call(renameFields)
|
||||
// // while (!path || !path.endsWith('.pdf')) path = prompt("Please provide path to PDF:") || '';
|
||||
// // while (!pattern) pattern = prompt("Please provide search string:") || '';
|
||||
// // while (!change) change = prompt("Please provide requested change:") || '';
|
||||
// await callWithArgPrompt(renameFields, [
|
||||
// ["Please provide path to PDF:", (p) => !!p && p.endsWith(".pdf")],
|
||||
// "Please provide search string:",
|
||||
// "Please provide requested change:",
|
||||
// ]);
|
||||
// }
|
||||
|
135
tools/fieldVisibility.ts
Normal file
135
tools/fieldVisibility.ts
Normal file
@ -0,0 +1,135 @@
|
||||
import { forceArgs } from "../cli/forceArgs.ts";
|
||||
import { selectMenuInteractive } from "../cli/selectMenu.ts";
|
||||
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||
import type { ITool } from "../types.ts";
|
||||
import { loadPdf, savePdf } from "util/saveLoadPdf.ts";
|
||||
|
||||
import { PDFName, type PDFNumber, type PDFWidgetAnnotation } from "pdf-lib";
|
||||
import { cliPrompt } from "../cli/prompts.ts";
|
||||
|
||||
type AcrobatVisibility =
|
||||
| "Visible"
|
||||
| "Hidden"
|
||||
| "VisibleButDoesNotPrint"
|
||||
| "HiddenButPrintable";
|
||||
|
||||
export function getAcrobatVisibility(
|
||||
widget: PDFWidgetAnnotation,
|
||||
): AcrobatVisibility {
|
||||
const raw = widget.dict.lookup(PDFName.of("F")) as PDFNumber;
|
||||
const flags = raw?.asNumber?.() ?? 0;
|
||||
|
||||
const isInvisible = (flags & (1 << 1)) !== 0;
|
||||
const isHidden = (flags & (1 << 2)) !== 0;
|
||||
const isNoPrint = (flags & (1 << 3)) !== 0;
|
||||
const isPrint = (flags & (1 << 2)) === 0 && !isNoPrint;
|
||||
|
||||
if (isInvisible && isHidden) {
|
||||
return isPrint ? "HiddenButPrintable" : "Hidden";
|
||||
} else if (isNoPrint) {
|
||||
return "VisibleButDoesNotPrint";
|
||||
} else {
|
||||
return "Visible";
|
||||
}
|
||||
}
|
||||
|
||||
export function setAcrobatVisibility(
|
||||
widget: PDFWidgetAnnotation,
|
||||
visibility: AcrobatVisibility,
|
||||
) {
|
||||
let flags = 0;
|
||||
|
||||
switch (visibility) {
|
||||
case "Visible":
|
||||
// No visibility bits set
|
||||
break;
|
||||
case "Hidden":
|
||||
flags |= 1 << 1; // Invisible
|
||||
flags |= 1 << 2; // Hidden
|
||||
break;
|
||||
case "VisibleButDoesNotPrint":
|
||||
flags |= 1 << 3; // NoPrint
|
||||
break;
|
||||
case "HiddenButPrintable":
|
||||
flags |= 1 << 1; // Invisible
|
||||
flags |= 1 << 2; // Hidden
|
||||
flags |= 1 << 3; // NoPrint — UNset this to allow print
|
||||
break;
|
||||
}
|
||||
|
||||
widget.dict.set(PDFName.of("F"), widget.dict.context.obj(flags));
|
||||
}
|
||||
|
||||
export class FieldVisibility implements ITool {
|
||||
name = "Field Visibility";
|
||||
description = "Change visibility of fields";
|
||||
block?: TerminalBlock;
|
||||
async run(pdfPath: string) {
|
||||
if (!this.block) this.block = new TerminalBlock();
|
||||
this.block.setPreserveHistory(false);
|
||||
[pdfPath] = await forceArgs([pdfPath], [[
|
||||
"Please provide path to PDF:",
|
||||
(e) => e?.endsWith(".pdf"),
|
||||
]], this.block);
|
||||
|
||||
const pdf = await loadPdf(pdfPath);
|
||||
const form = pdf.getForm();
|
||||
const fields = form.getFields();
|
||||
|
||||
let changesMade = false;
|
||||
|
||||
while (true) {
|
||||
const fieldAndVisibility = await selectMenuInteractive(
|
||||
`Select a field to change visibility (ESC to ${
|
||||
changesMade ? "continue" : "cancel"
|
||||
})`,
|
||||
fields.flatMap((f) => {
|
||||
const name = f.getName();
|
||||
const visibility = f.acroField.getWidgets().map((w, i, a) =>
|
||||
`${name}${a.length > 1 ? "#" + i : ""} :: ${
|
||||
getAcrobatVisibility(w)
|
||||
}`
|
||||
);
|
||||
return visibility;
|
||||
}),
|
||||
{ terminalBlock: this.block },
|
||||
);
|
||||
if (!fieldAndVisibility) break;
|
||||
const visibility = await selectMenuInteractive(
|
||||
fieldAndVisibility,
|
||||
[
|
||||
"Visible",
|
||||
"Hidden",
|
||||
"HiddenButPrintable",
|
||||
"VisibleButDoesNotPrint",
|
||||
] as AcrobatVisibility[],
|
||||
{ terminalBlock: this.block },
|
||||
) as AcrobatVisibility | null;
|
||||
if (!visibility) continue;
|
||||
|
||||
const [fName, widgetIndex] = fieldAndVisibility.split("::")[0].trim()
|
||||
.split("#");
|
||||
const field = fields.find((f) => f.getName() === fName);
|
||||
if (!field) break;
|
||||
|
||||
const widget = field.acroField.getWidgets()[Number(widgetIndex) || 0];
|
||||
setAcrobatVisibility(widget, visibility);
|
||||
changesMade = true;
|
||||
}
|
||||
|
||||
if (changesMade) {
|
||||
const path = await cliPrompt(
|
||||
"Save to path (or hit enter to keep current):",
|
||||
this.block,
|
||||
) || pdfPath;
|
||||
savePdf(pdf, path);
|
||||
}
|
||||
}
|
||||
help?: (() => Promise<void> | void) | undefined;
|
||||
done?: (() => Promise<void> | void) | undefined;
|
||||
setBlock(block: TerminalBlock) {
|
||||
this.block = block;
|
||||
}
|
||||
}
|
||||
|
||||
export default new FieldVisibility();
|
@ -1,8 +1,8 @@
|
||||
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";
|
||||
import { InputManager } from "../cli/InputManager.ts";
|
||||
|
||||
export class ListFormFields implements ITool {
|
||||
name = "listformfields";
|
||||
@ -12,42 +12,48 @@ export class ListFormFields implements ITool {
|
||||
if (!this.block) {
|
||||
this.block = new TerminalBlock();
|
||||
}
|
||||
this.block.setPreserveHistory(true);
|
||||
this.block.setPreserveHistory(false);
|
||||
[pdfPath] = await forceArgs([pdfPath], [[
|
||||
"Please provide path to PDF:",
|
||||
(p) => !!p && p.endsWith(".pdf"),
|
||||
]], this.block);
|
||||
const lines = [pdfPath];
|
||||
let rLines: string[] = [];
|
||||
|
||||
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();
|
||||
const fieldNames = fields.map((f) => f.getName());
|
||||
|
||||
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);
|
||||
let offset = 0;
|
||||
|
||||
const buildRLines = () => {
|
||||
rLines = fieldNames.slice(offset, this.block!.getRenderHeight());
|
||||
this.block!.setLines(lines.concat(rLines));
|
||||
};
|
||||
buildRLines();
|
||||
|
||||
await new Promise<void>((res) => {
|
||||
const im = InputManager.getInstance();
|
||||
const up = () => {
|
||||
if (fieldNames.length < this.block!.getRenderHeight() - 1) return;
|
||||
offset = Math.max(0, offset - 1);
|
||||
buildRLines();
|
||||
};
|
||||
const down = () => {
|
||||
if (fieldNames.length < this.block!.getRenderHeight() - 1) return;
|
||||
offset = Math.min(fieldNames.length, offset + 1);
|
||||
buildRLines();
|
||||
};
|
||||
const enter = () => {
|
||||
res();
|
||||
im.removeEventListener("arrow-up", up);
|
||||
im.removeEventListener("arrow-down", down);
|
||||
im.removeEventListener("enter", enter);
|
||||
};
|
||||
im.addEventListener("arrow-up", up);
|
||||
im.addEventListener("arrow-down", down);
|
||||
im.addEventListener("enter", enter);
|
||||
});
|
||||
}
|
||||
setBlock(terminalBlock: TerminalBlock) {
|
||||
this.block = terminalBlock;
|
||||
|
@ -1,4 +1,3 @@
|
||||
import { log } from "./logfile.ts";
|
||||
import { join } from "@std/path";
|
||||
|
||||
export async function getAsciiArt(art: string) {
|
||||
|
@ -7,9 +7,9 @@ const logFile = Deno.openSync("./log.txt", {
|
||||
|
||||
logFile.truncateSync(0);
|
||||
|
||||
export function log(message: any) {
|
||||
export function log(...message: any) {
|
||||
if (typeof message === "object") {
|
||||
message = JSON.stringify(message);
|
||||
message = Deno.inspect(message);
|
||||
}
|
||||
logFile.writeSync(new TextEncoder().encode(message + "\n"));
|
||||
}
|
||||
|
@ -15,10 +15,10 @@ export async function loadPdf(path: string) {
|
||||
export async function savePdf(doc: PDFDocument, path: string) {
|
||||
doc.getForm().getFields().forEach((field) => {
|
||||
if (field instanceof PDFTextField) {
|
||||
field.disableRichFormatting();
|
||||
field.disableRichFormatting?.();
|
||||
}
|
||||
});
|
||||
const pdfBytes = await doc.save();
|
||||
const pdfBytes = await doc.save({ updateFieldAppearances: true });
|
||||
if (Deno.env.get("DRYRUN") || path.includes("dryrun")) return;
|
||||
await Deno.writeFile(path, pdfBytes);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user