Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support GLAMOUR_STYLE for custom pager styles #587

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
st, err := os.Stat(arg)
if err == nil && st.IsDir() {
var src *source
_ = filepath.Walk(arg, func(path string, info os.FileInfo, err error) error {

Check warning on line 106 in main.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'info' seems to be unused, consider removing or renaming it as _ (revive)
for _, v := range readmeNames {
if strings.EqualFold(filepath.Base(path), v) {
r, err := os.Open(path)
Expand Down Expand Up @@ -134,6 +134,20 @@
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")
Expand All @@ -143,13 +157,8 @@

// 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()))
Expand Down Expand Up @@ -329,11 +338,15 @@
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 {
Expand Down
2 changes: 1 addition & 1 deletion ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
Loading