Skip to content

Commit

Permalink
Realign accessible color logic around new env var
Browse files Browse the repository at this point in the history
1. Introduce constant for `gh config` setting for accessible colors
2. Align existing constant for env var to match
3. Rename exported functions to distinguish use specifically for color
4. Minor enhancements to comments and tests
  • Loading branch information
andyfeller committed Mar 6, 2025
1 parent 0136321 commit e05c340
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
29 changes: 18 additions & 11 deletions pkg/accessibility/accessibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@ import (
"github.com/cli/go-gh/v2/pkg/config"
)

// ACCESSIBILITY_ENV is the name of the environment variable that can be used to enable
// accessibility features. If the value is empty, "0", or "false", the accessibility
// features are disabled. Any other value enables the accessibility features. Note that
// this environment variable supercedes the configuration file's accessible setting.
const ACCESSIBILITY_ENV = "ACCESSIBLE"

// IsEnabled returns true if accessibility features are enabled via the ACCESSIBLE
// environment variable or the configuration file.
func IsEnabled() bool {
envVar := os.Getenv(ACCESSIBILITY_ENV)
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 accessibility colors are enabled via environment variable
// or configuration settings.
//
// If the environment variable is empty, "0", or "false", the accessibility colors are disabled.
// Any other value enables the accessibility features.
//
// Note that this environment variable supercedes the configuration file's accessible setting.
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{"accessible"})
accessibleConfigValue, _ := cfg.Get([]string{AccessibleColorsSetting})

return accessibleConfigValue == "enabled"
}
Expand Down
25 changes: 13 additions & 12 deletions pkg/accessibility/accessibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package accessibility
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)
}
2 changes: 1 addition & 1 deletion pkg/markdown/markdown.go
Original file line number Diff line number Diff line change
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 := accessibility.IsAccessibleColorsEnabled()
if style == "" || style == "auto" {
switch theme {
case "light", "dark":
Expand Down
4 changes: 2 additions & 2 deletions pkg/markdown/markdown_test.go
Original file line number Diff line number Diff line change
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(accessibility.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(accessibility.AccessibleColorsEnv, tt.accessibleEnvVar)

if tt.styleEnvVar != "" {
path := filepath.Join(t.TempDir(), fmt.Sprintf("%s.json", tt.styleEnvVar))
Expand Down

0 comments on commit e05c340

Please sign in to comment.