Skip to content

Commit 80aba94

Browse files
author
SIN CI
committed
fix(tui): rendering quality — truncate tool output, wrap code blocks, handle empty responses
4 real bugs in existing rendering code: 1. renderToolOutput: no line limit → truncate to 50 lines + indicator 2. renderCodeBlock: no line wrapping → MaxWidth wrap + 200-line limit 3. renderAssistantBubble: empty response → Thinking… or header-only 4. renderToolCard: input truncation by byte len → display width Build ✅ | Tests ✅
1 parent bc5a9c0 commit 80aba94

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎cmd/sin-code/tui/syntax_highlight.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,18 @@ func renderCodeBlock(code, language string, h *SyntaxHighlighter, styles Styles,
313313
highlightLines := strings.Split(highlighted, "\n")
314314
codeLines := strings.Split(code, "\n")
315315

316+
const maxCodeLines = 200
317+
truncated := false
318+
totalLines := len(codeLines)
319+
if len(highlightLines) > maxCodeLines {
320+
highlightLines = highlightLines[:maxCodeLines]
321+
truncated = true
322+
}
323+
if len(codeLines) > maxCodeLines {
324+
codeLines = codeLines[:maxCodeLines]
325+
truncated = true
326+
}
327+
316328
var bodyLines []string
317329
maxLines := len(highlightLines)
318330
if len(codeLines) > maxLines {
@@ -324,6 +336,7 @@ func renderCodeBlock(code, language string, h *SyntaxHighlighter, styles Styles,
324336
if i < len(highlightLines) {
325337
hl = highlightLines[i]
326338
}
339+
hl = wrapLine(hl, innerWidth)
327340
if showLineNumbers {
328341
num = styles.Muted.Render(fmt.Sprintf("%3d ", i+1))
329342
} else {
@@ -333,6 +346,10 @@ func renderCodeBlock(code, language string, h *SyntaxHighlighter, styles Styles,
333346
}
334347
body := strings.Join(bodyLines, "\n")
335348

349+
if truncated {
350+
body += "\n" + styles.Muted.Render(fmt.Sprintf("⋯ %d more lines truncated", totalLines-maxCodeLines))
351+
}
352+
336353
langLabel := ""
337354
if language != "" {
338355
langLabel = strings.ToLower(strings.TrimSpace(language))
@@ -354,3 +371,13 @@ func renderCodeBlock(code, language string, h *SyntaxHighlighter, styles Styles,
354371

355372
return hdr.String() + "\n" + body + "\n" + ftr
356373
}
374+
375+
func wrapLine(line string, maxDisplayWidth int) string {
376+
if maxDisplayWidth < 10 {
377+
return line
378+
}
379+
if lipgloss.Width(line) <= maxDisplayWidth {
380+
return line
381+
}
382+
return lipgloss.NewStyle().MaxWidth(maxDisplayWidth).Render(line)
383+
}

‎cmd/sin-code/tui/update.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,13 @@ func renderToolOutput(output string, styles Styles, width int) string {
23262326
width = 10
23272327
}
23282328

2329+
lines := strings.Split(output, "\n")
2330+
const maxLines = 50
2331+
if len(lines) > maxLines {
2332+
output = strings.Join(lines[:maxLines], "\n")
2333+
output += fmt.Sprintf("\n⋯ %d more lines (use /tools to see full output)", len(lines)-maxLines)
2334+
}
2335+
23292336
highlighter := NewSyntaxHighlighter(styles.Theme)
23302337

23312338
if looksLikeGoCode(output) {
@@ -2638,6 +2645,14 @@ func renderAssistantBubble(msg ChatMessage, highlighter *SyntaxHighlighter, styl
26382645

26392646
rendered := renderMarkdownWithCodeBlocks(msg.Text, highlighter, styles, bodyWidth)
26402647

2648+
if strings.TrimSpace(rendered) == "" {
2649+
if streaming {
2650+
rendered = styles.Muted.Render("Thinking…")
2651+
} else {
2652+
return headerLine + "\n"
2653+
}
2654+
}
2655+
26412656
if streaming {
26422657
cursor := renderStreamingCursor(spinner, styles)
26432658
rendered = strings.TrimRight(rendered, "\n") + cursor
@@ -2725,7 +2740,7 @@ func renderToolCard(msg ChatMessage, styles Styles, width int, focused bool) str
27252740

27262741
if msg.ToolInput != "" {
27272742
inputText := msg.ToolInput
2728-
if len(inputText) > bodyWidth-10 {
2743+
if lipgloss.Width(inputText) > bodyWidth-10 {
27292744
inputText = truncateString(inputText, bodyWidth-13)
27302745
}
27312746
b.WriteString(styles.Muted.Render(" in: "))

0 commit comments

Comments
 (0)