Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(ide): delete selected text across a draft the CLI wraps itself (#290)

Ruben Lütked9f5a745d7d7
diff --git a/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.test.ts b/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.test.ts
index 3813e89e2b..11c8130abe 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.test.ts
+++ b/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.test.ts
@@ -77,6 +77,13 @@ describe("terminal prompt selection", () => {
expect(pane.sent).toHaveLength(1);
});
+ it("recognises Claude Code's marker, which is followed by a no-break space", async () => {
+ const pane = await setup("❯ csdadsfasdf");
+ pane.select(2, 15); // a drag past the end of the draft
+ pane.press("Delete");
+ expect(pane.sent).toEqual(["\x7f".repeat(11)]);
+ });
+
it("deletes a middle selection while leaving the suffix in place", async () => {
const pane = await setup();
pane.select(2, 7);
@@ -113,6 +120,106 @@ describe("terminal prompt selection", () => {
expect(pane.sent).toEqual(["\x7f".repeat(11)]);
});
+ describe("a draft the CLI wraps into indented rows itself", () => {
+ // Claude Code's layout, measured in a 40-column PTY: separate (not
+ // `isWrapped`) rows indented to the marker width, the space at each break
+ // undrawn yet still one arrow press away.
+ const words = Array.from({ length: 24 }, (_, i) => `w${String(i + 1).padStart(2, "0")}`);
+ const draft = words.join(" ");
+ const box = (below = "─".repeat(40)) => [
+ "─".repeat(40),
+ `❯ ${words.slice(0, 9).join(" ")}`,
+ ` ${words.slice(9, 18).join(" ")}`,
+ ` ${words.slice(18).join(" ")}`,
+ below,
+ ].join("\r\n");
+ const at = (word: string) => draft.indexOf(word);
+
+ it("deletes a selection spanning rows with one Backspace", async () => {
+ const pane = await setup(`${box()}\x1b[4;26H`);
+ pane.select(18, 21, 1, 2); // from "w05" through "w14"
+ pane.press("Backspace");
+ const end = at("w14") + 3;
+ expect(pane.sent).toEqual([
+ "\x1b[D".repeat(draft.length - end) + "\x7f".repeat(end - at("w05")),
+ ]);
+ });
+
+ it("selects the whole wrapped draft with select-all", async () => {
+ const pane = await setup(`${box()}\x1b[4;26H`);
+ pane.press("a", { ctrlKey: true });
+ expect(pane.selection()).toEqual({ start: { x: 2, y: 1 }, end: { x: 25, y: 3 } });
+ pane.press("Delete");
+ expect(pane.sent).toEqual(["\x7f".repeat(draft.length)]);
+ });
+
+ it("snaps a drag from a row's indent to its first letter", async () => {
+ const pane = await setup(`${box()}\x1b[4;26H`);
+ pane.select(0, 25, 2, 3); // indent of the second row to the end
+ pane.press("Backspace");
+ expect(pane.sent).toEqual(["\x7f".repeat(draft.length - at("w10"))]);
+ });
+
+ it("edits rows below the caret when the editor box closes under them", async () => {
+ const pane = await setup(`${box()}\x1b[2;11H`); // caret before "w03"
+ pane.select(2, 5, 3); // "w19"
+ pane.press("Delete");
+ const caret = at("w03");
+ expect(pane.sent).toEqual([
+ "\x1b[C".repeat(at("w19") + 3 - caret) + "\x7f".repeat(3),
+ ]);
+ });
+
+ it("never treats indented rows under the caret as draft unless the box closes", async () => {
+ const pane = await setup(`${box(" footer hint\r\nstatus: ready")}\x1b[2;11H`);
+ pane.select(2, 5, 3);
+ pane.press("Delete");
+ expect(pane.sent).toEqual([]);
+ });
+
+ it("never joins indented output that is not anchored to the marker row", async () => {
+ const pane = await setup("earlier output\r\n indented result\r\n more result");
+ pane.select(2, 10, 1); // output above the caret's line
+ pane.press("Backspace");
+ expect(pane.sent).toEqual([]);
+ expect(pane.selection()).toBeUndefined();
+ });
+ });
+
+ describe("an input line without a marker", () => {
+ it("edits a shell prompt line, leaving select-all to the shell", async () => {
+ const pane = await setup("PS C:\\work> git status");
+ expect(pane.press("a", { ctrlKey: true }).passthrough).toBe(true);
+ pane.select(12, 15); // "git"
+ pane.press("Backspace");
+ expect(pane.sent).toEqual(["\x1b[D".repeat(7) + "\x7f".repeat(3)]);
+ });
+
+ it("follows a shell line the terminal soft-wrapped", async () => {
+ const pane = await setup("PS C:\\> csdadsfasdf", false, 15);
+ pane.select(8, 4, 0, 1); // "csdadsfasdf", across the wrap
+ pane.press("Delete");
+ expect(pane.sent).toEqual(["\x7f".repeat(11)]);
+ });
+
+ it("leaves a box frame around the input out of the edit", async () => {
+ // OpenCode: the draft sits in a frame drawn with box characters.
+ const pane = await setup(" \u2503 csdadsfasdf \u2503\x1b[17G");
+ pane.select(0, 25); // the whole row, frame included
+ pane.press("Delete");
+ expect(pane.sent).toEqual(["\x7f".repeat(11)]);
+ });
+
+ it("drops a highlight on output so the next press edits normally", async () => {
+ const pane = await setup("some output\r\nPS> abc");
+ pane.select(0, 4, 0);
+ expect(pane.press("Backspace").passthrough).toBe(false);
+ expect(pane.sent).toEqual([]);
+ expect(pane.selection()).toBeUndefined();
+ expect(pane.press("Backspace").passthrough).toBe(true);
+ });
+ });
+
it("handles select-all ending at the last terminal column", async () => {
const pane = await setup("› abcdefgh", false, 10);
pane.press("a", { ctrlKey: true });
@@ -206,7 +313,7 @@ describe("terminal prompt selection", () => {
pane.cleanup();
});
- it.each(["shell output", "› [Pasted text 20 lines]", "› [Image #1]"])(
+ it.each(["› [Pasted text 20 lines]", "› [Image #1]"])(
"leaves an unsupported editor alone: %s", async (output) => {
const pane = await setup(output);
expect(pane.press("a", { ctrlKey: true }).passthrough).toBe(true);
diff --git a/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.ts b/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.ts
index aaebe50fe8..a43cd7e1df 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.ts
+++ b/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.ts
@@ -18,46 +18,143 @@ interface PromptLine {
end: IBufferCellPosition;
cursor: number;
offsets: Map<number, number>;
+ /** Found through an input marker, so its exact extent is known. */
+ anchored: boolean;
}
const segmenter = typeof Intl.Segmenter === "function"
? new Intl.Segmenter(undefined, { granularity: "grapheme" }) : null;
const graphemes = (text: string) => [...segmenter!.segment(text)].length;
+const PROMPT_MARKER = /^ *[❯›>] /;
+/** Rows below the draft that close an editor block: blank, or a box rule. */
+const CLOSING_ROW = /^\s*$|^\s*[─━═╌┄╰└]/;
+/** Box-drawing and block characters: an input box's frame, never typed text. */
+const FRAME = /^[\u2500-\u259f]$/;
+const MAX_PROMPT_ROWS = 200;
+
+/** Which buffer rows hold the input, and where each row's text begins. */
+interface PromptRows {
+ first: number;
+ last: number;
+ /** Column where input starts on `row`. */
+ left(row: number): number;
+ /** Whether the CLI (not the terminal) broke the line after `row`. */
+ editorWrap: boolean;
+ anchored: boolean;
+}
+
/**
- * Only the logical input line containing the live cursor is editable. A
- * terminal selection otherwise belongs to output, not to the CLI's editor.
- * Recognise the same input markers as agentic_ide/session.py, without using
- * provider names. Wrapped buffer rows are one line; separate TUI rows are not
- * guessed to be input (they can be menus, permission questions or output).
+ * Only the logical input containing the live cursor is editable. A terminal
+ * selection otherwise belongs to output, not to the CLI's editor.
+ *
+ * The input is found in one of two ways:
+ *
+ * - **Anchored** by an input marker (the same markers as
+ * agentic_ide/session.py, never provider names). A TUI editor (Claude Code,
+ * Codex) wraps a long draft itself: every continuation row is a separate
+ * line indented to the width of the marker, and the break character — the
+ * space at a word wrap or a typed newline — is not drawn but still costs one
+ * arrow press. Rows above the caret count only when every one of them up to
+ * the marker row is such a continuation; rows below it only when the block
+ * then closes like an editor box, so a footer, menu or permission question
+ * is never mistaken for draft text.
+ * - **The caret's line** everywhere else — a shell prompt, an editor drawn in
+ * a frame (OpenCode), any CLI without a marker: the row holding the cursor
+ * plus the rows the terminal soft-wrapped it onto. A frame drawn in box
+ * characters is left out at both ends. Text before the input on that row (a
+ * shell's `PS C:\>`) may be selected too; the surplus Backspaces then land
+ * at the start of the input, where every line editor ignores them.
*/
-function readPrompt(term: PromptSelectionTerminal): PromptLine | null {
+function findRows(term: PromptSelectionTerminal, cursorRow: number): PromptRows | null {
const buffer = term.buffer.active;
- if (!segmenter) return null;
- const cursorRow = buffer.baseY + buffer.cursorY;
+ // Claude Code draws its marker as "❯" + NO-BREAK SPACE; read it as a space.
+ const rowText = (row: number) =>
+ buffer.getLine(row)?.translateToString(false, 0, term.cols).replace(/\u00a0/g, " ");
let first = cursorRow;
- while (first > buffer.baseY && buffer.getLine(first)?.isWrapped) first--;
- const head = buffer.getLine(first)?.translateToString(false, 0, term.cols);
- const prefix = head?.match(/^ *[❯›>] /)?.[0];
- if (!prefix) return null;
+ let prefix: string | undefined;
+ for (;;) {
+ const line = buffer.getLine(first);
+ const shown = rowText(first);
+ if (!line || shown === undefined) return null;
+ if (!line.isWrapped) {
+ prefix = shown.match(PROMPT_MARKER)?.[0];
+ if (prefix || !/^ +\S/.test(shown)) break;
+ }
+ if (first === 0 || cursorRow - first >= MAX_PROMPT_ROWS) break;
+ first--;
+ }
+ if (prefix) {
+ const indent = " ".repeat(prefix.length);
+ const continuation = (row: number) => {
+ const shown = rowText(row);
+ return shown !== undefined && shown.startsWith(indent) && /\S/.test(shown[indent.length] ?? "");
+ };
+ let anchored = true;
+ for (let row = first + 1; row <= cursorRow; row++) {
+ if (!buffer.getLine(row)?.isWrapped && !continuation(row)) anchored = false;
+ }
+ if (anchored) {
+ let last = cursorRow;
+ for (let row = cursorRow + 1; row - cursorRow <= MAX_PROMPT_ROWS; row++) {
+ const line = buffer.getLine(row);
+ if (line?.isWrapped || (line && continuation(row))) continue;
+ const closing = rowText(row);
+ if (closing !== undefined && CLOSING_ROW.test(closing)) last = row - 1;
+ else while (buffer.getLine(last + 1)?.isWrapped) last++;
+ break;
+ }
+ const markerWidth = prefix.length;
+ return {
+ first, last, anchored: true, editorWrap: true,
+ left: (row) => row === first || !buffer.getLine(row)?.isWrapped ? markerWidth : 0,
+ };
+ }
+ }
+
+ first = cursorRow;
+ while (first > 0 && buffer.getLine(first)?.isWrapped) first--;
let last = cursorRow;
while (buffer.getLine(last + 1)?.isWrapped) last++;
+ const head = buffer.getLine(first);
+ if (!head) return null;
+ let frame = 0;
+ for (; frame < term.cols; frame++) {
+ const chars = head.getCell(frame)?.getChars() ?? "";
+ if (chars.trim() && !FRAME.test(chars)) break;
+ }
+ return {
+ first, last, anchored: false, editorWrap: false,
+ left: (row) => (row === first ? frame : 0),
+ };
+}
+
+function readPrompt(term: PromptSelectionTerminal): PromptLine | null {
+ const buffer = term.buffer.active;
+ if (!segmenter) return null;
+ const cursorRow = buffer.baseY + buffer.cursorY;
+ const rows = findRows(term, cursorRow);
+ if (!rows) return null;
+ const { first, last } = rows;
const offsets = new Map<number, number>();
let text = "";
- const start = { x: prefix.length, y: first };
+ const start = { x: rows.left(first), y: first };
let end = start;
for (let row = first; row <= last; row++) {
const line = buffer.getLine(row);
if (!line) return null;
- const left = row === first ? start.x : 0;
- // Keep typed spaces through the caret, but omit empty cells after input.
+ const left = rows.left(row);
+ const softWrapped = row < last && buffer.getLine(row + 1)?.isWrapped;
+ // Keep typed spaces through the caret, but omit empty cells after input
+ // and a frame closing the row.
let right = term.cols;
- if (row === last) {
+ if (!softWrapped) {
right = left;
for (let col = left; col < term.cols; col++) {
const cell = line.getCell(col);
- if (cell?.getChars().trim()) right = col + cell.getWidth();
+ const chars = cell?.getChars() ?? "";
+ if (cell && chars.trim() && !FRAME.test(chars)) right = col + cell.getWidth();
}
if (row === cursorRow) right = Math.max(right, buffer.cursorX);
}
@@ -68,13 +165,16 @@ function readPrompt(term: PromptSelectionTerminal): PromptLine | null {
text += cell.getChars() || " ";
}
offsets.set(row * term.cols + right, text.length);
+ // The editor's own wrap swallowed one break character here, unless a
+ // single word filled the row and was cut without one.
+ if (rows.editorWrap && row < last && !softWrapped && right < term.cols) text += " ";
end = { x: right, y: row };
}
const cursor = offsets.get(cursorRow * term.cols + buffer.cursorX);
// Collapsed paste/attachment tokens have editor-specific cursor widths.
// Sending one Backspace per displayed letter could erase adjacent input.
if (cursor === undefined || /\[(?:Pasted|Image|Attachment)\b/i.test(text)) return null;
- return { text, start, end, cursor, offsets };
+ return { text, start, end, cursor, offsets, anchored: rows.anchored };
}
const samePrompt = (a: PromptLine | null, b: PromptLine | null) =>
@@ -110,8 +210,15 @@ export function installPromptSelectionBridge(
const key = (pos: IBufferCellPosition) => pos.y * term.cols + pos.x;
if (selection.start.y < prompt.start.y ||
key(selection.end) > (prompt.end.y + 1) * term.cols) return false;
- const start = prompt.offsets.get(Math.max(key(selection.start), key(prompt.start)));
- const end = prompt.offsets.get(Math.min(key(selection.end), key(prompt.end)));
+ // A drag may begin in a continuation row's indent or end in the empty
+ // cells after a row's text; snap both ends onto the draft.
+ const cells = [...prompt.offsets.keys()];
+ const from = Math.max(key(selection.start), key(prompt.start));
+ const to = Math.min(key(selection.end), key(prompt.end));
+ const startCell = cells.find((cell) => cell >= from);
+ const endCell = cells.filter((cell) => cell <= to).pop();
+ const start = startCell === undefined ? undefined : prompt.offsets.get(startCell);
+ const end = endCell === undefined ? undefined : prompt.offsets.get(endCell);
if (start === undefined || end === undefined || start >= end) return false;
// A cell boundary can be inside an emoji composed of several code points.
const boundaries = new Set([0, ...[...segmenter!.segment(prompt.text)].map((s) => s.index + s.segment.length)]);
@@ -124,7 +231,7 @@ export function installPromptSelectionBridge(
selectedPrompt = null;
// One input frame, no Enter, no Ctrl+C and no clipboard round trip.
term.input(move + backspaces);
- return false;
+ return true;
};
const renderListener = term.onRender(() => {
paintedPrompt = readPrompt(term);
@@ -146,7 +253,9 @@ export function installPromptSelectionBridge(
(isMac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey);
if (selectAll) {
const prompt = paintedPrompt;
- if (!prompt) return true;
+ // Without a marker the input's start is a guess, and shells use this
+ // chord for "start of line".
+ if (!prompt?.anchored) return true;
event.preventDefault();
const { start, end } = prompt;
term.select(start.x, start.y, (end.y - start.y) * term.cols + end.x - start.x);
@@ -158,15 +267,24 @@ export function installPromptSelectionBridge(
if (!selection) return true;
// A selected output line must never delete unrelated prompt text.
event.preventDefault();
- if (!selectedPrompt) return false;
const prompt = readPrompt(term);
- if (prompt && samePrompt(selectedPrompt, prompt)) return erase(prompt, selection);
+ // A selection made while no input was recognisable still counts once the
+ // screen has settled: what is highlighted now is what the user sees.
+ const selected = selectedPrompt ?? (samePrompt(prompt, paintedPrompt) ? prompt : null);
+ if (prompt && samePrompt(selected, prompt)) {
+ if (!erase(prompt, selection)) term.clearSelection();
+ return false;
+ }
// Wait for one completed frame instead of silently discarding Delete in
// the middle of a synchronized repaint. Never retry a changed draft.
- if (samePrompt(selectedPrompt, paintedPrompt)) {
- pending = { prompt: selectedPrompt, selection };
+ if (selected && samePrompt(selected, paintedPrompt)) {
+ pending = { prompt: selected, selection };
pendingTimer = setTimeout(cancelPending, 1_500);
+ return false;
}
+ // Nothing editable is selected: drop the highlight so the next press
+ // edits normally instead of being swallowed again.
+ term.clearSelection();
return false;
});
return () => {