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
3 changes: 2 additions & 1 deletion internal/panels/preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ func (p *Preview) Update(msg tea.Msg) (panels.Panel, tea.Cmd) {
if p.hasSelection() {
return p.copySelection()
}
return p.copyFilePath()
case "escape", "esc":
if p.hasSelection() {
p.clearSelection()
Expand Down Expand Up @@ -631,7 +632,7 @@ func (p *Preview) KeyBindings() []panels.KeyBinding {
{Key: "n", Description: "Toggle line numbers", Action: "toggle_line_numbers"},
{Key: "m", Description: "Toggle markdown render", Action: "toggle_markdown_render"},
{Key: "B", Description: "Toggle blame", Action: "toggle_blame"},
{Key: "y/Ctrl+C", Description: "Copy selection", Action: "copy_selection"},
{Key: "y/Ctrl+C", Description: "Copy selection or file path", Action: "copy_selection"},
}
}

Expand Down
13 changes: 13 additions & 0 deletions internal/panels/preview/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,19 @@ func (p *Preview) copySelection() (panels.Panel, tea.Cmd) {
}
}

func (p *Preview) copyFilePath() (panels.Panel, tea.Cmd) {
if p.filePath == "" || p.ghMode {
return p, nil
}
path := p.filePath
return p, func() tea.Msg {
if err := panels.CopyToClipboard(context.Background(), path); err != nil {
return notify.ShowToastMsg{Message: "Copy failed: " + err.Error(), Level: notify.Error}
}
return notify.ShowToastMsg{Message: "Copied path", Level: notify.Info}
}
}

func pluralize(n int, word string) string {
if n == 1 {
return "1 " + word
Expand Down
19 changes: 19 additions & 0 deletions internal/panels/preview/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,25 @@ func TestKeyY_NoSelection_Noop(t *testing.T) {
assert.Nil(t, cmd)
}

func TestKeyY_NoSelection_CopiesFilePath(t *testing.T) {
p := newTestPreview([]string{"hello"})
p.filePath = `C:\repo\main.go`

_, cmd := p.Update(keyMsg("y"))

assert.NotNil(t, cmd, "should copy the file path when no text is selected")
}

func TestKeyY_GitHubModeDoesNotCopyFilePath(t *testing.T) {
p := newTestPreview([]string{"issue"})
p.filePath = `C:\repo\main.go`
p.ghMode = true

_, cmd := p.Update(keyMsg("y"))

assert.Nil(t, cmd)
}

func TestKeyEscape_ClearsSelection(t *testing.T) {
p := newTestPreview([]string{"hello"})
p.selAnchor = &selPoint{Line: 0, Col: 0}
Expand Down
Loading