From 346528822fc20b1016f0081c50808171afb85eb4 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Mon, 4 Mar 2024 09:01:31 -0800 Subject: [PATCH] feat: support GLAMOUR_STYLE for custom pager styles --- main.go | 29 +++++++++++++++++++++-------- ui/config.go | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 86a089c..ce5fcaf 100644 --- a/main.go +++ b/main.go @@ -134,6 +134,20 @@ func sourceFromArg(arg string) (*source, error) { return &source{r, u}, err } +// validateStyle checks if the style is a default style, if not, checks that +// the custom style exists. +func validateStyle(style string) error { + if style != "auto" && glamour.DefaultStyles[style] == nil { + style = utils.ExpandPath(style) + if _, err := os.Stat(style); os.IsNotExist(err) { + return fmt.Errorf("Specified style does not exist: %s", style) + } else if err != nil { + return err + } + } + return nil +} + func validateOptions(cmd *cobra.Command) error { // grab config values from Viper width = viper.GetUint("width") @@ -143,13 +157,8 @@ func validateOptions(cmd *cobra.Command) error { // validate the glamour style style = viper.GetString("style") - if style != "auto" && glamour.DefaultStyles[style] == nil { - style = utils.ExpandPath(style) - if _, err := os.Stat(style); os.IsNotExist(err) { - return fmt.Errorf("Specified style does not exist: %s", style) - } else if err != nil { - return err - } + if err := validateStyle(style); err != nil { + return err } isTerminal := term.IsTerminal(int(os.Stdout.Fd())) @@ -329,11 +338,15 @@ func runTUI(workingDirectory string, stashedOnly bool) error { defer f.Close() //nolint:errcheck } + // use style set in env, or auto if unset + if err := validateStyle(cfg.GlamourStyle); err != nil { + cfg.GlamourStyle = style + } + cfg.WorkingDirectory = workingDirectory cfg.DocumentTypes = ui.NewDocTypeSet() cfg.ShowAllFiles = showAllFiles cfg.GlamourMaxWidth = width - cfg.GlamourStyle = style cfg.EnableMouse = mouse if stashedOnly { diff --git a/ui/config.go b/ui/config.go index 9db2340..5367b1d 100644 --- a/ui/config.go +++ b/ui/config.go @@ -6,7 +6,7 @@ type Config struct { Gopath string `env:"GOPATH"` HomeDir string `env:"HOME"` GlamourMaxWidth uint - GlamourStyle string + GlamourStyle string `env:"GLAMOUR_STYLE"` EnableMouse bool // Which directory should we start from?