action_selector.go (1309B)
1 package main 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/charmbracelet/bubbles/list" 8 tea "github.com/charmbracelet/bubbletea" 9 ) 10 11 func newActionSelectorModel() list.Model { 12 items := []list.Item{ 13 actionItem{"code %s", "Open in Visual Studio Code"}, 14 actionItem{"open %s", "Open in Finder"}, 15 actionItem{`cd %s; exec "${SHELL:-sh}"`, "Start new terminal session in project directory"}, 16 } 17 18 model := list.New(items, actionItemDelegate{}, defaultWidth, listHeight) 19 model.Title = "Actions" 20 setupList(&model) 21 22 return model 23 } 24 25 type actionItem struct { 26 command string 27 label string 28 } 29 30 func (i actionItem) FilterValue() string { 31 return i.label 32 } 33 34 type actionItemDelegate struct{} 35 36 func (d actionItemDelegate) Height() int { 37 return 1 38 } 39 40 func (d actionItemDelegate) Spacing() int { 41 return 0 42 } 43 44 func (d actionItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { 45 return nil 46 } 47 48 func (d actionItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) { 49 fmt.Fprint(w, d.render(m, index, listItem)) 50 } 51 52 func (d actionItemDelegate) render(m list.Model, index int, listItem list.Item) string { 53 action, ok := listItem.(actionItem) 54 if !ok { 55 return "" 56 } 57 58 if index == m.Index() { 59 return selectedItemStyle.Render("> " + action.label) 60 } else { 61 return itemStyle.Render(action.label) 62 } 63 }