From fc8739cf9778e1a755dc76511ec3b05f0e625494 Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Wed, 13 Apr 2022 23:51:37 -0700 Subject: [PATCH] Add column for listing the label color (#5462) Fixes #5448 --- pkg/cmd/label/list/list.go | 13 +++------- pkg/cmd/label/list/list_test.go | 4 +-- pkg/iostreams/color.go | 9 +++++++ pkg/iostreams/color_test.go | 46 ++++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/label/list/list.go b/pkg/cmd/label/list/list.go index 5adf56ecd..99b5babfc 100644 --- a/pkg/cmd/label/list/list.go +++ b/pkg/cmd/label/list/list.go @@ -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() } diff --git a/pkg/cmd/label/list/list_test.go b/pkg/cmd/label/list/list_test.go index cb0cc482a..01ab6d056 100644 --- a/pkg/cmd/label/list/list_test.go +++ b/pkg/cmd/label/list/list_test.go @@ -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", diff --git a/pkg/iostreams/color.go b/pkg/iostreams/color.go index 73fb5aa3c..e2c2c1c9c 100644 --- a/pkg/iostreams/color.go +++ b/pkg/iostreams/color.go @@ -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 diff --git a/pkg/iostreams/color_test.go b/pkg/iostreams/color_test.go index b34c7a97f..74cb92048 100644 --- a/pkg/iostreams/color_test.go +++ b/pkg/iostreams/color_test.go @@ -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