diff --git a/cmd/bd/main.go b/cmd/bd/main.go index 93dba5507..de61d5758 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -197,6 +197,15 @@ func forcedMigratePreviewFlag(cmd *cobra.Command) string { return "" } +// applyNoColorFlag disables colorized output when --no-color is set. +// Complements the NO_COLOR / CLICOLOR=0 env detection in package ui, +// giving callers a per-invocation override. +func applyNoColorFlag() { + if noColorFlag { + ui.DisableColors() + } +} + // loadBeadsEnvFile loads .beads/.env into process environment for per-project // Dolt credentials (GH#2520). Uses gotenv.Load which is non-overriding — // existing shell env vars always take precedence. @@ -693,9 +702,7 @@ var rootCmd = &cobra.Command{ _ = cmd.Help() // Help() always returns nil for cobra commands }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if noColorFlag { - ui.DisableColors() - } + applyNoColorFlag() // Initialize CommandContext to hold runtime state (replaces scattered globals) initCommandContext() diff --git a/cmd/bd/no_color_test.go b/cmd/bd/no_color_test.go new file mode 100644 index 000000000..04050f4c5 --- /dev/null +++ b/cmd/bd/no_color_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "strings" + "testing" + + lipgloss "charm.land/lipgloss/v2" + "github.com/steveyegge/beads/internal/ui" +) + +func TestApplyNoColorFlag(t *testing.T) { + savedFlag := noColorFlag + savedStyle := ui.AccentStyle + savedColor := ui.ColorAccent + t.Cleanup(func() { + noColorFlag = savedFlag + ui.AccentStyle = savedStyle + ui.ColorAccent = savedColor + }) + + // Seed a colored style so we can observe it being cleared. + ui.ColorAccent = lipgloss.Color("#ff0000") + ui.AccentStyle = lipgloss.NewStyle().Foreground(ui.ColorAccent) + + t.Run("flag unset leaves styles untouched", func(t *testing.T) { + noColorFlag = false + applyNoColorFlag() + if _, ok := ui.ColorAccent.(lipgloss.NoColor); ok { + t.Error("colors disabled even though --no-color was not set") + } + }) + + t.Run("flag set disables colors", func(t *testing.T) { + noColorFlag = true + applyNoColorFlag() + if _, ok := ui.ColorAccent.(lipgloss.NoColor); !ok { + t.Errorf("ColorAccent not reset to NoColor, got %T", ui.ColorAccent) + } + if out := ui.AccentStyle.Render("hi"); strings.ContainsRune(out, '\x1b') { + t.Errorf("AccentStyle still emits ANSI after --no-color: %q", out) + } + }) +} diff --git a/internal/ui/styles_test.go b/internal/ui/styles_test.go index a76ae3779..890b939f2 100644 --- a/internal/ui/styles_test.go +++ b/internal/ui/styles_test.go @@ -5,6 +5,7 @@ import ( "strings" "testing" + lipgloss "charm.land/lipgloss/v2" "github.com/steveyegge/beads/internal/types" ) @@ -151,6 +152,29 @@ func TestRenderClosedUtilities(t *testing.T) { } } +func TestDisableColors(t *testing.T) { + // Save and restore mutated globals so other tests are unaffected. + savedStyle := AccentStyle + savedColor := ColorAccent + t.Cleanup(func() { + AccentStyle = savedStyle + ColorAccent = savedColor + }) + + // Simulate a color-enabled init: a style carrying an explicit foreground. + ColorAccent = lipgloss.Color("#ff0000") + AccentStyle = lipgloss.NewStyle().Foreground(ColorAccent) + + DisableColors() + + if _, ok := ColorAccent.(lipgloss.NoColor); !ok { + t.Errorf("ColorAccent not reset to NoColor, got %T", ColorAccent) + } + if out := AccentStyle.Render("hello"); strings.ContainsRune(out, '\x1b') { + t.Errorf("AccentStyle still emits ANSI escape after DisableColors: %q", out) + } +} + func TestRenderCommandAndCategoryAreUppercaseSafe(t *testing.T) { got := RenderCategory(" already upper ") if !strings.Contains(got, " ALREADY UPPER ") {