()
| 221 | } |
| 222 | |
| 223 | func (m snapshotListModel) renderTable() string { |
| 224 | if len(m.snapshots) == 0 { |
| 225 | return warningStyleTUI.Render("⚠ No snapshots found. Use 'tnr snapshot create' to create a snapshot.") |
| 226 | } |
| 227 | |
| 228 | colWidths := map[string]int{ |
| 229 | "Name": 30, |
| 230 | "Status": 12, |
| 231 | "Size": 10, |
| 232 | "Created": 22, |
| 233 | } |
| 234 | |
| 235 | var b strings.Builder |
| 236 | |
| 237 | headers := []string{"Name", "Status", "Size", "Created"} |
| 238 | headerRow := make([]string, len(headers)) |
| 239 | for i, h := range headers { |
| 240 | headerRow[i] = m.styles.header.Width(colWidths[h]).Render(h) |
| 241 | } |
| 242 | b.WriteString(strings.Join(headerRow, "")) |
| 243 | b.WriteString("\n") |
| 244 | |
| 245 | separatorRow := make([]string, len(headers)) |
| 246 | for i, h := range headers { |
| 247 | separatorRow[i] = strings.Repeat("─", colWidths[h]+2) |
| 248 | } |
| 249 | b.WriteString(strings.Join(separatorRow, "")) |
| 250 | b.WriteString("\n") |
| 251 | |
| 252 | snapshots := m.snapshots |
| 253 | if len(snapshots) > 1 { |
| 254 | sortedSnapshots := make([]api.Snapshot, len(snapshots)) |
| 255 | copy(sortedSnapshots, snapshots) |
| 256 | sort.Slice(sortedSnapshots, func(i, j int) bool { |
| 257 | return sortedSnapshots[i].CreatedAt < sortedSnapshots[j].CreatedAt |
| 258 | }) |
| 259 | snapshots = sortedSnapshots |
| 260 | } |
| 261 | |
| 262 | for _, snapshot := range snapshots { |
| 263 | name := truncate(snapshot.Name, colWidths["Name"]) |
| 264 | status := m.formatStatus(snapshot.Status, colWidths["Status"]) |
| 265 | size := truncate(fmt.Sprintf("%d GB", snapshot.MinimumDiskSizeGB), colWidths["Size"]) |
| 266 | createdTime := time.Unix(snapshot.CreatedAt, 0) |
| 267 | created := truncate(createdTime.Format("2006-01-02 15:04:05"), colWidths["Created"]) |
| 268 | |
| 269 | row := []string{ |
| 270 | m.styles.cell.Width(colWidths["Name"]).Render(name), |
| 271 | m.styles.cell.Width(colWidths["Status"]).Render(status), |
| 272 | m.styles.cell.Width(colWidths["Size"]).Render(size), |
| 273 | m.styles.cell.Width(colWidths["Created"]).Render(created), |
| 274 | } |
| 275 | b.WriteString(strings.Join(row, "")) |
| 276 | b.WriteString("\n") |
| 277 | } |
| 278 | |
| 279 | return b.String() |
| 280 | } |
no test coverage detected