cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / ui / palette.go
palette.go 1.9K
1package ui23import (4 "cmp"5 "slices"67 tea "charm.land/bubbletea/v2"89 "src.rest/cat/012/internal/ui/picker"10)1112// The command palette ("Search the menus" in Sheets, Alt+/) finds any13// registered command by fuzzy matching its title and menu path, and shows14// its shortcut. The function list uses the same picker.1516func init() {17 register(&command{id: "palette", macro: macroNever, title: "Search the menus", desc: "Find and run any command by name", run: func(m *Model) tea.Cmd {18 m.openOverlay(m.newPicker("Search the menus", "Type a command, e.g. save or width", 76, paletteItems(m)))19 return nil20 }})21 for _, k := range []string{"alt+/", "ctrl+k", "ctrl+shift+p"} {22 keymap[k] = "palette"23 }24}2526// paletteItems lists every command: those in menus first, in menu order27// with their menu path, then the rest by title.28func paletteItems(m *Model) []picker.Item {29 var items []picker.Item30 seen := map[string]bool{"palette": true}31 add := func(id, path string) {32 c := commands[id]33 if seen[id] || c.hidden != nil && c.hidden(m) {34 return35 }36 seen[id] = true37 items = append(items, picker.Item{38 Title: c.title, Name: len(c.title), Detail: path, Key: m.shortcut(id), Desc: c.desc,39 Off: !c.available(m), Pick: func() tea.Cmd { return m.runFromOverlay(id) },40 })41 }42 var walk func(items []menuItem, path string)43 walk = func(items []menuItem, path string) {44 for _, it := range items {45 switch {46 case it.items != nil:47 walk(it.items, path+" › "+it.label())48 case !it.sep:49 add(it.cmd, path)50 }51 }52 }53 for _, d := range menuBar {54 walk(visibleItems(d.items), d.title)55 }56 items = append(items, macroRunItems(m)...)57 rest := make([]string, 0, len(commands))58 for id := range commands {59 rest = append(rest, id)60 }61 slices.SortFunc(rest, func(a, b string) int { return cmp.Compare(commands[a].title, commands[b].title) })62 for _, id := range rest {63 add(id, "")64 }65 return items66}