Skip to content

Commit

Permalink
Merge pull request #862 from threepipes/fix-helpwidth
Browse files Browse the repository at this point in the history
fix: adjust the window size of the help output
  • Loading branch information
elliotforbes authored Mar 23, 2023
2 parents 667c7b3 + b1392f2 commit 6271ba1
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/CircleCI-Public/circleci-cli/version"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
"golang.org/x/term"
)

var defaultEndpoint = "graphql-unstable"
Expand Down Expand Up @@ -117,9 +118,16 @@ func MakeCommands() *cobra.Command {

rootOptions.Data = &data.Data

helpWidth := getHelpWidth()
// CircleCI Logo will only appear with enough window width
longHelp := ""
if helpWidth > 85 {
longHelp = rootHelpLong()
}

rootCmd = &cobra.Command{
Use: "circleci",
Long: rootHelpLong(),
Long: longHelp,
Short: rootHelpShort(rootOptions),
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
return rootCmdPreRun(rootOptions)
Expand All @@ -132,7 +140,7 @@ func MakeCommands() *cobra.Command {
cobra.AddTemplateFunc("FormatPositionalArg", md_docs.FormatPositionalArg)

if os.Getenv("TESTING") != trueString {
helpCmd := helpCmd{cmd: rootCmd}
helpCmd := helpCmd{width: helpWidth}
rootCmd.SetHelpFunc(helpCmd.helpTemplate)
}
rootCmd.SetUsageTemplate(usageTemplate)
Expand Down Expand Up @@ -332,7 +340,7 @@ For more help, see the documentation here: %s`, short, config.Data.Links.CLIDocs
}

type helpCmd struct {
cmd *cobra.Command
width int
}

// helpTemplate Building a custom help template with more finess and pizazz
Expand Down Expand Up @@ -413,9 +421,21 @@ func (helpCmd *helpCmd) helpTemplate(cmd *cobra.Command, s []string) {
//Border styles
borderStyle := lipgloss.NewStyle().
Padding(0, 1, 0, 1).
Width(120).
Width(helpCmd.width - 2).
BorderForeground(lipgloss.AdaptiveColor{Light: `#3B6385`, Dark: `#47A359`}).
Border(lipgloss.ThickBorder())

log.Println("\n" + borderStyle.Render(usageText.String()+"\n"))
}

func getHelpWidth() int {
const defaultHelpWidth = 122
if !term.IsTerminal(0) {
return defaultHelpWidth
}
w, _, err := term.GetSize(0)
if err == nil && w < defaultHelpWidth {
return w
}
return defaultHelpWidth
}

0 comments on commit 6271ba1

Please sign in to comment.