feat: open editor in current line

closes #547
This commit is contained in:
Carlos Alexandro Becker 2024-07-09 16:38:32 -03:00
parent 400228cff6
commit 597e56bad4
No known key found for this signature in database
3 changed files with 11 additions and 9 deletions

View file

@ -7,11 +7,11 @@ import (
type editorFinishedMsg struct{ err error }
func openEditor(path string) tea.Cmd {
func openEditor(path string, lineno int) tea.Cmd {
cb := func(err error) tea.Msg {
return editorFinishedMsg{err}
}
cmd, err := editor.Cmd("Glow", path)
cmd, err := editor.Cmd("Glow", path, editor.OpenAtLine(uint(lineno)))
if err != nil {
return func() tea.Msg { return cb(err) }
}

View file

@ -196,7 +196,14 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) {
}
case "e":
return m, openEditor(m.currentDocument.localPath)
l := int(math.Round(float64(m.viewport.TotalLineCount()) * m.viewport.ScrollPercent()))
if m.viewport.AtTop() {
l = 0
}
if m.viewport.AtBottom() {
l = m.viewport.TotalLineCount()
}
return m, openEditor(m.currentDocument.localPath, l)
case "c":
// Copy using OSC 52
@ -222,9 +229,6 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) {
cmds = append(cmds, viewport.Sync(m.viewport))
}
case editMardownMsg:
return m, openEditor(msg.md.localPath)
// 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.
@ -445,5 +449,3 @@ func glamourRender(m pagerModel, markdown string) (string, error) {
return content.String(), nil
}
type editMardownMsg struct{ md *markdown }

View file

@ -519,7 +519,7 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd {
// Edit document in EDITOR
case "e":
md := m.selectedMarkdown()
return openEditor(md.localPath)
return openEditor(md.localPath, 0)
// Open document
case keyEnter: