gemlog-cli/main.go

128 lines
2.3 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"
)
var actions = []Action{Write, Read, Edit, Delete}
func TODOCmd() tea.Msg {
return gemlog.Notification("This action has not been implemented yet. Try another.")
}
type uiState struct {
notification string
cursor int
errorTxt string
}
type context struct {
config *gemlog.Config
}
type model struct {
ui uiState
context *context
}
func initialModel(config *gemlog.Config) model {
return model{
ui: uiState{
// actions: []Action{Write, Read, Edit, Delete},
},
context: &context{
config: config,
},
}
}
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.ErrorMsg:
m.ui.errorTxt = fmt.Sprintf("%s\n\n", fmt.Errorf("%s", msg))
case gemlog.Notification:
m.ui.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.ui.cursor > 0 {
m.ui.cursor--
}
case "down", "j":
if m.ui.cursor < len(actions)-1 {
m.ui.cursor++
}
case "enter", " ":
action := actions[m.ui.cursor]
switch action {
case Write:
return m, gemlog.WritePostCMD(m.context.config)
case Read:
return m, TODOCmd
case Edit:
return m, TODOCmd
case Delete:
return m, TODOCmd
}
}
}
return m, nil
}
func (m model) View() string {
if len(m.ui.errorTxt) > 0 {
return m.ui.errorTxt
}
s := ""
if m.ui.notification != "" {
s += m.ui.notification
} else {
s += "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
}
for i, action := range actions {
cursor := " "
if m.ui.cursor == i {
cursor = ">"
}
s += fmt.Sprintf("%s %s\n", cursor, action)
}
s += "\nPress q to quit.\n"
return s
}
func main() {
config, err := gemlog.LoadConfig()
if err != nil {
fmt.Printf("Error loading config: %v", err)
os.Exit(1)
}
p := tea.NewProgram(initialModel(config))
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}