diff --git a/cmd/instance_list.go b/cmd/instance_list.go index 08e8cb4..228739e 100644 --- a/cmd/instance_list.go +++ b/cmd/instance_list.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "strconv" "cloudamqp-cli/client" "github.com/spf13/cobra" @@ -32,13 +33,45 @@ var instanceListCmd = &cobra.Command{ return nil } + // Calculate column widths + idWidth := len("ID") + nameWidth := len("NAME") + planWidth := len("PLAN") + regionWidth := len("REGION") + + for _, instance := range instances { + idLen := len(strconv.Itoa(instance.ID)) + if idLen > idWidth { + idWidth = idLen + } + if len(instance.Name) > nameWidth { + nameWidth = len(instance.Name) + } + if len(instance.Plan) > planWidth { + planWidth = len(instance.Plan) + } + if len(instance.Region) > regionWidth { + regionWidth = len(instance.Region) + } + } + + // Add padding + idWidth += 2 + nameWidth += 2 + planWidth += 2 + regionWidth += 2 + + // Create format strings + headerFormat := fmt.Sprintf("%%-%ds %%-%ds %%-%ds %%-%ds\n", idWidth, nameWidth, planWidth, regionWidth) + rowFormat := fmt.Sprintf("%%-%dd %%-%ds %%-%ds %%-%ds\n", idWidth, nameWidth, planWidth, regionWidth) + // Print table header - fmt.Printf("%-10s %-20s %-15s %-30s\n", "ID", "NAME", "PLAN", "REGION") - fmt.Printf("%-10s %-20s %-15s %-30s\n", "--", "----", "----", "------") + fmt.Printf(headerFormat, "ID", "NAME", "PLAN", "REGION") + fmt.Printf(headerFormat, "--", "----", "----", "------") // Print instance data for _, instance := range instances { - fmt.Printf("%-10d %-20s %-15s %-30s\n", + fmt.Printf(rowFormat, instance.ID, instance.Name, instance.Plan,