From 22c9b284dc67e05e4ef7aea077cac77c1a780787 Mon Sep 17 00:00:00 2001 From: "Vinicius C." Date: Thu, 12 Dec 2024 01:59:46 +0000 Subject: [PATCH] Dynamic width terminal calculation for help (#815) * feat: remove panic output * feat: remove panic output --- internal/tui/templates/help_printer.go | 8 ++++---- internal/tui/templates/templater.go | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/tui/templates/help_printer.go b/internal/tui/templates/help_printer.go index a6d435cf1..de20041f1 100644 --- a/internal/tui/templates/help_printer.go +++ b/internal/tui/templates/help_printer.go @@ -23,12 +23,12 @@ type HelpFlagPrinter struct { maxFlagLen int } -func NewHelpFlagPrinter(out io.Writer, wrapLimit uint, flags *pflag.FlagSet) *HelpFlagPrinter { +func NewHelpFlagPrinter(out io.Writer, wrapLimit uint, flags *pflag.FlagSet) (*HelpFlagPrinter, error) { if out == nil { - panic("output writer cannot be nil") + return nil, fmt.Errorf("invalid argument: output writer cannot be nil") } if flags == nil { - panic("flag set cannot be nil") + return nil, fmt.Errorf("invalid argument: flag set cannot be nil") } if wrapLimit < minWidth { @@ -39,7 +39,7 @@ func NewHelpFlagPrinter(out io.Writer, wrapLimit uint, flags *pflag.FlagSet) *He wrapLimit: wrapLimit, out: out, maxFlagLen: calculateMaxFlagLength(flags), - } + }, nil } func calculateMaxFlagLength(flags *pflag.FlagSet) int { diff --git a/internal/tui/templates/templater.go b/internal/tui/templates/templater.go index 9ba5c4525..3d5781ac9 100644 --- a/internal/tui/templates/templater.go +++ b/internal/tui/templates/templater.go @@ -120,7 +120,12 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e func WrappedFlagUsages(f *pflag.FlagSet) string { var builder strings.Builder width := getTerminalWidth() - printer := NewHelpFlagPrinter(&builder, uint(width), f) + printer, err := NewHelpFlagPrinter(&builder, uint(width), f) + if err != nil { + // If we can't create the printer, return empty string + // This is unlikely to happen since we're using a strings.Builder + return "" + } printer.maxFlagLen = calculateMaxFlagLength(f)