mirror of
https://github.com/charmbracelet/glow
synced 2025-03-04 14:57:13 +00:00
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:
parent
400f3f8e1f
commit
a07fdb73f0
3 changed files with 57 additions and 0 deletions
33
ui/editor.go
Normal file
33
ui/editor.go
Normal 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{}
|
||||
}
|
12
ui/pager.go
12
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:
|
||||
|
|
12
ui/stash.go
12
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()
|
||||
|
|
Loading…
Add table
Reference in a new issue