This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement NO_COLOR environment variable to enable/disable Aurora pack…
…age Color property (#3330) * use NO_COLOR environment variable to enable/disable Aurora Color * add check TERM environment variable to enable/disable Aurora Color * handle TTY on aurora pkg * move aurora funcs to component * sort imports * fix the sort imports * remove unused tests * unit tests
- Loading branch information
1 parent
c1caaaa
commit 3e3fb14
Showing
6 changed files
with
105 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package component | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"unicode" | ||
|
||
auroraPackage "github.com/logrusorgru/aurora" | ||
"github.com/mattn/go-isatty" | ||
) | ||
|
||
var aurora auroraPackage.Aurora | ||
|
||
func init() { | ||
NewAurora() | ||
} | ||
|
||
func NewAurora() auroraPackage.Aurora { | ||
if aurora == nil { | ||
aurora = auroraPackage.NewAurora(IsTTYEnabled()) | ||
} | ||
return aurora | ||
} | ||
|
||
func IsTTYEnabled() bool { | ||
ttyEnabled := true | ||
if os.Getenv("TANZU_CLI_NO_COLOR") != "" || os.Getenv("NO_COLOR") != "" || strings.EqualFold(os.Getenv("TERM"), "DUMB") || !isatty.IsTerminal(os.Stdout.Fd()) { | ||
ttyEnabled = false | ||
} | ||
return ttyEnabled | ||
} | ||
|
||
// Rpad adds padding to the right of a string. | ||
// from https://github.com/spf13/cobra/blob/993cc5372a05240dfd59e3ba952748b36b2cd117/cobra.go#L29 | ||
func Rpad(s string, padding int) string { | ||
tmpl := fmt.Sprintf("%%-%ds", padding) | ||
return fmt.Sprintf(tmpl, s) | ||
} | ||
|
||
func Underline(s string) string { | ||
return aurora.Underline(s).String() | ||
} | ||
|
||
func Bold(s string) string { | ||
return aurora.Bold(s).String() | ||
} | ||
|
||
func TrimRightSpace(s string) string { | ||
return strings.TrimRightFunc(s, unicode.IsSpace) | ||
} | ||
|
||
func BeginsWith(s, prefix string) bool { | ||
return strings.HasPrefix(s, prefix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package component | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestIsTTYEnabled(t *testing.T) { | ||
envTests := []struct { | ||
key, val string | ||
want bool | ||
}{ | ||
{"TANZU_CLI_NO_COLOR", "1", false}, | ||
{"NO_COLOR", "1", false}, | ||
{"TERM", "dumb", false}, | ||
{"TERM", "duMb", false}, | ||
} | ||
|
||
for _, tt := range envTests { | ||
testName := fmt.Sprintf("(%v,%v):%v", tt.key, tt.val, tt.want) | ||
t.Run(testName, func(t *testing.T) { | ||
err := os.Setenv(tt.key, tt.val) | ||
if err != nil { | ||
return | ||
} | ||
ans := IsTTYEnabled() | ||
if ans != tt.want { | ||
t.Errorf("got %v : want %v", ans, tt.want) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters