Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/tui/src/routes/session/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import { useConfig } from "../../config"
import { useClipboard } from "../../context/clipboard"
import { nextThinkingMode, reasoningSummary, type ThinkingMode } from "../../context/thinking"
import { getScrollAcceleration } from "../../util/scroll"
import { collapseToolOutput } from "../../util/collapse-tool-output"
import { collapseToolOutput, normalizeShellOutput } from "../../util/collapse-tool-output"
import { usePluginRuntime } from "../../plugin/runtime"
import { PluginSlot } from "../../plugin/context"
import { Keymap, type KeymapCommand } from "../../context/keymap"
Expand Down Expand Up @@ -2448,7 +2448,7 @@ function Shell(props: ToolProps) {
limit: 1024 * 1024,
location: location ? { directory: location.directory, workspace: location.workspaceID } : undefined,
})
.then((response) => setBackgroundOutput(stripAnsi(response.data.output.trim())))
.then((response) => setBackgroundOutput(normalizeShellOutput(response.data.output.trim())))
.catch(() => undefined)
loading = false
}
Expand All @@ -2461,7 +2461,7 @@ function Shell(props: ToolProps) {
if (props.part.state.status === "streaming") return ""
if (shellID()) return expanded() ? backgroundOutput() : ""
const content = props.part.state.content[0]
return stripAnsi(content?.type === "text" ? content.text.trim() : "")
return normalizeShellOutput(content?.type === "text" ? content.text.trim() : "")
})
const maxLines = 10
const maxChars = createMemo(() => maxLines * Math.max(20, ctx.width - 6))
Expand Down
6 changes: 6 additions & 0 deletions packages/tui/src/util/collapse-tool-output.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stripAnsi from "strip-ansi"

export function collapseToolOutput(output: string, maxLines: number, maxChars: number) {
const lines = output.split("\n")
if (lines.length <= maxLines && Array.from(output).length <= maxChars) {
Expand All @@ -19,3 +21,7 @@ export function collapseToolOutput(output: string, maxLines: number, maxChars: n

return { output: preview, overflow: true }
}

export function normalizeShellOutput(output: string) {
return stripAnsi(output).replace(/\r\n?/g, "\n")
}
12 changes: 11 additions & 1 deletion packages/tui/test/cli/tui/collapse-tool-output.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test"
import { collapseToolOutput } from "../../../src/util/collapse-tool-output"
import { collapseToolOutput, normalizeShellOutput } from "../../../src/util/collapse-tool-output"

test("limits command input and output to the same line budget", () => {
const command = Array.from({ length: 8 }, (_, index) => `command ${index + 1}`).join("\n")
Expand All @@ -12,3 +12,13 @@ test("limits command input and output to the same line budget", () => {
expect(collapsed.output).toContain("command 8\n\noutput 1…")
expect(collapsed.output).not.toContain("output 2")
})

test("normalizes carriage-return shell progress before collapsing", () => {
const output = Array.from({ length: 30 }, (_, index) => `progress ${index + 1}`).join("\r")
const collapsed = collapseToolOutput(normalizeShellOutput(output), 10, 1_000)

expect(collapsed.overflow).toBe(true)
expect(collapsed.output.split("\n")).toHaveLength(10)
expect(collapsed.output).toContain("progress 10…")
expect(collapsed.output).not.toContain("progress 11")
})
Loading