From 272ace404b4767fd79cdedc19d788b48c002f622 Mon Sep 17 00:00:00 2001 From: truffle Date: Sun, 24 May 2026 00:17:47 +0000 Subject: [PATCH] fix: --style flag overrides GLAMOUR_STYLE in TUI runTUI was reading GLAMOUR_STYLE and only falling back to the CLI flag when validation failed. That made an explicit --style/-s ignored whenever GLAMOUR_STYLE was set to a known style. Reverse the precedence: an explicit CLI flag wins; otherwise a valid env value wins; otherwise viper's resolved style. The helper resolveTUIStyle keeps the rule legible and testable. Fixes #953 --- glow_test.go | 25 +++++++++++++++++++++++++ main.go | 23 +++++++++++++++-------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/glow_test.go b/glow_test.go index 8743be2..5c930a8 100644 --- a/glow_test.go +++ b/glow_test.go @@ -39,3 +39,28 @@ func TestGlowFlags(t *testing.T) { } } } + +func TestResolveTUIStyle(t *testing.T) { + cases := []struct { + name string + envStyle string + cliStyle string + cliChanged bool + want string + }{ + {"cli flag wins over env", "dark", "light", true, "light"}, + {"env wins when valid and no cli", "dark", "auto", false, "dark"}, + {"cli wins when env unset", "", "light", false, "light"}, + {"cli wins when env unknown", "bogus", "light", false, "light"}, + {"env auto preserved without cli", "auto", "light", false, "auto"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := resolveTUIStyle(tc.envStyle, tc.cliStyle, tc.cliChanged) + if got != tc.want { + t.Errorf("resolveTUIStyle(%q, %q, %v) = %q, want %q", + tc.envStyle, tc.cliStyle, tc.cliChanged, got, tc.want) + } + }) + } +} diff --git a/main.go b/main.go index b31ca15..e7c0f76 100644 --- a/main.go +++ b/main.go @@ -164,6 +164,16 @@ func validateStyle(style string) error { return nil } +// resolveTUIStyle picks the glamour style for the TUI. An explicit --style +// flag wins over GLAMOUR_STYLE; otherwise a valid env value wins; otherwise +// we fall back to the viper-resolved CLI style. +func resolveTUIStyle(envStyle, cliStyle string, cliChanged bool) string { + if cliChanged || validateStyle(envStyle) != nil { + return cliStyle + } + return envStyle +} + func validateOptions(cmd *cobra.Command) error { // grab config values from Viper width = viper.GetUint("width") @@ -235,7 +245,7 @@ func execute(cmd *cobra.Command, args []string) error { switch len(args) { // TUI running on cwd case 0: - return runTUI("", "") + return runTUI(cmd, "", "") // TUI with possible dir argument case 1: @@ -245,7 +255,7 @@ func execute(cmd *cobra.Command, args []string) error { if err == nil && info.IsDir() { p, err := filepath.Abs(args[0]) if err == nil { - return runTUI(p, "") + return runTUI(cmd, p, "") } } fallthrough @@ -337,7 +347,7 @@ func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error { if !isURL(src.URL) { path = src.URL } - return runTUI(path, content) + return runTUI(cmd, path, content) default: if _, err = fmt.Fprint(w, out); err != nil { return fmt.Errorf("unable to write to writer: %w", err) @@ -346,17 +356,14 @@ func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error { } } -func runTUI(path string, content string) error { +func runTUI(cmd *cobra.Command, path string, content string) error { // Read environment to get debugging stuff cfg, err := env.ParseAs[ui.Config]() if err != nil { return fmt.Errorf("error parsing config: %v", err) } - // use style set in env, or auto if unset - if err := validateStyle(cfg.GlamourStyle); err != nil { - cfg.GlamourStyle = style - } + cfg.GlamourStyle = resolveTUIStyle(cfg.GlamourStyle, style, cmd.Flags().Changed("style")) cfg.Path = path cfg.ShowAllFiles = showAllFiles