Skip to content

Commit b4d28f1

Browse files
committed
Style gh pr list
1 parent 1009ad5 commit b4d28f1

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

command/pr.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package command
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/github/gh-cli/api"
8+
"github.com/github/gh-cli/git"
9+
"github.com/logrusorgru/aurora"
710
"github.com/spf13/cobra"
811
)
912

@@ -37,22 +40,62 @@ func ExecutePr() error {
3740
return err
3841
}
3942

40-
fmt.Printf("Current Pr\n")
43+
printHeader("Current branch")
4144
if prPayload.CurrentPR != nil {
42-
printPr(*prPayload.CurrentPR)
45+
printPrs(*prPayload.CurrentPR)
46+
} else {
47+
message := fmt.Sprintf(" There is no pull request associated with %s", aurora.Cyan("["+currentBranch()+"]"))
48+
printMessage(message)
4349
}
44-
fmt.Printf("Your Prs\n")
45-
for _, pr := range prPayload.ViewerCreated {
46-
printPr(pr)
50+
fmt.Println()
51+
52+
printHeader("Created by you")
53+
if len(prPayload.ViewerCreated) > 0 {
54+
printPrs(prPayload.ViewerCreated...)
55+
} else {
56+
printMessage(" You have no open pull requests")
4757
}
48-
fmt.Printf("Prs you need to review\n")
49-
for _, pr := range prPayload.ReviewRequested {
50-
printPr(pr)
58+
fmt.Println()
59+
60+
printHeader("Requesting a code review from you")
61+
if len(prPayload.ReviewRequested) > 0 {
62+
printPrs(prPayload.ReviewRequested...)
63+
} else {
64+
printMessage(" You have no pull requests to review")
5165
}
66+
fmt.Println()
5267

5368
return nil
5469
}
5570

56-
func printPr(pr api.PullRequest) {
57-
fmt.Printf(" #%d %s [%s]\n", pr.Number, pr.Title, pr.HeadRefName)
71+
func printPrs(prs ...api.PullRequest) {
72+
for _, pr := range prs {
73+
fmt.Printf(" #%d %s [%s]\n", pr.Number, truncateTitle(pr.Title), aurora.Cyan(pr.HeadRefName))
74+
}
75+
}
76+
77+
func printHeader(s string) {
78+
fmt.Println(aurora.Bold(s))
79+
}
80+
81+
func printMessage(s string) {
82+
fmt.Println(aurora.Gray(8, s))
83+
}
84+
85+
func truncateTitle(title string) string {
86+
const maxLength = 50
87+
88+
if len(title) > maxLength {
89+
return title[0:maxLength-3] + "..."
90+
}
91+
return title
92+
}
93+
94+
func currentBranch() string {
95+
currentBranch, err := git.Head()
96+
if err != nil {
97+
panic(err)
98+
}
99+
100+
return strings.Replace(currentBranch, "refs/heads/", "", 1)
58101
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.13
55
require (
66
github.com/BurntSushi/toml v0.3.1
77
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
8+
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b
89
github.com/mattn/go-colorable v0.1.2
910
github.com/mattn/go-isatty v0.0.9
1011
github.com/mitchellh/go-homedir v1.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
1212
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
1313
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
1414
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
15+
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b h1:PMbSa9CgaiQR9NLlUTwKi+7aeLl3GG5JX5ERJxfQ3IE=
16+
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
1517
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
1618
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
1719
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=

0 commit comments

Comments
 (0)