tui: a group's page keeps the row picked and its header in view (#2452)

What changed
- viewGroup drew every member and every rule and left the rest to the
  terminal. A group with more of them than the terminal has rows (a
  whole provider's thirty models at 80x24) scrolled the header and the
  highlight off the top, and j/k moved a pick no one could see. The
  library page (#2220) and the agents page (#2428, fed24aa1d) had the
  same disease; this was the last page with a cursor and no window.
- groupWindow draws the rows around the pick, as ggbdpq's patch does: a
  pick among the models keeps the first rules below it, a pick among the
  rules keeps the models' tail above it, a short group is drawn whole.
- groupRows counts every other line the page draws, including the
  pattern lines, the classifier line and the "none" line, which the
  patch's flat h-10 left out. With h-10 the page still ran three lines
  past the terminal and its header scrolled away.

Run
- New TestGroupViewKeepsTheRowPicked: thirty models; one model with 24
  rules; both, with three patterns and an intent rule's classifier.
  Every pick at 24 and 40 rows must draw no more lines than a short
  group's page, keep "◉ magpie" on screen, and mark the pick's own
  numbered row. A tall terminal draws the whole group.
- Broken by hand: old routing.go goes red (pick 0 draws 41 lines,
  22 fit), the patch's h-10 goes red (25 lines), and leaving the
  patterns uncounted goes red (23). With the fix it's green.
- Other cursor pages were audited (agents, providers, groups, library,
  sessions, pickers). All are windowed; only the group page had none.
- gofmt -l clean; go vet -tags nogui for darwin, linux and windows.
- Full suite: go test -timeout 45m ./... at f1c91a99c: 65 packages ok, internal/gui red only in TestPanelJoinsFullScreenSpaces ("the panel shown doesn't take the keyboard"), the macOS 26.2 red filed as #2450 and red on main without this change.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
yetone
2026-10-16 06:32:59 +08:00
co-authored by ggbdpq
parent ba17c4f90d
commit 1d227a7eb9
2 changed files with 158 additions and 3 deletions
+48 -3
View File
@@ -626,7 +626,10 @@ func (m model) viewGroup() string {
}
b.WriteString(pad + " " + sMuted.Render(strings.Join(head, " · ")) + "\n\n")
b.WriteString(pad + " " + sFaint.Render("models · the first that can take a request gets it") + "\n")
for i, id := range g.Members {
hits := provider.PatternHits(g)
memFrom, memTo, ruleFrom, ruleTo := groupWindow(len(g.Members), len(g.Rules), m.gsel, m.groupRows(g, len(hits)))
for i := memFrom; i < memTo; i++ {
id := g.Members[i]
marker, name := " ", sText.Render(id)
if i == m.gsel {
marker, name = sCursor.Render("▸ "), sNameOn.Render(id)
@@ -641,7 +644,7 @@ func (m model) viewGroup() string {
}
// patterns find models in the catalog each time (#766): how many now,
// and one that finds none said so rather than left an empty list
if hits := provider.PatternHits(g); len(hits) > 0 {
if len(hits) > 0 {
b.WriteString("\n" + pad + " " + sFaint.Render("patterns · every model they match now, after those named") + "\n")
for _, h := range hits {
n := sMuted.Render(fmt.Sprintf(" · %d model%s", h.Models, plural(h.Models)))
@@ -655,7 +658,8 @@ func (m model) viewGroup() string {
if len(g.Rules) == 0 {
b.WriteString(pad + " " + sMuted.Render("none · n adds one") + "\n")
}
for i, r := range g.Rules {
for i := ruleFrom; i < ruleTo; i++ {
r := g.Rules[i]
marker, use := " ", sText.Render(r.Use)
if len(g.Members)+i == m.gsel {
marker, use = sCursor.Render("▸ "), sNameOn.Render(r.Use)
@@ -671,3 +675,44 @@ func (m model) viewGroup() string {
}
return strings.TrimRight(b.String(), "\n")
}
// groupRows is how many members and rules the group page has room for:
// the terminal's height less every other line View draws around them —
// the header, the group's line, the section titles, the patterns, the
// classifier, and the blank line, the status and the two lines of keys
// under them, as a short group's page has. A group with
// more than that would otherwise scroll its header and the row picked
// off the top and freeze the page, as the library page did (#2220) and
// the agents page before it (#2428).
func (m model) groupRows(g provider.Group, hits int) int {
fixed := 7 + 6 // above the members and between them and the rules; below the body
if hits > 0 {
fixed += 2 + hits
}
if len(g.Rules) == 0 {
fixed++
}
if slices.ContainsFunc(g.Rules, func(r provider.Rule) bool { return r.Intent != "" }) {
fixed += 2
}
return max(3, m.h-fixed)
}
// groupWindow picks the members [memFrom, memTo) and rules [ruleFrom,
// ruleTo) to draw in rows lines, the row picked among them: a pick among
// the members keeps the first rules below it as room allows, a pick
// among the rules keeps the members' tail above it. Members and rules
// share one cursor, sel, the rules counted after the members.
func groupWindow(members, rules, sel, rows int) (memFrom, memTo, ruleFrom, ruleTo int) {
if members+rules <= rows {
return 0, members, 0, rules
}
if sel < members {
memFrom = max(0, sel+1-rows)
memTo = min(members, memFrom+rows)
return memFrom, memTo, 0, min(rules, rows-(memTo-memFrom))
}
ruleTo = min(rules, sel-members+1)
ruleFrom = max(0, ruleTo-rows)
return max(0, members-(rows-(ruleTo-ruleFrom))), members, ruleFrom, ruleTo
}
+110
View File
@@ -0,0 +1,110 @@
package tui
import (
"fmt"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The group page keeps the row picked in view, and its header with it: a
// group with more members and rules than the terminal has rows can't
// scroll them off the top and freeze the page, as the library page did
// (#2220) and the agents page before it (#2428). The page takes no more
// lines than a short group's, which fills the terminal exactly; what a
// terminal shows of a view taller than itself is its last lines.
func TestGroupViewKeepsTheRowPicked(t *testing.T) {
home(t)
var members []string
for i := 0; i < 30; i++ {
members = append(members, fmt.Sprintf("p/m-%02d", i))
}
var rules []provider.Rule
for i := 0; i < 24; i++ {
rules = append(rules, provider.Rule{Use: "p/m-00", Agents: []string{fmt.Sprintf("agent-%02d", i)}})
}
// the reporter's group (#2452): a whole provider's thirty models, no rules
pageKeepsThePick(t, provider.Group{ID: "many", Name: "Many", Members: members})
// one model and many rules: a pick among the rules
pageKeepsThePick(t, provider.Group{ID: "ruled", Name: "Ruled", Members: members[:1], Rules: rules})
// both, with patterns and an intent rule's classifier line around them
withIntent := append([]provider.Rule{{Use: "p/m-01", Intent: "a quick question"}}, rules...)
pageKeepsThePick(t, provider.Group{ID: "full", Name: "Full", Members: members, Rules: withIntent, Classifier: "a/m",
Match: []string{"nothing-a-*", "nothing-b-*", "nothing-c-*"}})
}
func pageKeepsThePick(t *testing.T, g provider.Group) {
t.Helper()
if err := provider.SaveGroup(provider.Group{ID: "short", Name: "Short", Members: g.Members[:1]}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
for _, h := range []int{24, 40} {
short := openGroup(t, "short", h)
room := strings.Count(short.View(), "\n")
m := openGroup(t, g.ID, h)
for pick := 0; pick < len(g.Members)+len(g.Rules); pick++ {
m.gsel = pick
v := m.View()
if n := strings.Count(v, "\n"); n > room {
t.Fatalf("%s, %d rows: pick %d draws %d lines, a short group's page %d:\n%s", g.ID, h, pick, n, room, v)
}
if !strings.Contains(v, "◉ magpie") {
t.Fatalf("%s, %d rows: pick %d scrolled the header off:\n%s", g.ID, h, pick, v)
}
want := ""
if pick < len(g.Members) {
want = g.Members[pick]
} else {
want = g.Rules[pick-len(g.Members)].Use
}
line := ""
for _, l := range strings.Split(v, "\n") {
if strings.Contains(l, "▸ ") {
line = l
}
}
if !strings.Contains(line, fmt.Sprintf("%d ", pickNumber(g, pick))) || !strings.Contains(line, want) {
t.Fatalf("%s, %d rows: pick %d (%s) isn't the row marked (%q):\n%s", g.ID, h, pick, want, line, v)
}
}
}
// a terminal tall enough draws the whole group, from its first model
m := openGroup(t, g.ID, len(g.Members)+len(g.Rules)+len(g.Match)+20)
m.gsel = len(g.Members) + len(g.Rules) - 1
v := m.View()
if !strings.Contains(v, "1 "+g.Members[0]) || !strings.Contains(v, g.Members[len(g.Members)-1]) {
t.Fatalf("%s: a tall terminal left part of the group out:\n%s", g.ID, v)
}
}
// pickNumber is the number the page shows beside a pick: its place among
// the members, or among the rules.
func pickNumber(g provider.Group, pick int) int {
if pick < len(g.Members) {
return pick + 1
}
return pick - len(g.Members) + 1
}
// openGroup is the group page for id, on a terminal h rows tall.
func openGroup(t *testing.T, id string, h int) model {
t.Helper()
m := press(t, model{w: 120, h: h}, "3")
for i, g := range m.groups {
if g.ID == id {
m.grow = i
}
}
m = press(t, m, "enter")
if m.mode != modeGroup {
t.Fatalf("mode %d, want the group page", m.mode)
}
if g, _ := m.group(); g.ID != id {
t.Fatalf("opened %s, want %s", g.ID, id)
}
return m
}