Add column for listing the label color (#5462)

Fixes #5448
This commit is contained in:
Heath Stewart 2022-04-13 23:51:37 -07:00 committed by GitHub
parent c847e935f1
commit fc8739cf97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 12 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/cli/cli/v2/pkg/cmd/label/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/text"
"github.com/cli/cli/v2/utils"
"github.com/spf13/cobra"
)
@ -101,15 +102,9 @@ func printLabels(io *iostreams.IOStreams, labels []shared.Label) error {
table := utils.NewTablePrinter(io)
for _, label := range labels {
labelName := ""
if table.IsTTY() {
labelName = cs.HexToRGB(label.Color, label.Name)
} else {
labelName = label.Name
}
table.AddField(labelName, nil, nil)
table.AddField(label.Description, nil, nil)
table.AddField(label.Name, nil, cs.ColorFromRGB(label.Color))
table.AddField(label.Description, text.Truncate, nil)
table.AddField("#"+label.Color, nil, nil)
table.EndRow()
}

View file

@ -120,7 +120,7 @@ func TestListRun(t *testing.T) {
),
)
},
wantStdout: "\nShowing 2 of 2 labels in OWNER/REPO\n\nbug This is a bug label\ndocs This is a docs label\n",
wantStdout: "\nShowing 2 of 2 labels in OWNER/REPO\n\nbug This is a bug label #d73a4a\ndocs This is a docs label #ffa8da\n",
},
{
name: "lists labels notty",
@ -158,7 +158,7 @@ func TestListRun(t *testing.T) {
),
)
},
wantStdout: "bug\tThis is a bug label\ndocs\tThis is a docs label\n",
wantStdout: "bug\tThis is a bug label\t#d73a4a\ndocs\tThis is a docs label\t#ffa8da\n",
},
{
name: "empty label list",

View file

@ -210,6 +210,15 @@ func (c *ColorScheme) ColorFromString(s string) func(string) string {
return fn
}
// ColorFromRGB returns a function suitable for TablePrinter.AddField
// that calls HexToRGB, coloring text if supported by the terminal.
func (c *ColorScheme) ColorFromRGB(hex string) func(string) string {
return func(s string) string {
return c.HexToRGB(hex, s)
}
}
// HexToRGB uses the given hex to color x if supported by the terminal.
func (c *ColorScheme) HexToRGB(hex string, x string) string {
if !c.enabled || !c.hasTrueColor || len(hex) != 6 {
return x

View file

@ -146,7 +146,51 @@ func TestEnvColorForced(t *testing.T) {
}
}
func Test_HextoRGB(t *testing.T) {
func TestColorFromRGB(t *testing.T) {
tests := []struct {
name string
hex string
text string
wants string
cs *ColorScheme
}{
{
name: "truecolor",
hex: "fc0303",
text: "red",
wants: "\033[38;2;252;3;3mred\033[0m",
cs: NewColorScheme(true, true, true),
},
{
name: "no truecolor",
hex: "fc0303",
text: "red",
wants: "red",
cs: NewColorScheme(true, true, false),
},
{
name: "no color",
hex: "fc0303",
text: "red",
wants: "red",
cs: NewColorScheme(false, false, false),
},
{
name: "invalid hex",
hex: "fc0",
text: "red",
wants: "red",
cs: NewColorScheme(false, false, false),
},
}
for _, tt := range tests {
fn := tt.cs.ColorFromRGB(tt.hex)
assert.Equal(t, tt.wants, fn(tt.text))
}
}
func TestHexToRGB(t *testing.T) {
tests := []struct {
name string
hex string