From 89094fecf4cb1c018f15c976641cd18c255eac28 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sat, 23 Nov 2024 18:12:03 -0500 Subject: Semi-working POC --- internal/switcher/keys.go | 33 ++++++++++++++++ internal/switcher/switcher.go | 89 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 internal/switcher/keys.go create mode 100644 internal/switcher/switcher.go (limited to 'internal/switcher') diff --git a/internal/switcher/keys.go b/internal/switcher/keys.go new file mode 100644 index 0000000..94eb6fb --- /dev/null +++ b/internal/switcher/keys.go @@ -0,0 +1,33 @@ +package switcher + +import "github.com/charmbracelet/bubbles/key" + +type keybinds struct { + next, prev, quit key.Binding +} + +func newKeybinds() keybinds { + return keybinds{ + next: key.NewBinding( + key.WithKeys("tab"), + key.WithHelp("tab", "next picker"), + ), + prev: key.NewBinding( + key.WithKeys("shift+tab"), + key.WithHelp("shift+tab", "previous picker"), + ), + quit: key.NewBinding( + key.WithKeys("q", "ctrl+c"), + key.WithHelp("q", "quit"), + ), + } +} + +func Keys() []key.Binding { + k := newKeybinds() + return []key.Binding{k.next, k.prev} +} + +func (m Model) AllKeys() []key.Binding { + return append(Keys(), m.pickers[m.active].AllKeys()...) +} diff --git a/internal/switcher/switcher.go b/internal/switcher/switcher.go new file mode 100644 index 0000000..d542df1 --- /dev/null +++ b/internal/switcher/switcher.go @@ -0,0 +1,89 @@ +package switcher + +import ( + "fmt" + + "github.com/charmbracelet/bubbles/key" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/bubbletea-app-template/internal/picker" + "github.com/charmbracelet/bubbletea-app-template/internal/quit" +) + +type Model struct { + active int + pickers []picker.Model +} + +func New(pickers []picker.Model) Model { + return Model{ + active: 0, + pickers: pickers, + } +} + +func (m Model) fixSel(val int) int { + size := len(m.pickers) + return (val%size + size) % size +} + +func (m *Model) Next() int { + m.active = m.fixSel(m.active + 1) + return m.active +} + +func (m *Model) Prev() int { + m.active = m.fixSel(m.active - 1) + return m.active +} + +func (m Model) Init() tea.Cmd { + cmds := []tea.Cmd{} + for _, p := range m.pickers { + cmds = append(cmds, p.Init()) + } + return tea.Batch(cmds...) +} + +func (m Model) View() string { + v := "|" + for i, p := range m.pickers { + if i == m.active { + v += fmt.Sprintf(">%s<|", p.Title()) + } else { + v += fmt.Sprintf(" %s |", p.Title()) + } + } + return fmt.Sprintf("%s\n%s", v, m.pickers[m.active].View()) +} + +func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + keys := newKeybinds() + cmds := []tea.Cmd{} + switch msg := msg.(type) { + case tea.KeyMsg: + switch { + case key.Matches(msg, keys.next): + cs := m.pickers[m.active].GetColor() + m.Next() + m.pickers[m.active].SetColor(cs) + case key.Matches(msg, keys.prev): + cs := m.pickers[m.active].GetColor() + m.Prev() + m.pickers[m.active].SetColor(cs) + case key.Matches(msg, keys.quit): + return quit.Model{}, tea.Quit + // return m, tea.Quit + default: + newActive, cmd := m.pickers[m.active].Update(msg) + m.pickers[m.active] = newActive.(picker.Model) + cmds = append(cmds, cmd) + return m, tea.Batch(cmds...) + } + } + for i, p := range m.pickers { + newActive, cmd := p.Update(msg) + m.pickers[i] = newActive.(picker.Model) + cmds = append(cmds, cmd) + } + return m, tea.Batch(cmds...) +} -- cgit v1.2.3