Skip to content

Commit 39e9e07

Browse files
committed
Fix colored output on Windows
1 parent fb8a7fb commit 39e9e07

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

cli/output.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,60 @@ package cli
33
import (
44
"errors"
55
"fmt"
6+
7+
"github.com/mattn/go-colorable"
68
)
79

810
var inProgress = false
911

12+
var out = colorable.NewColorableStdout()
13+
1014
func IsInProgress() bool {
1115
return inProgress
1216
}
1317

1418
func Info(format string, a ...any) {
1519
if inProgress {
16-
fmt.Println()
20+
fmt.Fprintln(out)
1721
inProgress = false
1822
}
19-
fmt.Printf("%s\n", fmt.Sprintf(format, a...))
23+
fmt.Fprintf(out, "%s\n", fmt.Sprintf(format, a...))
2024
}
2125

2226
func Begin(format string, a ...any) {
23-
fmt.Printf(format, a...)
27+
fmt.Fprintf(out, format, a...)
2428
inProgress = true
2529
}
2630

2731
func Finish() {
2832
if inProgress {
29-
fmt.Print(" done.\n")
33+
fmt.Fprint(out, " done.\n")
3034
inProgress = false
3135
}
3236
}
3337

3438
func Success(format string, a ...any) {
3539
if inProgress {
36-
fmt.Println()
40+
fmt.Fprintln(out)
3741
inProgress = false
3842
}
39-
fmt.Printf("\x1b[32m%s\x1b[0m\n", fmt.Sprintf(format, a...))
43+
fmt.Fprintf(out, "\x1b[32m%s\x1b[0m\n", fmt.Sprintf(format, a...))
4044
}
4145

4246
func Warn(format string, a ...any) {
4347
if inProgress {
44-
fmt.Println()
48+
fmt.Fprintln(out)
4549
inProgress = false
4650
}
47-
fmt.Printf("\x1b[33mWARNING: %s\x1b[0m\n", fmt.Sprintf(format, a...))
51+
fmt.Fprintf(out, "\x1b[33mWARNING: %s\x1b[0m\n", fmt.Sprintf(format, a...))
4852
}
4953

5054
func Error(format string, a ...any) error {
5155
if inProgress {
52-
fmt.Println()
56+
fmt.Fprintln(out)
5357
inProgress = false
5458
}
5559
message := fmt.Sprintf(format, a...)
56-
fmt.Printf("\x1b[1;31mERROR: %s\x1b[0m\n", message)
60+
fmt.Fprintf(out, "\x1b[1;31mERROR: %s\x1b[0m\n", message)
5761
return errors.New(message)
5862
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ go 1.18
55
require (
66
github.com/AlecAivazis/survey/v2 v2.3.4
77
github.com/adrg/xdg v0.4.0
8+
github.com/mattn/go-colorable v0.1.12
89
github.com/ogier/pflag v0.0.1
910
)
1011

1112
require (
1213
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
13-
github.com/mattn/go-colorable v0.1.12 // indirect
1414
github.com/mattn/go-isatty v0.0.14 // indirect
1515
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
1616
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect

main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ func main() {
3636
switch command {
3737
case "new":
3838
err = commands.New()
39-
if err == cli.ErrCanceled {
40-
cli.Info(err.Error())
41-
}
4239
case "docs":
4340
err = external.OpenBrowser("https://docs.code-game.org")
4441
if err != nil {

0 commit comments

Comments
 (0)