Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/keybindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Preview panel (panel 5).
| `B` | Toggle blame |
| `y/Ctrl+C` | Copy selection |
| `Y` | Copy GitHub permalink |
| `O` | Open file on GitHub |
| `Esc` | Clear selection |

Navigation keys (j/k/g/G/PgDn/PgUp) scroll content. Click+drag to select text, double-click to select a word.
Expand Down
1 change: 1 addition & 0 deletions internal/keybindings/keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
{ "key": "B", "action": "Toggle blame" },
{ "key": "y/Ctrl+C", "action": "Copy selection" },
{ "key": "Y", "action": "Copy GitHub permalink" },
{ "key": "O", "action": "Open file on GitHub" },
{ "key": "Esc", "action": "Clear selection" }
],
"note": "Navigation keys (j/k/g/G/PgDn/PgUp) scroll content. Click+drag to select text, double-click to select a word."
Expand Down
38 changes: 38 additions & 0 deletions internal/panels/preview/permalink.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
return p, func() tea.Msg {
ctx := context.Background()
if gc == nil {
return notify.ShowToastMsg{Message: "No git repository", Level: notify.Warn}

Check failure on line 92 in internal/panels/preview/permalink.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

string `No git repository` has 4 occurrences, make it a constant (goconst)
}
root, err := gc.RepoRoot(ctx)
if err != nil || root == "" {
Expand All @@ -112,3 +112,41 @@
return notify.ShowToastMsg{Message: "Copied GitHub permalink", Level: notify.Info}
}
}

// openOnGitHub builds a github.com permalink for the current file and line
// (or selected range) and opens it in the default browser. It is a no-op with
// a clear message when the preview is not showing an on-disk file or when the
// repository has no github remote.
func (p *Preview) openOnGitHub() (panels.Panel, tea.Cmd) {
if p.ghMode || p.filePath == "" {
return p, nil
}
path := p.filePath
gc := p.gitClient
startLine, endLine := p.permalinkLineRange()
return p, func() tea.Msg {
ctx := context.Background()
if gc == nil {
return notify.ShowToastMsg{Message: "No git repository", Level: notify.Warn}
}
root, err := gc.RepoRoot(ctx)
if err != nil || root == "" {
return notify.ShowToastMsg{Message: "No git repository", Level: notify.Warn}
}
relPath := path
if rel, err := filepath.Rel(root, path); err == nil {
relPath = filepath.ToSlash(rel)
}
if strings.HasPrefix(relPath, "..") {
return notify.ShowToastMsg{Message: "File is outside the repository", Level: notify.Warn}
}
link := buildPermalink(gitRemoteURL(ctx, root), gitHeadSHA(ctx, root), relPath, startLine, endLine)
if link == "" {
return notify.ShowToastMsg{Message: "No github remote to open", Level: notify.Warn}
}
if err := panels.OpenInBrowser(ctx, link); err != nil {
return notify.ShowToastMsg{Message: "Open failed: " + err.Error(), Level: notify.Error}
}
return notify.ShowToastMsg{Message: "Opened file on GitHub", Level: notify.Info}
}
}
15 changes: 15 additions & 0 deletions internal/panels/preview/permalink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,18 @@ func TestCopyPermalink_NoOpInGitHubMode(t *testing.T) {
_, cmd := p.copyPermalink()
assert.Nil(t, cmd)
}

func TestOpenOnGitHub_NoOpWhenNoFile(t *testing.T) {
p := newTestPreview([]string{"a"})
p.filePath = ""
_, cmd := p.openOnGitHub()
assert.Nil(t, cmd)
}

func TestOpenOnGitHub_NoOpInGitHubMode(t *testing.T) {
p := newTestPreview([]string{"a"})
p.filePath = "main.go"
p.ghMode = true
_, cmd := p.openOnGitHub()
assert.Nil(t, cmd)
}
3 changes: 3 additions & 0 deletions internal/panels/preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ func (p *Preview) Update(msg tea.Msg) (panels.Panel, tea.Cmd) {
return p.copyFilePath()
case "Y":
return p.copyPermalink()
case "O":
return p.openOnGitHub()
case keyEscape, keyEsc:
if p.hasSelection() {
p.clearSelection()
Expand Down Expand Up @@ -723,6 +725,7 @@ func (p *Preview) KeyBindings() []panels.KeyBinding {
{Key: "B", Description: "Toggle blame", Action: "toggle_blame"},
{Key: "y/Ctrl+C", Description: "Copy selection or file path", Action: "copy_selection"},
{Key: "Y", Description: "Copy GitHub permalink", Action: "copy_permalink"},
{Key: "O", Description: "Open file on GitHub", Action: "open_on_github"},
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/panels/preview/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ func TestKeyBindings(t *testing.T) {
bindings := p.KeyBindings()

assert.NotEmpty(t, bindings)
assert.Len(t, bindings, 15)
assert.Len(t, bindings, 16)

// Verify all expected bindings are present
actions := make([]string, len(bindings))
Expand All @@ -802,6 +802,7 @@ func TestKeyBindings(t *testing.T) {
assert.Contains(t, actions, "toggle_diff_mode")
assert.Contains(t, actions, "copy_selection")
assert.Contains(t, actions, "copy_permalink")
assert.Contains(t, actions, "open_on_github")
}

func TestKeysIgnoredWhenBlurred(t *testing.T) {
Expand Down
Loading