diff options
-rw-r--r-- | ui/editor.go | 33 | ||||
-rw-r--r-- | ui/pager.go | 12 | ||||
-rw-r--r-- | ui/stash.go | 12 |
3 files changed, 57 insertions, 0 deletions
diff --git a/ui/editor.go b/ui/editor.go new file mode 100644 index 0000000..60cc320 --- /dev/null +++ b/ui/editor.go @@ -0,0 +1,33 @@ +package ui + +import ( + "os" + "os/exec" + "strings" + + tea "github.com/charmbracelet/bubbletea" +) + +const defaultEditor = "nano" + +type editorFinishedMsg struct{ err error } + +func openEditor(path string) tea.Cmd { + editor, args := getEditor() + cmd := exec.Command(editor, append(args, path)...) + cb := func(err error) tea.Msg { + return editorFinishedMsg{err} + } + return tea.ExecProcess(cmd, cb) +} + +func getEditor() (string, []string) { + editor := strings.Fields(os.Getenv("EDITOR")) + if len(editor) > 1 { + return editor[0], editor[1:] + } + if len(editor) == 1 { + return editor[0], []string{} + } + return defaultEditor, []string{} +} diff --git a/ui/pager.go b/ui/pager.go index dea001b..b3c56e8 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -283,6 +283,12 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) { } return m, textinput.Blink + + case "e": + if m.currentDocument.docType == LocalDoc { + return m, openEditor(m.currentDocument.localPath) + } + case "s": if m.common.authStatus != authOK { break @@ -341,6 +347,12 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) { cmds = append(cmds, viewport.Sync(m.viewport)) } + // We've finished editing the document, potentially making changes. Let's + // retrieve the latest version of the document so that we display + // up-to-date contents. + case editorFinishedMsg: + return m, loadLocalMarkdown(&m.currentDocument) + // We've reveived terminal dimensions, either for the first time or // after a resize case tea.WindowSizeMsg: diff --git a/ui/stash.go b/ui/stash.go index e4ba115..a017911 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -761,6 +761,18 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd { } m.updatePagination() + // Edit document in EDITOR + case "e": + md := m.selectedMarkdown() + if md == nil { + break + } + file := m.selectedMarkdown().localPath + if file == "" { + break + } + return openEditor(file) + // Open document case keyEnter: m.hideStatusMessage() |