44 "errors"
55 "fmt"
66 "io"
7- "io/fs"
87 "net/http"
98 "net/url"
109 "os"
@@ -141,6 +140,20 @@ func sourceFromArg(arg string) (*source, error) {
141140 return & source {r , u }, err
142141}
143142
143+ // validateStyle checks if the style is a default style, if not, checks that
144+ // the custom style exists.
145+ func validateStyle (style string ) error {
146+ if style != "auto" && styles .DefaultStyles [style ] == nil {
147+ style = utils .ExpandPath (style )
148+ if _ , err := os .Stat (style ); os .IsNotExist (err ) {
149+ return fmt .Errorf ("Specified style does not exist: %s" , style )
150+ } else if err != nil {
151+ return err
152+ }
153+ }
154+ return nil
155+ }
156+
144157func validateOptions (cmd * cobra.Command ) error {
145158 // grab config values from Viper
146159 width = viper .GetUint ("width" )
@@ -151,13 +164,8 @@ func validateOptions(cmd *cobra.Command) error {
151164
152165 // validate the glamour style
153166 style = viper .GetString ("style" )
154- if style != styles .AutoStyle && styles .DefaultStyles [style ] == nil {
155- style = utils .ExpandPath (style )
156- if _ , err := os .Stat (style ); errors .Is (err , fs .ErrNotExist ) {
157- return fmt .Errorf ("Specified style does not exist: %s" , style )
158- } else if err != nil {
159- return err
160- }
167+ if err := validateStyle (style ); err != nil {
168+ return err
161169 }
162170
163171 isTerminal := term .IsTerminal (int (os .Stdout .Fd ()))
@@ -314,12 +322,15 @@ func runTUI(workingDirectory string) error {
314322 return fmt .Errorf ("error parsing config: %v" , err )
315323 }
316324
317- cfg .WorkingDirectory = workingDirectory
325+ // use style set in env, or auto if unset
326+ if err := validateStyle (cfg .GlamourStyle ); err != nil {
327+ cfg .GlamourStyle = style
328+ }
318329
330+ cfg .WorkingDirectory = workingDirectory
319331 cfg .ShowAllFiles = showAllFiles
320332 cfg .ShowLineNumbers = showLineNumbers
321333 cfg .GlamourMaxWidth = width
322- cfg .GlamourStyle = style
323334 cfg .EnableMouse = mouse
324335 cfg .PreserveNewLines = preserveNewLines
325336
0 commit comments