1package modes
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/patriceckhart/zot/internal/skills"
8 "github.com/patriceckhart/zot/internal/tui"
9)
10
11// skillsDialog lists every discovered skill and lets the user view
12// the body of one inline. View is read-only — the model loads skills
13// itself via the `skill` tool. This dialog is for inspection.
14type skillsDialog struct {
15 active bool
16 skills []*skills.Skill
17 cursor int
18 viewing *skills.Skill // when non-nil, render the body instead of the list
19 scroll int // body view scroll offset (in lines)
20}
21
22func newSkillsDialog() *skillsDialog { return &skillsDialog{} }
23
24// Open populates and shows the dialog with the given snapshot.
25func (d *skillsDialog) Open(s []*skills.Skill) {
26 d.active = true
27 d.skills = s
28 d.cursor = 0
29 d.viewing = nil
30 d.scroll = 0
31}
32
33// Close hides the dialog.
34func (d *skillsDialog) Close() { d.active = false }
35
36// Active reports whether the dialog is visible.
37func (d *skillsDialog) Active() bool { return d != nil && d.active }
38
39// HandleKey advances the dialog state.
40func (d *skillsDialog) HandleKey(k tui.Key) (closed bool) {
41 if !d.Active() {
42 return false
43 }
44
45 if d.viewing != nil {
46 // Body view keys.
47 switch k.Kind {
48 case tui.KeyEsc, tui.KeyEnter:
49 d.viewing = nil
50 d.scroll = 0
51 case tui.KeyUp:
52 if d.scroll > 0 {
53 d.scroll--
54 }
55 case tui.KeyDown:
56 d.scroll++
57 case tui.KeyPageUp:
58 d.scroll -= 8
59 if d.scroll < 0 {
60 d.scroll = 0
61 }
62 case tui.KeyPageDown:
63 d.scroll += 8
64 }
65 return false
66 }
67
68 // List view keys.
69 switch k.Kind {
70 case tui.KeyEsc:
71 d.Close()
72 return true
73 case tui.KeyUp:
74 if d.cursor > 0 {
75 d.cursor--
76 }
77 case tui.KeyDown:
78 if d.cursor < len(d.skills)-1 {
79 d.cursor++
80 }
81 case tui.KeyEnter:
82 if len(d.skills) > 0 {
83 d.viewing = d.skills[d.cursor]
84 d.scroll = 0
85 }
86 }
87 return false
88}
89
90// Render draws the picker or the body view.
91func (d *skillsDialog) Render(th tui.Theme, width int) []string {
92 if !d.Active() {
93 return nil
94 }
95
96 if d.viewing != nil {
97 return d.renderBody(th, width)
98 }
99
100 out := []string{frameHeader(th, "skills (enter to view, esc to close)", width)}
101 if len(d.skills) == 0 {
102 out = append(out, " "+th.FG256(th.Muted, "no skills found"))
103 out = append(out, " "+th.FG256(th.Muted, "see docs/skills.md to add some"))
104 out = append(out, frameRule(th, width))
105 return out
106 }
107
108 const maxRows = 12
109 start, end := visibleWindow(d.cursor, len(d.skills), maxRows)
110 if start > 0 {
111 out = append(out, " "+th.FG256(th.Muted, fmt.Sprintf("\u2191 %d more above", start)))
112 }
113 for i := start; i < end; i++ {
114 s := d.skills[i]
115 row := formatSkillRow(s, width-2)
116 if i == d.cursor {
117 out = append(out, th.PadHighlight(" "+row, width))
118 } else {
119 out = append(out, " "+th.FG256(th.Muted, row))
120 }
121 }
122 if end < len(d.skills) {
123 out = append(out, " "+th.FG256(th.Muted, fmt.Sprintf("\u2193 %d more below", len(d.skills)-end)))
124 }
125 out = append(out, frameRule(th, width))
126 return out
127}
128
129func (d *skillsDialog) renderBody(th tui.Theme, width int) []string {
130 s := d.viewing
131 out := []string{
132 frameHeader(th, "skill: "+s.Name+" (esc / enter to go back)", width),
133 " " + th.FG256(th.Muted, s.Description),
134 " " + th.FG256(th.Muted, "source: "+s.Source+" ("+s.Path+")"),
135 "",
136 }
137
138 rendered := tui.RenderMarkdown(s.Body, th, width-4)
139 bodyLines := strings.Split(rendered, "\n")
140 for i, l := range bodyLines {
141 if len(l) > 0 && l[0] == tui.FlushLeftSentinel {
142 bodyLines[i] = l[1:]
143 }
144 }
145
146 const maxRows = 16
147 if d.scroll > len(bodyLines)-1 {
148 d.scroll = len(bodyLines) - 1
149 }
150 if d.scroll < 0 {
151 d.scroll = 0
152 }
153 end := d.scroll + maxRows
154 if end > len(bodyLines) {
155 end = len(bodyLines)
156 }
157 for _, line := range bodyLines[d.scroll:end] {
158 out = append(out, " "+line)
159 }
160 if end < len(bodyLines) {
161 out = append(out, " "+th.FG256(th.Muted, fmt.Sprintf("\u2193 %d more lines (down/pgdn)", len(bodyLines)-end)))
162 }
163 out = append(out, frameRule(th, width))
164 return out
165}
166
167func formatSkillRow(s *skills.Skill, maxWidth int) string {
168 left := fmt.Sprintf("%-20s ", truncateLineSafe(s.Name, 20))
169 src := " " + truncateLineSafe(s.Source, 16)
170 room := maxWidth - len(left) - len(src)
171 if room < 10 {
172 room = 10
173 }
174 desc := s.Description
175 if len(desc) > room {
176 desc = desc[:room-1] + "\u2026"
177 }
178 return left + desc + src
179}
180
181// truncateLineSafe limits s to n runes (not bytes) so multibyte
182// names + sources don't blow past the column.
183func truncateLineSafe(s string, n int) string {
184 r := []rune(s)
185 if len(r) <= n {
186 return s
187 }
188 if n <= 1 {
189 return "\u2026"
190 }
191 return string(r[:n-1]) + "\u2026"
192}
193
194// visibleWindow centers cursor in a window of size n within total
195// items. Returns [start, end) bounds.
196func visibleWindow(cursor, total, n int) (start, end int) {
197 if total <= n {
198 return 0, total
199 }
200 start = cursor - n/2
201 if start < 0 {
202 start = 0
203 }
204 end = start + n
205 if end > total {
206 end = total
207 start = end - n
208 }
209 return
210}