summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaas Lalani <maas@lalani.dev>2022-07-14 22:53:13 -0400
committerMaas Lalani <maas@lalani.dev>2022-11-23 09:40:07 -0500
commita07fdb73f0cffd5ce027341fb2e98a26cc852d54 (patch)
treebc5d3974189390899edfe7843a1776e5d63e5acd
parent400f3f8e1f187857d20dc1613d4e840c32c04bfd (diff)
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.
-rw-r--r--ui/editor.go33
-rw-r--r--ui/pager.go12
-rw-r--r--ui/stash.go12
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()