Skip to content

Commit 60b1b29

Browse files
Use fatih/color for more correct colouring (#17)
## Summary <!-- Describe your changes in detail here, if it closes an open issue, include "Closes #<issue>" -->
1 parent 3f9b4b3 commit 60b1b29

File tree

3 files changed

+31
-44
lines changed

3 files changed

+31
-44
lines changed

go.mod

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@ module github.com/FollowTheProcess/snapshot
22

33
go 1.23
44

5-
require golang.org/x/tools v0.29.0
5+
require (
6+
github.com/fatih/color v1.18.0
7+
golang.org/x/tools v0.29.0
8+
)
9+
10+
require (
11+
github.com/mattn/go-colorable v0.1.14 // indirect
12+
github.com/mattn/go-isatty v0.0.20 // indirect
13+
golang.org/x/sys v0.29.0 // indirect
14+
)

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1+
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
2+
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
3+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
5+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
6+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
7+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
9+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
10+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
13+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
114
golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE=
215
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588=

internal/colour/colour.go

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,26 @@
22
package colour
33

44
import (
5-
"os"
6-
"sync"
5+
"github.com/fatih/color"
76
)
87

9-
// ANSI codes for coloured output, they are all the same length so as not to throw off
10-
// alignment of [text/tabwriter].
11-
const (
12-
codeRed = "\x1b[0;0031m" // Red, used for diff lines starting with '-'
13-
codeHeader = "\x1b[1;0036m" // Bold cyan, used for diff headers starting with '@@'
14-
codeGreen = "\x1b[0;0032m" // Green, used for diff lines starting with '+'
15-
codeReset = "\x1b[000000m" // Reset all attributes
8+
var (
9+
red = color.New(color.FgRed)
10+
header = color.New(color.FgCyan, color.Bold)
11+
green = color.New(color.FgGreen)
1612
)
1713

18-
// getColourOnce is a [sync.OnceValues] function that returns the state of
19-
// $NO_COLOR and $FORCE_COLOR, once and only once to avoid us calling
20-
// os.Getenv on every call to a colour function.
21-
var getColourOnce = sync.OnceValues(getColour)
22-
23-
// getColour returns whether $NO_COLOR and $FORCE_COLOR were set.
24-
func getColour() (noColour, forceColour bool) {
25-
no := os.Getenv("NO_COLOR") != ""
26-
force := os.Getenv("FORCE_COLOR") != ""
27-
28-
return no, force
29-
}
30-
3114
// Header returns a diff header styled string.
3215
func Header(text string) string {
33-
return sprint(codeHeader, text)
16+
return header.Sprint(text)
3417
}
3518

3619
// Green returns a green styled string.
3720
func Green(text string) string {
38-
return sprint(codeGreen, text)
21+
return green.Sprint(text)
3922
}
4023

4124
// Red returns a red styled string.
4225
func Red(text string) string {
43-
return sprint(codeRed, text)
44-
}
45-
46-
// sprint returns a string with a given colour and the reset code.
47-
//
48-
// It handles checking for NO_COLOR and FORCE_COLOR.
49-
func sprint(code, text string) string {
50-
noColor, forceColor := getColourOnce()
51-
52-
// $FORCE_COLOR overrides $NO_COLOR
53-
if forceColor {
54-
return code + text + codeReset
55-
}
56-
57-
// $NO_COLOR is next
58-
if noColor {
59-
return text
60-
}
61-
return code + text + codeReset
26+
return red.Sprint(text)
6227
}

0 commit comments

Comments
 (0)