Release RuneStone 0.1.4 with installable plugins and settings
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
<script lang="ts">
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { FitAddon } from "@xterm/addon-fit";
|
||||
import { Terminal } from "@xterm/xterm";
|
||||
import { onMount } from "svelte";
|
||||
import { ptyClose, ptyOpen, ptyResize, ptyWrite } from "./api";
|
||||
import { terminalShortcut, type ShellProfile, type TerminalControl, type TerminalSettings } from "./model";
|
||||
import "@xterm/xterm/css/xterm.css";
|
||||
|
||||
type Props = {
|
||||
id: string;
|
||||
cwd: string;
|
||||
active: boolean;
|
||||
focused: boolean;
|
||||
profile: ShellProfile;
|
||||
directory: string;
|
||||
settings: TerminalSettings;
|
||||
onFocus: () => void;
|
||||
onControl: (control: TerminalControl | null) => void;
|
||||
onCommand: (command: string) => void;
|
||||
onDirectory: (directory: string) => void;
|
||||
onState: (state: string) => void;
|
||||
};
|
||||
|
||||
let { id, cwd, active, focused, profile, directory, settings, onFocus, onControl, onCommand, onDirectory, onState }: Props = $props();
|
||||
let host: HTMLDivElement | undefined = $state();
|
||||
let focusTerminal: (() => void) | undefined = $state();
|
||||
let instance = $state.raw<Terminal>();
|
||||
let resizeTerminal: (() => void) | undefined;
|
||||
|
||||
$effect(() => { if (active && focused && !document.activeElement?.closest(".rail")) focusTerminal?.(); });
|
||||
$effect(() => {
|
||||
if (instance) {
|
||||
instance.options.fontSize = settings.fontSize;
|
||||
instance.options.scrollback = settings.scrollback;
|
||||
instance.options.cursorBlink = settings.cursorBlink;
|
||||
requestAnimationFrame(() => resizeTerminal?.());
|
||||
}
|
||||
});
|
||||
|
||||
onMount(() => {
|
||||
if (!host) {
|
||||
return;
|
||||
}
|
||||
const term = new Terminal({
|
||||
scrollback: settings.scrollback,
|
||||
fontFamily: "Cascadia Code, Cascadia Mono, Consolas, monospace",
|
||||
fontSize: settings.fontSize,
|
||||
cursorBlink: settings.cursorBlink,
|
||||
allowProposedApi: true,
|
||||
theme: {
|
||||
background: "#141414",
|
||||
foreground: "#e6e6e6",
|
||||
cursor: "#82aaff",
|
||||
selectionBackground: "#264f78",
|
||||
black: "#3b4252",
|
||||
red: "#bf616a",
|
||||
green: "#a3be8c",
|
||||
yellow: "#ebcb8b",
|
||||
blue: "#81a1c1",
|
||||
magenta: "#b48ead",
|
||||
cyan: "#88c0d0",
|
||||
white: "#e5e9f0",
|
||||
},
|
||||
});
|
||||
const fit = new FitAddon();
|
||||
term.loadAddon(fit);
|
||||
term.open(host);
|
||||
instance = term;
|
||||
term.attachCustomKeyEventHandler(event => !terminalShortcut(event));
|
||||
let disposed = false;
|
||||
let opened = false;
|
||||
let exited = false;
|
||||
const pendingInput: string[] = [];
|
||||
let writes = Promise.resolve();
|
||||
const unlisteners: (() => void)[] = [];
|
||||
const fail = (err: unknown) => {
|
||||
if (!disposed) term.writeln(`\r\n${String(err)}`);
|
||||
};
|
||||
const resize = () => {
|
||||
if (disposed || !host?.clientWidth || !host?.clientHeight) return;
|
||||
fit.fit();
|
||||
if (opened && !exited) void ptyResize(id, term.cols, term.rows).catch(fail);
|
||||
};
|
||||
resizeTerminal = resize;
|
||||
focusTerminal = () => { resize(); term.focus(); };
|
||||
const send = (data: string) => {
|
||||
if (exited || disposed) return;
|
||||
if (!opened) { pendingInput.push(data); return; }
|
||||
writes = writes.then(() => ptyWrite(id, data)).catch(fail);
|
||||
};
|
||||
const onData = term.onData(send);
|
||||
let markers: import("@xterm/xterm").IMarker[] = [];
|
||||
let prompt: { marker: import("@xterm/xterm").IMarker; column: number } | undefined;
|
||||
const promptParser = term.parser.registerOscHandler(133, data => {
|
||||
if (data === "A") {
|
||||
const marker = term.registerMarker(0);
|
||||
if (marker) markers = [...markers.filter(item => !item.isDisposed), marker].slice(-1000);
|
||||
}
|
||||
if (data === "B") {
|
||||
prompt?.marker.dispose();
|
||||
const marker = term.registerMarker(0);
|
||||
prompt = marker ? { marker, column: term.buffer.active.cursorX } : undefined;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
const integration = term.parser.registerOscHandler(633, data => {
|
||||
const separator = data.indexOf(";");
|
||||
const type = data.slice(0, separator), value = data.slice(separator + 1);
|
||||
try {
|
||||
if (type === "Cwd") onDirectory(value);
|
||||
if (type === "RSCwd" || type === "RSCommand") {
|
||||
const decoded = new TextDecoder().decode(Uint8Array.from(atob(value), char => char.charCodeAt(0)));
|
||||
if (type === "RSCwd") onDirectory(decoded); else onCommand(decoded);
|
||||
}
|
||||
} catch { /* Ignore malformed shell integration output. */ }
|
||||
return true;
|
||||
});
|
||||
const keys = term.onKey(({ domEvent }) => {
|
||||
if (profile.kind !== "cmd" || domEvent.key !== "Enter" || !prompt || prompt.marker.isDisposed) return;
|
||||
const buffer = term.buffer.active;
|
||||
let command = "";
|
||||
for (let line = prompt.marker.line; line <= buffer.baseY + buffer.cursorY; line++) {
|
||||
command += buffer.getLine(line)?.translateToString(true, line === prompt.marker.line ? prompt.column : 0) || "";
|
||||
}
|
||||
if (command.trim()) onCommand(command);
|
||||
prompt.marker.dispose(); prompt = undefined;
|
||||
});
|
||||
onControl({
|
||||
focus: () => { resize(); term.focus(); },
|
||||
clear: () => { markers.forEach(marker => marker.dispose()); markers = []; term.clear(); term.focus(); },
|
||||
scrollCommand: direction => {
|
||||
const lines = markers.filter(marker => !marker.isDisposed).map(marker => marker.line);
|
||||
const current = term.buffer.active.viewportY;
|
||||
const target = direction < 0 ? lines.reverse().find(line => line < current) : lines.find(line => line > current);
|
||||
if (target !== undefined) term.scrollToLine(target); else if (direction > 0) term.scrollToBottom();
|
||||
},
|
||||
send: (text, execute = true) => {
|
||||
if (exited) return;
|
||||
term.paste(text.replace(/\r?\n/g, "\r"));
|
||||
if (execute) send("\r");
|
||||
term.focus();
|
||||
},
|
||||
});
|
||||
const start = async () => {
|
||||
try {
|
||||
// Subscribe before spawning: shells and agents can print immediately.
|
||||
const offData = await listen<{ id: string; data: number[] }>("pty-data", (event) => {
|
||||
if (!disposed && event.payload.id === id) term.write(new Uint8Array(event.payload.data));
|
||||
});
|
||||
unlisteners.push(offData);
|
||||
if (disposed) return;
|
||||
const offExit = await listen<{ id: string; code: number | null }>("pty-exit", (event) => {
|
||||
if (!disposed && event.payload.id === id) {
|
||||
exited = true;
|
||||
onState(`Exited${event.payload.code === null ? "" : ` (${event.payload.code})`}`);
|
||||
term.writeln(`\r\n[Process exited${event.payload.code === null ? "" : ` with code ${event.payload.code}`} ]`);
|
||||
}
|
||||
});
|
||||
unlisteners.push(offExit);
|
||||
if (disposed) return;
|
||||
resize();
|
||||
await ptyOpen(id, term.cols, term.rows, cwd, profile.id, directory);
|
||||
opened = true;
|
||||
if (disposed) { await ptyClose(id); return; }
|
||||
// Includes xterm's replies to startup cursor-position queries.
|
||||
for (const data of pendingInput.splice(0)) {
|
||||
writes = writes.then(() => ptyWrite(id, data)).catch(fail);
|
||||
}
|
||||
if (!exited) onState("Running");
|
||||
resize();
|
||||
if (active && focused) term.focus();
|
||||
} catch (err) {
|
||||
fail(err);
|
||||
if (!disposed) onState("Failed");
|
||||
} finally {
|
||||
if (disposed) unlisteners.splice(0).forEach((off) => off());
|
||||
}
|
||||
};
|
||||
void start();
|
||||
const observer = new ResizeObserver(resize);
|
||||
observer.observe(host);
|
||||
return () => {
|
||||
disposed = true;
|
||||
focusTerminal = undefined;
|
||||
instance = undefined;
|
||||
resizeTerminal = undefined;
|
||||
onControl(null);
|
||||
observer.disconnect();
|
||||
onData.dispose();
|
||||
promptParser.dispose(); integration.dispose(); keys.dispose();
|
||||
unlisteners.splice(0).forEach((off) => off());
|
||||
if (opened) void ptyClose(id).catch(() => {});
|
||||
term.dispose();
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="term" data-cwd={directory} bind:this={host} onfocusin={onFocus}></div>
|
||||
|
||||
<style>
|
||||
.term {
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
padding: 4px;
|
||||
background: #141414;
|
||||
}
|
||||
|
||||
.term :global(.xterm),
|
||||
.term :global(.xterm-viewport) {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.term :global(.xterm-viewport) {
|
||||
/* Whole terminal rows can leave a gap above the bottom edge. */
|
||||
background-color: transparent;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user