mirror of
https://github.com/charmbracelet/glow
synced 2024-12-14 14:12:27 +00:00
Validate options once
This commit is contained in:
parent
c9af84b75a
commit
d433820a1b
1 changed files with 25 additions and 20 deletions
45
main.go
45
main.go
|
@ -122,7 +122,32 @@ func sourceFromArg(arg string) (*source, error) {
|
|||
return &source{r, u}, err
|
||||
}
|
||||
|
||||
func validateOptions(cmd *cobra.Command) {
|
||||
isTerminal := terminal.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 isTerminal && !cmd.Flags().Changed("width") {
|
||||
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
|
||||
if err == nil {
|
||||
width = uint(w)
|
||||
}
|
||||
}
|
||||
if width == 0 {
|
||||
width = 80
|
||||
}
|
||||
if width > 120 {
|
||||
width = 120
|
||||
}
|
||||
}
|
||||
|
||||
func execute(cmd *cobra.Command, args []string) error {
|
||||
validateOptions(cmd)
|
||||
|
||||
if len(args) == 0 {
|
||||
return executeArg(cmd, "", os.Stdout)
|
||||
}
|
||||
|
@ -147,26 +172,6 @@ func executeArg(cmd *cobra.Command, arg string, w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
isTerminal := terminal.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 isTerminal && !cmd.Flags().Changed("width") {
|
||||
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
|
||||
if err == nil {
|
||||
width = uint(w)
|
||||
}
|
||||
}
|
||||
if width == 0 {
|
||||
width = 80
|
||||
}
|
||||
if width > 120 {
|
||||
width = 120
|
||||
}
|
||||
|
||||
// render
|
||||
var baseURL string
|
||||
u, err := url.ParseRequestURI(src.URL)
|
||||
|
|
Loading…
Reference in a new issue