tui: the agents page keeps the row picked in view, however many agents are installed

What changed
- viewList drew every agent and left the window to the terminal: on a
  machine with more agents than the terminal has rows the highlight
  scrolled off the top and the page froze — pressing j changed the row
  while the screen stood still, the same disease the library page had
  (#2220). The rows around the one picked are drawn now, as many as fit
  (the providers page's window).

Run
- TestAgentsViewKeepsTheRowPicked (new): 30 agents on a plain 80x24
  terminal, the row picked at 0, 4, 8, 15, 22 and 29 stays on screen,
  and a tall terminal still shows the list's top. Red at 422d057d
  ("row 0 (agent-00) picked, but the screen stops short of it",
  agents 06-29 with no highlight), green after; red again with only
  internal/tui/tui.go stashed. internal/tui and internal/library whole
  (ok, -tags nogui -p 2). gofmt clean; vet darwin, linux -tags nogui
  and windows = 0; build darwin, linux -tags nogui and windows = 0
  (trayimage_darwin.go's cgo -Wenum-conversion warning as ever).

Generated-by: GLM-5.3-Flash (ZCode)
This commit is contained in:
ggbdpq
2026-10-16 00:44:19 +08:00
committed by yetone
parent 3960dd2d5d
commit fed24aa1d6
2 changed files with 56 additions and 1 deletions
+11 -1
View File
@@ -852,7 +852,17 @@ func (m model) viewList() string {
var b strings.Builder
b.WriteString(m.header())
b.WriteString("\n\n")
for i, a := range m.agents {
// the rows around the one picked, as many as fit — a machine with more
// agents than the terminal has rows would otherwise scroll the highlight
// off and freeze the page, as the library page did (#2220)
visible := max(3, m.h-7)
start := 0
if m.row >= visible {
start = m.row - visible + 1
}
end := min(len(m.agents), start+visible)
for i := start; i < end; i++ {
a := m.agents[i]
sel := i == m.row
marker := " "
name := sName.Render(padRight(a.Name, nameW))
+45
View File
@@ -2,6 +2,7 @@ package tui
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"slices"
@@ -10,6 +11,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/agentenv"
"github.com/yetone/magpie/internal/library"
"github.com/yetone/magpie/internal/provider"
@@ -411,3 +413,46 @@ func TestFirstRunShowsCatalogSync(t *testing.T) {
t.Fatal("S started a second sync while the first runs")
}
}
// The agents page keeps the row picked in view: a machine with more agents
// than the terminal has rows can't scroll the highlight off and freeze the
// page, as the library page did (#2220). What the terminal shows of a view
// taller than itself is its last h lines — the top scrolls away.
func TestAgentsViewKeepsTheRowPicked(t *testing.T) {
var agents []*agent.Agent
for i := 0; i < 30; i++ {
agents = append(agents, &agent.Agent{
ID: fmt.Sprintf("agent-%02d", i),
Name: fmt.Sprintf("agent-%02d", i),
Path: fmt.Sprintf("/opt/agent-%02d/bin/agent", i),
Fields: []agent.Field{{Key: "model", Label: "model"}},
})
}
m := model{w: 120, agents: agents, hidden: len(agents),
values: make([]map[string]string, len(agents)), failover: make([]string, len(agents))}
for i := range m.values {
m.values[i] = map[string]string{"model": ""}
}
screen := func(v string, h int) string {
lines := strings.Split(v, "\n")
if len(lines) > h {
lines = lines[len(lines)-h:]
}
return strings.Join(lines, "\n")
}
// a plain 80×24 terminal: thirty agents are nine rows past it
m.h = 24
for _, pick := range []int{0, 4, 8, 15, 22, 29} {
m.row = pick
v := screen(m.viewList(), m.h)
if !strings.Contains(v, "▸ "+agents[pick].Name) {
t.Fatalf("row %d (%s) picked, but the screen stops short of it:\n%s", m.row, agents[pick].Name, v)
}
}
// the page starts at its top while it all fits
m.h = 60
m.row = 29
if v := screen(m.viewList(), m.h); !strings.Contains(v, "agent-00") {
t.Fatalf("a tall terminal dropped the top of the list:\n%s", v)
}
}