Do not use FgWhite and FgBlack for terminal output (#435)

## Changes
Using white / black color for terminal output will lead to poorly
displayed content in either light or dark terminal backgrounds. Some
other CLIs experienced same issues
(https://github.com/qri-io/qri/pull/774)

Instead, let's just use color to highlight some of the output so it's
more compatible with different background styles

## Tests
<img width="772" alt="Screenshot 2023-06-05 at 16 05 09"
src="https://github.com/databricks/cli/assets/2969996/01790239-6a33-4059-86a8-d5117ea0b75f">

---

<img width="757" alt="Screenshot 2023-06-05 at 16 05 20"
src="https://github.com/databricks/cli/assets/2969996/ea3b9fdc-3782-4f4f-a9df-19e66af0c04f">
This commit is contained in:
Andrew Nester 2023-06-05 17:30:40 +02:00 committed by GitHub
parent 3dbf7a575a
commit 1f130f3722
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 33 additions and 33 deletions

View File

@ -99,8 +99,8 @@ var profilesCmd = &cobra.Command{
Short: "Lists profiles from ~/.databrickscfg",
Annotations: map[string]string{
"template": cmdio.Heredoc(`
{{white "Name"}} {{white "Host"}} {{white "Valid"}}
{{range .Profiles}}{{.Name | green}} {{.Host|white}} {{bool .Valid}}
{{header "Name"}} {{header "Host"}} {{header "Valid"}}
{{range .Profiles}}{{.Name | green}} {{.Host|cyan}} {{bool .Valid}}
{{end}}`),
},
RunE: func(cmd *cobra.Command, args []string) error {

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "Name"}} {{white "Type"}} {{white "Comment"}}
{{header "Name"}} {{header "Type"}} {{header "Comment"}}
{{range .}}{{.Name|green}} {{blue "%s" .CatalogType}} {{.Comment}}
{{end}}`)
}

View File

@ -4,8 +4,8 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "ID"}} {{white "Name"}} {{white "State"}}
{{range .}}{{.ClusterId | green}} {{.ClusterName|white}} {{if eq .State "RUNNING"}}{{green "%s" .State}}{{else if eq .State "TERMINATED"}}{{red "%s" .State}}{{else}}{{blue "%s" .State}}{{end}}
{{header "ID"}} {{header "Name"}} {{header "State"}}
{{range .}}{{.ClusterId | green}} {{.ClusterName | cyan}} {{if eq .State "RUNNING"}}{{green "%s" .State}}{{else if eq .State "TERMINATED"}}{{red "%s" .State}}{{else}}{{blue "%s" .State}}{{end}}
{{end}}`)
listNodeTypesCmd.Annotations["template"] = cmdio.Heredoc(`

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "ID"}} {{white "Name"}}
{{header "ID"}} {{header "Name"}}
{{range .}}{{.Id|green}} {{.Name}}
{{end}}`)
}

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "Name"}} {{white "Credential"}} {{white "URL"}}
{{range .}}{{.Name|green}} {{.CredentialName|blue}} {{.Url}}
{{header "Name"}} {{header "Credential"}} {{header "URL"}}
{{range .}}{{.Name|green}} {{.CredentialName|cyan}} {{.Url}}
{{end}}`)
}

View File

@ -8,7 +8,7 @@ func init() {
{{end}}`)
listRunsCmd.Annotations["template"] = cmdio.Heredoc(`
{{"Job ID"|white}} {{"Run ID"|white}} {{"Result State"|white}} URL
{{range .}}{{green "%d" .JobId}} {{blue "%d" .RunId}} {{if eq .State.ResultState "SUCCESS"}}{{"SUCCESS"|green}}{{else}}{{red "%s" .State.ResultState}}{{end}} {{.RunPageUrl}}
{{header "Job ID"}} {{header "Run ID"}} {{header "Result State"}} URL
{{range .}}{{green "%d" .JobId}} {{cyan "%d" .RunId}} {{if eq .State.ResultState "SUCCESS"}}{{"SUCCESS"|green}}{{else}}{{red "%s" .State.ResultState}}{{end}} {{.RunPageUrl}}
{{end}}`)
}

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "ID"}} {{white "Name"}} {{white "Region"}}
{{range .}}{{.MetastoreId|green}} {{.Name|white}} {{.Region}}
{{header "ID"}} {{header "Name"}} {{"Region"}}
{{range .}}{{.MetastoreId|green}} {{.Name|cyan}} {{.Region}}
{{end}}`)
}

View File

@ -5,7 +5,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
// TODO: figure out colored/non-colored headers and colspan shifts
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "ID"}} {{white "Name"}} {{white "Author"}}
{{range .}}{{.Id|green}} {{.Name|white}} {{.User.Email|white}}
{{header "ID"}} {{header "Name"}} {{header "Author"}}
{{range .}}{{.Id|green}} {{.Name|cyan}} {{.User.Email|cyan}}
{{end}}`)
}

View File

@ -5,6 +5,6 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
// TODO: figure out the right format
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{range .}}{{.UserName}} {{white "%s" .Status}} {{.QueryText}}
{{range .}}{{.UserName}} {{cyan "%s" .Status}} {{.QueryText}}
{{end}}`)
}

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "Full Name"}} {{white "Owner"}} {{white "Comment"}}
{{range .}}{{.FullName|green}} {{.Owner|white}} {{.Comment}}
{{header "Full Name"}} {{header "Owner"}} {{header "Comment"}}
{{range .}}{{.FullName|green}} {{.Owner|cyan}} {{.Comment}}
{{end}}`)
}

View File

@ -10,7 +10,7 @@ import (
func init() {
listScopesCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "Scope"}} {{white "Backend Type"}}
{{header "Scope"}} {{header "Backend Type"}}
{{range .}}{{.Name|green}} {{.BackendType}}
{{end}}`)

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "ID"}} {{white "Name"}} {{white "Credentials"}}
{{range .}}{{.Id|green}} {{.Name|white}} {{if .AwsIamRole}}{{.AwsIamRole.RoleArn}}{{end}}{{if .AzureServicePrincipal}}{{.AzureServicePrincipal.ApplicationId}}{{end}}{{if .GcpServiceAccountKey}}{{.Email}}{{end}}
{{header "ID"}} {{header "Name"}} {{header "Credentials"}}
{{range .}}{{.Id|green}} {{.Name|cyan}} {{if .AwsIamRole}}{{.AwsIamRole.RoleArn}}{{end}}{{if .AzureServicePrincipal}}{{.AzureServicePrincipal.ApplicationId}}{{end}}{{if .GcpServiceAccountKey}}{{.Email}}{{end}}
{{end}}`)
}

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "Full Name"}} {{white "Table Type"}}
{{header "Full Name"}} {{header "Table Type"}}
{{range .}}{{.FullName|green}} {{blue "%s" .TableType}}
{{end}}`)
}

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "ID"}} {{white "Created By"}} {{white "Comment"}}
{{range .}}{{.TokenId|green}} {{.CreatedByUsername|white}} {{.Comment|white}}
{{header "ID"}} {{header "Created By"}} {{header "Comment"}}
{{range .}}{{.TokenId|green}} {{.CreatedByUsername|cyan}} {{.Comment|cyan}}
{{end}}`)
}

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "ID"}} {{white "Expiry time"}} {{white "Comment"}}
{{range .}}{{.TokenId|green}} {{white "%d" .ExpiryTime}} {{.Comment|white}}
{{header "ID"}} {{header "Expiry time"}} {{header "Comment"}}
{{range .}}{{.TokenId|green}} {{cyan "%d" .ExpiryTime}} {{.Comment|cyan}}
{{end}}`)
}

View File

@ -4,7 +4,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "ID"}} {{white "Name"}} {{white "Size"}} {{white "State"}}
{{range .}}{{.Id|green}} {{.Name|white}} {{.ClusterSize|white}} {{if eq .State "RUNNING"}}{{"RUNNING"|green}}{{else if eq .State "STOPPED"}}{{"STOPPED"|red}}{{else}}{{blue "%s" .State}}{{end}}
{{header "ID"}} {{header "Name"}} {{header "Size"}} {{header "State"}}
{{range .}}{{.Id|green}} {{.Name|cyan}} {{.ClusterSize|cyan}} {{if eq .State "RUNNING"}}{{"RUNNING"|green}}{{else if eq .State "STOPPED"}}{{"STOPPED"|red}}{{else}}{{blue "%s" .State}}{{end}}
{{end}}`)
}

View File

@ -5,7 +5,7 @@ import "github.com/databricks/cli/libs/cmdio"
func init() {
listReq.Path = "/"
listCmd.Annotations["template"] = cmdio.Heredoc(`
{{white "ID"}} {{white "Type"}} {{white "Language"}} {{white "Path"}}
{{range .}}{{green "%d" .ObjectId}} {{blue "%s" .ObjectType}} {{cyan "%s" .Language}} {{.Path|white}}
{{header "ID"}} {{header "Type"}} {{header "Language"}} {{header "Path"}}
{{range .}}{{green "%d" .ObjectId}} {{blue "%s" .ObjectType}} {{cyan "%s" .Language}} {{.Path|cyan}}
{{end}}`)
}

View File

@ -58,8 +58,7 @@ func renderTemplate(w io.Writer, tmpl string, v any) error {
// we render colored output if stdout is TTY, otherwise we render text.
// in the future we'll check if we can explicitly check for stderr being
// a TTY
"black": color.BlackString,
"white": color.WhiteString,
"header": color.BlueString,
"red": color.RedString,
"green": color.GreenString,
"blue": color.BlueString,
@ -107,8 +106,9 @@ func fancyJSON(v any) ([]byte, error) {
f.FalseColor = color.New(color.FgRed)
f.NumberColor = color.New(color.FgCyan)
f.NullColor = color.New(color.FgMagenta)
f.FieldColor = color.New(color.FgWhite, color.Bold)
f.FieldQuoteColor = color.New(color.FgWhite)
f.ObjectColor = color.New(color.Reset)
f.CommaColor = color.New(color.Reset)
f.ColonColor = color.New(color.Reset)
return jsoncolor.MarshalIndentWithFormatter(v, "", " ", f)
}