Skip to content

Commit

Permalink
feat: support GLAMOUR_STYLE for custom pager styles (#587)
Browse files Browse the repository at this point in the history
* feat: support GLAMOUR_STYLE for custom pager styles

* fix: resolve errors
  • Loading branch information
bashbunni authored Dec 16, 2024
1 parent ae57cce commit f255f3a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
31 changes: 21 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -141,6 +140,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" && 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)
} else if err != nil {
return err
}
}
return nil
}

func validateOptions(cmd *cobra.Command) error {
// grab config values from Viper
width = viper.GetUint("width")
Expand All @@ -151,13 +164,8 @@ func validateOptions(cmd *cobra.Command) error {

// validate the glamour style
style = viper.GetString("style")
if style != styles.AutoStyle && styles.DefaultStyles[style] == nil {
style = utils.ExpandPath(style)
if _, err := os.Stat(style); errors.Is(err, fs.ErrNotExist) {
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()))
Expand Down Expand Up @@ -314,12 +322,15 @@ func runTUI(workingDirectory string) error {
return fmt.Errorf("error parsing config: %v", err)
}

cfg.WorkingDirectory = workingDirectory
// 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
cfg.GlamourStyle = style
cfg.EnableMouse = mouse
cfg.PreserveNewLines = preserveNewLines

Expand Down
2 changes: 1 addition & 1 deletion ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Config struct {
Gopath string `env:"GOPATH"`
HomeDir string `env:"HOME"`
GlamourMaxWidth uint
GlamourStyle string
GlamourStyle string `env:"GLAMOUR_STYLE"`
EnableMouse bool
PreserveNewLines bool

Expand Down

0 comments on commit f255f3a

Please sign in to comment.