gemlog-cli/main.go

102 lines
1.8 KiB
Go

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"
)
func TODOCmd() tea.Msg {
return gemlog.Notification("This action has not been implemented yet.")
}
var mainCommands = map[Action]tea.Cmd{
Write: gemlog.WritePostCMD,
Read: TODOCmd,
Edit: TODOCmd,
Delete: TODOCmd,
}
type model struct {
notification string
cursor int
actions []Action
errorTxt string
}
func initialModel() model {
return model{
actions: []Action{Write, Read, Edit, Delete},
}
}
func (m model) Init() tea.Cmd {
return tea.SetWindowTitle("Gemlog CLI")
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case gemlog.Notification:
m.notification = fmt.Sprintf("%s\n\n", string(msg))
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.actions)-1 {
m.cursor++
}
case "enter", " ":
return m, mainCommands[m.actions[m.cursor]]
}
}
return m, nil
}
func (m model) View() string {
if len(m.errorTxt) > 0 {
return m.errorTxt
}
s := ""
if m.notification != "" {
s += m.notification
} else {
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)
}
s += "\nPress q to quit.\n"
return s
}
func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}