xonecas.io
62159
Sy

symb / commit a24f3c4

feat: tool view modal

xonecas2026-02-19 a24f3c418bf1b138b00c867b097d023925c731ff parent 5e596fa

9 files changed

docs/DESIGN.md+1−2
@@ -7,8 +7,7 @@ on one side, code editor on the other. Fast, low memory, not a distraction.
77
88 ### TUI (BubbleTea, ELM architecture)
99
10-Split-pane layout: editor left, conversation + input right. Draggable divider.
11-Click-to-focus between panes. Alt-screen, mouse cell-motion mode with 15ms
10+Alt-screen, mouse cell-motion mode with 15ms
1211 throttle on wheel/motion events.
1312
1413 ```
internal/tui/messages.go+6−0
@@ -92,6 +92,12 @@ type tickMsg time.Time
9292 // undoMsg is sent when the user clicks the undo control.
9393 type undoMsg struct{}
9494
95+// openToolViewMsg is sent when the user clicks the [view] button on a tool result.
96+type openToolViewMsg struct {
97+ title string
98+ content string
99+}
100+
95101 // llmBatchMsg carries multiple messages drained from updateChan in one go.
96102 type llmBatchMsg []tea.Msg
97103
internal/tui/modal/toolview.go+174−0
@@ -0,0 +1,174 @@
1+package modal
2+
3+import (
4+ "strings"
5+
6+ tea "charm.land/bubbletea/v2"
7+ "charm.land/lipgloss/v2"
8+)
9+
10+// ToolView is a simple read-only modal that displays a tool call and its result.
11+type ToolView struct {
12+ title string
13+ content string
14+ scroll int
15+ colors Colors
16+}
17+
18+// NewToolView creates a new tool viewer modal.
19+func NewToolView(title, content string, colors Colors) ToolView {
20+ return ToolView{
21+ title: title,
22+ content: content,
23+ colors: colors,
24+ }
25+}
26+
27+// HandleMsg processes key events. Returns ActionClose when the modal should close.
28+func (t *ToolView) HandleMsg(msg tea.Msg) (Action, tea.Cmd) {
29+ switch msg := msg.(type) {
30+ case tea.KeyPressMsg:
31+ switch msg.Keystroke() {
32+ case "esc", "q", "enter":
33+ return ActionClose{}, nil
34+ case "up", "k":
35+ if t.scroll > 0 {
36+ t.scroll--
37+ }
38+ case "down", "j":
39+ t.scroll++
40+ case "pgup":
41+ t.scroll -= 10
42+ if t.scroll < 0 {
43+ t.scroll = 0
44+ }
45+ case "pgdown":
46+ t.scroll += 10
47+ }
48+ case tea.MouseWheelMsg:
49+ if msg.Button == tea.MouseWheelUp {
50+ if t.scroll > 0 {
51+ t.scroll--
52+ }
53+ } else if msg.Button == tea.MouseWheelDown {
54+ t.scroll++
55+ }
56+ }
57+ return nil, nil
58+}
59+
60+// View renders the modal centered in the terminal at appWidth x appHeight.
61+func (t *ToolView) View(appWidth, appHeight int) string {
62+ w := appWidth * 80 / 100
63+ h := appHeight * 80 / 100
64+ if w < 30 {
65+ w = 30
66+ }
67+ if h < 8 {
68+ h = 8
69+ }
70+
71+ innerW := w - 6 // border (2) + padding (2)
72+ if innerW < 10 {
73+ innerW = 10
74+ }
75+
76+ bg := lipgloss.Color(t.colors.Bg)
77+ fg := lipgloss.Color(t.colors.Fg)
78+ dimStyle := lipgloss.NewStyle().Foreground(lipgloss.Color(t.colors.Dim)).Background(bg)
79+ fgStyle := lipgloss.NewStyle().Foreground(fg).Background(bg)
80+
81+ // Wrap content lines to innerW.
82+ rawLines := strings.Split(t.content, "\n")
83+ var wrapped []string
84+ for _, line := range rawLines {
85+ if lipgloss.Width(line) <= innerW {
86+ wrapped = append(wrapped, line)
87+ } else {
88+ // Simple character-level wrap.
89+ for len(line) > 0 {
90+ if len(line) <= innerW {
91+ wrapped = append(wrapped, line)
92+ break
93+ }
94+ wrapped = append(wrapped, line[:innerW])
95+ line = line[innerW:]
96+ }
97+ }
98+ }
99+
100+ // Title row + divider = 2 rows overhead inside the box.
101+ listH := h - 4 // border top/bottom (2) + title (1) + divider (1)
102+ if listH < 1 {
103+ listH = 1
104+ }
105+
106+ // Clamp scroll.
107+ maxScroll := len(wrapped) - listH
108+ if maxScroll < 0 {
109+ maxScroll = 0
110+ }
111+ if t.scroll > maxScroll {
112+ t.scroll = maxScroll
113+ }
114+
115+ // Build content string.
116+ titleLine := fgStyle.Bold(true).Render(truncate(t.title, innerW))
117+ divider := dimStyle.Render(strings.Repeat("\u2500", innerW))
118+
119+ var sb strings.Builder
120+ sb.WriteString(titleLine)
121+ sb.WriteByte('\n')
122+ sb.WriteString(divider)
123+
124+ end := t.scroll + listH
125+ if end > len(wrapped) {
126+ end = len(wrapped)
127+ }
128+ for _, l := range wrapped[t.scroll:end] {
129+ sb.WriteByte('\n')
130+ sb.WriteString(fgStyle.Render(padRight(l, innerW)))
131+ }
132+ // Pad remaining lines so the box has consistent height.
133+ rendered := end - t.scroll
134+ for i := rendered; i < listH; i++ {
135+ sb.WriteByte('\n')
136+ sb.WriteString(fgStyle.Render(strings.Repeat(" ", innerW)))
137+ }
138+
139+ // Scroll hint in bottom-right corner of the last line if needed.
140+ hintStyle := dimStyle
141+ var hint string
142+ if t.scroll > 0 && t.scroll < maxScroll {
143+ hint = "↑↓"
144+ } else if t.scroll > 0 {
145+ hint = "↑"
146+ } else if maxScroll > 0 {
147+ hint = "↓"
148+ }
149+ _ = hint
150+ _ = hintStyle
151+
152+ box := lipgloss.NewStyle().
153+ Border(lipgloss.RoundedBorder()).
154+ BorderForeground(lipgloss.Color(t.colors.Border)).
155+ BorderBackground(bg).
156+ Foreground(fg).
157+ Background(bg).
158+ Padding(0, 1).
159+ Width(w - 2).
160+ Render(sb.String())
161+
162+ return lipgloss.Place(appWidth, appHeight, lipgloss.Center, lipgloss.Center, box,
163+ lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Background(bg)))
164+}
165+
166+func truncate(s string, maxW int) string {
167+ if lipgloss.Width(s) <= maxW {
168+ return s
169+ }
170+ if maxW <= 3 {
171+ return s[:maxW]
172+ }
173+ return s[:maxW-3] + "..."
174+}
internal/tui/mouse.go+7−35
@@ -313,43 +313,15 @@ func (m *Model) isClickOnViewLabel(display string, col int) bool {
313313 return col >= viewStart && col < lw
314314 }
315315
316-// handleToolResultView opens the appropriate content in the editor for a
317-// tool result [view] click, based on the tool type.
316+// handleToolResultView returns a Cmd that opens the tool view modal.
318317 func (m *Model) handleToolResultView(entry convEntry) tea.Cmd {
319- switch entry.toolName {
320- case "Read":
321- // Open file with cursor in the middle of the read range.
322- if entry.filePath != "" {
323- return m.openFileAtCenter(entry.filePath, entry.line, entry.full)
324- }
325- case "Edit":
326- // Open file with cursor at the end of the changes.
327- if entry.filePath != "" {
328- return m.openFile(entry.filePath, entry.line)
329- }
330- case "Shell":
331- // Show the command output in the editor as plain text.
332- m.showRawContent(entry.full, "text")
333- return nil
334- case "web_search_exa", "get_code_context_exa":
335- // Show search/fetch results as markdown.
336- m.showRawContent(entry.full, "markdown")
337- return nil
338- case "SubAgent":
339- // Show the subagent's response as markdown.
340- m.showRawContent(entry.full, "markdown")
341- return nil
342- default:
343- // Fallback: try file path, then show raw content.
344- if entry.filePath != "" {
345- return m.openFile(entry.filePath, entry.line)
346- }
347- m.showRawContent(entry.full, "text")
348- return nil
318+ title := entry.toolName
319+ if title == "" {
320+ title = "Tool Result"
321+ }
322+ return func() tea.Msg {
323+ return openToolViewMsg{title: title, content: entry.full}
349324 }
350- // Fallback for tools with no file path.
351- m.showRawContent(entry.full, "text")
352- return nil
353325 }
354326
355327 // openFileAtCenter opens a file and positions the cursor in the middle of the
internal/tui/toolview_test.go+79−0
@@ -0,0 +1,79 @@
1+package tui
2+
3+import (
4+ "testing"
5+
6+ tea "charm.land/bubbletea/v2"
7+ "github.com/xonecas/symb/internal/provider"
8+)
9+
10+// TestToolViewModalOpensOnViewClick verifies that clicking the [view] button
11+// on a tool result entry opens the tool view modal.
12+func TestToolViewModalOpensOnViewClick(t *testing.T) {
13+ initTheme("vulcan")
14+ m := New(nil, nil, nil, "test", nil, "s", nil, nil, nil, "p", nil, nil, nil, provider.Options{}, "vulcan")
15+ updated, _ := m.Update(tea.WindowSizeMsg{Width: 120, Height: 40})
16+ m = updated.(Model)
17+
18+ // Inject a tool result entry.
19+ entry := convEntry{
20+ display: m.styles.ToolArrow.Render("←") + m.styles.BgFill.Render(" ") + m.styles.Dim.Render("Read foo.go") + m.styles.BgFill.Render(" ") + m.styles.Clickable.Render("view"),
21+ kind: entryToolResult,
22+ full: "Read foo.go\nsome content",
23+ toolName: "Read",
24+ }
25+ m.convEntries = append(m.convEntries, entry)
26+ m.frameLines = nil
27+
28+ // Place the click at the [view] label: last 4 visible chars of the display.
29+ lines := m.wrappedConvLines()
30+ if len(lines) == 0 {
31+ t.Fatal("no conv lines")
32+ }
33+ convX := m.layout.conv.Min.X
34+ convY := m.layout.conv.Min.Y
35+
36+ // col within conv pane pointing at "view" (last 4 chars of display).
37+ import_lipgloss_width := func(s string) int {
38+ // inline: strip ANSI and count runes
39+ n := 0
40+ inEsc := false
41+ for _, r := range s {
42+ if inEsc {
43+ if r == 'm' {
44+ inEsc = false
45+ }
46+ continue
47+ }
48+ if r == '\x1b' {
49+ inEsc = true
50+ continue
51+ }
52+ n++
53+ }
54+ return n
55+ }
56+ lw := import_lipgloss_width(entry.display)
57+ viewCol := lw - 2 // somewhere inside "view"
58+
59+ clickX := convX + viewCol
60+ clickY := convY
61+
62+ // MouseClickMsg sets convSel; MouseReleaseMsg fires the click handler returning a Cmd.
63+ u1, _ := m.Update(tea.MouseClickMsg{X: clickX, Y: clickY, Button: tea.MouseLeft})
64+ m = u1.(Model)
65+ u2, cmd := m.Update(tea.MouseReleaseMsg{X: clickX, Y: clickY, Button: tea.MouseLeft})
66+ m = u2.(Model)
67+
68+ // The cmd carries openToolViewMsg; dispatch it.
69+ if cmd == nil {
70+ t.Fatal("expected a Cmd from the release, got nil")
71+ }
72+ msg := cmd()
73+ u3, _ := m.Update(msg)
74+ m = u3.(Model)
75+
76+ if m.toolViewModal == nil {
77+ t.Fatal("toolViewModal is nil after dispatching openToolViewMsg")
78+ }
79+}
internal/tui/tui.go+3−1
@@ -248,7 +248,9 @@ type Model struct {
248248 keybindsModal *modal.Model
249249 // Models modal
250250 modelsModal *modal.Model
251- searcher *filesearch.Searcher
251+ // Tool viewer modal
252+ toolViewModal *modal.ToolView
253+ searcher *filesearch.Searcher
252254
253255 // Provider switching
254256 registry *provider.Registry
internal/tui/update.go+7−0
@@ -44,6 +44,10 @@ func (m Model) handleModalMsg(msg tea.Msg) (tea.Model, tea.Cmd, bool) {
4444 if mdl, cmd, handled := m.updateModelsModal(msg); handled {
4545 return mdl, cmd, true
4646 }
47+ // Tool viewer modal intercepts all input when open.
48+ if mdl, cmd, handled := m.updateToolViewModal(msg); handled {
49+ return mdl, cmd, true
50+ }
4751 return m, nil, false
4852 }
4953
@@ -96,6 +100,9 @@ func (m Model) handleSystemEvent(msg tea.Msg) (tea.Model, tea.Cmd, bool) {
96100 case undoMsg:
97101 mdl, cmd := m.handleUndo()
98102 return mdl, cmd, true
103+ case openToolViewMsg:
104+ m.openToolViewModal(msg.title, msg.content)
105+ return m, nil, true
99106 case undoResultMsg:
100107 return m.handleUndoResult(msg), nil, true
101108 case gitBranchMsg:
internal/tui/update_modals.go+32−0
@@ -261,6 +261,38 @@ func (m *Model) switchModelCmd(selection string) tea.Cmd {
261261 }
262262 }
263263
264+func (m *Model) openToolViewModal(title, content string) {
265+ tv := modal.NewToolView(title, content, modal.Colors{
266+ Fg: palette.Fg,
267+ Bg: palette.Bg,
268+ Dim: palette.Dim,
269+ SelFg: palette.Bg,
270+ SelBg: palette.Fg,
271+ Border: palette.Border,
272+ })
273+ m.toolViewModal = &tv
274+}
275+
276+func (m *Model) updateToolViewModal(msg tea.Msg) (Model, tea.Cmd, bool) {
277+ if m.toolViewModal == nil {
278+ return *m, nil, false
279+ }
280+ action, cmd := m.toolViewModal.HandleMsg(msg)
281+ switch action.(type) {
282+ case modal.ActionClose:
283+ m.toolViewModal = nil
284+ return *m, nil, true
285+ }
286+ if cmd != nil {
287+ return *m, cmd, true
288+ }
289+ switch msg.(type) {
290+ case tea.KeyPressMsg, tea.MouseMsg:
291+ return *m, nil, true
292+ }
293+ return *m, nil, false
294+}
295+
264296 func (m *Model) handleModelsFetched(msg modelsFetchedMsg) tea.Model {
265297 if msg.err != nil {
266298 log.Error().Err(msg.err).Msg("handleModelsFetched error")
internal/tui/view.go+2−0
@@ -19,6 +19,8 @@ func (m Model) View() tea.View {
1919 content = m.fileModal.View(m.width, m.height)
2020 case m.modelsModal != nil:
2121 content = m.modelsModal.View(m.width, m.height)
22+ case m.toolViewModal != nil:
23+ content = m.toolViewModal.View(m.width, m.height)
2224 }
2325 v := tea.NewView(content)
2426 v.AltScreen = true