Skip to content

Commit 7cd6896

Browse files
committed
feat(cli): use full terminal width to display flags
1 parent 19ae411 commit 7cd6896

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

cli/root.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"golang.org/x/xerrors"
2121

2222
"github.com/charmbracelet/lipgloss"
23+
"github.com/creack/pty"
2324
"github.com/kirsle/configdir"
2425
"github.com/mattn/go-isatty"
2526
"github.com/spf13/cobra"
@@ -445,6 +446,7 @@ func isTTYWriter(cmd *cobra.Command, writer func() io.Writer) bool {
445446
var templateFunctions = template.FuncMap{
446447
"usageHeader": usageHeader,
447448
"isWorkspaceCommand": isWorkspaceCommand,
449+
"ttyWidth": ttyWidth,
448450
}
449451

450452
func usageHeader(s string) string {
@@ -466,6 +468,15 @@ func isWorkspaceCommand(cmd *cobra.Command) bool {
466468
return ws
467469
}
468470

471+
func ttyWidth() int {
472+
_, cols, err := pty.Getsize(os.Stderr)
473+
if err != nil {
474+
// Default width
475+
return 100
476+
}
477+
return cols
478+
}
479+
469480
func usageTemplate() string {
470481
// usageHeader is defined in init().
471482
return `{{usageHeader "Usage:"}}
@@ -508,12 +519,12 @@ func usageTemplate() string {
508519
509520
{{- if .HasAvailableLocalFlags}}
510521
{{usageHeader "Flags:"}}
511-
{{.LocalFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}}
522+
{{.LocalFlags.FlagUsagesWrapped ttyWidth | trimTrailingWhitespaces}}
512523
{{end}}
513524
514525
{{- if .HasAvailableInheritedFlags}}
515526
{{usageHeader "Global Flags:"}}
516-
{{.InheritedFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}}
527+
{{.InheritedFlags.FlagUsagesWrapped ttyWidth | trimTrailingWhitespaces}}
517528
{{end}}
518529
519530
{{- if .HasHelpSubCommands}}

0 commit comments

Comments
 (0)