From 346528822fc20b1016f0081c50808171afb85eb4 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Mon, 4 Mar 2024 09:01:31 -0800 Subject: [PATCH 1/2] 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 86a089cb..ce5fcaf9 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 9db23406..5367b1dc 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? From b20beb674aaa2403403d946e51afd3d8fd11ce20 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Fri, 15 Nov 2024 13:40:13 -0800 Subject: [PATCH 2/2] fix: resolve errors --- main.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/main.go b/main.go index bd1fe1ff..6915dae6 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io" - "io/fs" "net/http" "net/url" "os" @@ -144,7 +143,7 @@ func sourceFromArg(arg string) (*source, error) { // 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 { + if style != "auto" && styles.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) @@ -323,22 +322,12 @@ func runTUI(workingDirectory string) error { return fmt.Errorf("error parsing config: %v", err) } - // Log to file, if set - if cfg.Logfile != "" { - f, err := tea.LogToFile(cfg.Logfile, "glow") - if err != nil { - return err - } - 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.ShowAllFiles = showAllFiles cfg.ShowLineNumbers = showLineNumbers cfg.GlamourMaxWidth = width