Skip to content

Commit 51da8c3

Browse files
committed
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 <[email protected]>
1 parent 29804ac commit 51da8c3

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

cmd/label_list.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/MakeNowJust/heredoc/v2"
8+
"github.com/aybabtme/rgbterm"
89
"github.com/rsteube/carapace"
910
"github.com/spf13/cobra"
1011
"github.com/zaquestion/lab/internal/action"
@@ -53,7 +54,13 @@ var labelListCmd = &cobra.Command{
5354
description = " - " + label.Description
5455
}
5556

56-
fmt.Printf("%s%s\n", label.Name, description)
57+
// Convert hex color to rgb object
58+
c := HexToRGB(label.Color)
59+
60+
// Set background color to label.Color
61+
coloredLabelName := rgbterm.BgString(label.Name, c.R, c.G, c.B)
62+
63+
fmt.Printf("%s%s\n", coloredLabelName, description)
5764
}
5865
},
5966
}

cmd/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cmd
44

55
import (
66
"fmt"
7+
"image/color"
78
"os"
89
"os/exec"
910
"strconv"
@@ -719,6 +720,7 @@ func mapLabelsAsLabelOptions(rn string, labelTerms []string) (gitlab.LabelOption
719720

720721
return gitlab.LabelOptions(matches), nil
721722
}
723+
722724
// dumpToken dumps information about a specific Personal Access Token
723725
func dumpToken(tokendata *gitlab.PersonalAccessToken) {
724726
fmt.Println("ID: ", tokendata.ID)
@@ -732,3 +734,9 @@ func dumpToken(tokendata *gitlab.PersonalAccessToken) {
732734
fmt.Println("ExpiresAt: ", time.Time(*tokendata.ExpiresAt).String())
733735
fmt.Println("")
734736
}
737+
738+
// HexToRGB converts hex color to color.RGBA with "#FFFFFF" format
739+
func HexToRGB(hex string) color.RGBA {
740+
values, _ := strconv.ParseUint(string(hex[1:]), 16, 32)
741+
return color.RGBA{R: uint8(values >> 16), G: uint8((values >> 8) & 0xFF), B: uint8(values & 0xFF), A: 255}
742+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ require (
2727
)
2828

2929
require (
30+
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 // indirect
3031
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
32+
github.com/gookit/color v1.5.4 // indirect
3133
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
3234
github.com/rsteube/carapace-shlex v0.1.1 // indirect
3335
github.com/sagikazarmark/locafero v0.4.0 // indirect
3436
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
3537
github.com/sourcegraph/conc v0.3.0 // indirect
38+
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
3639
go.uber.org/atomic v1.11.0 // indirect
3740
go.uber.org/multierr v1.11.0 // indirect
3841
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
408408
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
409409
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
410410
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
411+
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 h1:WWB576BN5zNSZc/M9d/10pqEx5VHNhaQ/yOVAkmj5Yo=
412+
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
411413
github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=
412414
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
413415
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
@@ -597,6 +599,8 @@ github.com/googleapis/gax-go/v2 v2.6.0/go.mod h1:1mjbznJAPHFpesgE5ucqfYEscaz5kMd
597599
github.com/googleapis/gax-go/v2 v2.7.0/go.mod h1:TEop28CZZQ2y+c0VxMUmu1lV+fQx57QpBWsYpwqHJx8=
598600
github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4=
599601
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
602+
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
603+
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
600604
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
601605
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
602606
github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
@@ -878,6 +882,8 @@ github.com/xanzy/go-gitlab v0.83.0 h1:37p0MpTPNbsTMKX/JnmJtY8Ch1sFiJzVF342+RvZEG
878882
github.com/xanzy/go-gitlab v0.83.0/go.mod h1:5ryv+MnpZStBH8I/77HuQBsMbBGANtVpLWC15qOjWAw=
879883
github.com/xanzy/go-gitlab v0.95.2 h1:4p0IirHqEp5f0baK/aQqr4TR57IsD+8e4fuyAA1yi88=
880884
github.com/xanzy/go-gitlab v0.95.2/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI=
885+
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
886+
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
881887
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
882888
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
883889
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

0 commit comments

Comments
 (0)