Skip to content

Commit f319214

Browse files
authored
feat: add keybinding to open file in editor (#60)
Adds `o` keybinding to open the currently selected file in `$EDITOR`. Closes #42
1 parent 47787e2 commit f319214

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ If you want the exact delta configuration I'm using - [it can be found here](htt
9797
| <kbd>e</kbd> | Toggle the file tree |
9898
| <kbd>t</kbd> | Search/go-to file |
9999
| <kbd>y</kbd> | Copy file path |
100+
| <kbd>o</kbd> | Open file in $EDITOR |
100101
| <kbd>Tab</kbd> | Switch focus between the panes |
101102
| <kbd>q</kbd> | Quit |
102103

pkg/ui/keys.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type KeyMap struct {
1212
Quit key.Binding
1313
Copy key.Binding
1414
TogglePanel key.Binding
15+
OpenInEditor key.Binding
1516
}
1617

1718
var keys = &KeyMap{
@@ -51,6 +52,10 @@ var keys = &KeyMap{
5152
key.WithKeys("tab"),
5253
key.WithHelp("TAB", "switch panel"),
5354
),
55+
OpenInEditor: key.NewBinding(
56+
key.WithKeys("o"),
57+
key.WithHelp("o", "open"),
58+
),
5459
}
5560

5661
func getKeys() []key.Binding {
@@ -63,6 +68,7 @@ func getKeys() []key.Binding {
6368
keys.ToggleFileTree,
6469
keys.Search,
6570
keys.Copy,
71+
keys.OpenInEditor,
6672
keys.Quit,
6773
}
6874
}

pkg/ui/mainModel.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package ui
22

33
import (
44
"fmt"
5+
"os"
6+
"os/exec"
57
"strings"
68

79
"github.com/bluekeyes/go-gitdiff/gitdiff"
@@ -176,6 +178,11 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
176178
if cmd != nil {
177179
cmds = append(cmds, cmd)
178180
}
181+
case "o":
182+
cmd = m.openInEditor()
183+
if cmd != nil {
184+
cmds = append(cmds, cmd)
185+
}
179186
}
180187

181188
case tea.WindowSizeMsg:
@@ -456,6 +463,23 @@ func (m *mainModel) setCursor(cursor int) tea.Cmd {
456463
return cmd
457464
}
458465

466+
func (m mainModel) openInEditor() tea.Cmd {
467+
if len(m.files) == 0 {
468+
return nil
469+
}
470+
471+
editor := os.Getenv("EDITOR")
472+
if editor == "" {
473+
return nil
474+
}
475+
476+
filePath := filenode.GetFileName(m.files[m.cursor])
477+
c := exec.Command(editor, filePath)
478+
return tea.ExecProcess(c, func(err error) tea.Msg {
479+
return nil
480+
})
481+
}
482+
459483
func (m mainModel) handleMouse(msg tea.MouseMsg) (tea.Model, tea.Cmd) {
460484
// Handle scroll wheel first.
461485
if msg.Button == tea.MouseButtonWheelUp || msg.Button == tea.MouseButtonWheelDown {

0 commit comments

Comments
 (0)