Skip to content

Commit a0ec23a

Browse files
authored
(llorllale#27) Colored output (llorllale#29)
Also, issues.Collected() now collects all issues of a commit, not just the first one encountered.
1 parent 1804526 commit a0ec23a

File tree

7 files changed

+39
-15
lines changed

7 files changed

+39
-15
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ module github.com/llorllale/go-gitlint
33
require (
44
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
55
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
6+
github.com/fatih/color v1.7.0
67
github.com/google/uuid v1.1.0
8+
github.com/mattn/go-colorable v0.1.1 // indirect
9+
github.com/mattn/go-isatty v0.0.6 // indirect
710
github.com/stretchr/testify v1.2.2
811
gopkg.in/alecthomas/kingpin.v2 v2.2.6
912
gopkg.in/src-d/go-git.v4 v4.10.0

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1010
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1111
github.com/emirpasic/gods v1.9.0 h1:rUF4PuzEjMChMiNsVjdI+SyLu7rEqpQ5reNFnhC7oFo=
1212
github.com/emirpasic/gods v1.9.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
13+
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
14+
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
1315
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
1416
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
1517
github.com/gliderlabs/ssh v0.1.1 h1:j3L6gSLQalDETeEg/Jg0mGY0/y/N6zI2xX1978P0Uqw=
@@ -28,6 +30,11 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
2830
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
2931
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
3032
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
33+
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
34+
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
35+
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
36+
github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA=
37+
github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
3138
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
3239
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
3340
github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA=
@@ -50,6 +57,8 @@ golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9
5057
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
5158
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilGMUjI+Uy2Xu2JLUtTas=
5259
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
60+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
61+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5362
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
5463
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5564
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=

internal/commits/commits.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ func (c *Commit) ID() string {
4141
return c.Hash
4242
}
4343

44+
// ShortID returns the commit hash's short form.
45+
func (c *Commit) ShortID() string {
46+
return c.Hash[:7]
47+
}
48+
4449
// Subject is the commit message's subject line.
4550
func (c *Commit) Subject() string {
4651
return strings.Split(c.Message, "\n\n")[0]

internal/commits/commits_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ func TestCommitID(t *testing.T) {
3737
"Commit.ID() must return the commit's hash")
3838
}
3939

40+
func TestCommitShortID(t *testing.T) {
41+
const ID = "c26cf8af130955c5c67cfea96f9532680b963628"
42+
assert.Equal(t,
43+
(&Commit{Hash: ID}).ShortID(),
44+
ID[:7],
45+
"Commit.ShortID() must equal the first 7 characters of the commit's hash")
46+
}
47+
4048
func TestCommitSubject(t *testing.T) {
4149
const subject = "test subject"
4250
assert.Equal(t,

internal/issues/filters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func OfSubjectLength(length int) Filter {
9090
var issue Issue
9191
if len(c.Subject()) > length {
9292
issue = Issue{
93-
Desc: fmt.Sprintf("subject is longer than %d", length),
93+
Desc: fmt.Sprintf("subject exceeds length [%d]", length),
9494
Commit: *c,
9595
}
9696
}

internal/issues/issues.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
package issues
1616

1717
import (
18-
"fmt"
1918
"io"
2019

20+
"github.com/fatih/color"
2121
"github.com/llorllale/go-gitlint/internal/commits"
2222
)
2323

@@ -27,10 +27,6 @@ type Issue struct {
2727
Commit commits.Commit
2828
}
2929

30-
func (i *Issue) String() string {
31-
return fmt.Sprintf("Issue{Desc=%s Commit=%+v}", i.Desc, i.Commit)
32-
}
33-
3430
// Issues is a collection of Issues.
3531
type Issues func() []Issue
3632

@@ -42,7 +38,6 @@ func Collected(filters []Filter, cmts commits.Commits) Issues {
4238
for _, f := range filters {
4339
if issue := f(c); issue != (Issue{}) {
4440
issues = append(issues, issue)
45-
break
4641
}
4742
}
4843
}
@@ -54,10 +49,13 @@ func Collected(filters []Filter, cmts commits.Commits) Issues {
5449
func Printed(w io.Writer, sep string, issues Issues) Issues {
5550
return func() []Issue {
5651
iss := issues()
57-
for i := range iss {
58-
_, err := w.Write(
59-
[]byte(fmt.Sprintf("%s%s", iss[i].String(), sep)),
60-
)
52+
for idx := range iss {
53+
i := iss[idx]
54+
_, err := color.New(color.Bold).Fprintf(w, "%s: ", i.Commit.ShortID())
55+
if err != nil {
56+
panic(err)
57+
}
58+
_, err = color.New(color.FgRed).Fprintf(w, "%s%s", i.Desc, sep)
6159
if err != nil {
6260
panic(err)
6361
}

internal/issues/issues_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package issues
1616

1717
import (
18+
"fmt"
1819
"testing"
1920

2021
"github.com/llorllale/go-gitlint/internal/commits"
@@ -60,21 +61,21 @@ func TestPrinted(t *testing.T) {
6061
{
6162
Desc: "issueA",
6263
Commit: commits.Commit{
63-
Hash: "1",
64+
Hash: "18045269d8d2fd8f53d01883c6c7b548d0b9e3ae",
6465
Message: "first commit",
6566
},
6667
},
6768
{
6869
Desc: "issueB",
6970
Commit: commits.Commit{
70-
Hash: "2",
71+
Hash: "4be918ff8bfc91de77a1462707a8d2eb30956f93",
7172
Message: "second commit",
7273
},
7374
},
7475
}
7576
var expected string
7677
for _, i := range issues {
77-
expected = expected + i.String() + sep
78+
expected += fmt.Sprintf("%s: %s%s", i.Commit.ShortID(), i.Desc, sep)
7879
}
7980
writer := &mockWriter{}
8081
Printed(
@@ -85,7 +86,7 @@ func TestPrinted(t *testing.T) {
8586
)()
8687
assert.Equal(t,
8788
expected, writer.msg,
88-
"issues.Printed() must concatenate Issue.String() with the separator")
89+
"issues.Printed() must join Commit.ShortID() and the Issue.Desc with the separator")
8990
}
9091

9192
type mockWriter struct {

0 commit comments

Comments
 (0)