on main help sign in
fix(ui): use ansi.truncate everywhere
commit9c61c7d032504a21760478c4c72f81cce7a11efc authorKieran Klukas <kieran@dunkirk.sh> dateFri 26 Jun 2026, 09:00 (4 months ago) parent3a99992
3 files changed+8−4
internal/agent/agent.go+2−1 internal/agent/tools/grep.go+3−2 internal/agent/tools/view.go+3−1
internal/agent/agent.go+2−1
@@ -46,6 +46,7 @@ import (
4646 "github.com/charmbracelet/crush/internal/session"
4747 "github.com/charmbracelet/crush/internal/stringext"
4848 "github.com/charmbracelet/crush/internal/version"
49+ "github.com/charmbracelet/x/ansi"
4950 "github.com/charmbracelet/x/exp/charmtone"
5051)
5152
@@ -1762,7 +1763,7 @@ func (a *sessionAgent) GenerateTitle(ctx context.Context, sessionID string, user
17621763 fallback := strings.ReplaceAll(userPrompt, "\n", " ")
17631764 fallback = strings.TrimSpace(fallback)
17641765 if len(fallback) > 50 {
1765− fallback = fallback[:50]
1766+ fallback = ansi.Truncate(fallback, 50, "…")
17661767 }
17671768 title = cmp.Or(fallback, DefaultSessionName)
17681769 }
internal/agent/tools/grep.go+3−2
@@ -23,6 +23,7 @@ import (
2323 "github.com/charmbracelet/crush/internal/config"
2424 "github.com/charmbracelet/crush/internal/csync"
2525 "github.com/charmbracelet/crush/internal/fsext"
26+ "github.com/charmbracelet/x/ansi"
2627)
2728
2829// regexCache provides thread-safe caching of compiled regex patterns
@@ -161,8 +162,8 @@ func NewGrepTool(workingDir string, config config.ToolGrep) fantasy.AgentTool {
161162 }
162163 if match.lineNum > 0 {
163164 lineText := match.lineText
164− if len(lineText) > maxGrepContentWidth {
165− lineText = lineText[:maxGrepContentWidth] + "..."
165+ if ansi.StringWidth(lineText) > maxGrepContentWidth {
166+ lineText = ansi.Truncate(lineText, maxGrepContentWidth, "...")
166167 }
167168 if match.charNum > 0 {
168169 fmt.Fprintf(&output, " Line %d, Char %d: %s\n", match.lineNum, match.charNum, lineText)
internal/agent/tools/view.go+3−1
@@ -335,7 +335,9 @@ func readTextFile(filePath string, offset, limit, maxContentSize int) (string, b
335335 lineText = strings.TrimSuffix(lineText, "\n")
336336 lineText = strings.TrimSuffix(lineText, "\r")
337337 if len(lineText) > MaxLineLength {
338− lineText = lineText[:MaxLineLength] + "..."
338+ // Truncate at a rune boundary to avoid splitting
339+ // multi-byte characters.
340+ lineText = strings.ToValidUTF8(lineText[:MaxLineLength], "") + "..."
339341 }
340342 projectedSize := contentSize + len(lineText)
341343 if len(lines) > 0 {