Skip to content

feat(cli): organize flags #6269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Next Next commit
feat(cli): use full terminal width to display flags
  • Loading branch information
ammario committed Feb 17, 2023
commit 7cd6896cc3bd1a09e536265fbabf476096ffdb5e
15 changes: 13 additions & 2 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"golang.org/x/xerrors"

"github.com/charmbracelet/lipgloss"
"github.com/creack/pty"
"github.com/kirsle/configdir"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -445,6 +446,7 @@ func isTTYWriter(cmd *cobra.Command, writer func() io.Writer) bool {
var templateFunctions = template.FuncMap{
"usageHeader": usageHeader,
"isWorkspaceCommand": isWorkspaceCommand,
"ttyWidth": ttyWidth,
}

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

func ttyWidth() int {
_, cols, err := pty.Getsize(os.Stderr)
if err != nil {
// Default width
return 100
}
return cols
}

func usageTemplate() string {
// usageHeader is defined in init().
return `{{usageHeader "Usage:"}}
Expand Down Expand Up @@ -508,12 +519,12 @@ func usageTemplate() string {

{{- if .HasAvailableLocalFlags}}
{{usageHeader "Flags:"}}
{{.LocalFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}}
{{.LocalFlags.FlagUsagesWrapped ttyWidth | trimTrailingWhitespaces}}
{{end}}

{{- if .HasAvailableInheritedFlags}}
{{usageHeader "Global Flags:"}}
{{.InheritedFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}}
{{.InheritedFlags.FlagUsagesWrapped ttyWidth | trimTrailingWhitespaces}}
{{end}}

{{- if .HasHelpSubCommands}}
Expand Down