mirror of
https://github.com/charmbracelet/glow.git
synced 2026-08-18 06:09:11 +02:00
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
This commit is contained in:
parent
53788271b3
commit
272ace404b
2 changed files with 40 additions and 8 deletions
25
glow_test.go
25
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
23
main.go
23
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue