get callbacks working
This commit is contained in:
parent
b24cc4d427
commit
0c617c8185
3 changed files with 82 additions and 12 deletions
47
main.go
47
main.go
|
|
@ -2,19 +2,39 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"gemini_site/gemlog"
|
||||
"os"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type Action string
|
||||
|
||||
const (
|
||||
Write Action = "write"
|
||||
Read Action = "read"
|
||||
Edit Action = "edit"
|
||||
Delete Action = "delete"
|
||||
)
|
||||
|
||||
type ActionCallback func() error
|
||||
|
||||
var callbacks = map[Action]ActionCallback{
|
||||
Write: gemlog.WriteAction,
|
||||
Read: func() error { return fmt.Errorf("not implmented") },
|
||||
Edit: func() error { return nil },
|
||||
Delete: func() error { return nil },
|
||||
}
|
||||
|
||||
type model struct {
|
||||
cursor int
|
||||
actions []string
|
||||
cursor int
|
||||
actions []Action
|
||||
errorTxt string
|
||||
}
|
||||
|
||||
func initialModel() model {
|
||||
return model{
|
||||
actions: []string{"Write", "Read", "Edit", "Delete"},
|
||||
actions: []Action{Write, Read, Edit, Delete},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -36,13 +56,14 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
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", " ":
|
||||
err := callbacks[m.actions[m.cursor]]()
|
||||
if err == nil {
|
||||
return m, tea.Quit
|
||||
}
|
||||
m.errorTxt = "problem running action\n\nerror:\n"
|
||||
m.errorTxt += fmt.Sprintf("Error: %v\n", err)
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -50,14 +71,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
}
|
||||
|
||||
func (m model) View() string {
|
||||
s := "Welcome to gemlog cli!\n\nWhat would you like to do?\n\n"
|
||||
if len(m.errorTxt) > 0 {
|
||||
return m.errorTxt
|
||||
}
|
||||
s := "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
|
||||
|
||||
for i, action := range m.actions {
|
||||
cursor := " "
|
||||
if m.cursor == i {
|
||||
cursor = ">"
|
||||
}
|
||||
|
||||
s += fmt.Sprintf("%s %s\n", cursor, action)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue