Skip to content

Commit a07fdb7

Browse files
committed
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.
1 parent 400f3f8 commit a07fdb7

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

ui/editor.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ui
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"strings"
7+
8+
tea "github.com/charmbracelet/bubbletea"
9+
)
10+
11+
const defaultEditor = "nano"
12+
13+
type editorFinishedMsg struct{ err error }
14+
15+
func openEditor(path string) tea.Cmd {
16+
editor, args := getEditor()
17+
cmd := exec.Command(editor, append(args, path)...)
18+
cb := func(err error) tea.Msg {
19+
return editorFinishedMsg{err}
20+
}
21+
return tea.ExecProcess(cmd, cb)
22+
}
23+
24+
func getEditor() (string, []string) {
25+
editor := strings.Fields(os.Getenv("EDITOR"))
26+
if len(editor) > 1 {
27+
return editor[0], editor[1:]
28+
}
29+
if len(editor) == 1 {
30+
return editor[0], []string{}
31+
}
32+
return defaultEditor, []string{}
33+
}

ui/pager.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) {
283283
}
284284

285285
return m, textinput.Blink
286+
287+
case "e":
288+
if m.currentDocument.docType == LocalDoc {
289+
return m, openEditor(m.currentDocument.localPath)
290+
}
291+
286292
case "s":
287293
if m.common.authStatus != authOK {
288294
break
@@ -341,6 +347,12 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) {
341347
cmds = append(cmds, viewport.Sync(m.viewport))
342348
}
343349

350+
// We've finished editing the document, potentially making changes. Let's
351+
// retrieve the latest version of the document so that we display
352+
// up-to-date contents.
353+
case editorFinishedMsg:
354+
return m, loadLocalMarkdown(&m.currentDocument)
355+
344356
// We've reveived terminal dimensions, either for the first time or
345357
// after a resize
346358
case tea.WindowSizeMsg:

ui/stash.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,18 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd {
761761
}
762762
m.updatePagination()
763763

764+
// Edit document in EDITOR
765+
case "e":
766+
md := m.selectedMarkdown()
767+
if md == nil {
768+
break
769+
}
770+
file := m.selectedMarkdown().localPath
771+
if file == "" {
772+
break
773+
}
774+
return openEditor(file)
775+
764776
// Open document
765777
case keyEnter:
766778
m.hideStatusMessage()

0 commit comments

Comments
 (0)