gemlog-cli/actionsList.go

78 lines
1.5 KiB
Go

package main
import (
"fmt"
"gemini_site/gemlog"
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}
type ActionListPageModel struct {
cursor int
}
func initialActionListPageModel() ActionListPageModel {
return ActionListPageModel{
cursor: 0,
}
}
func (m ActionListPageModel) InitActionListPage() tea.Cmd {
return nil
}
func (m ActionListPageModel) Update(msg tea.Msg, ctx *context) (ActionListPageModel, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(actions)-1 {
m.cursor++
}
case "enter", " ":
action := actions[m.cursor]
switch action {
case Write:
return m, gemlog.WritePostCMD(ctx.config)
case Read, Delete, Edit:
switchPageCmd := func() tea.Msg {
return SwitchPages{Page: EntryList}
}
actionToTakeCmd := func() tea.Msg {
return SelectActionToTake{ActionToTake: action}
}
loadGemLogsCmd := gemlog.LoadGemlogCMD(ctx.config)
return m, tea.Batch(switchPageCmd, loadGemLogsCmd, actionToTakeCmd)
}
}
}
return m, nil
}
func (m ActionListPageModel) View() string {
s := "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
for i, action := range actions {
cursor := " "
if m.cursor == i {
cursor = ">"
}
s += fmt.Sprintf("%s %s\n", cursor, action)
}
return s
}