Skip to content

Commit

Permalink
Merge pull request #8 from SimonRichardson/no_color
Browse files Browse the repository at this point in the history
#8

The following supports the NO_COLOR standard. The idea is that we don't
write color to the term if the env var of NO_COLOR is set to anything
that can be truthy.

This brings juju inline with more new CLI.

See: https://no-color.org/
  • Loading branch information
jujubot committed Sep 29, 2021
2 parents 9283cdf + 496af2e commit 8b71cc9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/juju/ansiterm

go 1.14

require (
github.com/lunixbochs/vtclean v1.0.0
github.com/mattn/go-colorable v0.1.10
github.com/mattn/go-isatty v0.0.14
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lunixbochs/vtclean v1.0.0 h1:xu2sLAri4lGiovBDQKxl5mrXyESr3gUr5m5SM5+LVb8=
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/mattn/go-colorable v0.1.10 h1:KWqbp83oZ6YOEgIbNW3BM1Jbe2tz4jgmWA9FOuAF8bw=
github.com/mattn/go-colorable v0.1.10/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
12 changes: 12 additions & 0 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ func colorEnabledWriter(w io.Writer) (io.Writer, bool) {
if !ok {
return w, false
}

// NO_COLOR is a relatively new standard for preventing color enabled
// writers rather than using the TERM env.
//
// "...should check for the presence of a NO_COLOR environment variable
// that, when present (regardless of its value), prevents the addition of
// ANSI color."
// See: https://no-color.org/
if _, ok := os.LookupEnv("NO_COLOR"); ok {
return w, false
}

// Check the TERM environment variable specifically
// to check for "dumb" terminals.
if os.Getenv("TERM") == "dumb" {
Expand Down
40 changes: 40 additions & 0 deletions terminal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package ansiterm

import (
"io/ioutil"
"os"

gc "gopkg.in/check.v1"
)

type colorWriterSuite struct{}

var _ = gc.Suite(&colorWriterSuite{})

func (s *colorWriterSuite) TestNoColor(c *gc.C) {
file, err := ioutil.TempFile("", "")
c.Assert(err, gc.IsNil)

os.Setenv("NO_COLOR", "")
defer os.Unsetenv("NO_COLOR")

writer, ok := colorEnabledWriter(file)
c.Assert(ok, gc.Equals, false)
c.Assert(writer, gc.Equals, file)

}

func (s *colorWriterSuite) TestNoColorEvenIfFalse(c *gc.C) {
file, err := ioutil.TempFile("", "")
c.Assert(err, gc.IsNil)

os.Setenv("NO_COLOR", "false")
defer os.Unsetenv("NO_COLOR")

writer, ok := colorEnabledWriter(file)
c.Assert(ok, gc.Equals, false)
c.Assert(writer, gc.Equals, file)
}

0 comments on commit 8b71cc9

Please sign in to comment.