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:
Jaroslav Beneš
2026-08-02 17:28:38 +02:00
parent fc02eb5538
commit 6bbd398707
17 changed files with 1734 additions and 43 deletions
@@ -222,6 +222,22 @@
One per chat. Each holds an SSH connection open on the far machine.
</p>
</div>
<div class="field">
<label class="checkbox">
<input type="checkbox" name="terminal_integration"
{{ 'checked' if values.terminal_integration }}>
<span>Mark where commands begin and end</span>
</label>
<p class="field__hint">
Gives bash and zsh the same invisible markers VS Code and WezTerm use,
so <strong>Copy</strong>, <strong>Send</strong> and the automatic
toggle know which output belongs to which command. Written by the shell
into a temporary file it deletes itself, and any other shell is started
exactly as it was before. Off means those buttons fall back to copying
the last of the screen as it appeared, wraps and all.
</p>
</div>
</section>
<section class="card">
+13 -1
View File
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en" data-theme="{{ theme }}">
<html lang="en" data-theme="{{ theme }}"{% if layout %} style="{{ layout }}"{% endif %}>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
@@ -41,6 +41,18 @@
document.documentElement.dataset.theme = stored;
}
} catch (e) { /* private mode: the server-rendered theme stands */ }
/* Panel widths, for the same reason and in the same breath. app.js also
applies these, but it is deferred -- so without this a panel dragged
wider opens at its default and jumps once the script runs. */
try {
var widths = JSON.parse(localStorage.getItem("lembas-panel-widths") || "{}");
Object.keys(widths).forEach(function (name) {
if (name.indexOf("--") === 0) {
document.documentElement.style.setProperty(name, widths[name] + "px");
}
});
} catch (e) { /* the stylesheet's defaults stand */ }
})();
</script>
</head>
+33 -2
View File
@@ -14,18 +14,48 @@
data-terminal
data-url="/api/chats/{{ chat.id }}/terminal/ws"
data-label="{{ agent_profile.name if agent_profile else 'this connection' }}"
data-dir="{{ chat.project_dir }}">
data-dir="{{ chat.project_dir }}"
data-resize-target>
{#
The left edge, dragged. A separator rather than a decoration: it takes
focus and answers the arrow keys, or the panel is only resizable with a
mouse and the grip is a focus trap that does nothing.
#}
<div class="panel-resize" data-resize="--terminal-width" data-resize-min="384"
role="separator" aria-orientation="vertical" tabindex="0"
aria-label="Resize the terminal">
{{ icon("grip", "icon--sm") }}
</div>
<div class="panel-head">
<h2 class="panel-head__title">
{{ icon("terminal", "icon--sm") }}
<span>{{ agent_profile.name if agent_profile else "Terminal" }}</span>
<span class="terminal__where" data-terminal-where>{{ chat.project_dir }}</span>
</h2>
{#
Copy, Send and Auto. All three need to know where one command ends and
the next begins, which is what the shell integration provides; without it
the first two fall back to scraping the screen and say so, and Auto is
disabled rather than degraded. Forty arbitrary lines attached to every
message is worse than nothing attached at all.
#}
<button class="btn btn--icon btn--sm" type="button" data-terminal-copy
title="Copy the last command and its output"
aria-label="Copy the last command and its output">
{{ icon("copy", "icon--sm") }}
</button>
<button class="btn btn--icon btn--sm" type="button" data-terminal-send
title="Put the selection, or the last of the output, into the message box"
title="Put the last command and its output into the message box"
aria-label="Send to chat">
{{ icon("arrow-up", "icon--sm") }}
</button>
<button class="btn btn--icon btn--sm" type="button" data-terminal-auto
aria-pressed="false"
title="Attach every command you run to your next message"
aria-label="Send every command automatically">
{{ icon("sparkle", "icon--sm") }}
</button>
<button class="btn btn--icon btn--sm" type="button" data-toggle="#terminal"
aria-label="Close terminal">
{{ icon("x", "icon--sm") }}
@@ -36,6 +66,7 @@
<div class="terminal__status">
<span class="terminal__message" data-terminal-message>Connecting…</span>
<span class="terminal__last" data-terminal-last></span>
<span>Ctrl+Shift+C / V</span>
</div>
</aside>