Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions cmd/instance_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"strconv"

"cloudamqp-cli/client"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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,
Expand Down
Loading