Compare commits
No commits in common. "041129dc8316e6c0141457699bb4681b2a98d3e1" and "001b90744b28ba85c3b611657a82cd0556e35546" have entirely different histories.
041129dc83
...
001b90744b
@ -44,9 +44,7 @@ export class TerminalLayout {
|
||||
clearTimeout(this.debounceTimer);
|
||||
}
|
||||
this.debounceTimer = setTimeout(
|
||||
() => {
|
||||
this.renderLayout();
|
||||
},
|
||||
() => this.renderLayout(),
|
||||
this.debounceDelay,
|
||||
);
|
||||
}
|
||||
@ -78,10 +76,6 @@ export class TerminalLayout {
|
||||
block.renderInternal(usedLines + 1);
|
||||
usedLines += lines.length;
|
||||
}
|
||||
for (const name of this.layoutOrder) {
|
||||
const block = this.blocks[name];
|
||||
block.runPostRenderAction?.();
|
||||
}
|
||||
}
|
||||
|
||||
clearAll() {
|
||||
@ -223,9 +217,7 @@ export class TerminalBlock {
|
||||
const baseRow = startRow ?? this.lastRenderRow;
|
||||
const excessLines = this.renderHeight - this.renderLines.length;
|
||||
for (let i = 0; i < excessLines; i++) {
|
||||
const moveToLine = `\x1b[${
|
||||
baseRow + this.renderLines.length + i
|
||||
};1H\x1b[2K`;
|
||||
const moveToLine = `[${baseRow + this.renderLines.length + i};1H[2K`;
|
||||
Deno.stdout.writeSync(new TextEncoder().encode(moveToLine));
|
||||
}
|
||||
|
||||
@ -262,17 +254,6 @@ export class TerminalBlock {
|
||||
return this.fixedHeight ?? 0;
|
||||
}
|
||||
|
||||
private _postRenderAction?: () => void;
|
||||
setPostRenderAction(action: (this: TerminalBlock) => void) {
|
||||
this._postRenderAction = action;
|
||||
}
|
||||
runPostRenderAction() {
|
||||
if (this._postRenderAction) {
|
||||
this._postRenderAction.call(this);
|
||||
this._postRenderAction = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
get lineCount() {
|
||||
return this.renderLines.length;
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
// 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";
|
||||
@ -10,7 +9,6 @@ export async function cliPrompt(
|
||||
): Promise<string> {
|
||||
const encoder = new TextEncoder();
|
||||
const input: string[] = [];
|
||||
let cursorPos = 0;
|
||||
|
||||
await Deno.stdin.setRaw(true);
|
||||
|
||||
@ -24,61 +22,33 @@ export async function cliPrompt(
|
||||
Deno.stdout.writeSync(encoder.encode(message + " "));
|
||||
}
|
||||
|
||||
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);
|
||||
} else {
|
||||
Deno.stdout.writeSync(encoder.encode("\x1b[K" + line + moveTo));
|
||||
}
|
||||
};
|
||||
|
||||
render();
|
||||
|
||||
const buf = new Uint8Array(6); // 6 bytes is enough for all the keys
|
||||
const buf = new Uint8Array(1);
|
||||
while (true) {
|
||||
const n = await Deno.stdin.read(buf);
|
||||
if (n === null) break;
|
||||
const [a, b, c] = buf;
|
||||
const byte = buf[0];
|
||||
|
||||
if (a === 3) { // Ctrl+C
|
||||
block?.clear();
|
||||
block?.["layout"]?.clearAll();
|
||||
if (byte === 3) { // Ctrl+C
|
||||
Deno.stdin.setRaw(false);
|
||||
block?.["layout"]?.clearAll();
|
||||
block?.clear();
|
||||
Deno.exit(130);
|
||||
}
|
||||
|
||||
if (a === 13) { // Enter
|
||||
if (byte === 13) { // Enter
|
||||
break;
|
||||
} else if (a === 127 || a === 8) { // Backspace
|
||||
if (cursorPos > 0) {
|
||||
input.splice(cursorPos - 1, 1);
|
||||
cursorPos--;
|
||||
}
|
||||
} else if (a === 46) { // Delete
|
||||
if (cursorPos < input.length) {
|
||||
input.splice(cursorPos, 1);
|
||||
}
|
||||
} else if (a === 27 && b === 91) { // Arrow keys
|
||||
if (c === 51 && cursorPos < input.length) { // delete
|
||||
input.splice(cursorPos, 1);
|
||||
}
|
||||
if (c === 68 && cursorPos > 0) cursorPos--; // Left
|
||||
if (c === 67 && cursorPos < input.length) cursorPos++; // Right
|
||||
} else if (a >= 32 && a <= 126) { // Printable ASCII
|
||||
input.splice(cursorPos, 0, String.fromCharCode(a));
|
||||
cursorPos++;
|
||||
} else if (byte === 127 || byte === 8) { // Backspace
|
||||
input.pop();
|
||||
} else if (byte >= 32 && byte <= 126) { // Printable chars
|
||||
input.push(String.fromCharCode(byte));
|
||||
}
|
||||
|
||||
render();
|
||||
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);
|
||||
@ -122,18 +92,13 @@ if (import.meta.main) {
|
||||
const layout = new TerminalLayout();
|
||||
const title = new TerminalBlock();
|
||||
const block = new TerminalBlock();
|
||||
const footer = new TerminalBlock();
|
||||
block.setPreserveHistory(true);
|
||||
// ScrollManager.enable(block);
|
||||
title.setLines(["Hello, World!"]);
|
||||
title.setFixedHeight(1);
|
||||
|
||||
footer.setLines(["Press Ctrl+C to exit"]);
|
||||
footer.setFixedHeight(1);
|
||||
|
||||
layout.register("title", title);
|
||||
layout.register("block", block);
|
||||
layout.register("footer", footer);
|
||||
|
||||
Deno.addSignalListener("SIGINT", () => {
|
||||
layout.clearAll();
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@bearmetal/pdf-tools",
|
||||
"version": "1.0.4",
|
||||
"version": "1.0.3",
|
||||
"license": "GPL 3.0",
|
||||
"tasks": {
|
||||
"dev": "deno run -A --env-file=.env main.ts",
|
||||
|
@ -153,7 +153,6 @@ class RenameFields implements ITool {
|
||||
const fields = form.getFields();
|
||||
|
||||
const foundUpdates: [string, callback][] = [];
|
||||
let changesMade = false;
|
||||
|
||||
for (const field of fields) {
|
||||
const name = field.getName();
|
||||
@ -165,7 +164,6 @@ class RenameFields implements ITool {
|
||||
`${colorize(name, "red")} -> ${colorize(preview, "green")}`,
|
||||
() => {
|
||||
applyRename(field, name, patternRegex, toChange);
|
||||
changesMade = true;
|
||||
},
|
||||
]);
|
||||
}
|
||||
@ -180,15 +178,11 @@ class RenameFields implements ITool {
|
||||
);
|
||||
}
|
||||
|
||||
if (changesMade) {
|
||||
const path = await cliPrompt(
|
||||
"Save to path (or hit enter to keep current):",
|
||||
this.block,
|
||||
);
|
||||
await savePdf(pdf, path || pdfPath);
|
||||
} else {
|
||||
cliLog("No changes made, skipping", this.block);
|
||||
}
|
||||
const path = await cliPrompt(
|
||||
"Save to path (or hit enter to keep current):",
|
||||
this.block,
|
||||
);
|
||||
await savePdf(pdf, path || pdfPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user