-
Notifications
You must be signed in to change notification settings - Fork 372
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
fix: adjust validateOptions to handle preconfigured width #667
base: master
Are you sure you want to change the base?
Conversation
main.go
Outdated
@@ -156,6 +156,7 @@ func validateStyle(style string) error { | |||
|
|||
func validateOptions(cmd *cobra.Command) error { | |||
// grab config values from Viper | |||
width_in_config := viper.InConfig("width") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
width_in_config := viper.InConfig("width") | |
widthInConfig := viper.InConfig("width") |
main.go
Outdated
@@ -156,6 +156,7 @@ func validateStyle(style string) error { | |||
|
|||
func validateOptions(cmd *cobra.Command) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in commit and PR title
validateOptions not validaOptions
main.go
Outdated
// Detect terminal width | ||
if !cmd.Flags().Changed("width") { | ||
if isTerminal && width == 0 { | ||
w, _, err := term.GetSize(int(os.Stdout.Fd())) | ||
w := -1 | ||
if isTerminal { | ||
var err error | ||
w, _, err = term.GetSize(int(os.Stdout.Fd())) | ||
if err == nil { | ||
width = uint(w) | ||
} | ||
|
||
if width > 120 { | ||
width = 120 | ||
if w > 120 { | ||
w = 120 | ||
} | ||
} else { | ||
w = -1 | ||
} | ||
} | ||
if width == 0 { | ||
width = 80 | ||
if w == -1 { | ||
if !width_in_config { | ||
width = 80 | ||
} | ||
} else { | ||
width = uint(w) | ||
} | ||
} | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about this:
func validateOptions(cmd *cobra.Command) error {
width = viper.GetUint("width")
mouse = viper.GetBool("mouse")
pager = viper.GetBool("pager")
showAllFiles = viper.GetBool("all")
preserveNewLines = viper.GetBool("preserveNewLines")
// validate the glamour style
style = viper.GetString("style")
if err := validateStyle(style); err != nil {
return err
}
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
// We want to use a special no-TTY style, when stdout is not a terminal
// and there was no specific style passed by arg
if !isTerminal && !cmd.Flags().Changed("style") {
style = "notty"
}
// Detect terminal width
if cmd.Flags().Changed("width") {
return nil
}
if isTerminal {
w, _, err := term.GetSize(int(os.Stdout.Fd()))
if err == nil {
width = uint(max(w, 120))
return nil
}
}
// grab config values from Viper
widthInConfig := viper.InConfig("width")
if !widthInConfig {
width = 80
}
return nil
}
I wrote it on my phone in text mode, please test it and adapt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine and dandy, apart from having to have min(w, 120)
, not max
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm glad you find it dandy 😁
Adding a Co-authored-by tag to your commit would be appreciated then
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Indeed for the min vs max
8406ada
to
9f6c1b7
Compare
fixes charmbracelet#666 Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
9f6c1b7
to
d008b82
Compare
fixes #666