@@ -2,6 +2,7 @@ package tui
22
33 import (
44 "context"
5+ "fmt"
56 "strings"
67 "time"
78
@@ -48,6 +49,15 @@ type Model struct {
4849 // Pane resize state
4950 resizingPane bool // True when dragging divider
5051 customHalfWidth int // Custom half width (0 = use default calculated value)
52+
53+ // Wrap cache to avoid re-wrapping unchanged text
54+ wrapCache map[string][]string
55+ wrapCacheKey string // Key for current conversation state
56+
57+ // Pre-allocated styles to avoid repeated allocations
58+ borderStyle lipgloss.Style
59+ selectionStyle lipgloss.Style
60+ conversationWidth int // Track width for style invalidation
5161 }
5262
5363 // New creates a new TUI model
@@ -82,17 +92,20 @@ func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool) Model {
8292 ctx, cancel := context.WithCancel(context.Background())
8393
8494 return Model{
85- spinner: s,
86- editor: editor,
87- agentInput: agentInput,
88- provider: prov,
89- mcpProxy: proxy,
90- mcpTools: tools,
91- history: []provider.Message{},
92- conversation: []string{},
93- updateChan: updateChan,
94- ctx: ctx,
95- cancel: cancel,
95+ spinner: s,
96+ editor: editor,
97+ agentInput: agentInput,
98+ provider: prov,
99+ mcpProxy: proxy,
100+ mcpTools: tools,
101+ history: []provider.Message{},
102+ conversation: []string{},
103+ updateChan: updateChan,
104+ ctx: ctx,
105+ cancel: cancel,
106+ wrapCache: make(map[string][]string),
107+ borderStyle: lipgloss.NewStyle().Foreground(ColorBorder),
108+ selectionStyle: lipgloss.NewStyle().Background(ColorDarkGray),
96109 }
97110 }
98111
@@ -103,7 +116,7 @@ func (m Model) Init() tea.Cmd {
103116
104117 // wrapText wraps text to the conversation pane width, applying style after wrapping.
105118 // Returns lines that fit within the pane width.
106-func (m Model) wrapText(text string, style lipgloss.Style) []string {
119+func (m *Model) wrapText(text string, style lipgloss.Style) []string {
107120 if m.rightPaneWidth <= 0 {
108121 // Fallback if width not set yet
109122 lines := strings.Split(text, "\n")
@@ -114,6 +127,14 @@ func (m Model) wrapText(text string, style lipgloss.Style) []string {
114127 return result
115128 }
116129
130+ // Create cache key from text and width
131+ cacheKey := fmt.Sprintf("%d:%s", m.rightPaneWidth, text)
132+
133+ // Check cache
134+ if cached, ok := m.wrapCache[cacheKey]; ok {
135+ return cached
136+ }
137+
117138 // Word wrap first, then apply style to each line
118139 // ansi.Wordwrap preserves ANSI codes and handles wide characters
119140 wrapped := ansi.Wordwrap(text, m.rightPaneWidth, "")
@@ -124,6 +145,13 @@ func (m Model) wrapText(text string, style lipgloss.Style) []string {
124145 for i, line := range lines {
125146 result[i] = style.Render(line)
126147 }
148+
149+ // Cache result (limit cache size to prevent memory issues)
150+ if len(m.wrapCache) > 1000 {
151+ m.wrapCache = make(map[string][]string)
152+ }
153+ m.wrapCache[cacheKey] = result
154+
127155 return result
128156 }
129157
@@ -411,18 +439,18 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
411439 CreatedAt: now,
412440 })
413441 // Format and display user message first
414- grayStyle := lipgloss.NewStyle().Foreground(ColorGray)
442+ grayStyle := lipgloss.NewStyle().Foreground(lipgloss.NoColor{})
415443 wrappedLines := m.wrapText(msg.content, grayStyle)
416444 m.conversation = append(m.conversation, wrappedLines...)
417445 m.conversation = append(m.conversation, "")
418446 // Add timestamp separator after message
419- timestamp := now.Format("15:04")
447+ timestamp := now.Format("15:04:05")
420448 rightPaneWidth := m.width/2 - 1
421449 dashCount := rightPaneWidth - len("0s "+timestamp+" ")
422450 if dashCount < 0 {
423451 dashCount = 0
424452 }
425- separator := lipgloss.NewStyle().Foreground(ColorGray).Render(
453+ separator := lipgloss.NewStyle().Foreground(ColorDarkGray).Render(
426454 "0s " + timestamp + " " + strings.Repeat("─", dashCount),
427455 )
428456 m.conversation = append(m.conversation, separator)
@@ -473,7 +501,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
473501 case llmErrorMsg:
474502 // Display error message
475503 m.conversation = append(m.conversation, "")
476- errLine := lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000")).Render("Error: " + msg.err.Error())
504+ errLine := lipgloss.NewStyle().Foreground(ColorError).Render("Error: " + msg.err.Error())
477505 m.conversation = append(m.conversation, errLine)
478506 m.conversation = append(m.conversation, "")
479507 return m, nil // Done listening
@@ -483,11 +511,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
483511 m.conversation = append(m.conversation, "")
484512 durationStr := msg.duration.Round(time.Second).String()
485513 rightPaneWidth := m.width/2 - 1
486- dashCount := rightPaneWidth - len(durationStr+" "+msg.timestamp+" ")
514+ dashCount := rightPaneWidth - len(durationStr+" "+msg.timestamp+" ") // TODO: Using `leg` should be using lipgloss
487515 if dashCount < 0 {
488516 dashCount = 0
489517 }
490- separator := lipgloss.NewStyle().Foreground(ColorGray).Render(
518+ separator := lipgloss.NewStyle().Foreground(ColorDarkGray).Render(
491519 durationStr + " " + msg.timestamp + " " + strings.Repeat("─", dashCount),
492520 )
493521 m.conversation = append(m.conversation, separator)
@@ -568,7 +596,6 @@ func (m Model) View() string {
568596 contentHeight := m.height - 2
569597
570598 var b strings.Builder
571- borderStyle := lipgloss.NewStyle().Foreground(ColorBorder)
572599
573600 // Render editor in left pane
574601 editorView := m.editor.View()
@@ -578,6 +605,10 @@ func (m Model) View() string {
578605 agentInputView := m.agentInput.View()
579606 agentInputLines := strings.Split(agentInputView, "\n")
580607
608+ // Calculate row boundaries
609+ inputStartRow := contentHeight - 3 // 3 rows for input
610+ separatorRow := contentHeight - 4 // Separator before input
611+
581612 // Content rows - use the stored rightPaneWidth from model
582613 for i := 0; i < contentHeight; i++ {
583614 // Left pane: editor
@@ -593,17 +624,10 @@ func (m Model) View() string {
593624 } else {
594625 b.WriteString(strings.Repeat(" ", halfWidth))
595626 }
596-
597- // Middle divider (or T connection on separator row)
598- separatorRow := contentHeight - 4 // 1 row for separator
599- if i == separatorRow {
600- b.WriteString(borderStyle.Render("├"))
601- } else {
602- b.WriteString(borderStyle.Render("│"))
603- }
627+ // Divider
628+ b.WriteString(m.borderStyle.Render("│"))
604629
605630 // Right pane: conversation area (top), separator, input at bottom
606- inputStartRow := contentHeight - 3 // 3 rows for input
607631 if i < separatorRow {
608632 // Conversation area - show conversation log
609633 // Calculate which line to show (scroll based on offset)
@@ -637,8 +661,7 @@ func (m Model) View() string {
637661
638662 // Apply selection highlighting if selected
639663 if isSelected {
640- selStyle := lipgloss.NewStyle().Background(lipgloss.Color("#444444"))
641- line = selStyle.Render(line)
664+ line = m.selectionStyle.Render(line)
642665 }
643666
644667 lineWidth := lipgloss.Width(line)
@@ -647,8 +670,7 @@ func (m Model) View() string {
647670 if lineWidth < m.rightPaneWidth {
648671 padding := strings.Repeat(" ", m.rightPaneWidth-lineWidth)
649672 if isSelected {
650- selStyle := lipgloss.NewStyle().Background(lipgloss.Color("#444444"))
651- padding = selStyle.Render(padding)
673+ padding = m.selectionStyle.Render(padding)
652674 }
653675 b.WriteString(padding)
654676 }
@@ -657,7 +679,7 @@ func (m Model) View() string {
657679 }
658680 } else if i == separatorRow {
659681 // Separator line
660- b.WriteString(borderStyle.Render(strings.Repeat("─", m.rightPaneWidth)))
682+ b.WriteString(m.borderStyle.Render(strings.Repeat("─", m.rightPaneWidth)))
661683 } else {
662684 // Input area (last 3 rows)
663685 lineIdx := i - inputStartRow
@@ -679,9 +701,9 @@ func (m Model) View() string {
679701 }
680702
681703 // Status separator: ───...───┴───...───
682- b.WriteString(borderStyle.Render(strings.Repeat("─", halfWidth)))
683- b.WriteString(borderStyle.Render("┴"))
684- b.WriteString(borderStyle.Render(strings.Repeat("─", m.width-halfWidth-1)))
704+ b.WriteString(m.borderStyle.Render(strings.Repeat("─", halfWidth)))
705+ b.WriteString(m.borderStyle.Render("┴"))
706+ b.WriteString(m.borderStyle.Render(strings.Repeat("─", m.width-halfWidth-1)))
685707 b.WriteString("\n")
686708
687709 // Status bar: master* │<spaces>spinner