Skip to content

Commit 312994e

Browse files
dentargclaude
andauthored
Update cloudamqp instance list to handle long names (#12)
This should be fine, names are not allowed to exceed 100 characters. From Claude: 1. Added dynamic column width calculation: The code now scans through all instances first to determine the maximum width needed for each column (ID, NAME, PLAN, REGION). 2. Added padding: Each column gets an extra 2 spaces of padding for better readability. 3. Dynamic format strings: Instead of using fixed-width format specifiers (like %-20s), the code now generates format strings based on the actual data, ensuring perfect alignment regardless of content length. Close #10 Co-authored-by: Claude <noreply@anthropic.com>
1 parent e5d498a commit 312994e

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

cmd/instance_list.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"strconv"
56

67
"cloudamqp-cli/client"
78
"github.com/spf13/cobra"
@@ -32,13 +33,45 @@ var instanceListCmd = &cobra.Command{
3233
return nil
3334
}
3435

36+
// Calculate column widths
37+
idWidth := len("ID")
38+
nameWidth := len("NAME")
39+
planWidth := len("PLAN")
40+
regionWidth := len("REGION")
41+
42+
for _, instance := range instances {
43+
idLen := len(strconv.Itoa(instance.ID))
44+
if idLen > idWidth {
45+
idWidth = idLen
46+
}
47+
if len(instance.Name) > nameWidth {
48+
nameWidth = len(instance.Name)
49+
}
50+
if len(instance.Plan) > planWidth {
51+
planWidth = len(instance.Plan)
52+
}
53+
if len(instance.Region) > regionWidth {
54+
regionWidth = len(instance.Region)
55+
}
56+
}
57+
58+
// Add padding
59+
idWidth += 2
60+
nameWidth += 2
61+
planWidth += 2
62+
regionWidth += 2
63+
64+
// Create format strings
65+
headerFormat := fmt.Sprintf("%%-%ds %%-%ds %%-%ds %%-%ds\n", idWidth, nameWidth, planWidth, regionWidth)
66+
rowFormat := fmt.Sprintf("%%-%dd %%-%ds %%-%ds %%-%ds\n", idWidth, nameWidth, planWidth, regionWidth)
67+
3568
// Print table header
36-
fmt.Printf("%-10s %-20s %-15s %-30s\n", "ID", "NAME", "PLAN", "REGION")
37-
fmt.Printf("%-10s %-20s %-15s %-30s\n", "--", "----", "----", "------")
69+
fmt.Printf(headerFormat, "ID", "NAME", "PLAN", "REGION")
70+
fmt.Printf(headerFormat, "--", "----", "----", "------")
3871

3972
// Print instance data
4073
for _, instance := range instances {
41-
fmt.Printf("%-10d %-20s %-15s %-30s\n",
74+
fmt.Printf(rowFormat,
4275
instance.ID,
4376
instance.Name,
4477
instance.Plan,

0 commit comments

Comments
 (0)