Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions pkg/ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -93,6 +94,7 @@ type mainModel struct {
watchInterval time.Duration
pendingCursorPath string
watchInFlight bool
repoRoot string
}

func New(input string, cfg config.Config) mainModel {
Expand Down Expand Up @@ -131,6 +133,16 @@ func New(input string, cfg config.Config) mainModel {
return m
}

type repoRootMsg string

func (m mainModel) fetchRepoRoot() tea.Msg {
out, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
return repoRootMsg("")
}
return repoRootMsg(strings.TrimSpace(string(out)))
}

type watchTickMsg struct{ time.Time }

type watchResultMsg struct {
Expand All @@ -139,7 +151,7 @@ type watchResultMsg struct {
}

func (m mainModel) Init() tea.Cmd {
cmds := []tea.Cmd{m.fetchFileTree, m.diffViewer.Init()}
cmds := []tea.Cmd{m.fetchFileTree, m.diffViewer.Init(), m.fetchRepoRoot}
if m.watchEnabled {
cmds = append(cmds, m.scheduleWatchTick())
}
Expand Down Expand Up @@ -312,6 +324,9 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.watchInFlight = true
return m, m.fetchWatchDiff

case repoRootMsg:
m.repoRoot = string(msg)

case watchResultMsg:
m.watchInFlight = false
if msg.err != nil {
Expand Down Expand Up @@ -967,8 +982,15 @@ func (m mainModel) openInEditor() tea.Cmd {
return nil
}

fullpath := m.fileTree.CurrNodePath()
c := exec.Command(editor, fullpath)
relpath := m.fileTree.CurrNodePath()
var path string
if m.repoRoot != "" {
path = filepath.Join(m.repoRoot, relpath)
} else {
path = relpath
}

c := exec.Command(editor, path)
return tea.ExecProcess(c, func(err error) tea.Msg {
return nil
})
Expand Down
Loading