Correct or almost correct model

This commit is contained in:
Adam Cooper 2022-05-10 09:13:56 -04:00
parent f0d76461dd
commit 4e810304f2
1 changed files with 41 additions and 27 deletions

68
main.go
View File

@ -7,40 +7,54 @@ import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
type commandChoice struct { const (
BrowserSessions int = iota // Enumerating the sections
Commands
)
type sessionOrCommand struct {
displayString string displayString string
commandString string commandString string
} }
type Choices [][]sessionOrCommand
type model struct { type model struct {
browserSessionsCursor int activeSection int
commandsCursor int cursor []int
browserSessions []string choices Choices
commands []commandChoice selected []map[int]struct{}
selectedSessions map[int]struct{}
selectedCommands map[int]struct{}
} }
func getBrowserSessions() []string { func getBrowserSessions() []sessionOrCommand {
// Placeholder // Placeholder
return []string{"primary", "breakfast"} return []sessionOrCommand{{
displayString: "primary",
commandString: "",
}, {
displayString: "breakfast",
commandString: "",
}}
} }
func initialModel() model { func initialModel() model {
return model{ return model{
browserSessions: getBrowserSessions(), activeSection: BrowserSessions,
commands: []commandChoice{{ // Need syntax of two-dimensional slices
displayString: "neomutt", choices: Choices{
commandString: "neomutt", getBrowserSessions(),
}, { []sessionOrCommand{{
displayString: "bottom", displayString: "neomutt",
commandString: "btm --group --battery --color gruvbox-light", commandString: "neomutt",
}}, }, {
// A map which indicates which choices are selected. We're using displayString: "bottom",
// the map like a mathematical set. The keys refer to the indexes commandString: "btm --group --battery --color gruvbox-light",
// of the `choices` slice, above. }},
selectedSessions: make(map[int]struct{}), },
selectedCommands: make(map[int]struct{}), // An array maps which indicates which choices are selected. We're
// using the map like a mathematical set. The keys refer to the
// indexes of the `choices` slice, above.
selected: make([]map[int]struct{}, 32, 32),
} }
} }
@ -55,17 +69,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "ctrl+c", "q": case "ctrl+c", "q":
return m, tea.Quit return m, tea.Quit
case "up", "k": case "up", "k":
if m.cursor > 0 { if m.cursor[m.activeSection] > 0 {
m.cursor-- m.cursor[m.activeSection]--
} }
case "down", "j": case "down", "j":
if m.cursor < len(m.browserSessions)+len(m.commands)-1 { if m.cursor[m.activeSection] < len(m.browserSessions)+len(m.commands)-1 {
m.cursor++ m.cursor[m.activeSection]++
} }
case "enter", " ": case "enter", " ":
// TODO: Let's have two separate cursors, and have tab move the active // TODO: Let's have two separate cursors, and have tab move the active
// set from brower sessions to terminals. // set from brower sessions to terminals.
if m.cursor < len(m.browserSessions)-1 { if m.cursor[m.activeSection] < len(m.browserSessions)-1 {
_, ok := m.selected[m.cursor] _, ok := m.selected[m.cursor]
if ok { if ok {
delete(m.selected, m.cursor) delete(m.selected, m.cursor)