summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbashbunni <bunni@bashbunni.dev>2024-03-04 09:01:31 -0800
committerbashbunni <bunni@bashbunni.dev>2024-03-04 09:01:31 -0800
commit346528822fc20b1016f0081c50808171afb85eb4 (patch)
treea9d088ddda08d13bef15f54f2c954dbd86656dff
parent2430b0af3fd83bf776875da464a885bdb5f88d38 (diff)
feat: support GLAMOUR_STYLE for custom pager stylesfix/styles-from-env
-rw-r--r--main.go29
-rw-r--r--ui/config.go2
2 files changed, 22 insertions, 9 deletions
diff --git a/main.go b/main.go
index 86a089c..ce5fcaf 100644
--- a/main.go
+++ b/main.go
@@ -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 {
diff --git a/ui/config.go b/ui/config.go
index 9db2340..5367b1d 100644
--- a/ui/config.go
+++ b/ui/config.go
@@ -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?