81 lines
1.6 KiB
Go
81 lines
1.6 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, active bool, ctx *context) (ActionListPageModel, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case tea.KeyMsg:
|
|
if !active {
|
|
return m, nil
|
|
}
|
|
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", " ", "l":
|
|
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.LoadGemlogsCMD(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
|
|
}
|