feat: support GLAMOUR_STYLE for custom pager styles

This commit is contained in:
bashbunni 2024-03-04 09:01:31 -08:00
parent 2430b0af3f
commit 346528822f
No known key found for this signature in database
GPG key ID: 7AFA0946B031EB4A
2 changed files with 22 additions and 9 deletions

29
main.go
View file

@ -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 {

View file

@ -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?