get editor intergration working
This commit is contained in:
parent
01ce668160
commit
8ceb698f03
1 changed files with 48 additions and 13 deletions
|
|
@ -9,20 +9,55 @@ import (
|
|||
)
|
||||
|
||||
func WritePostCMD() tea.Cmd {
|
||||
editor := os.Getenv("EDITOR")
|
||||
if editor == "" {
|
||||
editor = "vim"
|
||||
}
|
||||
c := exec.Command(editor) //nolint:gosec
|
||||
return tea.ExecProcess(c, func(err error) tea.Msg {
|
||||
return Notification("Editor finished")
|
||||
})
|
||||
// id, err := write()
|
||||
return func() tea.Msg {
|
||||
// Create a temporary file
|
||||
tmpFile, err := os.CreateTemp("/tmp", "gemlog-*.md")
|
||||
if err != nil {
|
||||
return ErrorMsg{fmt.Errorf("failed to create temporary file: %w", err)}
|
||||
}
|
||||
|
||||
// if err != nil {
|
||||
// return ErrorMsg{err}
|
||||
// }
|
||||
// return Notification(fmt.Sprintf("Created post with id: %s", id))
|
||||
// Load initial content from template file
|
||||
initialContent, err := os.ReadFile("templates/default_post.gmi")
|
||||
if err != nil {
|
||||
tmpFile.Close()
|
||||
os.Remove(tmpFile.Name())
|
||||
return ErrorMsg{fmt.Errorf("failed to read template file: %w", err)}
|
||||
}
|
||||
|
||||
if _, err := tmpFile.Write(initialContent); err != nil {
|
||||
tmpFile.Close()
|
||||
os.Remove(tmpFile.Name())
|
||||
return ErrorMsg{fmt.Errorf("failed to write initial content: %w", err)}
|
||||
}
|
||||
tmpFile.Close()
|
||||
|
||||
// Get the editor from environment variable, default to vim
|
||||
editor := os.Getenv("EDITOR")
|
||||
if editor == "" {
|
||||
editor = "vim"
|
||||
}
|
||||
|
||||
// Create the command to open the editor with the temp file
|
||||
c := exec.Command(editor, tmpFile.Name()) //nolint:gosec
|
||||
|
||||
// Return tea.ExecProcess which will suspend the TUI and run the editor
|
||||
return tea.ExecProcess(c, func(err error) tea.Msg {
|
||||
defer os.Remove(tmpFile.Name()) // Clean up the temp file
|
||||
|
||||
if err != nil {
|
||||
return ErrorMsg{fmt.Errorf("editor command failed: %w", err)}
|
||||
}
|
||||
|
||||
// Read the contents of the file after editing
|
||||
content, readErr := os.ReadFile(tmpFile.Name())
|
||||
if readErr != nil {
|
||||
return ErrorMsg{fmt.Errorf("failed to read file contents: %w", readErr)}
|
||||
}
|
||||
|
||||
// Return success with the content
|
||||
return Notification(fmt.Sprintf("Post created with content: %s", string(content)))
|
||||
})()
|
||||
}
|
||||
}
|
||||
|
||||
// For messages that contain errors it's often handy to also implement the
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue