Working navigation and selection functionality

This commit is contained in:
Adam Cooper 2022-05-11 09:39:14 -04:00
parent 4e810304f2
commit bc1b6f60a4
3 changed files with 36 additions and 29 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
*.dll *.dll
*.so *.so
*.dylib *.dylib
breakfast
# Test binary, built with # Test binary, built with
*.test *.test

BIN
breakfast

Binary file not shown.

64
main.go
View File

@ -19,13 +19,18 @@ type sessionOrCommand struct {
type Choices [][]sessionOrCommand type Choices [][]sessionOrCommand
type SelectionSet map[int]struct{}
type model struct { type model struct {
activeSection int activeSection int
cursor []int cursor []int
choices Choices choices Choices
selected []map[int]struct{} selected []SelectionSet
} }
var browserSelection SelectionSet
var commandSelection SelectionSet
func getBrowserSessions() []sessionOrCommand { func getBrowserSessions() []sessionOrCommand {
// Placeholder // Placeholder
return []sessionOrCommand{{ return []sessionOrCommand{{
@ -38,9 +43,11 @@ func getBrowserSessions() []sessionOrCommand {
} }
func initialModel() model { func initialModel() model {
browserSelection = make(SelectionSet)
commandSelection = make(SelectionSet)
return model{ return model{
activeSection: BrowserSessions, activeSection: BrowserSessions,
// Need syntax of two-dimensional slices cursor: []int{0, 0},
choices: Choices{ choices: Choices{
getBrowserSessions(), getBrowserSessions(),
[]sessionOrCommand{{ []sessionOrCommand{{
@ -54,7 +61,7 @@ func initialModel() model {
// An array maps which indicates which choices are selected. We're // An array maps which indicates which choices are selected. We're
// using the map like a mathematical set. The keys refer to the // using the map like a mathematical set. The keys refer to the
// indexes of the `choices` slice, above. // indexes of the `choices` slice, above.
selected: make([]map[int]struct{}, 32, 32), selected: []SelectionSet{browserSelection, commandSelection},
} }
} }
@ -68,31 +75,26 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() { switch msg.String() {
case "ctrl+c", "q": case "ctrl+c", "q":
return m, tea.Quit return m, tea.Quit
case "tab":
if m.activeSection == BrowserSessions {
m.activeSection = Commands
} else {
m.activeSection = BrowserSessions
}
case "up", "k": case "up", "k":
if m.cursor[m.activeSection] > 0 { if m.cursor[m.activeSection] > 0 {
m.cursor[m.activeSection]-- m.cursor[m.activeSection]--
} }
case "down", "j": case "down", "j":
if m.cursor[m.activeSection] < len(m.browserSessions)+len(m.commands)-1 { if m.cursor[m.activeSection] < len(m.choices[m.activeSection])-1 {
m.cursor[m.activeSection]++ m.cursor[m.activeSection]++
} }
case "enter", " ": case "enter", " ":
// TODO: Let's have two separate cursors, and have tab move the active _, ok := m.selected[m.activeSection][m.cursor[m.activeSection]]
// set from brower sessions to terminals. if ok {
if m.cursor[m.activeSection] < len(m.browserSessions)-1 { delete(m.selected[m.activeSection], m.cursor[m.activeSection])
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
} else { } else {
_, ok := m.selected[m.cursor] m.selected[m.activeSection][m.cursor[m.activeSection]] = struct{}{}
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
} }
} }
} }
@ -103,20 +105,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) View() string { func (m model) View() string {
s := "What should we buy at the market?\n\n" s := "What should we buy at the market?\n\n"
for i, choice := range m.choices { for j := 0; j < 2; j++ {
cursor := " " for i, choice := range m.choices[j] {
if m.cursor == i { cursor := " "
cursor = ">" if m.cursor[j] == i {
} cursor = ">"
}
checked := " " checked := " "
if _, ok := m.selected[i]; ok { if _, ok := m.selected[j][i]; ok {
checked = "x" checked = "x"
} }
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice) s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice.displayString)
}
s += "\n\n"
} }
// s += fmt.Sprintf("\n%+v", m.selected) // debug
s += "\nPress q to quit.\n" s += "\nPress q to quit.\n"
return s return s