Skip to content

Commit

Permalink
Move to fatih/color for proper colouring (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Jan 11, 2025
1 parent a85d96c commit 6871566
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 53 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ jobs:
permissions:
contents: read
uses: FollowTheProcess/ci/.github/workflows/Go.yml@v1
with:
env: '{"NO_COLOR": "true"}'
2 changes: 0 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ tasks:
- testdata/**/*.snap.txt
cmds:
- go test -race ./... {{ .CLI_ARGS }}
env:
NO_COLOR: true

bench:
desc: Run all project benchmarks
Expand Down
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ module github.com/FollowTheProcess/test
go 1.23

require (
github.com/FollowTheProcess/snapshot v0.1.0
golang.org/x/tools v0.28.0
github.com/FollowTheProcess/snapshot v0.2.0
github.com/fatih/color v1.18.0
golang.org/x/tools v0.29.0
)

require (
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.29.0 // indirect
)
17 changes: 13 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
github.com/FollowTheProcess/snapshot v0.1.0 h1:G5tWpBXVre6cTrmN1OWZLiTnqEi9IKLiewtQQYOnQqc=
github.com/FollowTheProcess/snapshot v0.1.0/go.mod h1:sJv2oq83QK5Yj6+JVts8fQIaAJjQfsxN/WtGqX8oKIg=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
github.com/FollowTheProcess/snapshot v0.2.0 h1:9pJ50rK5LCX1n6PSrCDZWlM0+m2fh3RUTAjfdVam0/A=
github.com/FollowTheProcess/snapshot v0.2.0/go.mod h1:TM4sJgVLQHuQi+zCNrTUYxw2WtiSHdzLycVO8ix4AZ4=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE=
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588=
51 changes: 8 additions & 43 deletions internal/colour/colour.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,26 @@
package colour

import (
"os"
"sync"
"github.com/fatih/color"
)

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

// getColourOnce is a [sync.OnceValues] function that returns the state of
// $NO_COLOR and $FORCE_COLOR, once and only once to avoid us calling
// os.Getenv on every call to a colour function.
var getColourOnce = sync.OnceValues(getColour)

// getColour returns whether $NO_COLOR and $FORCE_COLOR were set.
func getColour() (noColour bool, forceColour bool) {
no := os.Getenv("NO_COLOR") != ""
force := os.Getenv("FORCE_COLOR") != ""

return no, force
}

// Header returns a diff header styled string.
func Header(text string) string {
return sprint(codeHeader, text)
return header.Sprint(text)
}

// Green returns a green styled string.
func Green(text string) string {
return sprint(codeGreen, text)
return green.Sprint(text)
}

// Red returns a red styled string.
func Red(text string) string {
return sprint(codeRed, text)
}

// sprint returns a string with a given colour and the reset code.
//
// It handles checking for NO_COLOR and FORCE_COLOR.
func sprint(code, text string) string {
noColor, forceColor := getColourOnce()

// $FORCE_COLOR overrides $NO_COLOR
if forceColor {
return code + text + codeReset
}

// $NO_COLOR is next
if noColor {
return text
}
return code + text + codeReset
return red.Sprint(text)
}

0 comments on commit 6871566

Please sign in to comment.