feat(ui): e to open edit local markdown files in editor

Introduce key binding to allow users to open their `EDITOR` to edit local markdown files in stash and pager view.
This commit is contained in:
Maas Lalani 2022-07-14 22:53:13 -04:00
parent 400f3f8e1f
commit a07fdb73f0
3 changed files with 57 additions and 0 deletions

33
ui/editor.go Normal file
View file

@ -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{}
}

View file

@ -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:

View file

@ -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()