summaryrefslogtreecommitdiff
path: root/internal/askcli/formatter.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-26 22:11:15 +0200
committerPaul Buetow <paul@buetow.org>2026-03-26 22:11:15 +0200
commitc8108deb77f1872e6a55925d5abf9c0ae12813c6 (patch)
treee6c0ae91926f0cf68c7dfb89ce46ab1d8facf933 /internal/askcli/formatter.go
parentc9d96d4c1918881f771165e97e71de49917d4181 (diff)
ask: align list header columns for task da6d3d36
Diffstat (limited to 'internal/askcli/formatter.go')
-rw-r--r--internal/askcli/formatter.go92
1 files changed, 81 insertions, 11 deletions
diff --git a/internal/askcli/formatter.go b/internal/askcli/formatter.go
index 886e819..828588b 100644
--- a/internal/askcli/formatter.go
+++ b/internal/askcli/formatter.go
@@ -7,23 +7,93 @@ import (
)
func FormatTaskList(tasks []TaskExport) string {
+ widths := taskListWidthsFor(tasks)
var b strings.Builder
- io.WriteString(&b, "Urgency | Priority | UUID | Status | Tags | Description\n")
- io.WriteString(&b, strings.Repeat("-", 120)+"\n")
+ writeTaskListHeader(&b, widths)
+ writeTaskListSeparator(&b, widths)
for _, t := range tasks {
- tags := strings.Join(t.Tags, ",")
- if tags == "" {
- tags = "-"
- }
- desc := t.Description
- if len(desc) > 50 {
- desc = desc[:47] + "..."
- }
- fmt.Fprintf(&b, "%.1f | %s | %s | %s | %s | %s\n", t.Urgency, t.Priority, t.UUID, t.Status, tags, desc)
+ writeTaskListRow(&b, widths, t)
}
return b.String()
}
+type taskListWidths struct {
+ Urgency int
+ Priority int
+ UUID int
+ Status int
+ Tags int
+ Description int
+}
+
+func taskListWidthsFor(tasks []TaskExport) taskListWidths {
+ widths := taskListWidths{
+ Urgency: len("Urgency"),
+ Priority: len("Priority"),
+ UUID: len("UUID"),
+ Status: len("Status"),
+ Tags: len("Tags"),
+ Description: len("Description"),
+ }
+ for _, t := range tasks {
+ widths.Urgency = maxInt(widths.Urgency, len(fmt.Sprintf("%.1f", t.Urgency)))
+ widths.Priority = maxInt(widths.Priority, len(t.Priority))
+ widths.UUID = maxInt(widths.UUID, len(t.UUID))
+ widths.Status = maxInt(widths.Status, len(t.Status))
+ widths.Tags = maxInt(widths.Tags, len(formatTaskTags(t.Tags)))
+ widths.Description = maxInt(widths.Description, len(formatTaskDescription(t.Description)))
+ }
+ return widths
+}
+
+func writeTaskListHeader(b *strings.Builder, widths taskListWidths) {
+ fmt.Fprintf(b, "%-*s | %-*s | %-*s | %-*s | %-*s | %-*s\n",
+ widths.Urgency, "Urgency",
+ widths.Priority, "Priority",
+ widths.UUID, "UUID",
+ widths.Status, "Status",
+ widths.Tags, "Tags",
+ widths.Description, "Description",
+ )
+}
+
+func writeTaskListSeparator(b *strings.Builder, widths taskListWidths) {
+ total := widths.Urgency + widths.Priority + widths.UUID + widths.Status + widths.Tags + widths.Description + 15
+ io.WriteString(b, strings.Repeat("-", total)+"\n")
+}
+
+func writeTaskListRow(b *strings.Builder, widths taskListWidths, t TaskExport) {
+ fmt.Fprintf(b, "%-*s | %-*s | %-*s | %-*s | %-*s | %-*s\n",
+ widths.Urgency, fmt.Sprintf("%.1f", t.Urgency),
+ widths.Priority, t.Priority,
+ widths.UUID, t.UUID,
+ widths.Status, t.Status,
+ widths.Tags, formatTaskTags(t.Tags),
+ widths.Description, formatTaskDescription(t.Description),
+ )
+}
+
+func formatTaskTags(tags []string) string {
+ if len(tags) == 0 {
+ return "-"
+ }
+ return strings.Join(tags, ",")
+}
+
+func formatTaskDescription(desc string) string {
+ if len(desc) > 50 {
+ return desc[:47] + "..."
+ }
+ return desc
+}
+
+func maxInt(a, b int) int {
+ if a > b {
+ return a
+ }
+ return b
+}
+
func FormatTaskInfo(t TaskExport) string {
var b strings.Builder
fmt.Fprintf(&b, "UUID: %s\n", t.UUID)