1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
package askcli
import (
"fmt"
"io"
"slices"
"strings"
"codeberg.org/snonux/hexai/internal/termprint"
)
// FormatTaskList renders a table of tasks using the terminal's width when possible.
func FormatTaskList(tasks []TaskExport, aliases map[string]string) string {
return FormatTaskListForWidth(tasks, aliases, 0)
}
// FormatTaskListForWidth renders a table of tasks constrained to the provided terminal width.
func FormatTaskListForWidth(tasks []TaskExport, aliases map[string]string, terminalWidth int) string {
widths := taskListWidthsFor(tasks, aliases, terminalWidth)
var b strings.Builder
writeTaskListHeader(&b, widths)
writeTaskListSeparator(&b, widths)
for _, t := range tasks {
writeTaskListRow(&b, widths, t, aliases)
}
return b.String()
}
type taskListWidths struct {
Urgency int
Priority int
ID int
Status int
Started int
Tags int
Description int
}
func taskListWidthsFor(tasks []TaskExport, aliases map[string]string, terminalWidth int) taskListWidths {
widths := taskListWidths{
Urgency: len("Urgency"),
Priority: len("Prio"),
ID: len("ID"),
Status: len("Status"),
Started: len("Started"),
Tags: len("Tags"),
Description: len("Description"),
}
longestDescription := widths.Description
for _, t := range tasks {
widths.Urgency = max(widths.Urgency, len(fmt.Sprintf("%.1f", t.Urgency)))
widths.Priority = max(widths.Priority, len(t.Priority))
widths.ID = max(widths.ID, len(displayTaskAlias(t.UUID, aliases)))
widths.Status = max(widths.Status, len(t.Status))
widths.Started = max(widths.Started, len(formatTaskStarted(t)))
widths.Tags = max(widths.Tags, len(formatTaskTags(t.Tags)))
longestDescription = max(longestDescription, len(t.Description))
}
widths.Description = taskListDescriptionWidth(widths, terminalWidth, longestDescription)
return widths
}
func writeTaskListHeader(b *strings.Builder, widths taskListWidths) {
fmt.Fprintf(b, "%-*s | %-*s | %-*s | %-*s | %-*s | %-*s | %-*s\n",
widths.Urgency, "Urgency",
widths.Priority, "Prio",
widths.ID, "ID",
widths.Status, "Status",
widths.Started, "Started",
widths.Tags, "Tags",
widths.Description, "Description",
)
}
func writeTaskListSeparator(b *strings.Builder, widths taskListWidths) {
total := widths.Urgency + widths.Priority + widths.ID + widths.Status + widths.Started + widths.Tags + widths.Description + 18
_, _ = io.WriteString(b, strings.Repeat("-", total)+"\n")
}
func writeTaskListRow(b *strings.Builder, widths taskListWidths, t TaskExport, aliases map[string]string) {
fmt.Fprintf(b, "%-*s | %-*s | %-*s | %-*s | %-*s | %-*s | %-*s\n",
widths.Urgency, fmt.Sprintf("%.1f", t.Urgency),
widths.Priority, t.Priority,
widths.ID, displayTaskAlias(t.UUID, aliases),
widths.Status, t.Status,
widths.Started, formatTaskStarted(t),
widths.Tags, formatTaskTags(t.Tags),
widths.Description, formatTaskDescription(t.Description, widths.Description),
)
}
func formatTaskTags(tags []string) string {
if len(tags) == 0 {
return "-"
}
return strings.Join(tags, ",")
}
func formatTaskDescription(desc string, width int) string {
if width <= 0 || len(desc) <= width {
return desc
}
if width <= 3 {
return desc[:width]
}
return desc[:width-3] + "..."
}
func formatTaskStarted(t TaskExport) string {
if t.Start == "" {
return "no"
}
return "yes"
}
func taskListDescriptionWidth(widths taskListWidths, terminalWidth, longestDescription int) int {
if terminalWidth <= 0 {
return longestDescription
}
available := terminalWidth - taskListFixedWidth(widths)
if available < len("Description") {
return len("Description")
}
if available < longestDescription {
return available
}
return longestDescription
}
func taskListFixedWidth(widths taskListWidths) int {
return widths.Urgency + widths.Priority + widths.ID + widths.Status + widths.Started + widths.Tags + 18
}
func detectTaskListTerminalWidth(w io.Writer) int {
return termprint.DetectTerminalWidth(w)
}
// FormatTaskInfo builds a human-readable block describing a single task.
func FormatTaskInfo(t TaskExport, alias string, dependencyAliases map[string]string) string {
var b strings.Builder
fmt.Fprintf(&b, "ID: %s\n", alias)
fmt.Fprintf(&b, "UUID: %s\n", t.UUID)
fmt.Fprintf(&b, "Description: %s\n", t.Description)
fmt.Fprintf(&b, "Status: %s\n", t.Status)
fmt.Fprintf(&b, "Started: %s\n", formatTaskStarted(t))
fmt.Fprintf(&b, "Priority: %s\n", t.Priority)
fmt.Fprintf(&b, "Urgency: %.1f\n", t.Urgency)
if len(t.Tags) > 0 {
fmt.Fprintf(&b, "Tags: %s\n", strings.Join(t.Tags, ", "))
}
if t.Start != "" {
fmt.Fprintf(&b, "Start time: %s\n", t.Start)
}
if len(t.Depends) > 0 {
fmt.Fprintf(&b, "Depends: %s\n", formatTaskDependencies(t.Depends, dependencyAliases))
}
if len(t.Annotations) > 0 {
_, _ = io.WriteString(&b, "Annotations:\n")
for _, a := range t.Annotations {
fmt.Fprintf(&b, " - %s (added %s)\n", a.Description, a.Entry)
}
}
return b.String()
}
// FormatSuccess returns the success string written to stdout after a task command runs.
func FormatSuccess(alias string) string {
return fmt.Sprintf("ok %s\n", alias)
}
// FormatError formats error output using the optional task identifier when available.
func FormatError(err error, taskID string) string {
if taskID != "" {
return fmt.Sprintf("error %s: %v\n", taskID, err)
}
return fmt.Sprintf("error: %v\n", err)
}
func formatTaskDependencies(depends []string, aliases map[string]string) string {
items := make([]string, 0, len(depends))
for _, uuid := range depends {
items = append(items, formatTaskReference(uuid, aliases))
}
slices.Sort(items)
return strings.Join(items, ", ")
}
func formatTaskReference(uuid string, aliases map[string]string) string {
alias := strings.TrimSpace(aliases[uuid])
if alias == "" || alias == uuid {
return uuid
}
return fmt.Sprintf("%s (%s)", alias, uuid)
}
|