Skip to content
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

fix: adjust the window size of the help output #862

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -111,9 +112,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 @@ -126,7 +134,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 @@ -325,7 +333,7 @@ For more help, see the documentation here: %s`, short, config.Data.Links.CLIDocs
}

type helpCmd struct {
cmd *cobra.Command
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it because cmd is passed as an argument of helpTemplate, and this cmd is not used.

width int
}

// helpTemplate Building a custom help template with more finess and pizazz
Expand Down Expand Up @@ -406,9 +414,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
}