summaryrefslogtreecommitdiff
path: root/internal/askcli/formatter.go
blob: a181c1af2cdbadb4d51ed2f71f15caaea36d2247 (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package askcli

import (
	"fmt"
	"io"
	"slices"
	"strings"

	"codeberg.org/snonux/hexai/internal/termprint"
)

func FormatTaskList(tasks []TaskExport, aliases map[string]string) string {
	return FormatTaskListForWidth(tasks, aliases, 0)
}

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 = maxInt(widths.Urgency, len(fmt.Sprintf("%.1f", t.Urgency)))
		widths.Priority = maxInt(widths.Priority, len(t.Priority))
		widths.ID = maxInt(widths.ID, len(displayTaskAlias(t.UUID, aliases)))
		widths.Status = maxInt(widths.Status, len(t.Status))
		widths.Started = maxInt(widths.Started, len(formatTaskStarted(t)))
		widths.Tags = maxInt(widths.Tags, len(formatTaskTags(t.Tags)))
		longestDescription = maxInt(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 maxInt(a, b int) int {
	if a > b {
		return a
	}
	return b
}

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)
}

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()
}

func FormatSuccess(alias string) string {
	return fmt.Sprintf("ok %s\n", alias)
}

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 displayResolvedTaskID(resolved resolvedTaskSelector) string {
	return displayTaskAlias(resolved.UUID, map[string]string{resolved.UUID: resolved.Alias})
}

func displayTaskAlias(uuid string, aliases map[string]string) string {
	if alias := strings.TrimSpace(aliases[uuid]); alias != "" {
		return alias
	}
	return uuid
}

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)
}

// NormalizeUUID strips any leading "uuid:" prefix so callers can accept
// both "uuid:<value>" and bare UUID strings interchangeably. The returned
// value is always a plain UUID ready to be prefixed again when building
// taskwarrior filter arguments.
func NormalizeUUID(s string) string {
	return strings.TrimPrefix(s, "uuid:")
}

func IsNumericID(s string) bool {
	if s == "" {
		return false
	}
	for _, c := range s {
		if c < '0' || c > '9' {
			return false
		}
	}
	return true
}

func RejectNumericID() string {
	return "error: use a task alias ID or UUID, not a numeric Taskwarrior task ID\n"
}