From ffa0127570984eaeec532a43fcf26ab19083cc1e Mon Sep 17 00:00:00 2001 From: Arfey Date: Fri, 9 Jan 2026 18:05:05 +0200 Subject: [PATCH] Fixed indentation issues for long commands. --- cmd/root.go | 50 +++++++++++++++++---- config/config/command.go | 2 +- tests/command_group.bats | 4 +- tests/command_group_long.bats | 70 ++++++++++++++++++++++++++++++ tests/command_group_long/lets.yaml | 25 +++++++++++ tests/help_long.bats | 48 ++++++++++++++++++++ tests/help_long/lets.yaml | 18 ++++++++ 7 files changed, 206 insertions(+), 11 deletions(-) create mode 100644 tests/command_group_long.bats create mode 100644 tests/command_group_long/lets.yaml create mode 100644 tests/help_long.bats create mode 100644 tests/help_long/lets.yaml diff --git a/cmd/root.go b/cmd/root.go index 3226173..7fa55de 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,11 +2,11 @@ package cmd import ( "fmt" - "strings" "sort" + "strings" - "github.com/spf13/cobra" "github.com/lets-cli/lets/set" + "github.com/spf13/cobra" ) // newRootCmd represents the base command when called without any subcommands. @@ -60,6 +60,36 @@ func PrintHelpMessage(cmd *cobra.Command) error { return err } +func maxCommandNameLen(cmd *cobra.Command) int { + maxLen := 0 + + for _, c := range cmd.Commands() { + if l := len(c.Name()); l > maxLen { + maxLen = l + } + } + + return maxLen +} + +func rpad(s string, padding int) string { + formattedString := fmt.Sprintf("%%-%ds", padding) + return fmt.Sprintf(formattedString, s) +} + +func hasSubgroup(cmd *cobra.Command) bool { + subgroups := make(map[string]struct{}) + for _, c := range cmd.Commands() { + if subgroup, ok := c.Annotations["SubGroupName"]; ok && subgroup != "" { + subgroups[subgroup] = struct{}{} + if len(subgroups) > 1 { + return true + } + } + } + return false +} + func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string { help := "" cmds := []*cobra.Command{} @@ -71,6 +101,8 @@ func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string { } } + padding := maxCommandNameLen(cmd) + sort.Slice(cmds, func(i, j int) bool { return cmds[i].Name() < cmds[j].Name() }) @@ -88,24 +120,27 @@ func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string { sort.Strings(subGroupNameList) // generate output - help += fmt.Sprintf("%s\n", group.Title) + help += group.Title + "\n" + + intend := "" + if hasSubgroup(cmd) { + intend = " " + } for _, subgroupName := range subGroupNameList { - intend := "" if len(subGroupNameList) > 1 { help += fmt.Sprintf("\n %s\n", subgroupName) - intend = " " } for _, c := range cmds { if subgroup, ok := c.Annotations["SubGroupName"]; ok && subgroup == subgroupName { - help += fmt.Sprintf("%s %-*s %s\n", intend, cmd.NamePadding(), c.Name(), c.Short) + help += fmt.Sprintf(" %s%s %s\n", intend, rpad(c.Name(), padding), c.Short) } } } for _, c := range cmds { if _, ok := c.Annotations["SubGroupName"]; !ok { - help += fmt.Sprintf(" %-*s %s\n", cmd.NamePadding(), c.Name(), c.Short) + help += fmt.Sprintf(" %s%s %s\n", rpad(c.Name(), padding), intend, c.Short) } } @@ -114,7 +149,6 @@ func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string { return help } - func PrintRootHelpMessage(cmd *cobra.Command) error { help := "" help = fmt.Sprintf("%s\n\n%s", cmd.Short, help) diff --git a/config/config/command.go b/config/config/command.go index 68af75b..d524948 100644 --- a/config/config/command.go +++ b/config/config/command.go @@ -13,7 +13,7 @@ import ( ) type Command struct { - Name string + Name string GroupName string // Represents a list of commands (scripts) Cmds Cmds diff --git a/tests/command_group.bats b/tests/command_group.bats index 3bde50a..0de5fa0 100644 --- a/tests/command_group.bats +++ b/tests/command_group.bats @@ -26,8 +26,8 @@ Commands: d d command Internal commands: - help Help about any command - self Manage lets CLI itself + help Help about any command + self Manage lets CLI itself Flags: --all show all commands (including the ones with _) diff --git a/tests/command_group_long.bats b/tests/command_group_long.bats new file mode 100644 index 0000000..8c8531f --- /dev/null +++ b/tests/command_group_long.bats @@ -0,0 +1,70 @@ +load test_helpers + +setup() { + load "${BATS_UTILS_PATH}/bats-support/load.bash" + load "${BATS_UTILS_PATH}/bats-assert/load.bash" + cd ./tests/command_group_long +} + +HELP_MESSAGE=$(cat <