diff options
author | Christian Muehlhaeuser <muesli@gmail.com> | 2020-05-05 07:48:05 +0200 |
---|---|---|
committer | Christian Muehlhaeuser <muesli@gmail.com> | 2020-05-05 07:48:05 +0200 |
commit | d433820a1b4c658dca5314de3470d6f267027755 (patch) | |
tree | 83e292d1155aa17fd06fce5f3d5760248667f100 /main.go | |
parent | c9af84b75a401f35b01c029c1b12592e0c6bb19e (diff) |
Validate options once
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 45 |
1 files changed, 25 insertions, 20 deletions
@@ -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) |