v0.1.20 e52a560

skills_dialog.go RawHistoryFolder

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
141	const maxRows = 16
142	if d.scroll > len(bodyLines)-1 {
143		d.scroll = len(bodyLines) - 1
144	}
145	if d.scroll < 0 {
146		d.scroll = 0
147	}
148	end := d.scroll + maxRows
149	if end > len(bodyLines) {
150		end = len(bodyLines)
151	}
152	for _, line := range bodyLines[d.scroll:end] {
153		out = append(out, "    "+line)
154	}
155	if end < len(bodyLines) {
156		out = append(out, "  "+th.FG256(th.Muted, fmt.Sprintf("\u2193 %d more lines (down/pgdn)", len(bodyLines)-end)))
157	}
158	out = append(out, frameRule(th, width))
159	return out
160}
161
162func formatSkillRow(s *skills.Skill, maxWidth int) string {
163	left := fmt.Sprintf("%-20s  ", truncateLineSafe(s.Name, 20))
164	src := "  " + truncateLineSafe(s.Source, 16)
165	room := maxWidth - len(left) - len(src)
166	if room < 10 {
167		room = 10
168	}
169	desc := s.Description
170	if len(desc) > room {
171		desc = desc[:room-1] + "\u2026"
172	}
173	return left + desc + src
174}
175
176// truncateLineSafe limits s to n runes (not bytes) so multibyte
177// names + sources don't blow past the column.
178func truncateLineSafe(s string, n int) string {
179	r := []rune(s)
180	if len(r) <= n {
181		return s
182	}
183	if n <= 1 {
184		return "\u2026"
185	}
186	return string(r[:n-1]) + "\u2026"
187}
188
189// visibleWindow centers cursor in a window of size n within total
190// items. Returns [start, end) bounds.
191func visibleWindow(cursor, total, n int) (start, end int) {
192	if total <= n {
193		return 0, total
194	}
195	start = cursor - n/2
196	if start < 0 {
197		start = 0
198	}
199	end = start + n
200	if end > total {
201		end = total
202		start = end - n
203	}
204	return
205}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close