Skip to content
Merged
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
9 changes: 5 additions & 4 deletions pkg/cmd/cache/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ func listRun(opts *ListOptions) error {
}
client := api.NewClientFromHTTP(httpClient)

cs := opts.IO.ColorScheme()
opts.IO.StartProgressIndicator()
result, err := shared.GetCaches(client, repo, shared.GetCachesOptions{Limit: opts.Limit, Sort: opts.Sort, Order: opts.Order, Key: opts.Key, Ref: opts.Ref})
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("%s Failed to get caches: %w", opts.IO.ColorScheme().FailureIcon(), err)
return fmt.Errorf("%s Failed to get caches: %w", cs.FailureIcon(), err)
}

if len(result.ActionsCaches) == 0 && opts.Exporter == nil {
Expand All @@ -130,11 +131,11 @@ func listRun(opts *ListOptions) error {

tp := tableprinter.New(opts.IO, tableprinter.WithHeader("ID", "KEY", "SIZE", "CREATED", "ACCESSED"))
for _, cache := range result.ActionsCaches {
tp.AddField(opts.IO.ColorScheme().Cyan(fmt.Sprintf("%d", cache.Id)))
tp.AddField(cs.Cyanf("%d", cache.Id))
tp.AddField(cache.Key)
tp.AddField(humanFileSize(cache.SizeInBytes))
tp.AddTimeField(opts.Now, cache.CreatedAt, opts.IO.ColorScheme().Gray)
tp.AddTimeField(opts.Now, cache.LastAccessedAt, opts.IO.ColorScheme().Gray)
tp.AddTimeField(opts.Now, cache.CreatedAt, cs.Muted)
tp.AddTimeField(opts.Now, cache.LastAccessedAt, cs.Muted)
tp.EndRow()
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/codespace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (a *App) List(ctx context.Context, opts *listOptions, exporter cmdutil.Expo
case false:
nameColor = cs.Yellow
case true:
nameColor = cs.Gray
nameColor = cs.Muted
}

tp.AddField(formattedName, tableprinter.WithColor(nameColor))
Expand All @@ -172,7 +172,7 @@ func (a *App) List(ctx context.Context, opts *listOptions, exporter cmdutil.Expo
if err != nil {
return fmt.Errorf("error parsing date %q: %w", c.CreatedAt, err)
}
tp.AddTimeField(time.Now(), ct, cs.Gray)
tp.AddTimeField(time.Now(), ct, cs.Muted)

if hasNonProdVSCSTarget {
tp.AddField(c.VSCSTarget)
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/factory/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cli/cli/v2/pkg/cmd/extension"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
xcolor "github.com/cli/go-gh/v2/pkg/x/color"
)

var ssoHeader string
Expand Down Expand Up @@ -292,6 +293,8 @@ func ioStreams(f *cmdutil.Factory) *iostreams.IOStreams {
io.SetPager(pager.Value)
}

io.SetAccessibleColorsEnabled(xcolor.IsAccessibleColorsEnabled())

return io
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gist/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func printTable(io *iostreams.IOStreams, gists []shared.Gist, filter *regexp.Reg
tableprinter.WithColor(highlightFilesFunc(&gist)),
)
tp.AddField(visibility, tableprinter.WithColor(visColor))
tp.AddTimeField(time.Now(), gist.UpdatedAt, cs.Gray)
tp.AddTimeField(time.Now(), gist.UpdatedAt, cs.Muted)
tp.EndRow()
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/gist/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ func Test_listRun(t *testing.T) {
},
wantOut: heredoc.Docf(`
%[1]s[0;4;39mID %[1]s[0m %[1]s[0;4;39mDESCRIPTION %[1]s[0m %[1]s[0;4;39mFILES %[1]s[0m %[1]s[0;4;39mVISIBILITY%[1]s[0m %[1]s[0;4;39mUPDATED %[1]s[0m
1234 %[1]s[0;30;43mocto%[1]s[0m%[1]s[0;1;39m match in the description%[1]s[0m 1 file %[1]s[0;32mpublic %[1]s[0m %[1]s[38;5;242mabout 6 hours ago%[1]s[m
2345 %[1]s[0;1;39mmatch in the file name %[1]s[0m %[1]s[0;30;43m2 files%[1]s[0m %[1]s[0;31msecret %[1]s[0m %[1]s[38;5;242mabout 6 hours ago%[1]s[m
1234 %[1]s[0;30;43mocto%[1]s[0m%[1]s[0;1;39m match in the description%[1]s[0m 1 file %[1]s[0;32mpublic %[1]s[0m %[1]s[38;5;242mabout 6 hours ago%[1]s[0m
2345 %[1]s[0;1;39mmatch in the file name %[1]s[0m %[1]s[0;30;43m2 files%[1]s[0m %[1]s[0;31msecret %[1]s[0m %[1]s[38;5;242mabout 6 hours ago%[1]s[0m
Comment on lines +490 to +491
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this change is due to a minor enhancement to 8-bit gray color logic to make this easier to standardize testing with our dependency which use the ESC[0m escape sequence.

`, "\x1b"),
},
{
Expand Down Expand Up @@ -694,7 +694,7 @@ func Test_highlightMatch(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cs := iostreams.NewColorScheme(tt.color, false, false, iostreams.NoTheme)
cs := iostreams.NewColorScheme(tt.color, false, false, false, iostreams.NoTheme)

matched := false
got, err := highlightMatch(tt.input, regex, &matched, cs.Blue, cs.Highlight)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/gpg-key/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func listRun(opts *ListOptions) error {
t.AddField(gpgKey.Emails.String())
t.AddField(gpgKey.KeyID)
t.AddField(gpgKey.PublicKey, tableprinter.WithTruncate(truncateMiddle))
t.AddTimeField(now, gpgKey.CreatedAt, cs.Gray)
t.AddTimeField(now, gpgKey.CreatedAt, cs.Muted)
expiresAt := gpgKey.ExpiresAt.Format(time.RFC3339)
if t.IsTTY() {
if gpgKey.ExpiresAt.IsZero() {
Expand All @@ -87,7 +87,7 @@ func listRun(opts *ListOptions) error {
expiresAt = gpgKey.ExpiresAt.Format("2006-01-02")
}
}
t.AddField(expiresAt, tableprinter.WithColor(cs.Gray))
t.AddField(expiresAt, tableprinter.WithColor(cs.Muted))
t.EndRow()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pr/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func listRun(opts *ListOptions) error {
if !isTTY {
table.AddField(shared.PrStateWithDraft(&pr))
}
table.AddTimeField(opts.Now(), pr.CreatedAt, cs.Gray)
table.AddTimeField(opts.Now(), pr.CreatedAt, cs.Muted)
table.EndRow()
}
err = table.Render()
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/release/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func listRun(opts *ListOptions) error {
}

table := tableprinter.New(opts.IO, tableprinter.WithHeader("Title", "Type", "Tag name", "Published"))
iofmt := opts.IO.ColorScheme()
cs := opts.IO.ColorScheme()
for _, rel := range releases {
title := text.RemoveExcessiveWhitespace(rel.Name)
if title == "" {
Expand All @@ -100,13 +100,13 @@ func listRun(opts *ListOptions) error {
var badgeColor func(string) string
if rel.IsLatest {
badge = "Latest"
badgeColor = iofmt.Green
badgeColor = cs.Green
} else if rel.IsDraft {
badge = "Draft"
badgeColor = iofmt.Red
badgeColor = cs.Red
} else if rel.IsPrerelease {
badge = "Pre-release"
badgeColor = iofmt.Yellow
badgeColor = cs.Yellow
}
table.AddField(badge, tableprinter.WithColor(badgeColor))

Expand All @@ -116,7 +116,7 @@ func listRun(opts *ListOptions) error {
if rel.PublishedAt.IsZero() {
pubDate = rel.CreatedAt
}
table.AddTimeField(time.Now(), pubDate, iofmt.Gray)
table.AddTimeField(time.Now(), pubDate, cs.Muted)
table.EndRow()
}
err = table.Render()
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/repo/deploy-key/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func listRun(opts *ListOptions) error {
}
t.AddField(sshType)
t.AddField(deployKey.Key, tableprinter.WithTruncate(truncateMiddle))
t.AddTimeField(now, deployKey.CreatedAt, cs.Gray)
t.AddTimeField(now, deployKey.CreatedAt, cs.Muted)
t.EndRow()
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/repo/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func listRun(opts *ListOptions) error {
totalMatchCount := len(listResult.Repositories)
for _, repo := range listResult.Repositories {
info := repoInfo(repo)
infoColor := cs.Gray
infoColor := cs.Muted
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: this changes the info column in gh repo list, expanding the scope of this issue/PR by a hair.


if repo.IsPrivate {
infoColor = cs.Yellow
Expand All @@ -195,7 +195,7 @@ func listRun(opts *ListOptions) error {
tp.AddField(repo.NameWithOwner, tableprinter.WithColor(cs.Bold))
tp.AddField(text.RemoveExcessiveWhitespace(repo.Description))
tp.AddField(info, tableprinter.WithColor(infoColor))
tp.AddTimeField(opts.Now(), *t, cs.Gray)
tp.AddTimeField(opts.Now(), *t, cs.Muted)
tp.EndRow()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/run/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func listRun(opts *ListOptions) error {
tp.AddField(string(run.Event))
tp.AddField(fmt.Sprintf("%d", run.ID), tableprinter.WithColor(cs.Cyan))
tp.AddField(run.Duration(opts.now).String())
tp.AddTimeField(opts.now, run.StartedTime(), cs.Gray)
tp.AddTimeField(opts.now, run.StartedTime(), cs.Muted)
tp.EndRow()
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/ssh-key/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func listRun(opts *ListOptions) error {
t.AddField(id)
t.AddField(sshKey.Key, tableprinter.WithTruncate(truncateMiddle))
t.AddField(sshKey.Type)
t.AddTimeField(now, sshKey.CreatedAt, cs.Gray)
t.AddTimeField(now, sshKey.CreatedAt, cs.Muted)
} else {
t.AddField(sshKey.Title)
t.AddField(sshKey.Key)
t.AddTimeField(now, sshKey.CreatedAt, cs.Gray)
t.AddTimeField(now, sshKey.CreatedAt, cs.Muted)
t.AddField(id)
t.AddField(sshKey.Type)
}
Expand Down
51 changes: 41 additions & 10 deletions pkg/iostreams/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,36 @@ var (
greenBold = ansi.ColorFunc("green+b")
highlightStart = ansi.ColorCode(highlightStyle)
highlight = ansi.ColorFunc(highlightStyle)
darkThemeMuted = ansi.ColorFunc("white+d")
darkThemeTableHeader = ansi.ColorFunc("white+du")
lightThemeMuted = ansi.ColorFunc("black+h")
lightThemeTableHeader = ansi.ColorFunc("black+hu")
noThemeTableHeader = ansi.ColorFunc("default+u")

gray256 = func(t string) string {
return fmt.Sprintf("\x1b[%d;5;%dm%s\x1b[m", 38, 242, t)
return fmt.Sprintf("\x1b[%d;5;%dm%s\x1b[0m", 38, 242, t)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: ESC[m and ESC[0m are equivalent, however most of our dependencies have standardized on the latter. I'm changing this to make testing easier and consistent as this fact might not stand out to others.

}
)

// NewColorScheme initializes color logic based on provided terminal capabilities.
// Logic dealing with terminal theme detected, such as whether color is enabled, 8-bit color supported, true color supported,
// and terminal theme detected.
func NewColorScheme(enabled, is256enabled, trueColor bool, theme string) *ColorScheme {
func NewColorScheme(enabled, is256enabled, trueColor, accessibleColors bool, theme string) *ColorScheme {
return &ColorScheme{
enabled: enabled,
is256enabled: is256enabled,
hasTrueColor: trueColor,
theme: theme,
enabled: enabled,
is256enabled: is256enabled,
hasTrueColor: trueColor,
accessibleColors: accessibleColors,
theme: theme,
}
}

type ColorScheme struct {
enabled bool
is256enabled bool
hasTrueColor bool
theme string
enabled bool
is256enabled bool
hasTrueColor bool
accessibleColors bool
theme string
}

func (c *ColorScheme) Enabled() bool {
Expand All @@ -73,6 +77,31 @@ func (c *ColorScheme) Boldf(t string, args ...interface{}) string {
return c.Bold(fmt.Sprintf(t, args...))
}

func (c *ColorScheme) Muted(t string) string {
// Fallback to previous logic if accessible colors preview is disabled.
if !c.accessibleColors {
return c.Gray(t)
}

// Muted text is only stylized if color is enabled.
if !c.enabled {
return t
}

switch c.theme {
case LightTheme:
return lightThemeMuted(t)
case DarkTheme:
return darkThemeMuted(t)
default:
return t
}
}

func (c *ColorScheme) Mutedf(t string, args ...interface{}) string {
return c.Muted(fmt.Sprintf(t, args...))
}

func (c *ColorScheme) Red(t string) string {
if !c.enabled {
return t
Expand Down Expand Up @@ -113,6 +142,7 @@ func (c *ColorScheme) GreenBold(t string) string {
return greenBold(t)
}

// Use Muted instead for thematically contrasting color.
func (c *ColorScheme) Gray(t string) string {
if !c.enabled {
return t
Expand All @@ -123,6 +153,7 @@ func (c *ColorScheme) Gray(t string) string {
return gray(t)
}

// Use Mutedf instead for thematically contrasting color.
func (c *ColorScheme) Grayf(t string, args ...interface{}) string {
return c.Gray(fmt.Sprintf(t, args...))
}
Expand Down
Loading
Loading