Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125package 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}