diff --git a/go.mod b/go.mod index 997bfd4..6378f80 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,13 @@ module github.com/FollowTheProcess/snapshot go 1.23 -require golang.org/x/tools v0.29.0 +require ( + 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 +) diff --git a/go.sum b/go.sum index f44ce0e..a35824c 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,15 @@ +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.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +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.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +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.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +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= diff --git a/internal/colour/colour.go b/internal/colour/colour.go index e50b03b..a335c49 100644 --- a/internal/colour/colour.go +++ b/internal/colour/colour.go @@ -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 ( + red = color.New(color.FgRed) + header = color.New(color.FgCyan, color.Bold) + green = color.New(color.FgGreen) ) -// 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, 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) }