|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + |
| 7 | + "github.com/charmbracelet/bubbles/list" |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + "github.com/charmbracelet/lipgloss" |
| 10 | + "github.com/hmans/beans/internal/config" |
| 11 | + "github.com/hmans/beans/internal/ui" |
| 12 | +) |
| 13 | + |
| 14 | +// typeSelectedMsg is sent when a type is selected from the picker |
| 15 | +type typeSelectedMsg struct { |
| 16 | + beanID string |
| 17 | + beanType string |
| 18 | +} |
| 19 | + |
| 20 | +// closeTypePickerMsg is sent when the type picker is cancelled |
| 21 | +type closeTypePickerMsg struct{} |
| 22 | + |
| 23 | +// openTypePickerMsg requests opening the type picker for a bean |
| 24 | +type openTypePickerMsg struct { |
| 25 | + beanID string |
| 26 | + currentType string |
| 27 | +} |
| 28 | + |
| 29 | +// typeItem wraps a type to implement list.Item |
| 30 | +type typeItem struct { |
| 31 | + name string |
| 32 | + description string |
| 33 | + color string |
| 34 | + isCurrent bool |
| 35 | +} |
| 36 | + |
| 37 | +func (i typeItem) Title() string { return i.name } |
| 38 | +func (i typeItem) Description() string { return i.description } |
| 39 | +func (i typeItem) FilterValue() string { return i.name + " " + i.description } |
| 40 | + |
| 41 | +// typeItemDelegate handles rendering of type picker items |
| 42 | +type typeItemDelegate struct{} |
| 43 | + |
| 44 | +func (d typeItemDelegate) Height() int { return 1 } |
| 45 | +func (d typeItemDelegate) Spacing() int { return 0 } |
| 46 | +func (d typeItemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil } |
| 47 | + |
| 48 | +func (d typeItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) { |
| 49 | + item, ok := listItem.(typeItem) |
| 50 | + if !ok { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + var cursor string |
| 55 | + if index == m.Index() { |
| 56 | + cursor = lipgloss.NewStyle().Foreground(ui.ColorPrimary).Bold(true).Render("▌") + " " |
| 57 | + } else { |
| 58 | + cursor = " " |
| 59 | + } |
| 60 | + |
| 61 | + // Render type with color |
| 62 | + typeText := ui.RenderTypeText(item.name, item.color) |
| 63 | + |
| 64 | + // Add current indicator |
| 65 | + var currentIndicator string |
| 66 | + if item.isCurrent { |
| 67 | + currentIndicator = ui.Muted.Render(" (current)") |
| 68 | + } |
| 69 | + |
| 70 | + fmt.Fprint(w, cursor+typeText+currentIndicator) |
| 71 | +} |
| 72 | + |
| 73 | +// typePickerModel is the model for the type picker view |
| 74 | +type typePickerModel struct { |
| 75 | + list list.Model |
| 76 | + beanID string |
| 77 | + currentType string |
| 78 | + width int |
| 79 | + height int |
| 80 | +} |
| 81 | + |
| 82 | +func newTypePickerModel(beanID, currentType string, cfg *config.Config, width, height int) typePickerModel { |
| 83 | + // Get all types (hardcoded in config package) |
| 84 | + types := config.DefaultTypes |
| 85 | + |
| 86 | + delegate := typeItemDelegate{} |
| 87 | + |
| 88 | + // Build items list |
| 89 | + items := make([]list.Item, 0, len(types)) |
| 90 | + selectedIndex := 0 |
| 91 | + |
| 92 | + for i, t := range types { |
| 93 | + isCurrent := t.Name == currentType |
| 94 | + if isCurrent { |
| 95 | + selectedIndex = i |
| 96 | + } |
| 97 | + items = append(items, typeItem{ |
| 98 | + name: t.Name, |
| 99 | + description: t.Description, |
| 100 | + color: t.Color, |
| 101 | + isCurrent: isCurrent, |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + // Calculate modal dimensions |
| 106 | + modalWidth := max(40, min(60, width*50/100)) |
| 107 | + modalHeight := max(10, min(16, height*50/100)) |
| 108 | + listWidth := modalWidth - 6 |
| 109 | + listHeight := modalHeight - 7 |
| 110 | + |
| 111 | + l := list.New(items, delegate, listWidth, listHeight) |
| 112 | + l.Title = "Select Type" |
| 113 | + l.SetShowStatusBar(false) |
| 114 | + l.SetFilteringEnabled(true) |
| 115 | + l.SetShowHelp(false) |
| 116 | + l.SetShowPagination(false) |
| 117 | + l.Styles.Title = listTitleStyle |
| 118 | + l.Styles.TitleBar = lipgloss.NewStyle().Padding(0, 0, 0, 0) |
| 119 | + l.Styles.FilterPrompt = lipgloss.NewStyle().Foreground(ui.ColorPrimary) |
| 120 | + l.Styles.FilterCursor = lipgloss.NewStyle().Foreground(ui.ColorPrimary) |
| 121 | + |
| 122 | + // Select the current type |
| 123 | + if selectedIndex < len(items) { |
| 124 | + l.Select(selectedIndex) |
| 125 | + } |
| 126 | + |
| 127 | + return typePickerModel{ |
| 128 | + list: l, |
| 129 | + beanID: beanID, |
| 130 | + currentType: currentType, |
| 131 | + width: width, |
| 132 | + height: height, |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (m typePickerModel) Init() tea.Cmd { |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +func (m typePickerModel) Update(msg tea.Msg) (typePickerModel, tea.Cmd) { |
| 141 | + var cmd tea.Cmd |
| 142 | + |
| 143 | + switch msg := msg.(type) { |
| 144 | + case tea.WindowSizeMsg: |
| 145 | + m.width = msg.Width |
| 146 | + m.height = msg.Height |
| 147 | + modalWidth := max(40, min(60, msg.Width*50/100)) |
| 148 | + modalHeight := max(10, min(16, msg.Height*50/100)) |
| 149 | + listWidth := modalWidth - 6 |
| 150 | + listHeight := modalHeight - 7 |
| 151 | + m.list.SetSize(listWidth, listHeight) |
| 152 | + |
| 153 | + case tea.KeyMsg: |
| 154 | + if m.list.FilterState() != list.Filtering { |
| 155 | + switch msg.String() { |
| 156 | + case "enter": |
| 157 | + if item, ok := m.list.SelectedItem().(typeItem); ok { |
| 158 | + return m, func() tea.Msg { |
| 159 | + return typeSelectedMsg{beanID: m.beanID, beanType: item.name} |
| 160 | + } |
| 161 | + } |
| 162 | + case "esc", "backspace": |
| 163 | + return m, func() tea.Msg { |
| 164 | + return closeTypePickerMsg{} |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + m.list, cmd = m.list.Update(msg) |
| 171 | + return m, cmd |
| 172 | +} |
| 173 | + |
| 174 | +func (m typePickerModel) View() string { |
| 175 | + if m.width == 0 { |
| 176 | + return "Loading..." |
| 177 | + } |
| 178 | + |
| 179 | + modalWidth := max(40, min(60, m.width*50/100)) |
| 180 | + |
| 181 | + subtitle := ui.Muted.Render(fmt.Sprintf("Changing type for %s", m.beanID)) |
| 182 | + |
| 183 | + // Get description of currently selected type |
| 184 | + var description string |
| 185 | + if item, ok := m.list.SelectedItem().(typeItem); ok && item.description != "" { |
| 186 | + description = ui.Muted.Render(item.description) |
| 187 | + } |
| 188 | + |
| 189 | + help := helpKeyStyle.Render("enter") + " " + helpStyle.Render("select") + " " + |
| 190 | + helpKeyStyle.Render("/") + " " + helpStyle.Render("filter") + " " + |
| 191 | + helpKeyStyle.Render("esc") + " " + helpStyle.Render("cancel") |
| 192 | + |
| 193 | + border := lipgloss.NewStyle(). |
| 194 | + Border(lipgloss.RoundedBorder()). |
| 195 | + BorderForeground(ui.ColorPrimary). |
| 196 | + Padding(0, 1). |
| 197 | + Width(modalWidth) |
| 198 | + |
| 199 | + content := subtitle + "\n\n" + m.list.View() + "\n\n" + description + "\n\n" + help |
| 200 | + |
| 201 | + return border.Render(content) |
| 202 | +} |
| 203 | + |
| 204 | +// ModalView returns the picker rendered as a centered modal overlay on top of the background |
| 205 | +func (m typePickerModel) ModalView(bgView string, fullWidth, fullHeight int) string { |
| 206 | + modal := m.View() |
| 207 | + return overlayModal(bgView, modal, fullWidth, fullHeight) |
| 208 | +} |
0 commit comments