This commit is contained in:
Tony Soloveyv 2026-08-07 11:20:27 -07:00 committed by GitHub
commit ff6a646172
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 11 deletions

13
main.go
View file

@ -316,10 +316,7 @@ func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error {
// display
switch {
case pager || cmd.Flags().Changed("pager"):
pagerCmd := os.Getenv("PAGER")
if pagerCmd == "" {
pagerCmd = "less -r"
}
pagerCmd := utils.Getenv("PAGER", "less -r")
fields, err := shell.Fields(pagerCmd, os.Getenv)
if err != nil || len(fields) == 0 {
@ -353,15 +350,11 @@ func runTUI(path string, content string) error {
return fmt.Errorf("error parsing config: %v", err)
}
// use style set in env, or auto if unset
if err := validateStyle(cfg.GlamourStyle); err != nil {
cfg.GlamourStyle = style
}
cfg.Path = path
cfg.ShowAllFiles = showAllFiles
cfg.ShowLineNumbers = showLineNumbers
cfg.GlamourMaxWidth = width
cfg.GlamourStyle = style
cfg.EnableMouse = mouse
cfg.PreserveNewLines = preserveNewLines
@ -421,7 +414,7 @@ func init() {
_ = viper.BindPFlag("showLineNumbers", rootCmd.Flags().Lookup("line-numbers"))
_ = viper.BindPFlag("all", rootCmd.Flags().Lookup("all"))
viper.SetDefault("style", styles.AutoStyle)
viper.SetDefault("style", utils.Getenv("GLAMOUR_STYLE", styles.AutoStyle))
viper.SetDefault("width", 0)
viper.SetDefault("all", true)

View file

@ -7,7 +7,7 @@ type Config struct {
Gopath string `env:"GOPATH"`
HomeDir string `env:"HOME"`
GlamourMaxWidth uint
GlamourStyle string `env:"GLAMOUR_STYLE"`
GlamourStyle string
EnableMouse bool
PreserveNewLines bool

View file

@ -40,6 +40,15 @@ func ExpandPath(path string) string {
return os.ExpandEnv(path)
}
// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, or fallback value if the variable is not present.
func Getenv(key string, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
// WrapCodeBlock wraps a string in a code block with the given language.
func WrapCodeBlock(s, language string) string {
return "```" + language + "\n" + s + "```"