diff --git a/packages/tui/src/routes/session/index.tsx b/packages/tui/src/routes/session/index.tsx index 9e251742ed9..31eeeb3c453 100644 --- a/packages/tui/src/routes/session/index.tsx +++ b/packages/tui/src/routes/session/index.tsx @@ -2965,7 +2965,7 @@ function ShellDisplay(props: { return stripAnsi(props.output?.trim() ?? "") }) const maxLines = 10 - const maxChars = createMemo(() => maxLines * Math.max(20, ctx.width - 6)) + const maxChars = createMemo(() => maxLines * Math.max(20, ctx.width - 6 - (isRunning() ? 2 : 0))) const prefix = createMemo(() => (workdir() && workdir() !== "." ? `cd ${workdir()} && ` : "")) const input = createMemo(() => (props.command ? `${isRunning() ? "" : "$ "}${prefix()}${props.command}` : "")) const collapsed = createMemo(() => collapseShellOutput(input(), output(), maxLines, maxChars())) @@ -2991,8 +2991,30 @@ function ShellDisplay(props: { ) } > - {limitedInput()}}> - {limitedInput()} + + {limitedInput()} + + } + > + + + + {limitedInput()} + + {limitedOutput()} diff --git a/packages/tui/src/util/collapse-tool-output.ts b/packages/tui/src/util/collapse-tool-output.ts index 3a2c3bf1644..dbdf6e5a190 100644 --- a/packages/tui/src/util/collapse-tool-output.ts +++ b/packages/tui/src/util/collapse-tool-output.ts @@ -1,3 +1,6 @@ +import { Locale } from "./locale" +import { stringWidth } from "./string-width" + export function collapseToolOutput(output: string, maxLines: number, maxChars: number) { const lines = output.split("\n") if (lines.length <= maxLines && Array.from(output).length <= maxChars) { @@ -21,21 +24,60 @@ export function collapseToolOutput(output: string, maxLines: number, maxChars: n } export function collapseShellOutput(input: string, output: string, maxLines: number, maxChars: number) { - const content = [input, output].filter(Boolean).join("\n\n") - const collapsed = collapseToolOutput(content, maxLines, maxChars) - if (!collapsed.overflow) return { input, output, overflow: false } - if (!input) return { input, output: collapseTail(output, maxLines, maxChars), overflow: true } - if (!output) return { input: collapsed.output, output, overflow: true } + if (!input) { + const collapsed = collapseToolOutput(output, maxLines, maxChars) + return { + input, + output: collapsed.overflow ? collapseTail(output, maxLines, maxChars) : output, + overflow: collapsed.overflow, + } + } - const reserved = Math.max(1, Math.floor(maxChars / maxLines)) - const command = collapseToolOutput( - input, - Math.max(1, maxLines - 2), - Math.max(1, maxChars - reserved - 2), - ).output - const lines = Math.max(1, maxLines - command.split("\n").length - 1) - const chars = Math.max(1, maxChars - Array.from(command).length - 2) - return { input: command, output: collapseTail(output, lines, chars), overflow: true } + const commandLines = Math.min(2, maxLines) + const lineChars = Math.max(1, Math.floor(maxChars / Math.max(1, maxLines))) + const command = collapseShellCommand(input, commandLines, lineChars) + if (!output) return { input: command.output, output, overflow: command.overflow } + + const lines = Math.max(1, maxLines - command.lines - 1) + const chars = Math.max(1, maxChars - Array.from(command.output).length - 2) + const collapsed = collapseToolOutput(output, lines, chars) + return { + input: command.output, + output: collapsed.overflow ? collapseTail(output, lines, chars) : output, + overflow: command.overflow || collapsed.overflow, + } +} + +function collapseShellCommand(input: string, maxLines: number, lineWidth: number) { + const visible: string[] = [] + let lines = 1 + let width = 0 + const overflow = Locale.graphemes(input).some((segment) => { + if (segment === "\n") { + if (lines >= maxLines) return true + visible.push(segment) + lines++ + width = 0 + return false + } + + const next = stringWidth(segment) + if (width + next > lineWidth) { + if (lines >= maxLines) return true + lines++ + width = 0 + } + visible.push(segment) + width += next + return false + }) + if (!overflow) return { output: input, overflow, lines } + + if (width >= lineWidth) { + const removed = visible.pop() + if (removed !== undefined && removed !== "\n") width -= stringWidth(removed) + } + return { output: visible.join("") + "…", overflow, lines } } function collapseTail(output: string, maxLines: number, maxChars: number) {