From f7359402ef5e56dfc1f29dbd9a56ffbfbfaf6fa7 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 23 Sep 2024 22:26:15 +0100 Subject: [PATCH] fix: improve formatting of the help menu Use text/tabwriter to improve the formatting of the help menu. --- internal/commands/help.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/commands/help.go b/internal/commands/help.go index 4545354..2549170 100644 --- a/internal/commands/help.go +++ b/internal/commands/help.go @@ -3,7 +3,10 @@ package commands import ( "fmt" "maps" + "os" "slices" + "strings" + "text/tabwriter" ) func HelpFunc(summaries map[string]string) CommandFunc { @@ -16,13 +19,21 @@ func HelpFunc(summaries map[string]string) CommandFunc { slices.Sort(keys) - fmt.Printf("\nCommands:\n") + var builder strings.Builder + + builder.WriteString("\nCommands:\n") + + tableWriter := tabwriter.NewWriter(&builder, 0, 8, 0, '\t', 0) for _, key := range slices.All(keys) { - fmt.Printf("\n%s: %s", key, summaries[key]) + fmt.Fprintf(tableWriter, "\n%s\t%s", key, summaries[key]) } - fmt.Printf("\n\n") + tableWriter.Flush() + + builder.WriteString("\n\n") + + fmt.Fprint(os.Stdout, builder.String()) return nil }