|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// TableWriter provides simple table formatting for CLI output |
| 11 | +type TableWriter struct { |
| 12 | + w io.Writer |
| 13 | + headers []string |
| 14 | + widths []int |
| 15 | + rows [][]string |
| 16 | +} |
| 17 | + |
| 18 | +// NewTableWriter creates a new table writer |
| 19 | +func NewTableWriter(w io.Writer, headers ...string) *TableWriter { |
| 20 | + widths := make([]int, len(headers)) |
| 21 | + for i, h := range headers { |
| 22 | + widths[i] = len(h) |
| 23 | + } |
| 24 | + return &TableWriter{ |
| 25 | + w: w, |
| 26 | + headers: headers, |
| 27 | + widths: widths, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// AddRow adds a row to the table |
| 32 | +func (t *TableWriter) AddRow(cells ...string) { |
| 33 | + // Pad or truncate to match header count |
| 34 | + row := make([]string, len(t.headers)) |
| 35 | + for i := range row { |
| 36 | + if i < len(cells) { |
| 37 | + row[i] = cells[i] |
| 38 | + } |
| 39 | + if len(row[i]) > t.widths[i] { |
| 40 | + t.widths[i] = len(row[i]) |
| 41 | + } |
| 42 | + } |
| 43 | + t.rows = append(t.rows, row) |
| 44 | +} |
| 45 | + |
| 46 | +// Render outputs the table |
| 47 | +func (t *TableWriter) Render() { |
| 48 | + // Print headers |
| 49 | + for i, h := range t.headers { |
| 50 | + fmt.Fprintf(t.w, "%-*s", t.widths[i]+2, h) |
| 51 | + } |
| 52 | + fmt.Fprintln(t.w) |
| 53 | + |
| 54 | + // Print rows |
| 55 | + for _, row := range t.rows { |
| 56 | + for i, cell := range row { |
| 57 | + fmt.Fprintf(t.w, "%-*s", t.widths[i]+2, cell) |
| 58 | + } |
| 59 | + fmt.Fprintln(t.w) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// FormatTimeAgo formats a time as "X ago" string |
| 64 | +func FormatTimeAgo(t time.Time) string { |
| 65 | + if t.IsZero() { |
| 66 | + return "N/A" |
| 67 | + } |
| 68 | + |
| 69 | + d := time.Since(t) |
| 70 | + |
| 71 | + switch { |
| 72 | + case d < time.Minute: |
| 73 | + return fmt.Sprintf("%d seconds ago", int(d.Seconds())) |
| 74 | + case d < time.Hour: |
| 75 | + mins := int(d.Minutes()) |
| 76 | + if mins == 1 { |
| 77 | + return "1 minute ago" |
| 78 | + } |
| 79 | + return fmt.Sprintf("%d minutes ago", mins) |
| 80 | + case d < 24*time.Hour: |
| 81 | + hours := int(d.Hours()) |
| 82 | + if hours == 1 { |
| 83 | + return "1 hour ago" |
| 84 | + } |
| 85 | + return fmt.Sprintf("%d hours ago", hours) |
| 86 | + default: |
| 87 | + days := int(d.Hours() / 24) |
| 88 | + if days == 1 { |
| 89 | + return "1 day ago" |
| 90 | + } |
| 91 | + return fmt.Sprintf("%d days ago", days) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// TruncateID truncates an ID to 12 characters (like Docker) |
| 96 | +func TruncateID(id string) string { |
| 97 | + if len(id) > 12 { |
| 98 | + return id[:12] |
| 99 | + } |
| 100 | + return id |
| 101 | +} |
| 102 | + |
| 103 | +// TruncateString truncates a string to max length with ellipsis |
| 104 | +func TruncateString(s string, max int) string { |
| 105 | + if len(s) <= max { |
| 106 | + return s |
| 107 | + } |
| 108 | + if max <= 3 { |
| 109 | + return s[:max] |
| 110 | + } |
| 111 | + return s[:max-3] + "..." |
| 112 | +} |
| 113 | + |
| 114 | +// GenerateInstanceName generates a name from image reference |
| 115 | +func GenerateInstanceName(image string) string { |
| 116 | + // Extract image name without registry/tag |
| 117 | + name := image |
| 118 | + |
| 119 | + // Remove registry prefix |
| 120 | + if idx := strings.LastIndex(name, "/"); idx != -1 { |
| 121 | + name = name[idx+1:] |
| 122 | + } |
| 123 | + |
| 124 | + // Remove tag/digest |
| 125 | + if idx := strings.Index(name, ":"); idx != -1 { |
| 126 | + name = name[:idx] |
| 127 | + } |
| 128 | + if idx := strings.Index(name, "@"); idx != -1 { |
| 129 | + name = name[:idx] |
| 130 | + } |
| 131 | + |
| 132 | + // Add random suffix |
| 133 | + suffix := randomSuffix(4) |
| 134 | + return fmt.Sprintf("%s-%s", name, suffix) |
| 135 | +} |
| 136 | + |
| 137 | +// randomSuffix generates a random alphanumeric suffix |
| 138 | +func randomSuffix(n int) string { |
| 139 | + const chars = "abcdefghijklmnopqrstuvwxyz0123456789" |
| 140 | + b := make([]byte, n) |
| 141 | + for i := range b { |
| 142 | + // Simple pseudo-random using time |
| 143 | + b[i] = chars[(time.Now().UnixNano()+int64(i))%int64(len(chars))] |
| 144 | + } |
| 145 | + return string(b) |
| 146 | +} |
| 147 | + |
| 148 | + |
0 commit comments