fix(tui): limit shell command previews

This commit is contained in:
Dax Raad
2026-09-16 00:20:32 -04:00
Unverified
parent c57733a647
commit 3c1f87d2d4
2 changed files with 81 additions and 17 deletions
+25 -3
View File
@@ -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: {
)
}
>
<Show when={isRunning()} fallback={<text fg={theme.text.default}>{limitedInput()}</text>}>
<Spinner color={color()}>{limitedInput()}</Spinner>
<Show
when={isRunning()}
fallback={
<text
fg={theme.text.default}
wrapMode={expanded() ? "word" : "char"}
maxHeight={expanded() ? undefined : 2}
>
{limitedInput()}
</text>
}
>
<box flexDirection="row" gap={1}>
<Spinner color={color()} />
<text
fg={color()}
wrapMode={expanded() ? "word" : "char"}
maxHeight={expanded() ? undefined : 2}
flexGrow={1}
minWidth={0}
>
{limitedInput()}
</text>
</box>
</Show>
<Show when={limitedOutput()}>
<text fg={theme.text.subdued}>{limitedOutput()}</text>
+56 -14
View File
@@ -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) {