X Tutup
Skip to content

Commit 47314a6

Browse files
author
bchadwic
committed
modified HexToRGB to check whether terminal and gh have color enabled, as well as created tests for HexToRGB
1 parent af2499c commit 47314a6

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed

pkg/cmd/issue/shared/display.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func PrintIssues(io *iostreams.IOStreams, prefix string, totalCount int, issues
2222
issueNum = "#" + issueNum
2323
}
2424
issueNum = prefix + issueNum
25-
labels := IssueLabelList(issue)
25+
labels := IssueLabelList(issue, cs)
2626
if labels != "" && table.IsTTY() {
2727
labels = fmt.Sprintf("(%s)", labels)
2828
}
@@ -56,14 +56,14 @@ func truncateLabels(w int, t string) string {
5656
return fmt.Sprintf("(%s)", truncated)
5757
}
5858

59-
func IssueLabelList(issue api.Issue) string {
59+
func IssueLabelList(issue api.Issue, cs *iostreams.ColorScheme) string {
6060
if len(issue.Labels.Nodes) == 0 {
6161
return ""
6262
}
6363

6464
labelNames := make([]string, 0, len(issue.Labels.Nodes))
6565
for _, label := range issue.Labels.Nodes {
66-
labelNames = append(labelNames, iostreams.HexToRGB(label.Color, label.Name))
66+
labelNames = append(labelNames, cs.HexToRGB(label.Color, label.Name))
6767
}
6868

6969
return strings.Join(labelNames, ", ")

pkg/cmd/issue/view/view.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func viewRun(opts *ViewOptions) error {
125125
return nil
126126
}
127127

128-
return printRawIssuePreview(opts.IO.Out, issue)
128+
return printRawIssuePreview(opts.IO.Out, issue, opts.IO.ColorScheme())
129129
}
130130

131131
func findIssue(client *http.Client, baseRepoFn func() (ghrepo.Interface, error), selector string, loadComments bool) (*api.Issue, error) {
@@ -141,9 +141,9 @@ func findIssue(client *http.Client, baseRepoFn func() (ghrepo.Interface, error),
141141
return issue, err
142142
}
143143

144-
func printRawIssuePreview(out io.Writer, issue *api.Issue) error {
144+
func printRawIssuePreview(out io.Writer, issue *api.Issue, cs *iostreams.ColorScheme) error {
145145
assignees := issueAssigneeList(*issue)
146-
labels := shared.IssueLabelList(*issue)
146+
labels := shared.IssueLabelList(*issue, cs)
147147
projects := issueProjectList(*issue)
148148

149149
// Print empty strings for empty values so the number of metadata lines is consistent when
@@ -193,7 +193,7 @@ func printHumanIssuePreview(opts *ViewOptions, issue *api.Issue) error {
193193
fmt.Fprint(out, cs.Bold("Assignees: "))
194194
fmt.Fprintln(out, assignees)
195195
}
196-
if labels := shared.IssueLabelList(*issue); labels != "" {
196+
if labels := shared.IssueLabelList(*issue, cs); labels != "" {
197197
fmt.Fprint(out, cs.Bold("Labels: "))
198198
fmt.Fprintln(out, labels)
199199
}

pkg/cmd/pr/view/view.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func printRawPrPreview(io *iostreams.IOStreams, pr *api.PullRequest) error {
139139

140140
reviewers := prReviewerList(*pr, cs)
141141
assignees := prAssigneeList(*pr)
142-
labels := prLabelList(*pr)
142+
labels := prLabelList(*pr, cs)
143143
projects := prProjectList(*pr)
144144

145145
fmt.Fprintf(out, "title:\t%s\n", pr.Title)
@@ -197,7 +197,7 @@ func printHumanPrPreview(opts *ViewOptions, pr *api.PullRequest) error {
197197
fmt.Fprint(out, cs.Bold("Assignees: "))
198198
fmt.Fprintln(out, assignees)
199199
}
200-
if labels := prLabelList(*pr); labels != "" {
200+
if labels := prLabelList(*pr, cs); labels != "" {
201201
fmt.Fprint(out, cs.Bold("Labels: "))
202202
fmt.Fprintln(out, labels)
203203
}
@@ -367,14 +367,14 @@ func prAssigneeList(pr api.PullRequest) string {
367367
return list
368368
}
369369

370-
func prLabelList(pr api.PullRequest) string {
370+
func prLabelList(pr api.PullRequest, cs *iostreams.ColorScheme) string {
371371
if len(pr.Labels.Nodes) == 0 {
372372
return ""
373373
}
374374

375375
labelNames := make([]string, 0, len(pr.Labels.Nodes))
376376
for _, label := range pr.Labels.Nodes {
377-
labelNames = append(labelNames, iostreams.HexToRGB(label.Color, label.Name))
377+
labelNames = append(labelNames, cs.HexToRGB(label.Color, label.Name))
378378
}
379379

380380
list := strings.Join(labelNames, ", ")

pkg/iostreams/color.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ func (c *ColorScheme) ColorFromString(s string) func(string) string {
204204
return fn
205205
}
206206

207-
func HexToRGB(hex string, x string) string {
207+
func (c *ColorScheme) HexToRGB(hex string, x string) string {
208+
if !c.enabled || !c.is256enabled {
209+
return x
210+
}
211+
208212
r, _ := strconv.ParseInt(hex[0:2], 16, 64)
209213
g, _ := strconv.ParseInt(hex[2:4], 16, 64)
210214
b, _ := strconv.ParseInt(hex[4:6], 16, 64)

pkg/iostreams/color_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package iostreams
33
import (
44
"os"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestEnvColorDisabled(t *testing.T) {
@@ -143,3 +145,54 @@ func TestEnvColorForced(t *testing.T) {
143145
})
144146
}
145147
}
148+
149+
func Test_HextoRGB(t *testing.T) {
150+
tests := []struct {
151+
name string
152+
hex string
153+
arg string
154+
expectedOutput string
155+
expectedError bool
156+
cs *ColorScheme
157+
}{
158+
{
159+
name: "Colored red enabled color",
160+
hex: "fc0303",
161+
arg: "red",
162+
expectedOutput: "\033[38;2;252;3;3mred\033[0m",
163+
cs: NewColorScheme(true, true),
164+
},
165+
{
166+
name: "Failed colored red enabled color",
167+
hex: "fc0303",
168+
arg: "red",
169+
expectedOutput: "\033[38;2;252;2;3mred\033[0m",
170+
expectedError: true,
171+
cs: NewColorScheme(true, true),
172+
},
173+
{
174+
name: "Colored red disabled color",
175+
hex: "fc0303",
176+
arg: "red",
177+
expectedOutput: "red",
178+
cs: NewColorScheme(false, false),
179+
},
180+
{
181+
name: "Failed colored red disabled color",
182+
hex: "fc0303",
183+
arg: "red",
184+
expectedOutput: "\033[38;2;252;3;3mred\033[0m",
185+
expectedError: true,
186+
cs: NewColorScheme(false, false),
187+
},
188+
}
189+
190+
for _, tt := range tests {
191+
output := tt.cs.HexToRGB(tt.hex, tt.arg)
192+
if tt.expectedError {
193+
assert.NotEqual(t, tt.expectedOutput, output)
194+
} else {
195+
assert.Equal(t, tt.expectedOutput, output)
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)
X Tutup