Skip to content

Commit f255f3a

Browse files
authored
feat: support GLAMOUR_STYLE for custom pager styles (#587)
* feat: support GLAMOUR_STYLE for custom pager styles * fix: resolve errors
1 parent ae57cce commit f255f3a

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

main.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"io/fs"
87
"net/http"
98
"net/url"
109
"os"
@@ -141,6 +140,20 @@ func sourceFromArg(arg string) (*source, error) {
141140
return &source{r, u}, err
142141
}
143142

143+
// validateStyle checks if the style is a default style, if not, checks that
144+
// the custom style exists.
145+
func validateStyle(style string) error {
146+
if style != "auto" && styles.DefaultStyles[style] == nil {
147+
style = utils.ExpandPath(style)
148+
if _, err := os.Stat(style); os.IsNotExist(err) {
149+
return fmt.Errorf("Specified style does not exist: %s", style)
150+
} else if err != nil {
151+
return err
152+
}
153+
}
154+
return nil
155+
}
156+
144157
func validateOptions(cmd *cobra.Command) error {
145158
// grab config values from Viper
146159
width = viper.GetUint("width")
@@ -151,13 +164,8 @@ func validateOptions(cmd *cobra.Command) error {
151164

152165
// validate the glamour style
153166
style = viper.GetString("style")
154-
if style != styles.AutoStyle && styles.DefaultStyles[style] == nil {
155-
style = utils.ExpandPath(style)
156-
if _, err := os.Stat(style); errors.Is(err, fs.ErrNotExist) {
157-
return fmt.Errorf("Specified style does not exist: %s", style)
158-
} else if err != nil {
159-
return err
160-
}
167+
if err := validateStyle(style); err != nil {
168+
return err
161169
}
162170

163171
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
@@ -314,12 +322,15 @@ func runTUI(workingDirectory string) error {
314322
return fmt.Errorf("error parsing config: %v", err)
315323
}
316324

317-
cfg.WorkingDirectory = workingDirectory
325+
// use style set in env, or auto if unset
326+
if err := validateStyle(cfg.GlamourStyle); err != nil {
327+
cfg.GlamourStyle = style
328+
}
318329

330+
cfg.WorkingDirectory = workingDirectory
319331
cfg.ShowAllFiles = showAllFiles
320332
cfg.ShowLineNumbers = showLineNumbers
321333
cfg.GlamourMaxWidth = width
322-
cfg.GlamourStyle = style
323334
cfg.EnableMouse = mouse
324335
cfg.PreserveNewLines = preserveNewLines
325336

ui/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type Config struct {
77
Gopath string `env:"GOPATH"`
88
HomeDir string `env:"HOME"`
99
GlamourMaxWidth uint
10-
GlamourStyle string
10+
GlamourStyle string `env:"GLAMOUR_STYLE"`
1111
EnableMouse bool
1212
PreserveNewLines bool
1313

0 commit comments

Comments
 (0)