Skip to content

Commit

Permalink
feat: added copy checkout command to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
mlveggo committed Aug 19, 2022
1 parent fe273ce commit a09e25b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/einride/gh-dependabot

go 1.17
go 1.18

require (
github.com/atotto/clipboard v0.1.4
github.com/charmbracelet/bubbles v0.13.0
github.com/charmbracelet/bubbletea v0.22.0
github.com/charmbracelet/lipgloss v0.5.0
Expand All @@ -11,7 +12,6 @@ require (
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand Down
7 changes: 5 additions & 2 deletions pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"path"
"time"

"github.com/charmbracelet/bubbles/list"
Expand All @@ -22,13 +23,14 @@ type pullRequest struct {
mergeable githubv4.MergeableState
reviewDecision githubv4.PullRequestReviewDecision
bodyText string
number string
}

var _ list.DefaultItem = pullRequest{}

// Title implements list.DefaultItem.
func (p pullRequest) Title() string {
return p.repository
return p.repository + " #" + p.number
}

// Description implements list.DefaultItem.
Expand All @@ -51,7 +53,7 @@ func (p pullRequest) Description() string {

// FilterValue implements list.DefaultItem.
func (p pullRequest) FilterValue() string {
return p.repository + " " + p.title
return p.repository + " " + p.title + " #" + p.number
}

func checkStatusEmoji(c githubv4.StatusState) string {
Expand Down Expand Up @@ -171,6 +173,7 @@ func loadPullRequestPage(client *githubv4.Client, prQuery pullRequestQuery) (*pu
state: node.PullRequest.State,
mergeable: node.PullRequest.Mergeable,
reviewDecision: node.PullRequest.ReviewDecision,
number: path.Base(node.PullRequest.URL),
})
if len(node.PullRequest.Commits.Nodes) > 0 {
state := node.PullRequest.Commits.Nodes[0].Commit.StatusCheckRollup.State
Expand Down
14 changes: 14 additions & 0 deletions tui_cmds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/atotto/clipboard"
tea "github.com/charmbracelet/bubbletea"
"github.com/einride/gh-dependabot/internal/gh"
)
Expand Down Expand Up @@ -83,3 +84,16 @@ func viewPullRequestDetailsCmd(pr pullRequest) tea.Cmd {
return viewPullRequestDetails{pr: pr}
}
}

type copyCheckout struct {
pr pullRequest
}

func copyCheckoutCmd(pr pullRequest) tea.Cmd {
return func() tea.Msg {
if err := clipboard.WriteAll("gh pr checkout " + pr.number); err != nil {
return errorMessage{err: err}
}
return copyCheckout{pr: pr}
}
}
10 changes: 10 additions & 0 deletions tui_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type keyMap struct {
rebase key.Binding
view key.Binding
browse key.Binding // open PR in default browser.
copyCheckout key.Binding
}

func newKeyMap() *keyMap {
Expand Down Expand Up @@ -53,6 +54,10 @@ func newKeyMap() *keyMap {
key.WithKeys("v"),
key.WithHelp("v", "view details"),
),
copyCheckout: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "copy checkout command"),
),
}
}

Expand All @@ -65,6 +70,7 @@ func (d keyMap) Bindings() []key.Binding {
d.rebase,
d.view,
d.browse,
d.copyCheckout,
}
}

Expand Down Expand Up @@ -162,6 +168,10 @@ func (m ListView) Update(msg tea.Msg) (ListView, tea.Cmd) {
if selectedItem, ok := m.listModel.SelectedItem().(pullRequest); ok {
cmds = append(cmds, viewPullRequestDetailsCmd(selectedItem))
}
case key.Matches(msg, m.keyMap.copyCheckout):
if selectedItem, ok := m.listModel.SelectedItem().(pullRequest); ok {
cmds = append(cmds, copyCheckoutCmd(selectedItem))
}
}
}
newListModel, cmd := m.listModel.Update(msg)
Expand Down

0 comments on commit a09e25b

Please sign in to comment.