Skip to content

Commit 88af63d

Browse files
heathsmislav
andauthored
Re-enable label colors for issue list (cli#4106)
* Re-enable label colors for issue list * Drop parentheses wrapping issue labels * Support ANSI escape codes in TablePrinter cells * Switch to a Truncate implementation that correctly measures ANSI escape codes * Only output RGB color if terminal has truecolor capabilities * Enable `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on Windows - fixes wrapping issues with full lines and allows truecolor rendering Co-authored-by: Mislav Marohnić <mislav@github.com>
1 parent 25b150a commit 88af63d

File tree

13 files changed

+101
-138
lines changed

13 files changed

+101
-138
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ require (
2121
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
2222
github.com/mattn/go-colorable v0.1.8
2323
github.com/mattn/go-isatty v0.0.13
24-
github.com/mattn/go-runewidth v0.0.10
2524
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
25+
github.com/muesli/reflow v0.2.1-0.20210502190812-c80126ec2ad5
2626
github.com/muesli/termenv v0.8.1
27-
github.com/rivo/uniseg v0.2.0
2827
github.com/shurcooL/githubv4 v0.0.0-20200928013246-d292edc3691b
2928
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f
3029
github.com/spf13/cobra v1.2.1

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,9 @@ github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
268268
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
269269
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
270270
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
271-
github.com/muesli/reflow v0.2.0 h1:2o0UBJPHHH4fa2GCXU4Rg4DwOtWPMekCeyc5EWbAQp0=
272271
github.com/muesli/reflow v0.2.0/go.mod h1:qT22vjVmM9MIUeLgsVYe/Ye7eZlbv9dZjL3dVhUqLX8=
272+
github.com/muesli/reflow v0.2.1-0.20210502190812-c80126ec2ad5 h1:T+Fc6qGlSfM+z0JPlp+n5rijvlg6C6JYFSNaqnCifDU=
273+
github.com/muesli/reflow v0.2.1-0.20210502190812-c80126ec2ad5/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
273274
github.com/muesli/termenv v0.8.1 h1:9q230czSP3DHVpkaPDXGp0TOfAwyjyYwXlUCQxQSaBk=
274275
github.com/muesli/termenv v0.8.1/go.mod h1:kzt/D/4a88RoheZmwfqorY3A+tnsSMA9HJC/fQSFKo0=
275276
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=

pkg/cmd/gist/view/view_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,6 @@ func Test_promptGists(t *testing.T) {
450450
}
451451

452452
io, _, _, _ := iostreams.Test()
453-
cs := iostreams.NewColorScheme(io.ColorEnabled(), io.ColorSupport256())
454453

455454
for _, tt := range tests {
456455
reg := &httpmock.Registry{}
@@ -472,7 +471,7 @@ func Test_promptGists(t *testing.T) {
472471
as.StubOne(tt.gistIndex)
473472

474473
t.Run(tt.name, func(t *testing.T) {
475-
gistID, err := promptGists(client, ghinstance.Default(), cs)
474+
gistID, err := promptGists(client, ghinstance.Default(), io.ColorScheme())
476475
assert.NoError(t, err)
477476
assert.Equal(t, tt.wantOut, gistID)
478477
reg.Verify(t)

pkg/cmd/issue/list/list_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ func TestIssueList_tty(t *testing.T) {
100100
101101
Showing 3 of 3 open issues in OWNER/REPO
102102
103-
#1 number won (label) about X years ago
104-
#2 number too (label) about X years ago
105-
#4 number fore (label) about X years ago
103+
#1 number won label about X years ago
104+
#2 number too label about X years ago
105+
#4 number fore label about X years ago
106106
`), out)
107107
assert.Equal(t, ``, output.Stderr())
108108
}

pkg/cmd/issue/shared/display.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@ func PrintIssues(io *iostreams.IOStreams, prefix string, totalCount int, issues
2222
issueNum = "#" + issueNum
2323
}
2424
issueNum = prefix + issueNum
25-
labels := issueLabelList(&issue)
26-
if labels != "" && table.IsTTY() {
27-
labels = fmt.Sprintf("(%s)", labels)
28-
}
2925
now := time.Now()
3026
ago := now.Sub(issue.UpdatedAt)
3127
table.AddField(issueNum, nil, cs.ColorFromString(prShared.ColorForState(issue.State)))
3228
if !table.IsTTY() {
3329
table.AddField(issue.State, nil, nil)
3430
}
3531
table.AddField(text.ReplaceExcessiveWhitespace(issue.Title), nil, nil)
36-
table.AddField(labels, truncateLabels, cs.Gray)
32+
table.AddField(issueLabelList(&issue, cs, table.IsTTY()), nil, nil)
3733
if table.IsTTY() {
3834
table.AddField(utils.FuzzyAgo(ago), nil, cs.Gray)
3935
} else {
@@ -48,22 +44,18 @@ func PrintIssues(io *iostreams.IOStreams, prefix string, totalCount int, issues
4844
}
4945
}
5046

