Skip to content

Commit

Permalink
Dynamic width terminal calculation for help (#815)
Browse files Browse the repository at this point in the history
* feat: remove panic output

* feat: remove panic output
  • Loading branch information
Cerebrovinny authored Dec 12, 2024
1 parent b144ba2 commit 22c9b28
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 4 additions & 4 deletions internal/tui/templates/help_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion internal/tui/templates/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 22c9b28

Please sign in to comment.