From 597e56bad4599857dde4c36174ce7a5ec371b7cc Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 9 Jul 2024 16:38:32 -0300 Subject: [PATCH] feat: open editor in current line closes #547 --- ui/editor.go | 4 ++-- ui/pager.go | 14 ++++++++------ ui/stash.go | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ui/editor.go b/ui/editor.go index e2d4439..5eb2a06 100644 --- a/ui/editor.go +++ b/ui/editor.go @@ -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) } } diff --git a/ui/pager.go b/ui/pager.go index 95b70c3..02a7b12 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -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 } diff --git a/ui/stash.go b/ui/stash.go index d6849c1..2334c66 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -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: