Working navigation and selection functionality
This commit is contained in:
parent
4e810304f2
commit
bc1b6f60a4
3 changed files with 36 additions and 29 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,6 +4,7 @@
|
|||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
breakfast
|
||||
|
||||
# Test binary, built with
|
||||
*.test
|
||||
|
|
BIN
breakfast
BIN
breakfast
Binary file not shown.
64
main.go
64
main.go
|
@ -19,13 +19,18 @@ type sessionOrCommand struct {
|
|||
|
||||
type Choices [][]sessionOrCommand
|
||||
|
||||
type SelectionSet map[int]struct{}
|
||||
|
||||
type model struct {
|
||||
activeSection int
|
||||
cursor []int
|
||||
choices Choices
|
||||
selected []map[int]struct{}
|
||||
selected []SelectionSet
|
||||
}
|
||||
|
||||
var browserSelection SelectionSet
|
||||
var commandSelection SelectionSet
|
||||
|
||||
func getBrowserSessions() []sessionOrCommand {
|
||||
// Placeholder
|
||||
return []sessionOrCommand{{
|
||||
|
@ -38,9 +43,11 @@ func getBrowserSessions() []sessionOrCommand {
|
|||
}
|
||||
|
||||
func initialModel() model {
|
||||
browserSelection = make(SelectionSet)
|
||||
commandSelection = make(SelectionSet)
|
||||
return model{
|
||||
activeSection: BrowserSessions,
|
||||
// Need syntax of two-dimensional slices
|
||||
cursor: []int{0, 0},
|
||||
choices: Choices{
|
||||
getBrowserSessions(),
|
||||
[]sessionOrCommand{{
|
||||
|
@ -54,7 +61,7 @@ func initialModel() model {
|
|||
// 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),
|
||||
selected: []SelectionSet{browserSelection, commandSelection},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,31 +75,26 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
switch msg.String() {
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Quit
|
||||
case "tab":
|
||||
if m.activeSection == BrowserSessions {
|
||||
m.activeSection = Commands
|
||||
} else {
|
||||
m.activeSection = BrowserSessions
|
||||
}
|
||||
case "up", "k":
|
||||
if m.cursor[m.activeSection] > 0 {
|
||||
m.cursor[m.activeSection]--
|
||||
}
|
||||
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]++
|
||||
}
|
||||
case "enter", " ":
|
||||
// TODO: Let's have two separate cursors, and have tab move the active
|
||||
// set from brower sessions to terminals.
|
||||
if m.cursor[m.activeSection] < len(m.browserSessions)-1 {
|
||||
_, ok := m.selected[m.cursor]
|
||||
if ok {
|
||||
delete(m.selected, m.cursor)
|
||||
} else {
|
||||
m.selected[m.cursor] = struct{}{}
|
||||
}
|
||||
_, ok := m.selected[m.activeSection][m.cursor[m.activeSection]]
|
||||
if ok {
|
||||
delete(m.selected[m.activeSection], m.cursor[m.activeSection])
|
||||
} else {
|
||||
_, ok := m.selected[m.cursor]
|
||||
if ok {
|
||||
delete(m.selected, m.cursor)
|
||||
} else {
|
||||
m.selected[m.cursor] = struct{}{}
|
||||
}
|
||||
m.selected[m.activeSection][m.cursor[m.activeSection]] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -103,20 +105,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
func (m model) View() string {
|
||||
s := "What should we buy at the market?\n\n"
|
||||
|
||||
for i, choice := range m.choices {
|
||||
cursor := " "
|
||||
if m.cursor == i {
|
||||
cursor = ">"
|
||||
}
|
||||
for j := 0; j < 2; j++ {
|
||||
for i, choice := range m.choices[j] {
|
||||
cursor := " "
|
||||
if m.cursor[j] == i {
|
||||
cursor = ">"
|
||||
}
|
||||
|
||||
checked := " "
|
||||
if _, ok := m.selected[i]; ok {
|
||||
checked = "x"
|
||||
}
|
||||
checked := " "
|
||||
if _, ok := m.selected[j][i]; ok {
|
||||
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"
|
||||
|
||||
return s
|
||||
|
|
Loading…
Reference in a new issue