Skip to content

Commit e625508

Browse files
authored
Merge pull request #207 from go-task/colored-output
Use colors for some output messages
2 parents 4e09fc7 + 623db0e commit e625508

File tree

15 files changed

+102
-52
lines changed

15 files changed

+102
-52
lines changed

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: '2'
1+
version: '3'
22

33
includes:
44
docs: ./docs

cmd/task/task.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/go-task/task/v2"
1111
"github.com/go-task/task/v2/internal/args"
12+
"github.com/go-task/task/v2/internal/logger"
1213

1314
"github.com/spf13/pflag"
1415
)
@@ -59,6 +60,7 @@ func main() {
5960
summary bool
6061
dir string
6162
output string
63+
color bool
6264
)
6365

6466
pflag.BoolVar(&versionFlag, "version", false, "show Task version")
@@ -73,6 +75,7 @@ func main() {
7375
pflag.BoolVar(&summary, "summary", false, "show summary about a task")
7476
pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution")
7577
pflag.StringVarP(&output, "output", "o", "", "sets output style: [interleaved|group|prefixed]")
78+
pflag.BoolVarP(&color, "color", "c", true, "colored output")
7679
pflag.Parse()
7780

7881
if versionFlag {
@@ -99,6 +102,7 @@ func main() {
99102
Dir: dir,
100103
Dry: dry,
101104
Summary: summary,
105+
Color: color,
102106

103107
Stdin: os.Stdin,
104108
Stdout: os.Stdout,
@@ -117,7 +121,7 @@ func main() {
117121

118122
arguments := pflag.Args()
119123
if len(arguments) == 0 {
120-
log.Println("task: No argument given, trying default task")
124+
e.Logger.Errf(logger.Yellow, "task: No argument given, trying default task")
121125
arguments = []string{"default"}
122126
}
123127

@@ -139,7 +143,8 @@ func main() {
139143
}
140144

141145
if err := e.Run(ctx, calls...); err != nil {
142-
log.Fatal(err)
146+
e.Logger.Errf(logger.Red, "%v", err)
147+
os.Exit(1)
143148
}
144149
}
145150

docs/Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: '2'
1+
version: '3'
22

33
tasks:
44
install:

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
module github.com/go-task/task/v2
22

33
require (
4+
github.com/fatih/color v1.7.0
45
github.com/go-task/slim-sprig v0.0.0-20190623010546-24867827a98b
56
github.com/kr/pretty v0.1.0 // indirect
7+
github.com/mattn/go-colorable v0.1.2 // indirect
68
github.com/mattn/go-zglob v0.0.1
79
github.com/mitchellh/go-homedir v1.0.0
810
github.com/radovskyb/watcher v1.0.5
@@ -11,7 +13,6 @@ require (
1113
golang.org/x/crypto v0.0.0-20180830192347-182538f80094 // indirect
1214
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d // indirect
1315
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f
14-
golang.org/x/sys v0.0.0-20180831094639-fa5fdf94c789 // indirect
1516
gopkg.in/yaml.v2 v2.2.1
1617
mvdan.cc/sh v2.6.4+incompatible
1718
)

go.sum

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
5+
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
46
github.com/go-task/slim-sprig v0.0.0-20190623010546-24867827a98b h1:z6iCP1USASmEZtKTzynd/rP4vOtBLlsD3v24wItbJIs=
57
github.com/go-task/slim-sprig v0.0.0-20190623010546-24867827a98b/go.mod h1:XLIiFDBy2M8pA/fEL5rx9xr2EAzrDEO0S5brm5iekOE=
68
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
79
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
810
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
911
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1012
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
13+
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
14+
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
15+
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
16+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
1117
github.com/mattn/go-zglob v0.0.1 h1:xsEx/XUoVlI6yXjqBK062zYhRTZltCNmYPx6v+8DNaY=
1218
github.com/mattn/go-zglob v0.0.1/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
1319
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
@@ -28,8 +34,8 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPF
2834
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
2935
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
3036
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
31-
golang.org/x/sys v0.0.0-20180831094639-fa5fdf94c789 h1:T8D7l6WB3tLu+VpKvw06ieD/OhBi1XpJmG1U/FtttZg=
32-
golang.org/x/sys v0.0.0-20180831094639-fa5fdf94c789/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
37+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
38+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3339
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3440
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3541
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=

help.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import (
55
"sort"
66
"text/tabwriter"
77

8+
"github.com/go-task/task/v2/internal/logger"
89
"github.com/go-task/task/v2/internal/taskfile"
910
)
1011

1112
// PrintTasksHelp prints help os tasks that have a description
1213
func (e *Executor) PrintTasksHelp() {
1314
tasks := e.tasksWithDesc()
1415
if len(tasks) == 0 {
15-
e.Logger.Outf("task: No tasks with description available")
16+
e.Logger.Outf(logger.Yellow, "task: No tasks with description available")
1617
return
1718
}
18-
e.Logger.Outf("task: Available tasks for this project:")
19+
e.Logger.Outf(logger.Default, "task: Available tasks for this project:")
1920

2021
// Format in tab-separated columns with a tab stop of 8.
2122
w := tabwriter.NewWriter(e.Stdout, 0, 8, 0, '\t', 0)

internal/compiler/v1/compiler_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (c *CompilerV1) HandleDynamicVar(v taskfile.Var) (string, error) {
131131
result := strings.TrimSuffix(stdout.String(), "\n")
132132

133133
c.dynamicCache[v.Sh] = result
134-
c.Logger.VerboseErrf(`task: dynamic variable: '%s' result: '%s'`, v.Sh, result)
134+
c.Logger.VerboseErrf(logger.Magenta, `task: dynamic variable: '%s' result: '%s'`, v.Sh, result)
135135

136136
return result, nil
137137
}

internal/compiler/v2/compiler_v2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (c *CompilerV2) HandleDynamicVar(v taskfile.Var) (string, error) {
103103
result := strings.TrimSuffix(stdout.String(), "\n")
104104

105105
c.dynamicCache[v.Sh] = result
106-
c.Logger.VerboseErrf(`task: dynamic variable: '%s' result: '%s'`, v.Sh, result)
106+
c.Logger.VerboseErrf(logger.Magenta, `task: dynamic variable: '%s' result: '%s'`, v.Sh, result)
107107

108108
return result, nil
109109
}

internal/logger/logger.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,65 @@
11
package logger
22

33
import (
4-
"fmt"
54
"io"
5+
6+
"github.com/fatih/color"
7+
)
8+
9+
type PrintFunc func(io.Writer, string, ...interface{})
10+
11+
var (
12+
Default PrintFunc = color.New(color.Reset).FprintfFunc()
13+
Bold PrintFunc = color.New(color.Bold).FprintfFunc()
14+
Blue PrintFunc = color.New(color.FgBlue, color.Bold).FprintfFunc()
15+
Green PrintFunc = color.New(color.FgGreen, color.Bold).FprintfFunc()
16+
Cyan PrintFunc = color.New(color.FgCyan, color.Bold).FprintfFunc()
17+
Yellow PrintFunc = color.New(color.FgYellow, color.Bold).FprintfFunc()
18+
Magenta PrintFunc = color.New(color.FgMagenta, color.Bold).FprintfFunc()
19+
Red PrintFunc = color.New(color.FgRed, color.Bold).FprintfFunc()
620
)
721

22+
// Logger is just a wrapper that prints stuff to STDOUT or STDERR,
23+
// with optional color.
824
type Logger struct {
925
Stdout io.Writer
1026
Stderr io.Writer
1127
Verbose bool
28+
Color bool
1229
}
1330

14-
func (l *Logger) Outf(s string, args ...interface{}) {
31+
// Outf prints stuff to STDOUT.
32+
func (l *Logger) Outf(print PrintFunc, s string, args ...interface{}) {
1533
if len(args) == 0 {
1634
s, args = "%s", []interface{}{s}
1735
}
18-
fmt.Fprintf(l.Stdout, s+"\n", args...)
36+
if !l.Color {
37+
print = Default
38+
}
39+
print(l.Stdout, s+"\n", args...)
1940
}
2041

21-
func (l *Logger) VerboseOutf(s string, args ...interface{}) {
42+
// VerboseOutf prints stuff to STDOUT if verbose mode is enabled.
43+
func (l *Logger) VerboseOutf(print PrintFunc, s string, args ...interface{}) {
2244
if l.Verbose {
23-
l.Outf(s, args...)
45+
l.Outf(print, s, args...)
2446
}
2547
}
2648

27-
func (l *Logger) Errf(s string, args ...interface{}) {
49+
// Errf prints stuff to STDERR.
50+
func (l *Logger) Errf(print PrintFunc, s string, args ...interface{}) {
2851
if len(args) == 0 {
2952
s, args = "%s", []interface{}{s}
3053
}
31-
fmt.Fprintf(l.Stderr, s+"\n", args...)
54+
if !l.Color {
55+
print = Default
56+
}
57+
print(l.Stderr, s+"\n", args...)
3258
}
3359

34-
func (l *Logger) VerboseErrf(s string, args ...interface{}) {
60+
// VerboseErrf prints stuff to STDERR if verbose mode is enabled.
61+
func (l *Logger) VerboseErrf(print PrintFunc, s string, args ...interface{}) {
3562
if l.Verbose {
36-
l.Errf(s, args...)
63+
l.Errf(print, s, args...)
3764
}
3865
}

internal/summary/summary.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func printSpaceBetweenSummaries(l *logger.Logger, i int) {
2020
return
2121
}
2222

23-
l.Outf("")
24-
l.Outf("")
23+
l.Outf(logger.Default, "")
24+
l.Outf(logger.Default, "")
2525
}
2626

2727
func PrintTask(l *logger.Logger, t *taskfile.Task) {
@@ -50,38 +50,38 @@ func printTaskSummary(l *logger.Logger, t *taskfile.Task) {
5050
for i, line := range lines {
5151
notLastLine := i+1 < len(lines)
5252
if notLastLine || line != "" {
53-
l.Outf(line)
53+
l.Outf(logger.Default, line)
5454
}
5555
}
5656
}
5757

5858
func printTaskName(l *logger.Logger, t *taskfile.Task) {
59-
l.Outf("task: %s", t.Task)
60-
l.Outf("")
59+
l.Outf(logger.Default, "task: %s", t.Task)
60+
l.Outf(logger.Default, "")
6161
}
6262

6363
func hasDescription(t *taskfile.Task) bool {
6464
return t.Desc != ""
6565
}
6666

6767
func printTaskDescription(l *logger.Logger, t *taskfile.Task) {
68-
l.Outf(t.Desc)
68+
l.Outf(logger.Default, t.Desc)
6969
}
7070

7171
func printNoDescriptionOrSummary(l *logger.Logger) {
72-
l.Outf("(task does not have description or summary)")
72+
l.Outf(logger.Default, "(task does not have description or summary)")
7373
}
7474

7575
func printTaskDependencies(l *logger.Logger, t *taskfile.Task) {
7676
if len(t.Deps) == 0 {
7777
return
7878
}
7979

80-
l.Outf("")
81-
l.Outf("dependencies:")
80+
l.Outf(logger.Default, "")
81+
l.Outf(logger.Default, "dependencies:")
8282

8383
for _, d := range t.Deps {
84-
l.Outf(" - %s", d.Task)
84+
l.Outf(logger.Default, " - %s", d.Task)
8585
}
8686
}
8787

@@ -90,14 +90,14 @@ func printTaskCommands(l *logger.Logger, t *taskfile.Task) {
9090
return
9191
}
9292

93-
l.Outf("")
94-
l.Outf("commands:")
93+
l.Outf(logger.Default, "")
94+
l.Outf(logger.Default, "commands:")
9595
for _, c := range t.Cmds {
9696
isCommand := c.Cmd != ""
9797
if isCommand {
98-
l.Outf(" - %s", c.Cmd)
98+
l.Outf(logger.Default, " - %s", c.Cmd)
9999
} else {
100-
l.Outf(" - Task: %s", c.Task)
100+
l.Outf(logger.Default, " - Task: %s", c.Task)
101101
}
102102
}
103103
}

0 commit comments

Comments
 (0)