package tui import ( "slices" "charm.land/bubbles/v2/textinput" tea "charm.land/bubbletea/v2" ) // agentFind is the canvas's search. Typing on the canvas writes to the agent, // so the search opens on ctrl+f: each key moves the selection to the best // match, opening the fold of a closed agent it finds, and dims the rest. // enter keeps the agent found, esc goes back to the one selected before. type agentFind struct { was string input textinput.Model open bool } func (f agentFind) active() bool { return f.open } // matches is the agents the query finds, or nil when it finds everything. func (f agentFind) matches(m *AgentsViewModel) map[string]bool { words := searchWords(f.input.Value()) if !f.open || len(words) == 0 { return nil } found := map[string]bool{} for id, n := range m.nodes { if id == agentsYou { continue } if _, _, ok := matchFields(words, n.name, n.address, n.model); ok { found[id] = true } } return found } // best is the agent the query finds best, the first shown among equals. func (f agentFind) best(m *AgentsViewModel) string { words := searchWords(f.input.Value()) best, bestRank := "", matchRank{} for _, id := range m.order { n := m.nodes[id] if n == nil || id == agentsYou { continue } rank, _, ok := matchFields(words, n.name, n.address, n.model) if ok && (best == "" || rank.compare(bestRank) > 0) { best, bestRank = id, rank } } return best } func (m *AgentsViewModel) openFind() tea.Cmd { m.find = agentFind{input: newField(), was: m.selected, open: true} m.find.input.Placeholder = "an agent's name or model" return tea.Batch(m.find.input.Focus(), m.hold()) } func (m *AgentsViewModel) closeFind(keep bool) { if !keep { m.selected = m.find.was } m.find = agentFind{} m.layout() } // findKey takes every key while the search is open. func (m AgentsViewModel) findKey(msg tea.KeyPressMsg) (AgentsViewModel, tea.Cmd) { switch s := msg.String(); s { case "esc", "ctrl+c": m.closeFind(false) return m, m.seedCmd() case "enter": m.closeFind(true) return m, m.seedCmd() case "tab", "down", "ctrl+n": m.cycleMatches(1) return m, m.seedCmd() case "shift+tab", "up", "ctrl+p": m.cycleMatches(-1) return m, m.seedCmd() } query := m.find.input.Value() var cmd tea.Cmd m.find.input, cmd = m.find.input.Update(msg) if m.find.input.Value() == query { return m, cmd } m.layout() if best := m.find.best(&m); best != "" { m.selected = best } return m, tea.Batch(cmd, m.seedCmd()) } // cycleMatches moves to the next agent the query finds, in the order shown. func (m *AgentsViewModel) cycleMatches(d int) { matches := m.find.matches(m) if matches == nil { m.cycle(d) return } found := slices.DeleteFunc(slices.Clone(m.order), func(id string) bool { return !matches[id] }) if n := len(found); n > 0 { i := slices.Index(found, m.selected) if i < 0 && d < 0 { i = 0 } m.selected = found[(i+d+n)%n] } } // findLine is the prompt row while the search is open. func (m AgentsViewModel) findLine() string { line := DefaultStyles.Prompt.Render("find ") + m.find.input.View() if m.find.input.Value() != "" && len(m.find.matches(&m)) == 0 { line += DefaultStyles.Faint.Render(" no agent matches") } return line }