Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print labels with their colors on lab label list #888

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cmd/label_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -31,6 +32,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)
Expand All @@ -53,13 +59,22 @@ var labelListCmd = &cobra.Command{
description = " - " + label.Description
}

fmt.Printf("%s%s\n", label.Name, description)
// 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(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(),
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