71 lines
4.6 KiB
TypeScript
71 lines
4.6 KiB
TypeScript
export type ShellProfile = { id: string; title: string; kind: "powershell" | "bash" | "cmd" | "node" };
|
|
export type TerminalSettings = { defaultProfile: string; fontSize: number; scrollback: number; cursorBlink: boolean };
|
|
export type TerminalControl = { focus(): void; clear(): void; scrollCommand(direction: -1 | 1): void; send(text: string, execute?: boolean): void };
|
|
export type TerminalTask = { label: string; command: string; profile?: string };
|
|
export const defaultSettings: TerminalSettings = { defaultProfile: "", fontSize: 13, scrollback: 10000, cursorBlink: true };
|
|
|
|
export function readSettings(): TerminalSettings {
|
|
try {
|
|
const value = JSON.parse(localStorage.getItem("runestone.terminal") || "{}");
|
|
return { defaultProfile: typeof value.defaultProfile === "string" ? value.defaultProfile : "",
|
|
fontSize: Number.isFinite(value.fontSize) ? Math.min(28, Math.max(9, value.fontSize)) : 13,
|
|
scrollback: Number.isFinite(value.scrollback) ? Math.min(100000, Math.max(100, value.scrollback)) : 10000,
|
|
cursorBlink: typeof value.cursorBlink === "boolean" ? value.cursorBlink : true };
|
|
} catch { return { ...defaultSettings }; }
|
|
}
|
|
|
|
export function quoteShell(value: string, kind: ShellProfile["kind"]): string {
|
|
if (/[\r\n\0]/.test(value)) throw new Error("Paths cannot contain line breaks or null characters.");
|
|
if (kind === "cmd") {
|
|
// Expansion happens even inside cmd quotes. Reject rather than execute a different path.
|
|
if (/["%!]/.test(value)) throw new Error("Use PowerShell for paths containing quotes, % or !.");
|
|
return `"${value}"`;
|
|
}
|
|
if (kind === "bash") return `'${value.replaceAll("'", "'\\''")}'`;
|
|
return `'${value.replaceAll("'", "''")}'`;
|
|
}
|
|
|
|
export function directoryCommand(path: string, kind: ShellProfile["kind"]): string {
|
|
if (kind === "node") return `process.chdir(${JSON.stringify(path)})`;
|
|
const normalized = kind === "bash" ? path.replaceAll("\\", "/") : path;
|
|
return `${kind === "powershell" ? "Set-Location -LiteralPath" : kind === "cmd" ? "cd /d" : "cd --"} ${quoteShell(normalized, kind)}`;
|
|
}
|
|
|
|
export function fileCommand(path: string, kind: ShellProfile["kind"]): string {
|
|
if (kind === "node") throw new Error("Choose a shell terminal to run files. The debug terminal is a Node.js REPL.");
|
|
const ext = path.split(".").pop()?.toLowerCase();
|
|
const quoted = quoteShell(kind === "bash" ? path.replaceAll("\\", "/") : path, kind);
|
|
const runner = ext === "py" ? "python" : ["js", "mjs", "cjs"].includes(ext || "") ? "node"
|
|
: ext === "ps1" ? "powershell -NoLogo -File" : ext === "sh" ? "bash" : ext === "rb" ? "ruby"
|
|
: ext === "bat" || ext === "cmd" ? "cmd /c" : ext === "exe" ? "" : null;
|
|
if (runner === null) throw new Error("No runner for this file type. Use Run Selected Text or configure a task.");
|
|
return `${runner || (kind === "powershell" ? "&" : "")}${runner || kind === "powershell" ? " " : ""}${quoted}`;
|
|
}
|
|
|
|
export function parseTasks(text: string): TerminalTask[] {
|
|
const value = JSON.parse(text);
|
|
if (!Array.isArray(value.tasks)) throw new Error('Tasks must contain a "tasks" array.');
|
|
return value.tasks.map((task: unknown) => {
|
|
if (!task || typeof task !== "object" || !("label" in task) || typeof task.label !== "string" || !task.label.trim()
|
|
|| !("command" in task) || typeof task.command !== "string" || !task.command.trim()
|
|
|| ("profile" in task && typeof task.profile !== "string")) throw new Error("Each task needs a label, command, and optional profile ID.");
|
|
return task as TerminalTask;
|
|
});
|
|
}
|
|
|
|
export function remember(items: string[], value: string): string[] {
|
|
return value.trim() ? [value, ...items.filter(item => item !== value)].slice(0, 100) : items;
|
|
}
|
|
|
|
export function terminalShortcut(event: Pick<KeyboardEvent, "ctrlKey" | "metaKey" | "altKey" | "shiftKey" | "code" | "key">): "toggle" | "new" | "window" | "split" | "previous" | "next" | "commands" | "directories" | null {
|
|
if (!event.ctrlKey && !event.metaKey) return null;
|
|
if (event.code === "Backquote" || event.key === "`") return event.shiftKey ? event.altKey ? "window" : "new" : "toggle";
|
|
if (event.shiftKey && event.code === "Digit5" && !event.altKey) return "split";
|
|
if (!event.shiftKey && !event.altKey && event.key.toLowerCase() === "j") return "toggle";
|
|
if (!event.shiftKey && !event.altKey && event.key === "ArrowUp") return "previous";
|
|
if (!event.shiftKey && !event.altKey && event.key === "ArrowDown") return "next";
|
|
if (!event.shiftKey && event.altKey && event.key.toLowerCase() === "r") return "commands";
|
|
if (!event.shiftKey && !event.altKey && event.key.toLowerCase() === "g") return "directories";
|
|
return null;
|
|
}
|