51-
func truncateLabels(w int, t string) string {
52-
if len(t) < 2 {
53-
return t
54-
}
55-
truncated := text.Truncate(w-2, t[1:len(t)-1])
56-
return fmt.Sprintf("(%s)", truncated)
57-
}
58-
59-
func issueLabelList(issue *api.Issue) string {
47+
func issueLabelList(issue *api.Issue, cs *iostreams.ColorScheme, colorize bool) string {
6048
if len(issue.Labels.Nodes) == 0 {
6149
return ""
6250
}
6351

64-
labelNames := make([]string, len(issue.Labels.Nodes))
65-
for i, label := range issue.Labels.Nodes {
66-
labelNames[i] = label.Name
52+
labelNames := make([]string, 0, len(issue.Labels.Nodes))
53+
for _, label := range issue.Labels.Nodes {
54+
if colorize {
55+
labelNames = append(labelNames, cs.HexToRGB(label.Color, label.Name))
56+
} else {
57+
labelNames = append(labelNames, label.Name)
58+
}
6759
}
6860

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

pkg/cmd/issue/shared/display_test.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

pkg/cmd/run/watch/watch.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ func watchRun(opts *WatchOptions) error {
135135
prNumber = fmt.Sprintf(" #%d", number)
136136
}
137137

138-
opts.IO.EnableVirtualTerminalProcessing()
139-
// clear entire screen
140-
fmt.Fprintf(out, "\x1b[2J")
138+
if err := opts.IO.EnableVirtualTerminalProcessing(); err == nil {
139+
// clear entire screen
140+
fmt.Fprintf(out, "\x1b[2J")
141+
}
141142

142143
annotationCache := map[int64][]shared.Annotation{}
143144

pkg/iostreams/color.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,33 @@ func EnvColorForced() bool {
3434
}
3535

3636
func Is256ColorSupported() bool {
37+
return IsTrueColorSupported() ||
38+
strings.Contains(os.Getenv("TERM"), "256") ||
39+
strings.Contains(os.Getenv("COLORTERM"), "256")
40+
}
41+
42+
func IsTrueColorSupported() bool {
3743
term := os.Getenv("TERM")
3844
colorterm := os.Getenv("COLORTERM")
3945

40-
return strings.Contains(term, "256") ||
41-
strings.Contains(term, "24bit") ||
46+
return strings.Contains(term, "24bit") ||
4247
strings.Contains(term, "truecolor") ||
43-
strings.Contains(colorterm, "256") ||
4448
strings.Contains(colorterm, "24bit") ||
4549
strings.Contains(colorterm, "truecolor")
4650
}
4751

48-
func NewColorScheme(enabled, is256enabled bool) *ColorScheme {
52+
func NewColorScheme(enabled, is256enabled bool, trueColor bool) *ColorScheme {
4953
return &ColorScheme{
5054
enabled: enabled,
5155
is256enabled: is256enabled,
56+
hasTrueColor: trueColor,
5257
}
5358
}
5459

5560
type ColorScheme struct {
5661
enabled bool
5762
is256enabled bool
63+
hasTrueColor bool
5864
}
5965

6066
func (c *ColorScheme) Bold(t string) string {
@@ -205,7 +211,7 @@ func (c *ColorScheme) ColorFromString(s string) func(string) string {
205211
}
206212

207213
func (c *ColorScheme) HexToRGB(hex string, x string) string {
208-
if !c.enabled || !c.is256enabled {
214+
if !c.enabled || !c.hasTrueColor {
209215
return x
210216
}
211217

pkg/iostreams/color_test.go

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -148,51 +148,37 @@ func TestEnvColorForced(t *testing.T) {
148148

149149
func Test_HextoRGB(t *testing.T) {
150150
tests := []struct {
151-
name string
152-
hex string
153-
arg string
154-
expectedOutput string
155-
expectedError bool
156-
cs *ColorScheme
151+
name string
152+
hex string
153+
text string
154+
wants string
155+
cs *ColorScheme
157156
}{
158157
{
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),
158+
name: "truecolor",
159+
hex: "fc0303",
160+
text: "red",
161+
wants: "\033[38;2;252;3;3mred\033[0m",
162+
cs: NewColorScheme(true, true, true),
172163
},
173164
{
174-
name: "Colored red disabled color",
175-
hex: "fc0303",
176-
arg: "red",
177-
expectedOutput: "red",
178-
cs: NewColorScheme(false, false),
165+
name: "no truecolor",
166+
hex: "fc0303",
167+
text: "red",
168+
wants: "red",
169+
cs: NewColorScheme(true, true, false),
179170
},
180171
{
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),
172+
name: "no color",
173+
hex: "fc0303",
174+
text: "red",
175+
wants: "red",
176+
cs: NewColorScheme(false, false, false),
187177
},
188178
}
189179

190180
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-
}
181+
output := tt.cs.HexToRGB(tt.hex, tt.text)
182+
assert.Equal(t, tt.wants, output)
197183
}
198184
}

pkg/iostreams/console.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,15 @@
22

33
package iostreams
44

5-
func (s *IOStreams) EnableVirtualTerminalProcessing() {}
5+
import (
6+
"errors"
7+
"os"
8+
)
9+
10+
func (s *IOStreams) EnableVirtualTerminalProcessing() error {
11+
return nil
12+
}
13+
14+
func enableVirtualTerminalProcessing(f *os.File) error {
15+
return errors.New("not implemented")
16+
}

0 commit comments

Comments
 (0)