Skip to content

Commit 7e2f1c7

Browse files
Merge pull request cli#22 from github/style-pr-list
Style `gh pr list`
2 parents 0e18bb6 + 9c21807 commit 7e2f1c7

File tree

4 files changed

+105
-10
lines changed

4 files changed

+105
-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/github/gh-cli/utils"
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", utils.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), utils.Cyan("["+pr.HeadRefName+"]"))
74+
}
75+
}
76+
77+
func printHeader(s string) {
78+
fmt.Println(utils.Bold(s))
79+
}
80+
81+
func printMessage(s string) {
82+
fmt.Println(utils.Gray(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
@@ -4,6 +4,7 @@ go 1.13
44

55
require (
66
github.com/BurntSushi/toml v0.3.1
7+
github.com/gookit/color v1.2.0
78
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
89
github.com/mattn/go-colorable v0.1.2
910
github.com/mattn/go-isatty v0.0.9

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc
55
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
66
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
77
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
8+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
810
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
911
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
12+
github.com/gookit/color v1.2.0 h1:lHA77Kuyi5JpBnA9ESvwkY+nanLjRZ0mHbWQXRYk2Lk=
13+
github.com/gookit/color v1.2.0/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg=
1014
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
1115
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
1216
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
@@ -22,6 +26,7 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
2226
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
2327
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
2428
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
29+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2530
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2631
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
2732
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
@@ -32,7 +37,10 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6
3237
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
3338
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
3439
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
40+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3541
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
42+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
43+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3644
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
3745
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
3846
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

utils/color.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package utils
2+
3+
import "github.com/gookit/color"
4+
5+
func Black(a ...interface{}) string {
6+
return color.Black.Render(a...)
7+
}
8+
9+
func White(a ...interface{}) string {
10+
return color.White.Render(a...)
11+
}
12+
13+
func Gray(a ...interface{}) string {
14+
return color.Gray.Render(a...)
15+
}
16+
17+
func Red(a ...interface{}) string {
18+
return color.Red.Render(a...)
19+
}
20+
21+
func Green(a ...interface{}) string {
22+
return color.Green.Render(a...)
23+
}
24+
25+
func Yellow(a ...interface{}) string {
26+
return color.Yellow.Render(a...)
27+
}
28+
29+
func Blue(a ...interface{}) string {
30+
return color.Blue.Render(a...)
31+
}
32+
33+
func Magenta(a ...interface{}) string {
34+
return color.Magenta.Render(a...)
35+
}
36+
37+
func Cyan(a ...interface{}) string {
38+
return color.Cyan.Render(a...)
39+
}
40+
41+
func Bold(a ...interface{}) string {
42+
return color.Bold.Render(a...)
43+
}

0 commit comments

Comments
 (0)