@@ -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+}