From 83be724f55220fb39af02035caf8fcfdd07c2952 Mon Sep 17 00:00:00 2001 From: Lucas Zampieri Date: Fri, 16 Feb 2024 16:02:47 -0300 Subject: [PATCH 1/3] Print labels with their colors on lab label list This takes the label.color hex and converts it to color in HexToRGB in utils, then uses rgbterm lib to convert rgb to terminal color. Signed-off-by: Lucas Zampieri --- cmd/label_list.go | 5 ++++- cmd/util.go | 8 ++++++++ go.mod | 2 ++ go.sum | 4 ++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/label_list.go b/cmd/label_list.go index cf21688d..594d2dfa 100644 --- a/cmd/label_list.go +++ b/cmd/label_list.go @@ -53,7 +53,10 @@ var labelListCmd = &cobra.Command{ description = " - " + label.Description } - fmt.Printf("%s%s\n", label.Name, description) + // Convert hex color to rgb object + c := HexToRGB(label.Color) + + fmt.Printf("\033[48;2;%d;%d;%dm%s\033[0m%s\n", c.R, c.G, c.B, label.Name, description) } }, } diff --git a/cmd/util.go b/cmd/util.go index 83b80fbb..fc84c4c6 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -4,6 +4,7 @@ package cmd import ( "fmt" + "image/color" "os" "os/exec" "strconv" @@ -719,6 +720,7 @@ func mapLabelsAsLabelOptions(rn string, labelTerms []string) (gitlab.LabelOption return gitlab.LabelOptions(matches), nil } + // dumpToken dumps information about a specific Personal Access Token func dumpToken(tokendata *gitlab.PersonalAccessToken) { fmt.Println("ID: ", tokendata.ID) @@ -732,3 +734,9 @@ func dumpToken(tokendata *gitlab.PersonalAccessToken) { fmt.Println("ExpiresAt: ", time.Time(*tokendata.ExpiresAt).String()) fmt.Println("") } + +// HexToRGB converts hex color to color.RGBA with "#FFFFFF" format +func HexToRGB(hex string) color.RGBA { + values, _ := strconv.ParseUint(string(hex[1:]), 16, 32) + return color.RGBA{R: uint8(values >> 16), G: uint8((values >> 8) & 0xFF), B: uint8(values & 0xFF), A: 255} +} diff --git a/go.mod b/go.mod index daf1e92c..11fca03c 100644 --- a/go.mod +++ b/go.mod @@ -28,11 +28,13 @@ require ( require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/gookit/color v1.5.4 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/rsteube/carapace-shlex v0.1.1 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect + github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect go.uber.org/atomic v1.11.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect diff --git a/go.sum b/go.sum index 5ec165f5..22960f71 100644 --- a/go.sum +++ b/go.sum @@ -597,6 +597,8 @@ github.com/googleapis/gax-go/v2 v2.6.0/go.mod h1:1mjbznJAPHFpesgE5ucqfYEscaz5kMd github.com/googleapis/gax-go/v2 v2.7.0/go.mod h1:TEop28CZZQ2y+c0VxMUmu1lV+fQx57QpBWsYpwqHJx8= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= +github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= +github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8= @@ -878,6 +880,8 @@ github.com/xanzy/go-gitlab v0.83.0 h1:37p0MpTPNbsTMKX/JnmJtY8Ch1sFiJzVF342+RvZEG github.com/xanzy/go-gitlab v0.83.0/go.mod h1:5ryv+MnpZStBH8I/77HuQBsMbBGANtVpLWC15qOjWAw= github.com/xanzy/go-gitlab v0.95.2 h1:4p0IirHqEp5f0baK/aQqr4TR57IsD+8e4fuyAA1yi88= github.com/xanzy/go-gitlab v0.95.2/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= From c17d79fd331fb0f14a4e73bd4b7e9b9a169073a5 Mon Sep 17 00:00:00 2001 From: Lucas Zampieri Date: Tue, 20 Feb 2024 15:17:35 -0300 Subject: [PATCH 2/3] Made Color an arg Label colored printing can be enabled by the --color param, or by using the config file. Signed-off-by: Lucas Zampieri --- cmd/label_list.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/label_list.go b/cmd/label_list.go index 594d2dfa..940771ac 100644 --- a/cmd/label_list.go +++ b/cmd/label_list.go @@ -31,6 +31,11 @@ var labelListCmd = &cobra.Command{ log.Fatal(err) } + color, err := cmd.Flags().GetBool("color") + if err != nil { + log.Fatal(err) + } + labelSearch = strings.ToLower(labelSearch) labels, err := lab.LabelList(rn) @@ -53,16 +58,22 @@ var labelListCmd = &cobra.Command{ description = " - " + label.Description } - // Convert hex color to rgb object - c := HexToRGB(label.Color) + // Default format without color + format := "%s%s\n" + if color { + // Convert hex color to rgb object + c := HexToRGB(label.Color) + format = fmt.Sprintf("\033[48;2;%d;%d;%dm%%s\033[0m%%s\n", c.R, c.G, c.B) + } - fmt.Printf("\033[48;2;%d;%d;%dm%s\033[0m%s\n", c.R, c.G, c.B, label.Name, description) + fmt.Printf(format, label.Name, description) } }, } func init() { labelListCmd.Flags().Bool("name-only", false, "only list label names, not descriptions") + labelListCmd.Flags().Bool("color", false, "print colored labels") labelCmd.AddCommand(labelListCmd) carapace.Gen(labelCmd).PositionalCompletion( action.Remotes(), From f3a4bda3d6330d5e656364b00ebc55a500d15373 Mon Sep 17 00:00:00 2001 From: Lucas Zampieri Date: Tue, 20 Feb 2024 15:20:02 -0300 Subject: [PATCH 3/3] Added color to the example table title Signed-off-by: Lucas Zampieri --- cmd/label_list.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/label_list.go b/cmd/label_list.go index 940771ac..ce81b18f 100644 --- a/cmd/label_list.go +++ b/cmd/label_list.go @@ -18,7 +18,8 @@ var labelListCmd = &cobra.Command{ Example: heredoc.Doc(` lab label list lab label list "search term" - lab label list remote "search term"`), + lab label list remote "search term + lab label list --color"`), PersistentPreRun: labPersistentPreRun, Run: func(cmd *cobra.Command, args []string) { rn, labelSearch, err := parseArgsRemoteAndProject(args)