get notifications working
This commit is contained in:
parent
0c617c8185
commit
1160665595
3 changed files with 46 additions and 25 deletions
4
gemlog/common.go
Normal file
4
gemlog/common.go
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
package gemlog
|
||||||
|
|
||||||
|
type Notification string
|
||||||
|
type ErrorMsg struct{ err error }
|
||||||
|
|
@ -4,10 +4,25 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func WritePostCMD() tea.Msg {
|
||||||
|
id, err := write()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return ErrorMsg{err}
|
||||||
|
}
|
||||||
|
return Notification(fmt.Sprintf("Created post with id: %s", id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// For messages that contain errors it's often handy to also implement the
|
||||||
|
// error interface on the message.
|
||||||
|
// func (e ErrorMsg) Error() string { return e.err.Error() }
|
||||||
|
|
||||||
// WriteAction opens a shell editor and prints the output to console
|
// WriteAction opens a shell editor and prints the output to console
|
||||||
func WriteAction() error {
|
func write() (string, error) {
|
||||||
// Get the editor from environment variable, default to vi
|
// Get the editor from environment variable, default to vi
|
||||||
editor := os.Getenv("EDITOR")
|
editor := os.Getenv("EDITOR")
|
||||||
if editor == "" {
|
if editor == "" {
|
||||||
|
|
@ -17,7 +32,7 @@ func WriteAction() error {
|
||||||
// Create a temporary file
|
// Create a temporary file
|
||||||
tmpFile, err := os.CreateTemp("", "gemlog-*.md")
|
tmpFile, err := os.CreateTemp("", "gemlog-*.md")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create temporary file: %w", err)
|
return "", fmt.Errorf("failed to create temporary file: %w", err)
|
||||||
}
|
}
|
||||||
defer os.Remove(tmpFile.Name()) // Clean up
|
defer os.Remove(tmpFile.Name()) // Clean up
|
||||||
|
|
||||||
|
|
@ -31,17 +46,15 @@ func WriteAction() error {
|
||||||
|
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("editor command failed: %w", err)
|
return "", fmt.Errorf("editor command failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the contents of the file
|
// Read the contents of the file
|
||||||
content, err := os.ReadFile(tmpFile.Name())
|
content, err := os.ReadFile(tmpFile.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read file contents: %w", err)
|
return "", fmt.Errorf("failed to read file contents: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the contents to console
|
contentStr := string(content)
|
||||||
fmt.Print(string(content))
|
return contentStr, nil
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
32
main.go
32
main.go
|
|
@ -17,16 +17,19 @@ const (
|
||||||
Delete Action = "delete"
|
Delete Action = "delete"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ActionCallback func() error
|
func TODOCmd() tea.Msg {
|
||||||
|
return gemlog.Notification("This action has not been implemented yet.")
|
||||||
|
}
|
||||||
|
|
||||||
var callbacks = map[Action]ActionCallback{
|
var mainCommands = map[Action]tea.Cmd{
|
||||||
Write: gemlog.WriteAction,
|
Write: gemlog.WritePostCMD,
|
||||||
Read: func() error { return fmt.Errorf("not implmented") },
|
Read: TODOCmd,
|
||||||
Edit: func() error { return nil },
|
Edit: TODOCmd,
|
||||||
Delete: func() error { return nil },
|
Delete: TODOCmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
|
notification string
|
||||||
cursor int
|
cursor int
|
||||||
actions []Action
|
actions []Action
|
||||||
errorTxt string
|
errorTxt string
|
||||||
|
|
@ -44,6 +47,8 @@ func (m model) Init() tea.Cmd {
|
||||||
|
|
||||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
|
case gemlog.Notification:
|
||||||
|
m.notification = fmt.Sprintf("%s\n\n", string(msg))
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c", "q":
|
case "ctrl+c", "q":
|
||||||
|
|
@ -57,13 +62,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
m.cursor++
|
m.cursor++
|
||||||
}
|
}
|
||||||
case "enter", " ":
|
case "enter", " ":
|
||||||
err := callbacks[m.actions[m.cursor]]()
|
return m, mainCommands[m.actions[m.cursor]]
|
||||||
if err == nil {
|
|
||||||
return m, tea.Quit
|
|
||||||
}
|
|
||||||
m.errorTxt = "problem running action\n\nerror:\n"
|
|
||||||
m.errorTxt += fmt.Sprintf("Error: %v\n", err)
|
|
||||||
return m, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,7 +73,12 @@ func (m model) View() string {
|
||||||
if len(m.errorTxt) > 0 {
|
if len(m.errorTxt) > 0 {
|
||||||
return m.errorTxt
|
return m.errorTxt
|
||||||
}
|
}
|
||||||
s := "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
|
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 {
|
for i, action := range m.actions {
|
||||||
cursor := " "
|
cursor := " "
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue