From 5db00a6eb43e2a76e7d420064fc94b37f72b5fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A4=A7=E7=8C=AB?= <16399091+binaricat@users.noreply.github.com> Date: Wed, 25 Nov 2026 03:32:32 +0000 Subject: [PATCH] fix(terminal): ignore right-aligned prompt decorations in after-cursor readiness check --- components/sftp/SftpTerminalPathMenus.test.ts | 53 ++++++++++++++ .../terminal/autocomplete/promptDetector.ts | 71 +++++++++++++++++-- 2 files changed, 118 insertions(+), 6 deletions(-) diff --git a/components/sftp/SftpTerminalPathMenus.test.ts b/components/sftp/SftpTerminalPathMenus.test.ts index 43f0a69fb..5e94e2a58 100644 --- a/components/sftp/SftpTerminalPathMenus.test.ts +++ b/components/sftp/SftpTerminalPathMenus.test.ts @@ -362,6 +362,59 @@ test("after-cursor text after Ctrl+A on recalled history blocks idle cd injectio ); }); +test("right-aligned shell prompt decorations (e.g. zsh RPROMPT) do not block idle readiness while recalled commands still do", () => { + const cols = 80; + const makeTerm = (line: string, cursorX: number) => ({ + cols, + buffer: { + active: { + baseY: 0, + cursorY: 0, + cursorX, + length: 1, + getLine: (row: number) => (row === 0 ? { isWrapped: false, translateToString: () => line } : undefined), + }, + }, + }); + + // Zsh RPROMPT clock with default ZLE_RPROMPT_INDENT=1 (`$ ` + 69 spaces + `12:34:56 ` = 80 cols). + const rpromptClock = "$ " + " ".repeat(69) + "12:34:56 "; + const rpromptTerm = makeTerm(rpromptClock, 2); + const rprompt = detectPrompt(rpromptTerm as never); + assert.equal(rprompt.isAtPrompt, true); + assert.equal(rprompt.userInput, ""); + const rpromptAfter = getPromptLineTextAfterCursor(rpromptTerm as never).trim(); + assert.equal(rpromptAfter, ""); + assert.equal( + isIdleShellReadyForCommandInjection({ + sensitiveInputActive: false, + hasLiveTerminal: true, + alternateScreenActive: false, + isAtPrompt: rprompt.isAtPrompt, + userInputLength: rprompt.userInput.length, + pendingTypedInputLength: 0, + afterCursorInputLength: rpromptAfter.length, + }), + true, + ); + + // Flush-right multi-segment RPROMPT (ZLE_RPROMPT_INDENT=0). + const flushRight = "user@host:~$ " + " ".repeat(53) + "main 12:34:56"; + assert.equal(getPromptLineTextAfterCursor(makeTerm(flushRight, 13) as never).trim(), ""); + + // Recalled command at cursor with RPROMPT still present MUST remain blocked. + const recalledWithRprompt = "$ rm -rf /tmp/foo" + " ".repeat(54) + "12:34:56 "; + assert.ok(getPromptLineTextAfterCursor(makeTerm(recalledWithRprompt, 2) as never).trim().length > 0); + + // Recalled command with leading spaces + RPROMPT MUST remain blocked. + const spacedRecalledWithRprompt = "$ rm -rf /tmp/foo" + " ".repeat(52) + "12:34:56 "; + assert.ok(getPromptLineTextAfterCursor(makeTerm(spacedRecalledWithRprompt, 2) as never).trim().length > 0); + + // Recalled command with leading spaces and no RPROMPT (not right-aligned) MUST remain blocked. + const spacedRecalled = ("$ rm -rf /tmp/foo").padEnd(cols, " "); + assert.ok(getPromptLineTextAfterCursor(makeTerm(spacedRecalled, 2) as never).trim().length > 0); +}); + test("prompt draft reader blocks insertion when cursor is inside an open double quote and cleans up on unregister", () => { const sessionId = "sftp-insert-draft-test"; let draft = { promptText: "user@host:~$ ", userInput: "cat \"", pendingTypedInput: "cat \"" }; diff --git a/components/terminal/autocomplete/promptDetector.ts b/components/terminal/autocomplete/promptDetector.ts index 54ed2e35c..335d9d814 100644 --- a/components/terminal/autocomplete/promptDetector.ts +++ b/components/terminal/autocomplete/promptDetector.ts @@ -9,7 +9,7 @@ import type { Terminal as XTerm } from "@xterm/xterm"; import { isSensitiveTerminalChallenge } from "../../../domain/terminalPromptSecurity"; -import { sliceStringByCellColumns } from "./terminalStringCellWidth"; +import { sliceStringByCellColumns, stringCellWidth } from "./terminalStringCellWidth"; import { COMMON_SHELL_COMMANDS, NON_PROMPT_PATTERNS, PROMPT_CHARS } from "./promptDetectorPatterns"; export interface PromptDetectionResult { @@ -681,8 +681,56 @@ export function detectPrompt(term: XTerm): PromptDetectionResult { } /** - * Extract the text on the active logical line to the right of the cursor, - * including any wrapped continuation rows belonging to the same line. + * True when single-row text after the cursor looks like a right-aligned shell + * prompt decoration (e.g. zsh `RPROMPT`, fish `fish_right_prompt`, Powerlevel10k, + * Starship) rather than editable command text left behind after `Ctrl+A`. + * + * Right prompts sit flush against the terminal's right margin (allowing up to + * 2 trailing indent cells, e.g. zsh's default `ZLE_RPROMPT_INDENT=1`), separated + * from the cursor by a run of padding spaces wider than any internal gap inside + * the decoration itself. + */ +function isRightPromptDecorationAfterCursor( + rowAfter: string, + cursorX: number, + cols: number, + term: XTerm, +): boolean { + if (!Number.isFinite(cols) || cols <= 0) return false; + const trimmedRight = rowAfter.trimEnd(); + if (!trimmedRight) return false; + + const leadingSpaces = trimmedRight.length - trimmedRight.trimStart().length; + if (leadingSpaces < 2) return false; + + const endCol = cursorX + stringCellWidth(trimmedRight, term); + if (endCol < cols - 2) return false; + + // Reject lines where editable text sits near the cursor before a wider gap + // leading into the right prompt (e.g. `$ rm -rf /foo 12:34:56 `). + const body = trimmedRight.trimStart(); + let maxInternalSpaceRun = 0; + let currentSpaceRun = 0; + for (let i = 0; i < body.length; i += 1) { + if (body[i] === " ") { + currentSpaceRun += 1; + if (currentSpaceRun > maxInternalSpaceRun) { + maxInternalSpaceRun = currentSpaceRun; + } + } else { + currentSpaceRun = 0; + } + } + if (maxInternalSpaceRun >= leadingSpaces) return false; + + return true; +} + +/** + * Extract editable text on the active logical line to the right of the cursor, + * including any wrapped continuation rows belonging to the same line, while + * ignoring right-aligned prompt decorations (e.g. zsh `RPROMPT`) on an + * unwrapped row. */ export function getPromptLineTextAfterCursor(term: XTerm): string { const buffer = term.buffer.active; @@ -691,14 +739,25 @@ export function getPromptLineTextAfterCursor(term: XTerm): string { const line = buffer.getLine(cursorY); if (!line) return ""; - let after = sliceStringByCellColumns(line.translateToString(false), cursorX, undefined, term); + const rowAfter = sliceStringByCellColumns(line.translateToString(false), cursorX, undefined, term); + let wrappedAfter = ""; const maxRow = typeof buffer.length === "number" ? buffer.length : Number.POSITIVE_INFINITY; for (let row = cursorY + 1; row < maxRow; row += 1) { const nextLine = buffer.getLine(row); if (!nextLine || !nextLine.isWrapped) break; - after += nextLine.translateToString(false); + wrappedAfter += nextLine.translateToString(false); } - return after; + + if (wrappedAfter.trim().length > 0) { + return rowAfter + wrappedAfter; + } + if (rowAfter.trim().length === 0) { + return ""; + } + if (isRightPromptDecorationAfterCursor(rowAfter, cursorX, term.cols, term)) { + return ""; + } + return rowAfter; } /**