Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Windows colors in sensuctl #2922

Merged
merged 6 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic
Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed
- Fixed `sensuctl` color output on Windows.

## [5.6.0] - 2019-04-30

### Added
Expand Down
28 changes: 19 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,7 @@ required = [
[[constraint]]
branch = "master"
name = "golang.org/x/sys"

[[constraint]]
name = "github.com/mattn/go-colorable"
version = "0.1.1"
4 changes: 4 additions & 0 deletions cli/commands/root/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package root

import (
"github.com/mattn/go-colorable"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/client/config"
"github.com/sensu/sensu-go/cli/commands/version"
Expand All @@ -19,6 +20,9 @@ func Command() *cobra.Command {
},
}

// Use colorable (fixes Windows color support)
cmd.SetOutput(colorable.NewColorableStdout())

// Templates
cmd.SetUsageTemplate(usageTemplate)

Expand Down
5 changes: 3 additions & 2 deletions cli/commands/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
func TestCommand(t *testing.T) {
assert := assert.New(t)
stdout := test.NewFileCapture(&os.Stdout)

// Run command w/o any flags
stdout.Start()
cmd := Command()

assert.NotNil(cmd, "Returns a Command instance")
assert.Equal("sensuctl", cmd.Use, "Configures the name")

// Run command w/o any flags
stdout.Start()
cmd.Run(cmd, []string{})
stdout.Stop()
assert.Regexp("Usage:", stdout.Output())
Expand Down
25 changes: 21 additions & 4 deletions cli/elements/globals/colors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package globals

import (
"runtime"
"strings"

"github.com/mgutz/ansi"
Expand All @@ -24,15 +25,31 @@ var (

// SuccessStyle is used to format strings to indicate a successfull operation
SuccessStyle = ansi.ColorFunc("green+bh")

// BooleanTrueStyle is used to format strings that indicate a boolean true value
BooleanTrueStyle = ansi.ColorFunc("blue")

// BooleanFalseStyle is used to format strings that indicate a boolean false value
BooleanFalseStyle = ansi.ColorFunc("red")
)

func init() {
if runtime.GOOS == "windows" {
TitleStyle = ansi.ColorFunc("default+b")
PrimaryTextStyle = ansi.ColorFunc("default+bh")
CTATextStyle = ansi.ColorFunc("red+bh:white+h")
ErrorTextStyle = ansi.ColorFunc("red+bh:black")
WarningStyle = ansi.ColorFunc("yellow+bh:black")
BooleanTrueStyle = ansi.ColorFunc("cyan+h")
BooleanFalseStyle = ansi.ColorFunc("red+h")
}
}

// BooleanStyle colors instances of 'true' & 'false' blue & red respectively
func BooleanStyle(in string) string {
trueStyle := ansi.ColorFunc("blue")
falseStyle := ansi.ColorFunc("red")
replacer := strings.NewReplacer(
"false", falseStyle("false"),
"true", trueStyle("true"),
"false", BooleanFalseStyle("false"),
"true", BooleanTrueStyle("true"),
)

return replacer.Replace(in)
Expand Down
15 changes: 13 additions & 2 deletions cli/elements/globals/colors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package globals

import (
"runtime"
"testing"

"github.com/mgutz/ansi"
Expand All @@ -14,13 +15,23 @@ func TestBooleanStyle(t *testing.T) {
trueIn := "true"
trueOut := BooleanStyle(trueIn)
assert.NotEqual(trueIn, trueOut)
assert.Contains(trueOut, ansi.ColorCode("blue"))

if runtime.GOOS == "windows" {
assert.Contains(trueOut, ansi.ColorCode("cyan+h"))
} else {
assert.Contains(trueOut, ansi.ColorCode("blue"))
}

// changes false
falseIn := "false"
falseOut := BooleanStyle(falseIn)
assert.NotEqual(falseIn, falseOut)
assert.Contains(falseOut, ansi.ColorCode("red"))

if runtime.GOOS == "windows" {
assert.Contains(falseOut, ansi.ColorCode("red+h"))
} else {
assert.Contains(falseOut, ansi.ColorCode("red"))
}

// neither 'true' or 'false'
neitherIn := "neither lol"
Expand Down
1 change: 0 additions & 1 deletion vendor/github.com/coreos/etcd/Documentation/README.md

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/coreos/etcd/cmd/etcd

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/coreos/etcd/cmd/etcdctl

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/coreos/etcd/cmd/tools

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/docker/docker/project/CONTRIBUTING.md

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading