diff --git a/docs/reference.md b/docs/reference.md index 5b9ea59..62ea0d3 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -209,7 +209,7 @@ A key after the prefix never reaches the program, and a program can't receive `c | `r` | list everything again | | `esc` | back to the conversation | -Results take the same keys (`j`/`k`, `enter`, `m`, `y`, `p`). A preview is a tab of its own, titled with the path: code is highlighted by its extension, markdown is rendered, images are drawn, and other binary files show their size. `j`/`k` scroll it, `space`/`pgdn` and `pgup` page, `g`/`G` go to the ends, and `ctrl+\ x` or the tab's ✕ closes it and returns to Files. Files are read through the server, so previews work over SSH too. The Files tab can't be closed; `d` hides the column. Creating, renaming or deleting files is left to the agent or your editor. +Results take the same keys (`j`/`k`, `enter`, `m`, `y`, `p`). A preview is a tab of its own, titled with the path: code is highlighted by its extension, markdown is rendered, images are drawn, and other binary files show their size. `j`/`k` scroll it, `space`/`pgdn` and `pgup` page, `g`/`G` go to the ends, and `ctrl+\ x` or the tab's ✕ closes it and returns to Files. Files are read through the server, so previews work over SSH too. The Files tab can't be closed; `d` hides the column. Creating, renaming or deleting files is left to the agent or your editor. On a Windows server, search and the `@` menu need the Microsoft Visual C++ Redistributable; without it they fail with `Failed to search workspace entries`, until it's installed and the server restarted. ## Workspace diff --git a/src/ui/TerminalPane.tsx b/src/ui/TerminalPane.tsx index bc3eee1..11bb67d 100644 --- a/src/ui/TerminalPane.tsx +++ b/src/ui/TerminalPane.tsx @@ -3,8 +3,9 @@ import { extend } from "@opentui/react"; import { useEffect, useRef, type ReactNode } from "react"; import type { T3Session } from "../client/session.ts"; +import { isWindowsAbsolute } from "../model/paths.ts"; import { BAR } from "./edges.ts"; -import type { TerminalTarget } from "./terminalAttach.ts"; +import { keepConptyWrap, type TerminalTarget } from "./terminalAttach.ts"; import { colors } from "./theme.ts"; import { useEvent } from "./useEvent.ts"; @@ -114,6 +115,7 @@ function paint(term: EmbeddedTerminalRenderable): void { function attachServer(session: T3Session, target: TerminalTarget, term: EmbeddedTerminalRenderable, cols: number, rows: number, close: (message?: string) => void): Link { const { threadId, terminalId } = target; const decoder = new TextDecoder(); + const output = isWindowsAbsolute(target.cwd) ? keepConptyWrap : (data: string) => data; let stopped = false; let detach = () => {}; const end = (message?: string) => { @@ -131,10 +133,10 @@ function attachServer(session: T3Session, target: TerminalTarget, term: Embedded case "restarted": term.write("\x1bc"); paint(term); - term.write(event.snapshot.history); + term.write(output(event.snapshot.history)); return; case "output": - term.write(event.data); + term.write(output(event.data)); return; case "cleared": term.write("\x1b[H\x1b[2J"); @@ -176,12 +178,13 @@ function withoutInBandResize(data: Uint8Array): Uint8Array { /** A program run here on its own pty, answering its terminal queries through the emulator. */ function spawnLocal(program: Extract, term: EmbeddedTerminalRenderable, cols: number, rows: number, close: (message?: string) => void): Link { + const output = process.platform === "win32" ? (data: Uint8Array) => Buffer.from(keepConptyWrap(Buffer.from(data).toString("latin1")), "latin1") : (data: Uint8Array) => data; let stopped = false; let child: ReturnType; try { child = Bun.spawn([...program.command], { ...(program.cwd ? { cwd: program.cwd } : {}), - terminal: { cols, rows, data: (_pty, data) => term.write(data) }, + terminal: { cols, rows, data: (_pty, data) => term.write(output(data)) }, }); } catch (error) { // A missing program throws here, inside a resize; let the pane say so rather than the render loop. diff --git a/src/ui/terminalAttach.ts b/src/ui/terminalAttach.ts index 4c2f370..122bf91 100644 --- a/src/ui/terminalAttach.ts +++ b/src/ui/terminalAttach.ts @@ -2,6 +2,7 @@ import type { CliRenderer } from "@opentui/core"; import type { ThreadId } from "@t3tools/contracts"; import type { T3Session } from "../client/session.ts"; +import { isWindowsAbsolute } from "../model/paths.ts"; /** Ctrl+J (LF), Ctrl+] and Ctrl+J under the kitty keyboard protocol leave the terminal. */ const DETACH_KEYS = new Set(["\n", "\x1d", "\x1b[106;5u"]); @@ -17,6 +18,17 @@ export interface TerminalTarget { readonly label: string; } +/** + * ConPTY's output, for terminals that aren't Windows Terminal. ConPTY fills a row and goes on to + * the next by writing, relying on the wrap staying pending through a `\\x1b[K` in between; other + * terminals drop it there, as xterm does, and the next row's text lands on the last column. It + * only erases and writes on without moving when the row is full, so that erase has nothing to do. + */ +export function keepConptyWrap(data: string): string { + // biome-ignore lint/suspicious/noControlCharactersInRegex: this matches escape sequences + return data.replace(/\x1b\[K(?=(?:\x1b\[\??[\d;]*[mhl])*[^\x00-\x1f\x7f])/g, ""); +} + /** * Gives the whole screen to one of T3's terminals: its scrollback, then live output, with * keystrokes sent to it. Ctrl+J comes back. The terminal keeps running on the server. @@ -26,6 +38,7 @@ export async function attachTerminal(renderer: CliRenderer, session: T3Session, const stdin = process.stdin; const stdout = process.stdout; const size = () => ({ cols: stdout.columns || 80, rows: stdout.rows || 24 }); + const output = isWindowsAbsolute(target.cwd) ? keepConptyWrap : (data: string) => data; let finished = false; await renderer.suspend(); @@ -50,10 +63,10 @@ export async function attachTerminal(renderer: CliRenderer, session: T3Session, case "snapshot": case "restarted": stdout.write("\x1b[H\x1b[2J"); - stdout.write(event.snapshot.history); + stdout.write(output(event.snapshot.history)); return; case "output": - stdout.write(event.data); + stdout.write(output(event.data)); return; case "cleared": stdout.write("\x1b[H\x1b[2J"); diff --git a/test/terminalAttach.test.ts b/test/terminalAttach.test.ts new file mode 100644 index 0000000..d06d3b3 --- /dev/null +++ b/test/terminalAttach.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, test } from "bun:test"; + +import { keepConptyWrap } from "../src/ui/terminalAttach.ts"; + +describe("keepConptyWrap", () => { + test("drops the erase between a full row and the next one's text", () => { + expect(keepConptyWrap("~ \x1b[K~ \x1b[K~")).toBe("~ ~ ~"); + }); + + test("sees past styling and cursor visibility", () => { + expect(keepConptyWrap("~ \x1b[K\x1b[m\x1b[1m\x1b[7mstatus")).toBe("~ \x1b[m\x1b[1m\x1b[7mstatus"); + expect(keepConptyWrap("~ \x1b[K\x1b[?25l~")).toBe("~ \x1b[?25l~"); + }); + + test("keeps an erase that ends a row", () => { + for (const data of ["short\x1b[K", "short\x1b[K\r\nnext", "short\x1b[K\x1b[3;1Hnext", "short\x1b[K\x1b[?25l\x1b[14C"]) expect(keepConptyWrap(data)).toBe(data); + }); + + test("leaves other erases alone", () => { + expect(keepConptyWrap("a\x1b[2Kb\x1b[1Kc")).toBe("a\x1b[2Kb\x1b[1Kc"); + }); +});