Skip to content

Commit

Permalink
Print labels with their colors on lab label list
Browse files Browse the repository at this point in the history
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 <lzampier@redhat.com>
  • Loading branch information
zampierilucas committed Feb 16, 2024
1 parent 29804ac commit 83be724
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/label_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd

import (
"fmt"
"image/color"
"os"
"os/exec"
"strconv"
Expand Down Expand Up @@ -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)
Expand All @@ -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}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 83be724

Please sign in to comment.