setup base entity and change cli to print actions

This commit is contained in:
Travis Shears 2025-09-30 11:09:27 +02:00
parent d1f4527b1b
commit b24cc4d427
6 changed files with 97 additions and 26 deletions

41
main.go
View file

@ -8,24 +8,18 @@ import (
)
type model struct {
cursor int
choices []string
selected map[int]struct{}
cursor int
actions []string
}
func initialModel() model {
return model{
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
// A map 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{}),
actions: []string{"Write", "Read", "Edit", "Delete"},
}
}
func (m model) Init() tea.Cmd {
return tea.SetWindowTitle("Grocery List")
return tea.SetWindowTitle("Gemlog CLI")
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@ -39,16 +33,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.choices)-1 {
if m.cursor < len(m.actions)-1 {
m.cursor++
}
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
// case "enter", " ":
// _, ok := m.selected[m.cursor]
// if ok {
// delete(m.selected, m.cursor)
// } else {
// m.selected[m.cursor] = struct{}{}
// }
}
}
@ -56,20 +50,15 @@ 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"
s := "Welcome to gemlog cli!\n\nWhat would you like to do?\n\n"
for i, choice := range m.choices {
for i, action := range m.actions {
cursor := " "
if m.cursor == i {
cursor = ">"
}
checked := " "
if _, ok := m.selected[i]; ok {
checked = "x"
}
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
s += fmt.Sprintf("%s %s\n", cursor, action)
}
s += "\nPress q to quit.\n"