get write action working and entries saving to db

This commit is contained in:
Travis Shears 2025-10-01 09:21:34 +02:00
parent 472c1b36a7
commit 09fb974bd2
5 changed files with 136 additions and 257 deletions

69
main.go
View file

@ -17,32 +17,35 @@ const (
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.")
}
var mainCommands = map[Action]tea.Cmd{
Write: gemlog.WritePostCMD(),
Read: TODOCmd,
Edit: TODOCmd,
Delete: TODOCmd,
type uiState struct {
notification string
cursor int
errorTxt string
}
type context struct {
config *gemlog.Config
}
type model struct {
// ui state
notification string
cursor int
actions []Action
errorTxt string
// deps
config *gemlog.Config
ui uiState
context *context
}
func initialModel(config *gemlog.Config) model {
return model{
actions: []Action{Write, Read, Edit, Delete},
config: config,
ui: uiState{
// actions: []Action{Write, Read, Edit, Delete},
},
context: &context{
config: config,
},
}
}
@ -52,22 +55,34 @@ func (m model) Init() tea.Cmd {
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.notification = fmt.Sprintf("%s\n\n", string(msg))
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.cursor > 0 {
m.cursor--
if m.ui.cursor > 0 {
m.ui.cursor--
}
case "down", "j":
if m.cursor < len(m.actions)-1 {
m.cursor++
if m.ui.cursor < len(actions)-1 {
m.ui.cursor++
}
case "enter", " ":
return m, mainCommands[m.actions[m.cursor]]
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
}
}
}
@ -75,19 +90,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (m model) View() string {
if len(m.errorTxt) > 0 {
return m.errorTxt
if len(m.ui.errorTxt) > 0 {
return m.ui.errorTxt
}
s := ""
if m.notification != "" {
s += m.notification
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 m.actions {
for i, action := range actions {
cursor := " "
if m.cursor == i {
if m.ui.cursor == i {
cursor = ">"
}
s += fmt.Sprintf("%s %s\n", cursor, action)