@@ -2,7 +2,6 @@ package tui
22
33 import (
44 "context"
5- "fmt"
65 "image"
76 "strings"
87 "time"
@@ -234,7 +233,9 @@ type Model struct {
234233 cancel context.CancelFunc
235234
236235 // Conversation
237- conversation []string // Pre-wrapped, pre-styled lines
236+ convEntries []string // Raw styled lines (not wrapped)
237+ convLines []string // Wrapped visual lines (cache, rebuilt on width change)
238+ convCachedW int // Width used for current convLines cache
238239 scrollOffset int // Lines from bottom (0 = pinned)
239240
240241 // Mouse state
@@ -242,9 +243,6 @@ type Model struct {
242243 selecting bool
243244 selectStart int
244245 selectEnd int
245-
246- // Cache
247- wrapCache map[string][]string
248246 }
249247
250248 // New creates a new TUI model.
@@ -284,15 +282,14 @@ func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool, modelID str
284282 styles: sty,
285283 focus: focusInput,
286284
287- provider: prov,
288- mcpProxy: proxy,
289- mcpTools: tools,
290- history: []provider.Message{{Role: "system", Content: systemPrompt, CreatedAt: time.Now()}},
291- conversation: []string{},
292- updateChan: ch,
293- ctx: ctx,
294- cancel: cancel,
295- wrapCache: make(map[string][]string),
285+ provider: prov,
286+ mcpProxy: proxy,
287+ mcpTools: tools,
288+ history: []provider.Message{{Role: "system", Content: systemPrompt, CreatedAt: time.Now()}},
289+ convEntries: []string{},
290+ updateChan: ch,
291+ ctx: ctx,
292+ cancel: cancel,
296293 }
297294 }
298295
@@ -308,46 +305,46 @@ func (m Model) Init() tea.Cmd {
308305 // convWidth returns the usable width of the conversation pane.
309306 func (m Model) convWidth() int { return m.layout.conv.Dx() }
310307
311-// wrapText word-wraps text to conversation width and applies style.
312-// Wrapping is cached (width+text); styling is applied after lookup.
313-func (m Model) wrapText(text string, style lipgloss.Style) ([]string, Model) {
314- w := m.convWidth()
315- if w <= 0 {
316- lines := strings.Split(text, "\n")
317- out := make([]string, len(lines))
318- for i, l := range lines {
319- out[i] = style.Render(l)
320- }
321- return out, m
322- }
323-
324- // Cache stores unstyled wrapped lines — safe to reuse across styles.
325- key := fmt.Sprintf("%d:%s", w, text)
326- lines, ok := m.wrapCache[key]
327- if !ok {
328- wrapped := ansi.Wordwrap(text, w, "")
329- lines = strings.Split(wrapped, "\n")
330- if len(m.wrapCache) > 1000 {
331- m.wrapCache = make(map[string][]string)
332- }
333- m.wrapCache[key] = lines
334- }
335-
336- // Apply style after cache lookup.
337- out := make([]string, len(lines))
338- for i, l := range lines {
308+// styledLines applies a lipgloss style to each line in a multi-line text.
309+// No wrapping — lines are stored raw for later wrapping at render time.
310+func styledLines(text string, style lipgloss.Style) []string {
311+ raw := strings.Split(text, "\n")
312+ out := make([]string, len(raw))
313+ for i, l := range raw {
339314 out[i] = style.Render(l)
340315 }
341- return out, m
316+ return out
342317 }
343318
344-// appendConv appends lines and returns whether we were at bottom (for sticky scroll).
345-func (m *Model) appendConv(lines ...string) bool {
319+// appendConv appends raw styled entries and returns whether we were at bottom
320+// (for sticky scroll). Invalidates the wrapped-lines cache.
321+func (m *Model) appendConv(entries ...string) bool {
346322 atBottom := m.scrollOffset == 0
347- m.conversation = append(m.conversation, lines...)
323+ m.convEntries = append(m.convEntries, entries...)
324+ m.convLines = nil // invalidate cache
348325 return atBottom
349326 }
350327
328+// wrappedConvLines returns the conversation wrapped to the current convWidth.
329+// Cached — only recomputed when entries change (nil) or width changes.
330+func (m *Model) wrappedConvLines() []string {
331+ w := m.convWidth()
332+ if m.convLines != nil && m.convCachedW == w {
333+ return m.convLines
334+ }
335+ m.convCachedW = w
336+ lines := make([]string, 0, len(m.convEntries))
337+ for _, entry := range m.convEntries {
338+ if entry == "" {
339+ lines = append(lines, "")
340+ } else {
341+ lines = append(lines, wrapANSI(entry, w)...)
342+ }
343+ }
344+ m.convLines = lines
345+ return m.convLines
346+}
347+
351348 // makeSeparator builds a timestamp separator line.
352349 func (m Model) makeSeparator(dur string, ts string) string {
353350 label := dur + " " + ts + " "
@@ -419,9 +416,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
419416 m.history = append(m.history, provider.Message{
420417 Role: "user", Content: msg.content, CreatedAt: now,
421418 })
422- lines, m2 := m.wrapText(msg.content, m.styles.Text)
423- m = m2
424- m.appendConv(lines...)
419+ m.appendConv(styledLines(msg.content, m.styles.Text)...)
425420 m.appendConv("")
426421 sep := m.makeSeparator("0s", now.Format("15:04:05"))
427422 wasBottom := m.appendConv(sep)
@@ -437,27 +432,22 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
437432
438433 case llmAssistantMsg:
439434 if msg.reasoning != "" {
440- lines, m2 := m.wrapText(msg.reasoning, m.styles.Muted)
441- m = m2
442- wasBottom := m.appendConv(lines...)
435+ wasBottom := m.appendConv(styledLines(msg.reasoning, m.styles.Muted)...)
443436 m.appendConv("")
444437 if wasBottom {
445438 m.scrollOffset = 0
446439 }
447440 }
448441 if msg.content != "" {
449- lines, m2 := m.wrapText(msg.content, m.styles.Text)
450- m = m2
451- wasBottom := m.appendConv(lines...)
442+ wasBottom := m.appendConv(styledLines(msg.content, m.styles.Text)...)
452443 m.appendConv("")
453444 if wasBottom {
454445 m.scrollOffset = 0
455446 }
456447 }
457448 for _, tc := range msg.toolCalls {
458- lines, m2 := m.wrapText(m.styles.ToolArrow.Render("→")+" "+m.styles.ToolCall.Render(tc.Name+"(...)"), lipgloss.NewStyle())
459- m = m2
460- wasBottom := m.appendConv(lines...)
449+ entry := m.styles.ToolArrow.Render("→") + " " + m.styles.ToolCall.Render(tc.Name+"(...)")
450+ wasBottom := m.appendConv(entry)
461451 if wasBottom {
462452 m.scrollOffset = 0
463453 }
@@ -465,9 +455,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
465455 return m, m.waitForLLMUpdate()
466456
467457 case llmToolResultMsg:
468- lines, m2 := m.wrapText(m.styles.ToolArrow.Render("←")+" "+m.styles.Dim.Render(msg.content), lipgloss.NewStyle())
469- m = m2
470- wasBottom := m.appendConv(lines...)
458+ entry := m.styles.ToolArrow.Render("←") + " " + m.styles.Dim.Render(msg.content)
459+ wasBottom := m.appendConv(entry)
471460 if wasBottom {
472461 m.scrollOffset = 0
473462 }
@@ -583,7 +572,8 @@ func (m Model) handleMouse(msg tea.MouseMsg) (tea.Model, tea.Cmd) {
583572 // --- Conversation: scroll + selection -----------------------------------
584573 if inRect(msg.X, msg.Y, m.layout.conv) {
585574 convH := m.layout.conv.Dy()
586- totalLines := len(m.conversation)
575+ lines := m.wrappedConvLines()
576+ totalLines := len(lines)
587577
588578 switch msg.Button {
589579 case tea.MouseButtonLeft:
@@ -612,7 +602,7 @@ func (m Model) handleMouse(msg tea.MouseMsg) (tea.Model, tea.Cmd) {
612602
613603 var sb strings.Builder
614604 for i := start; i <= end; i++ {
615- sb.WriteString(ansi.Strip(m.conversation[i]))
605+ sb.WriteString(ansi.Strip(lines[i]))
616606 if i < end {
617607 sb.WriteByte('\n')
618608 }
@@ -637,9 +627,10 @@ func (m Model) handleMouse(msg tea.MouseMsg) (tea.Model, tea.Cmd) {
637627 return m, tea.Batch(cmds...)
638628 }
639629
640-// visibleStartLine returns the index of the first visible conversation line.
641-func (m Model) visibleStartLine() int {
642- total := len(m.conversation)
630+// visibleStartLine returns the index of the first visible wrapped conversation line.
631+func (m *Model) visibleStartLine() int {
632+ lines := m.wrappedConvLines()
633+ total := len(lines)
643634 visible := m.layout.conv.Dy()
644635 if total <= visible {
645636 return 0
@@ -668,7 +659,8 @@ func (m Model) View() string {
668659 editorLines := strings.Split(m.editor.View(), "\n")
669660 inputLines := strings.Split(m.agentInput.View(), "\n")
670661
671- // Conversation visible window
662+ // Conversation visible window (wrapped to current width)
663+ convLines := m.wrappedConvLines()
672664 startLine := m.visibleStartLine()
673665
674666 bgFill := m.styles.BgFill
@@ -701,8 +693,8 @@ func (m Model) View() string {
701693 if relY < ly.conv.Dy() {
702694 // Conversation area
703695 lineIdx := startLine + relY
704- if lineIdx < len(m.conversation) {
705- line := m.conversation[lineIdx]
696+ if lineIdx < len(convLines) {
697+ line := convLines[lineIdx]
706698
707699 // Selection highlight
708700 isSelected := false
@@ -718,10 +710,6 @@ func (m Model) View() string {
718710 }
719711
720712 lw := lipgloss.Width(line)
721- if lw > rw {
722- line = ansi.Truncate(line, rw, "")
723- lw = lipgloss.Width(line)
724- }
725713 b.WriteString(line)
726714 if lw < rw {
727715 pad := strings.Repeat(" ", rw-lw)