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 3 commits 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
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
Loading