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

Realign accessible color logic around new env var #185

Open
wants to merge 3 commits into
base: accessible-colors
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
33 changes: 0 additions & 33 deletions pkg/accessibility/accessibility.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/cli/glamour"
"github.com/cli/go-gh/v2/pkg/accessibility"
"github.com/cli/go-gh/v2/pkg/x/color"
)

// WithoutIndentation is a rendering option that removes indentation from the markdown rendering.
Expand Down Expand Up @@ -34,7 +34,7 @@ func WithWrap(w int) glamour.TermRendererOption {
// If the environment variable GLAMOUR_STYLE is set, it will take precedence over the provided theme.
func WithTheme(theme string) glamour.TermRendererOption {
style := os.Getenv("GLAMOUR_STYLE")
accessible := accessibility.IsEnabled()
accessible := color.IsAccessibleColorsEnabled()
if style == "" || style == "auto" {
switch theme {
case "light", "dark":
Expand Down
6 changes: 3 additions & 3 deletions pkg/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/MakeNowJust/heredoc"
"github.com/alecthomas/chroma/v2/styles"
"github.com/cli/go-gh/v2/pkg/accessibility"
"github.com/cli/go-gh/v2/pkg/x/color"
ansi "github.com/leaanthony/go-ansi-parser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -133,7 +133,7 @@ func Test_RenderAccessible(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.accessible {
t.Setenv(accessibility.ACCESSIBILITY_ENV, "true")
t.Setenv(color.AccessibleColorsEnv, "true")
}

out, err := Render(tt.text, WithTheme(tt.theme))
Expand Down Expand Up @@ -247,7 +247,7 @@ func Test_RenderColor(t *testing.T) {
// Chroma caches charm style used to render codeblocks, it must be unregistered to avoid previously used style being reused.
delete(styles.Registry, "charm")
})
t.Setenv(accessibility.ACCESSIBILITY_ENV, tt.accessibleEnvVar)
t.Setenv(color.AccessibleColorsEnv, tt.accessibleEnvVar)

if tt.styleEnvVar != "" {
path := filepath.Join(t.TempDir(), fmt.Sprintf("%s.json", tt.styleEnvVar))
Expand Down
40 changes: 40 additions & 0 deletions pkg/x/color/accessibility.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package color

import (
"os"

"github.com/cli/go-gh/v2/pkg/config"
)

const (
// AccessibleColorsEnv is the name of the environment variable to enable accessibile color features.
AccessibleColorsEnv = "GH_ACCESSIBLE_COLORS"

// AccessibleColorsSetting is the name of the `gh config` setting to enable accessibile color features.
AccessibleColorsSetting = "accessible_colors"
)

// IsAccessibleColorsEnabled returns true if accessible colors are enabled via environment variable
// or configuration setting with the environment variable having higher precedence.
//
// If the environment variable is empty, "0", or "false", the accessible colors are disabled.
// Any other value enables the accessibility feature.
//
// Note this is an experimental feature that is subject to change.
func IsAccessibleColorsEnabled() bool {
envVar := os.Getenv(AccessibleColorsEnv)
if envVar != "" {
return isEnvVarEnabled(envVar)
}

// We are not handling errors because we don't want to fail if the config is not
// read. Instead, we assume an empty configuration is equivalent to "disabled".
cfg, _ := config.Read(nil)
accessibleConfigValue, _ := cfg.Get([]string{AccessibleColorsSetting})

return accessibleConfigValue == "enabled"
}

func isEnvVarEnabled(envVar string) bool {
return envVar != "" && envVar != "0" && envVar != "false"
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package accessibility
package color

import (
"testing"

"github.com/MakeNowJust/heredoc"
"github.com/cli/go-gh/v2/internal/testutils"
"github.com/stretchr/testify/assert"
)

func TestIsEnabled(t *testing.T) {
func TestIsAccessibleColorsEnabled(t *testing.T) {
tests := []struct {
name string
envVarValue string
Expand All @@ -21,19 +22,19 @@ func TestIsEnabled(t *testing.T) {
wantOut: false,
},
{
name: "When the accessibility configuration is unset but the ACCESSIBLE env var is set to something truthy (not '0' or 'false'), it should return true",
name: "When the accessibility configuration is unset but the env var is set to something truthy (not '0' or 'false'), it should return true",
envVarValue: "1",
cfgStr: "",
wantOut: true,
},
{
name: "When the accessibility configuration is unset and the ACCESSIBLE env var returns '0', it should return false",
name: "When the accessibility configuration is unset and the env var returns '0', it should return false",
envVarValue: "0",
cfgStr: "",
wantOut: false,
},
{
name: "When the accessibility configuration is unset and the ACCESSIBLE env var returns 'false', it should return false",
name: "When the accessibility configuration is unset and the env var returns 'false', it should return false",
envVarValue: "false",
cfgStr: "",
wantOut: false,
Expand Down Expand Up @@ -71,21 +72,21 @@ func TestIsEnabled(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("ACCESSIBLE", tt.envVarValue)
t.Setenv("GH_ACCESSIBLE_COLORS", tt.envVarValue)
testutils.StubConfig(t, tt.cfgStr)
assert.Equal(t, tt.wantOut, IsEnabled())
assert.Equal(t, tt.wantOut, IsAccessibleColorsEnabled())
})
}
}

func accessibilityEnabledConfig() string {
return `
accessible: enabled
`
return heredoc.Docf(`
%s: enabled
`, AccessibleColorsSetting)
}

func accessibilityDisabledConfig() string {
return `
accessible: disabled
`
return heredoc.Docf(`
%s: disabled
`, AccessibleColorsSetting)
}
4 changes: 4 additions & 0 deletions pkg/x/x.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// `x` is a collection of experimental features being used within the GitHub CLI following common Go conventions.
//
// Anything contained is subject to change without notice until it is considered stable enough to be promoted.
package x
Loading