Skip to content

Commit

Permalink
Merge pull request #501 from Vanello1908/ps-output
Browse files Browse the repository at this point in the history
PS color output fix
  • Loading branch information
daveyarwood authored Aug 4, 2024
2 parents 93b8d42 + fd32c27 commit f42f8e7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
3 changes: 3 additions & 0 deletions client/color/color.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !windows

package color

import (
Expand Down Expand Up @@ -41,5 +43,6 @@ func init() {
// config option so that we can disable color manually.
//
// See the longer comment above EnableColor.

Aurora = auroraLib.NewAurora(EnableColor)
}
61 changes: 61 additions & 0 deletions client/color/color_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package color

import (
"os"

auroraLib "github.com/logrusorgru/aurora"
"github.com/mattn/go-isatty"
"golang.org/x/sys/windows/registry"
)

// We're using a couple of libraries that produce ANSI escape sequences to print
// colored text:
//
// * aurora
// * zerolog
//
// Ideally, both of these libraries would support the standard NO_COLOR
// environment variable, but they don't, so we need to handle it ourselves.
// Fortunately, both libraries include an option to disable colors, so we can
// check for NO_COLOR ourselves and disable color manually if needed.
//
// Another consideration is that not all terminal environments support ANSI
// escape codes (the Windows 7 CMD terminal doesn't, for example). So, we try to
// detect that scenario and disable colors. It would be good to do this in a
// more robust way, something like checking the terminfo/termcap capabilities of
// the terminal to see if it's capable of interpreting ANSI escape sequences,
// but that seems complex and error prone and I'm not really sure that it's
// worth it. So I'm taking a more basic approach here, where we just check to
// see if the terminal is a TTY and assume that if it is a TTY, then it can
// probably interpret ANSI escape sequences.
//
// Reference:
// https://github.com/logrusorgru/aurora/issues/2
// https://eklitzke.org/ansi-color-codes
var EnableColor = isatty.IsTerminal(os.Stdout.Fd()) &&
len(os.Getenv("NO_COLOR")) == 0

var Aurora auroraLib.Aurora

func init() {
// Check registry for enabled color printing(only for windows)

var key, err = registry.OpenKey(registry.CURRENT_USER, "Console", registry.QUERY_VALUE)
if err != nil {
EnableColor = false
} else {
var val, _, err = key.GetIntegerValue("VirtualTerminalLevel")
if err != nil {
EnableColor = false
} else if val == 0 {
EnableColor = false
}
}

// HACK: Ideally, aurora would support NO_COLOR, but at least they give us a
// config option so that we can disable color manually.
//
// See the longer comment above EnableColor.

Aurora = auroraLib.NewAurora(EnableColor)
}
4 changes: 2 additions & 2 deletions client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.1.1
github.com/jackpal/bencode-go v1.0.0
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mattn/go-isatty v0.0.8
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/rs/zerolog v1.21.0
Expand All @@ -36,7 +36,7 @@ require (
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.4.0 // indirect
)
4 changes: 4 additions & 0 deletions client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNU
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381 h1:bqDmpDG49ZRnB5PcgP0RXtQvnMSgIF14M7CBd2shtXs=
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
Expand Down Expand Up @@ -106,6 +108,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down

0 comments on commit f42f8e7

Please sign in to comment.