From d433820a1b4c658dca5314de3470d6f267027755 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Tue, 5 May 2020 07:48:05 +0200 Subject: [PATCH] Validate options once --- main.go | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index af39239..a0b1fd4 100644 --- a/main.go +++ b/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)