From c8187587a88c2d67c2da6841a279b4662c718c93 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Thu, 6 Jun 2024 06:21:16 +0100 Subject: [PATCH] fix: print commands in aligned columns Use the text/tabwriter to write the commands and their summaries in aligned columns when printing the help text. --- cmd/enbas/usage.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/enbas/usage.go b/cmd/enbas/usage.go index c670e53..eb0a214 100644 --- a/cmd/enbas/usage.go +++ b/cmd/enbas/usage.go @@ -9,6 +9,7 @@ import ( "fmt" "slices" "strings" + "text/tabwriter" ) func usageFunc(summaries map[string]string) func() { @@ -31,18 +32,22 @@ func usageFunc(summaries map[string]string) func() { builder.WriteString("VERSION:\n " + binaryVersion + "\n\n") } - builder.WriteString("USAGE:\n enbas [flags]\n enbas [command]\n\nCOMMANDS:") + builder.WriteString("USAGE:\n enbas [flags]\n enbas [flags] [command]\n\nCOMMANDS:") + + tableWriter := tabwriter.NewWriter(&builder, 0, 8, 0, '\t', 0) for _, cmd := range cmds { - fmt.Fprintf(&builder, "\n %s\t%s", cmd, summaries[cmd]) + fmt.Fprintf(tableWriter, "\n %s\t%s", cmd, summaries[cmd]) } - builder.WriteString("\n\nFLAGS:\n --help\n print the help message\n") + tableWriter.Flush() + + builder.WriteString("\n\nFLAGS:\n --help\n print the help message") flag.VisitAll(func(f *flag.Flag) { - fmt.Fprintf(&builder, "\n --%s\n %s\n", f.Name, f.Usage) + fmt.Fprintf(&builder, "\n --%s\n %s", f.Name, f.Usage) }) - builder.WriteString("\nUse \"enbas [command] --help\" for more information about a command.\n") + builder.WriteString("\n\nUse \"enbas [command] --help\" for more information about a command.\n") w := flag.CommandLine.Output() fmt.Fprint(w, builder.String())