improves block functionality

adds cli compatible prompts/logs
adds logfile function for debug
adds multiselect support
new fieldRename
adds listFieldNames
This commit is contained in:
2025-04-30 01:17:45 -06:00
parent 2634f40f2b
commit 9535222fb7
14 changed files with 623 additions and 70 deletions

View File

@@ -1,3 +1,6 @@
import { log } from "util/logfile.ts";
import { Cursor } from "./cursor.ts";
export class TerminalLayout {
private static ALT_BUFFER_ENABLE = "\x1b[?1049h";
private static ALT_BUFFER_DISABLE = "\x1b[?1049l";
@@ -12,10 +15,16 @@ export class TerminalLayout {
constructor() {
Deno.stdout.writeSync(
new TextEncoder().encode(
TerminalLayout.ALT_BUFFER_ENABLE + TerminalLayout.CURSOR_HIDE,
TerminalLayout.ALT_BUFFER_ENABLE,
),
);
Cursor.hide();
this.height = Deno.consoleSize().rows;
Deno.addSignalListener("SIGINT", () => {
this.clearAll();
Deno.exit(0);
});
}
register(name: string, block: TerminalBlock, fixedHeight?: number) {
@@ -71,16 +80,25 @@ export class TerminalLayout {
}
clearAll() {
log("clearAll");
Deno.stdout.writeSync(
new TextEncoder().encode(
TerminalLayout.ALT_BUFFER_DISABLE + TerminalLayout.CURSOR_SHOW,
TerminalLayout.ALT_BUFFER_DISABLE,
),
);
Cursor.show();
for (const name of this.layoutOrder) {
this.blocks[name].clear();
}
}
clear() {
log("clear " + this.height);
for (let i = 0; i < this.height; i++) {
Deno.stdout.writeSync(new TextEncoder().encode("\x1b[2K\x1b[1E"));
}
}
get availableHeight() {
return this.height;
}
@@ -96,14 +114,24 @@ export class TerminalBlock {
private renderHeight: number = 0;
private lastRenderRow = 1;
private preserveHistory = false;
constructor(private prepend: string = "") {}
setPreserveHistory(preserveHistory: boolean) {
this.preserveHistory = preserveHistory;
}
setLayout(layout: TerminalLayout) {
this.layout = layout;
}
setLines(lines: string[]) {
this.lines = lines;
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;
}
if (this.scrollOffset > lines.length - 1) {
this.scrollOffset = Math.max(0, lines.length - 1);
}
@@ -115,6 +143,19 @@ export class TerminalBlock {
);
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) {
@@ -144,15 +185,6 @@ export class TerminalBlock {
setRenderLines(lines: string[]) {
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) {
@@ -165,19 +197,30 @@ export class TerminalBlock {
renderInternal(startRow?: number) {
this.lastRenderRow = startRow ?? this.lastRenderRow;
this.clear();
let output = this.renderLines.map((line) => `${this.prepend}${line}\x1b[K`)
.join("\n");
this.clear(); // uses old renderedLineCount
const outputLines = this.renderLines.map((line) =>
`${this.prepend}${line}\x1b[K`
);
const output = outputLines.join("\n");
if (startRow !== undefined) {
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,
);
}
clear() {
log(this.renderedLineCount);
if (this.renderedLineCount === 0) return;
const moveCursor = `\x1b[${this.lastRenderRow};1H`;
Deno.stdout.writeSync(new TextEncoder().encode(moveCursor));
@@ -187,6 +230,11 @@ export class TerminalBlock {
this.renderedLineCount = 0;
}
clearAll() {
this.clear();
this.lines = [];
}
setFixedHeight(height: number) {
this.fixedHeight = height;
}

29
cli/cursor.ts Normal file
View 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
View 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;
}

View File

@@ -1,9 +1,10 @@
import { getAsciiArt } from "../util/asciiArt.ts";
import { toCase } from "util/caseManagement.ts";
import { ArgParser } from "./argParser.ts";
import { colorize } from "./colorize.ts";
import { colorize } from "./style.ts";
import { selectMenuInteractive } from "./selectMenu.ts";
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
import { cliAlert, cliLog } from "./prompts.ts";
export class PdfToolsCli {
private tools: Map<string, ITool> = new Map();
@@ -30,56 +31,98 @@ export class PdfToolsCli {
private async 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() {
console.log("BearMetal PDF CLI");
private async help() {
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() {
try {
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"));
}
const titleBlock = new TerminalBlock();
this.terminalLayout.register("title", titleBlock);
const bodyBlock = new TerminalBlock();
this.terminalLayout.register("body", bodyBlock);
titleBlock.setFixedHeight(lines.length);
titleBlock.setLines(lines);
this.embiggenHeader();
if (Deno.args.length === 0) {
// console.log(
// colorize("No tool specified. Importing all tools...", "gray"),
// );
await this.importTools();
}
this.toolMenu();
await this.toolMenu();
} finally {
this.terminalLayout.clearAll();
Deno.stdin.setRaw(false);
}
}
private async toolMenu() {
const tools = this.tools.keys().toArray();
const bodyBlock = this.terminalLayout.getBlock("body");
const selected = await selectMenuInteractive("Choose a tool", tools, {
terminalBlock: bodyBlock,
});
bodyBlock.clear();
bodyBlock.clearAll();
bodyBlock.setPreserveHistory(false);
const selected = await selectMenuInteractive(
"Choose a tool",
tools.concat(["Help", "Exit"]),
{
terminalBlock: bodyBlock,
},
);
if (!selected) return;
if (selected === "Exit") {
return;
}
await this.runTool(selected);
this.toolMenu();
await this.toolMenu();
}
private async runTool(toolName: string) {
if (toolName === "Help") {
return await this.help();
}
const tool = this.tools.get(toolName);
if (tool) {
this.ensmallenHeader(tool.name + " - " + tool.description);
const bodyBlock = this.terminalLayout.getBlock("body");
bodyBlock.clearAll();
tool.setBlock?.(bodyBlock);
await tool.run();
await tool.done?.();
this.embiggenHeader();
}
}
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);
}
}

124
cli/prompts.ts Normal file
View File

@@ -0,0 +1,124 @@
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);
let cursorVisible = true;
if (!block) {
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 (!block && !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, block?: TerminalBlock) {
if (!block) {
console.log(message);
} else {
block.setLines(message.split("\n"));
}
}
if (import.meta.main) {
const layout = new TerminalLayout();
const title = new TerminalBlock();
const block = new TerminalBlock();
block.setPreserveHistory(true);
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);
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);
}

View File

@@ -1,9 +1,10 @@
import { colorize } from "./colorize.ts";
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
import { colorize } from "./style.ts";
import { TerminalBlock } from "./TerminalLayout.ts";
interface ISelectMenuConfig {
multiSelect?: boolean;
terminalBlock?: TerminalBlock;
initialSelection?: number;
initialSelections?: number[];
}
export function selectMenu(items: string[]) {
@@ -21,18 +22,12 @@ export async function selectMenuInteractive(
Deno.stdin.setRaw(true);
let selected = 0;
if (config?.multiSelect) {
console.warn("Multi-select not implemented yet");
return null;
}
const terminalBlock = config?.terminalBlock || new TerminalBlock();
if (!config?.terminalBlock) {
terminalBlock.setRenderHeight(Deno.consoleSize().rows);
}
console.log(terminalBlock.getRenderHeight());
let range: [number, number] = [terminalBlock.lineCount, 1];
function renderMenu() {
const { rows } = Deno.consoleSize();
const terminalHeight = terminalBlock.getRenderHeight() || rows;
@@ -54,7 +49,7 @@ export async function selectMenuInteractive(
}
}
terminalBlock.setLines(lines);
range = terminalBlock.setLines(lines, range);
}
function numberAndPadding(i: number, prefix?: string) {
@@ -78,7 +73,7 @@ export async function selectMenuInteractive(
if (a === 3) {
Deno.stdin.setRaw(false);
console.log("\nInterrupted\n");
terminalBlock?.["layout"]?.clearAll();
Deno.exit(130);
}
@@ -105,13 +100,112 @@ export async function selectMenuInteractive(
}
}
terminalBlock.clear();
Deno.stdin.setRaw(false);
return options[selected];
}
terminalBlock.setLines(["Selected: " + options[selected]], range);
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) {
// const layout = new TerminalLayout();
// const block = new TerminalBlock();
@@ -154,7 +248,7 @@ if (import.meta.main) {
// layout.clearAll();
// console.log(val);
const val = await selectMenuInteractive("choose a fruit", [
const val = await multiSelectMenuInteractive("choose a fruit", [
"apple",
"banana",
"cherry",
@@ -183,4 +277,6 @@ if (import.meta.main) {
"zucchini",
]);
console.log(val);
// Deno.stdout.writeSync(new TextEncoder().encode("\x07"));
}