The terminal learns where one command ends, and can be dragged wider
"The last command and its output" was not something the panel could honestly offer. sendToChat took the last forty rows of the screen buffer, hard-wrapped at the terminal's width with no way to tell a wrap from a newline -- its own comment said so. So bash and zsh are given the OSC 133 markers VS Code and WezTerm use, and Copy, Send and an Auto toggle are built on those. The integration is written by the PTY command string itself, with printf. sshd runs that string through $SHELL -c, so it can case on the shell's own name and needs no probe, no second channel and no writable home. Passing it through the environment does not work -- every distribution ships AcceptEnv LANG LC_*, so anything else is dropped silently -- and feeding `source ...` in as keystrokes races a slow .zshrc, echoes into the scrollback and lands in shell history. Nothing needs hiding, which is the point of choosing it: the setup runs before the shell exists and never writes to the PTY's input side, so there is nothing to echo and no fan-out gate to build. Two things were wrong in the first version and both were found by running it against real shells rather than the fake one. bash: the DEBUG trap fires before every simple command *including each one inside PROMPT_COMMAND*, so $? read from there is whatever ran a moment ago -- every command reported success. The status is captured in the trap now, which also removes the two-entry PROMPT_COMMAND dance entirely. zsh: $ZDOTDIR is already ours by the time .zshenv runs, so the shims were sourcing themselves and none of the user's configuration loaded; the original is passed on the exec line. Parsing is server-side. The `behind` path resets the terminal and replays a truncated scrollback, so a client parser routinely sees a finish with no start; two tabs share one shell and can disagree; and what comes out of this ends up inside a prompt, so deriving it here leaves nothing to disbelieve. The bytes are fanned out unchanged -- xterm consumes an OSC it has no handler for. Output is bounded head and tail, 48KB and 16KB: a build that fails ten megabytes in has the invocation at the top and the error at the bottom. Carriage returns collapse to the last state of each line, which is the difference between a usable prompt and two megabytes of spinner. The fence is sized to its content, because output containing three backticks would otherwise break out and read as prose. Any shell that is not bash or zsh starts exactly as it did before. The buttons then scrape the screen and say so, and Auto is disabled rather than degraded: forty arbitrary lines on every message is worse than nothing. Also a generic [data-resize] handle, keyboard included, persisted the way the theme is. The inspector and sidebar can have it whenever they want it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -544,14 +544,57 @@ button, input, textarea, select {
|
||||
`hidden` attribute. */
|
||||
.terminal {
|
||||
width: var(--terminal-width);
|
||||
min-width: var(--terminal-width-min);
|
||||
max-width: 80vw;
|
||||
flex: none;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: 0;
|
||||
/* So the resize handle can sit on the edge. */
|
||||
position: relative;
|
||||
background: var(--bg-sunken);
|
||||
border-left: 1px solid var(--border);
|
||||
}
|
||||
|
||||
/* The drag handle on a panel's left edge. Wider than it looks -- a one-pixel
|
||||
border is a target nobody can hit -- and it sits *outside* the panel's own
|
||||
padding so it never overlaps what is being resized. */
|
||||
.panel-resize {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
left: -3px;
|
||||
width: 9px;
|
||||
z-index: var(--z-handle);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
cursor: col-resize;
|
||||
color: transparent;
|
||||
touch-action: none;
|
||||
transition: background var(--transition-fast), color var(--transition-fast);
|
||||
}
|
||||
.panel-resize:hover,
|
||||
.panel-resize:focus-visible,
|
||||
body.is-resizing .panel-resize {
|
||||
background: var(--accent-soft);
|
||||
color: var(--accent);
|
||||
outline: none;
|
||||
}
|
||||
/* While dragging, nothing else may take the pointer -- a text selection
|
||||
starting mid-drag makes the whole page flicker blue, and the iframe-shaped
|
||||
hazard is the terminal itself swallowing pointermove. */
|
||||
body.is-resizing {
|
||||
cursor: col-resize;
|
||||
user-select: none;
|
||||
}
|
||||
body.is-resizing .terminal__screen { pointer-events: none; }
|
||||
|
||||
@media (max-width: 64rem) {
|
||||
/* A full-height overlay has no edge to drag, and no room to spare. */
|
||||
.panel-resize { display: none; }
|
||||
}
|
||||
|
||||
.terminal__where {
|
||||
font-weight: 400;
|
||||
font-family: var(--font-mono);
|
||||
@@ -586,6 +629,19 @@ button, input, textarea, select {
|
||||
}
|
||||
.terminal__status strong { color: var(--ink-muted); font-weight: 600; }
|
||||
.terminal__message { flex: 1; min-width: 0; overflow-wrap: anywhere; }
|
||||
/* What the last command was. Monospace and clipped: it is a command line, and
|
||||
one long enough to wrap would push the status bar into two rows. */
|
||||
.terminal__last {
|
||||
flex: 0 1 auto;
|
||||
min-width: 0;
|
||||
max-width: 16rem;
|
||||
font-family: var(--font-mono);
|
||||
color: var(--ink-muted);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
.terminal__last:empty { display: none; }
|
||||
.terminal__message--error { color: var(--danger); }
|
||||
|
||||
@media (max-width: 64rem) {
|
||||
|
||||
@@ -493,6 +493,113 @@
|
||||
);
|
||||
}
|
||||
|
||||
/* --- Dragging a panel wider ---------------------------------------------
|
||||
Generic rather than terminal-specific: the inspector and the sidebar want
|
||||
the same handle, and a second copy of this is how two panels end up
|
||||
resizing differently.
|
||||
|
||||
The width lands on a CSS variable on <html> rather than on the panel, so
|
||||
the ≤64rem overlay rule -- which clamps it with min() -- keeps working
|
||||
without knowing anything about dragging. Persisted the way the theme is:
|
||||
localStorage for this tab, best-effort POST for the next device. */
|
||||
var RESIZE_KEY = "lembas-panel-widths";
|
||||
|
||||
function storedWidths() {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(RESIZE_KEY) || "{}") || {};
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function applyWidths() {
|
||||
var widths = storedWidths();
|
||||
Object.keys(widths).forEach(function (name) {
|
||||
document.documentElement.style.setProperty(name, widths[name] + "px");
|
||||
});
|
||||
}
|
||||
|
||||
function rememberWidth(name, pixels) {
|
||||
var widths = storedWidths();
|
||||
widths[name] = Math.round(pixels);
|
||||
try {
|
||||
localStorage.setItem(RESIZE_KEY, JSON.stringify(widths));
|
||||
} catch (e) { /* private mode */ }
|
||||
if (document.body.dataset.authenticated === "true") {
|
||||
fetch("/api/preferences/layout", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(widths)
|
||||
}).catch(function () { /* already applied locally */ });
|
||||
}
|
||||
}
|
||||
|
||||
function setupResize() {
|
||||
document.addEventListener("pointerdown", function (event) {
|
||||
var handle = event.target.closest("[data-resize]");
|
||||
if (!handle || event.button !== 0) return;
|
||||
var panel = handle.closest("[data-resize-target]") || handle.parentElement;
|
||||
if (!panel) return;
|
||||
|
||||
var name = handle.dataset.resize;
|
||||
var min = parseFloat(handle.dataset.resizeMin || "320");
|
||||
var startX = event.clientX;
|
||||
var startWidth = panel.getBoundingClientRect().width;
|
||||
var frame = null;
|
||||
var pending = startWidth;
|
||||
|
||||
event.preventDefault();
|
||||
handle.setPointerCapture(event.pointerId);
|
||||
document.body.classList.add("is-resizing");
|
||||
|
||||
function move(moveEvent) {
|
||||
/* The handle is on the panel's *left* edge and the panel is on the
|
||||
right of the shell, so dragging left makes it wider. */
|
||||
var max = Math.max(min, window.innerWidth - 360);
|
||||
pending = Math.min(Math.max(startWidth - (moveEvent.clientX - startX), min), max);
|
||||
/* Coalesced to a frame: the ResizeObserver on the panel calls xterm's
|
||||
fit() and sends a resize frame up the socket, and doing that once
|
||||
per pointermove is a frame per pixel of drag. */
|
||||
if (frame) return;
|
||||
frame = requestAnimationFrame(function () {
|
||||
frame = null;
|
||||
document.documentElement.style.setProperty(name, pending + "px");
|
||||
});
|
||||
}
|
||||
|
||||
function stop() {
|
||||
handle.removeEventListener("pointermove", move);
|
||||
handle.removeEventListener("pointerup", stop);
|
||||
handle.removeEventListener("pointercancel", stop);
|
||||
document.body.classList.remove("is-resizing");
|
||||
if (frame) cancelAnimationFrame(frame);
|
||||
document.documentElement.style.setProperty(name, pending + "px");
|
||||
rememberWidth(name, pending);
|
||||
}
|
||||
|
||||
handle.addEventListener("pointermove", move);
|
||||
handle.addEventListener("pointerup", stop);
|
||||
handle.addEventListener("pointercancel", stop);
|
||||
});
|
||||
|
||||
/* A keyboard has to be able to do this too, or the panel is only resizable
|
||||
with a mouse and the handle is a focus trap that does nothing. */
|
||||
document.addEventListener("keydown", function (event) {
|
||||
var handle = event.target.closest("[data-resize]");
|
||||
if (!handle) return;
|
||||
var step = event.key === "ArrowLeft" ? 32 : event.key === "ArrowRight" ? -32 : 0;
|
||||
if (!step) return;
|
||||
event.preventDefault();
|
||||
var panel = handle.closest("[data-resize-target]") || handle.parentElement;
|
||||
var name = handle.dataset.resize;
|
||||
var min = parseFloat(handle.dataset.resizeMin || "320");
|
||||
var max = Math.max(min, window.innerWidth - 360);
|
||||
var width = Math.min(Math.max(panel.getBoundingClientRect().width + step, min), max);
|
||||
document.documentElement.style.setProperty(name, width + "px");
|
||||
rememberWidth(name, width);
|
||||
});
|
||||
}
|
||||
|
||||
window.lembas = {
|
||||
setPanel: setPanel,
|
||||
applyTheme: applyTheme,
|
||||
@@ -590,8 +697,13 @@
|
||||
scrollThread(true);
|
||||
applyTheme(currentTheme());
|
||||
setupDropzone();
|
||||
setupResize();
|
||||
});
|
||||
|
||||
/* Before first paint rather than on DOMContentLoaded, so a panel that was
|
||||
dragged wider does not open at its default and jump. */
|
||||
applyWidths();
|
||||
|
||||
/* After any htmx swap: re-measure the composer and follow new content. */
|
||||
document.body.addEventListener("htmx:afterSwap", function () {
|
||||
document.querySelectorAll("[data-autosize]").forEach(autosize);
|
||||
|
||||
@@ -32,6 +32,11 @@
|
||||
var messageEl = null;
|
||||
var observer = null;
|
||||
var closedOnPurpose = false;
|
||||
/* Whether this shell tells us where commands begin and end -- "live",
|
||||
"loading" or "none". Everything the three buttons do keys off it. */
|
||||
var integration = "loading";
|
||||
var autoSend = false;
|
||||
var lastCommand = null;
|
||||
|
||||
function say(text, isError) {
|
||||
if (!messageEl) return;
|
||||
@@ -148,6 +153,9 @@
|
||||
var where = panel.querySelector("[data-terminal-where]");
|
||||
if (where) where.textContent = payload.dir;
|
||||
}
|
||||
integration = payload.integration || "none";
|
||||
showLast(payload.last);
|
||||
applyIntegration();
|
||||
/* The server may have opened the shell at a size chosen by whoever got
|
||||
here first, so ask for ours now that there is something to ask. */
|
||||
refit();
|
||||
@@ -155,6 +163,18 @@
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.t === "command") {
|
||||
/* A command finished. Tens of bytes, not the output: a 64KB text frame
|
||||
would compete with PTY bytes on the one path that has to stay quick,
|
||||
and the buttons fetch what they need when they are pressed. */
|
||||
if (integration !== "live") { integration = "live"; applyIntegration(); }
|
||||
showLast(payload.command);
|
||||
if (autoSend) {
|
||||
capture(true).then(function (text) { intoComposer(text, true); });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.t === "behind") {
|
||||
/* This window stopped reading and was disconnected so the others kept
|
||||
up. Reconnecting costs nothing: the scrollback is the state. */
|
||||
@@ -253,38 +273,133 @@
|
||||
});
|
||||
}
|
||||
|
||||
/* --- Send to chat ------------------------------------------------------- */
|
||||
/* Into the composer, never sent. What a machine printed is exactly the sort
|
||||
of text somebody should read before a model does, and the box is where
|
||||
that happens. */
|
||||
function sendToChat() {
|
||||
if (!term) return;
|
||||
var text = term.getSelection();
|
||||
if (!text) {
|
||||
var lines = [];
|
||||
var buffer = term.buffer.active;
|
||||
var last = buffer.baseY + buffer.cursorY;
|
||||
for (var y = Math.max(0, last - 40); y <= last; y++) {
|
||||
var line = buffer.getLine(y);
|
||||
if (line) lines.push(line.translateToString(true));
|
||||
}
|
||||
text = lines.join("\n").replace(/\n+$/, "");
|
||||
/* --- Handing a command to the chat --------------------------------------
|
||||
Three buttons over one idea: the last command, its output, and where it
|
||||
ran. The server renders the block, so the text a model eventually reads
|
||||
exists in exactly one place -- and the screen buffer could not produce it
|
||||
anyway, holding as it does what is *on screen*, hard-wrapped at the
|
||||
terminal's width with no way to tell a wrap from a newline. */
|
||||
|
||||
function applyIntegration() {
|
||||
if (!panel) return;
|
||||
var auto = panel.querySelector("[data-terminal-auto]");
|
||||
if (!auto) return;
|
||||
var usable = integration === "live";
|
||||
auto.disabled = !usable;
|
||||
auto.title = usable
|
||||
? "Attach every command you run to your next message"
|
||||
: "This shell did not load LLeMbas's command markers, so there is no way " +
|
||||
"to tell where one command's output ends.";
|
||||
if (!usable && autoSend) setAuto(false);
|
||||
}
|
||||
|
||||
function showLast(command) {
|
||||
lastCommand = command || null;
|
||||
var slot = panel && panel.querySelector("[data-terminal-last]");
|
||||
if (!slot) return;
|
||||
// textContent, always: this is a command line off somebody's machine.
|
||||
slot.textContent = lastCommand ? lastCommand.summary : "";
|
||||
}
|
||||
|
||||
function setAuto(on) {
|
||||
autoSend = !!on;
|
||||
var button = panel.querySelector("[data-terminal-auto]");
|
||||
if (button) {
|
||||
button.setAttribute("aria-pressed", autoSend ? "true" : "false");
|
||||
button.classList.toggle("is-active", autoSend);
|
||||
}
|
||||
if (!text.trim()) {
|
||||
say("Nothing to send: select some output first.");
|
||||
return;
|
||||
say(autoSend
|
||||
? "Every command you run will be attached to your next message."
|
||||
: "Commands are no longer attached automatically.");
|
||||
}
|
||||
|
||||
/* A selection always wins, in every state. People rely on it, and it is the
|
||||
only way to send part of something. */
|
||||
function selected() {
|
||||
var text = term ? term.getSelection() : "";
|
||||
return text && text.trim() ? text : "";
|
||||
}
|
||||
|
||||
function scraped() {
|
||||
var lines = [];
|
||||
var buffer = term.buffer.active;
|
||||
var last = buffer.baseY + buffer.cursorY;
|
||||
for (var y = Math.max(0, last - 40); y <= last; y++) {
|
||||
var line = buffer.getLine(y);
|
||||
if (line) lines.push(line.translateToString(true));
|
||||
}
|
||||
return lines.join("\n").replace(/\n+$/, "");
|
||||
}
|
||||
|
||||
/* Fetches the rendered block, or falls back to the screen. `quiet` is the
|
||||
auto path, which must not narrate every command it collects. */
|
||||
function capture(quiet) {
|
||||
var chosen = selected();
|
||||
if (chosen && !quiet) return Promise.resolve("```\n" + chosen + "\n```\n");
|
||||
|
||||
if (integration !== "live") {
|
||||
if (quiet) return Promise.resolve("");
|
||||
var text = scraped();
|
||||
if (!text.trim()) {
|
||||
say("Nothing to send: select some output first.");
|
||||
return Promise.resolve("");
|
||||
}
|
||||
/* Said plainly rather than dressed up. Without markers this is the last
|
||||
forty rows as they appeared, wraps and all, and pretending otherwise
|
||||
would put a precise-looking block in front of a model that is not. */
|
||||
say("Copied the last of the screen, as it appeared. This shell does not " +
|
||||
"mark where commands begin.");
|
||||
return Promise.resolve("```\n" + text + "\n```\n");
|
||||
}
|
||||
|
||||
return fetch(panel.dataset.url.replace(/\/ws$/, "/last"), { credentials: "same-origin" })
|
||||
.then(function (response) { return response.json(); })
|
||||
.then(function (body) {
|
||||
if (!body.ok) {
|
||||
if (!quiet) say(body.message || "Nothing to send yet.");
|
||||
return "";
|
||||
}
|
||||
return body.text + "\n";
|
||||
})
|
||||
.catch(function () {
|
||||
if (!quiet) say("Could not read the last command.", true);
|
||||
return "";
|
||||
});
|
||||
}
|
||||
|
||||
function intoComposer(text, quiet) {
|
||||
if (!text) return;
|
||||
var input = document.querySelector("[data-composer-input]");
|
||||
if (!input) return;
|
||||
var fence = "```\n" + text + "\n```\n";
|
||||
input.value = input.value ? input.value.replace(/\s*$/, "\n\n") + fence : fence;
|
||||
input.value = input.value ? input.value.replace(/\s*$/, "\n\n") + text : text;
|
||||
if (window.lembas && window.lembas.autosize) window.lembas.autosize(input);
|
||||
input.focus();
|
||||
/* "As it appeared" and not "as it was written": the buffer holds what is on
|
||||
screen, hard-wrapped at the terminal's width, with no way to tell a wrap
|
||||
from a newline. */
|
||||
say("Copied into the message box as it appeared on screen.");
|
||||
/* Auto-send never steals focus: it fires while somebody is typing in the
|
||||
terminal, and yanking the caret out of a shell mid-command is the sort
|
||||
of thing that gets a feature switched off for good. */
|
||||
if (!quiet) input.focus();
|
||||
}
|
||||
|
||||
function sendToChat() {
|
||||
if (!term) return;
|
||||
capture(false).then(function (text) {
|
||||
if (!text) return;
|
||||
intoComposer(text, false);
|
||||
/* Into the composer, never sent. What a machine printed is exactly the
|
||||
sort of text somebody should read before a model does, and the box is
|
||||
where that happens. */
|
||||
if (integration === "live") say("Put into the message box. It is not sent yet.");
|
||||
});
|
||||
}
|
||||
|
||||
function copyToClipboard() {
|
||||
if (!term) return;
|
||||
capture(false).then(function (text) {
|
||||
if (!text) return;
|
||||
if (window.lembas && window.lembas.copyText) {
|
||||
window.lembas.copyText(text);
|
||||
say("Copied.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* --- Wiring ------------------------------------------------------------- */
|
||||
@@ -305,7 +420,15 @@
|
||||
panel.addEventListener("click", function (event) {
|
||||
if (event.target.closest("[data-terminal-send]")) {
|
||||
event.preventDefault();
|
||||
sendToChat();
|
||||
return sendToChat();
|
||||
}
|
||||
if (event.target.closest("[data-terminal-copy]")) {
|
||||
event.preventDefault();
|
||||
return copyToClipboard();
|
||||
}
|
||||
if (event.target.closest("[data-terminal-auto]")) {
|
||||
event.preventDefault();
|
||||
return setAuto(!autoSend);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user