@zinn-dev/cli 0.36.1 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +264 -78
- package/index.ts +7 -15
- package/package.json +7 -5
- package/release-stamp.json +1 -0
- package/src/database.test.ts +70 -0
- package/src/database.ts +41 -0
- package/src/lib.test.ts +86 -0
- package/src/lib.ts +75 -10
- package/src/presentation/activity-presentation.test.ts +71 -0
- package/src/presentation/activity-presentation.ts +489 -0
- package/src/presentation/color.ts +1 -1
- package/src/presentation/column-presentation.ts +48 -0
- package/src/presentation/description-presentation.ts +7 -4
- package/src/presentation/row-layout.ts +77 -0
- package/src/presentation/section.ts +16 -0
- package/src/presentation/task-presentation.ts +50 -63
- package/src/presentation/time.ts +17 -0
- package/src/routes/activity-router.ts +77 -0
- package/src/routes/backup-router.ts +3 -2
- package/src/routes/comment-router.ts +54 -38
- package/src/routes/label-router.ts +47 -27
- package/src/routes/migrate-router.test.ts +31 -0
- package/src/routes/migrate-router.ts +48 -0
- package/src/routes/project-router.ts +129 -106
- package/src/routes/reindex-router.ts +27 -0
- package/src/routes/relation-router.ts +20 -13
- package/src/routes/restore-router.test.ts +44 -0
- package/src/routes/restore-router.ts +90 -0
- package/src/routes/router.test.ts +41 -0
- package/src/routes/router.ts +133 -24
- package/src/routes/routes.ts +8 -0
- package/src/routes/task-router.ts +228 -142
- package/src/text-edit.ts +68 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import type { Activity, ActivityAction, ActivityTaskIdentity } from "@zinn-dev/core";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
activity,
|
|
5
|
+
activityTaskKeySchema,
|
|
6
|
+
column,
|
|
7
|
+
label,
|
|
8
|
+
minimumActivityIdLength,
|
|
9
|
+
project,
|
|
10
|
+
task,
|
|
11
|
+
} from "@zinn-dev/core";
|
|
12
|
+
import { formatDescriptionPreview } from "./description-presentation";
|
|
13
|
+
import { formatRows } from "./row-layout";
|
|
14
|
+
import { formatSection } from "./section";
|
|
15
|
+
import { formatLocalTime } from "./time";
|
|
16
|
+
|
|
17
|
+
type NavigationIdentity = {
|
|
18
|
+
name: string;
|
|
19
|
+
status: "current" | "archived" | "deleted" | "recorded";
|
|
20
|
+
distinguishesTaskId?: boolean;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type PresentedActivity = {
|
|
24
|
+
event: Activity;
|
|
25
|
+
displayId: string;
|
|
26
|
+
navigation?: NavigationIdentity;
|
|
27
|
+
removedTasks: Array<ActivityTaskIdentity>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const RECORDED_FIELDS = new Set([
|
|
31
|
+
"id",
|
|
32
|
+
"key",
|
|
33
|
+
"name",
|
|
34
|
+
"title",
|
|
35
|
+
"description",
|
|
36
|
+
"priority",
|
|
37
|
+
"color",
|
|
38
|
+
"column",
|
|
39
|
+
"columns",
|
|
40
|
+
"project",
|
|
41
|
+
"tasks",
|
|
42
|
+
"labels",
|
|
43
|
+
"added",
|
|
44
|
+
"removed",
|
|
45
|
+
"type",
|
|
46
|
+
"source",
|
|
47
|
+
"target",
|
|
48
|
+
"comment",
|
|
49
|
+
"text",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
const ENTITY_PREVIEW_LENGTH = 24;
|
|
53
|
+
const ACTION_LABELS: Record<ActivityAction, string> = {
|
|
54
|
+
create: "created",
|
|
55
|
+
edit: "edited",
|
|
56
|
+
move: "moved",
|
|
57
|
+
order: "reordered",
|
|
58
|
+
archive: "archived",
|
|
59
|
+
unarchive: "unarchived",
|
|
60
|
+
delete: "deleted",
|
|
61
|
+
relation_create: "added relation",
|
|
62
|
+
relation_delete: "removed relation",
|
|
63
|
+
comment_create: "added comment",
|
|
64
|
+
comment_edit: "edited comment",
|
|
65
|
+
comment_delete: "deleted comment",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
69
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function commonPrefixLength(left: string, right: string) {
|
|
73
|
+
let length = 0;
|
|
74
|
+
while (length < left.length && length < right.length && left[length] === right[length]) {
|
|
75
|
+
length++;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return length;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function canonicalPrefix(id: string, hexLength: number) {
|
|
82
|
+
let digits = 0;
|
|
83
|
+
|
|
84
|
+
for (let index = 0; index < id.length; index++) {
|
|
85
|
+
if (id[index] !== "-") {
|
|
86
|
+
digits++;
|
|
87
|
+
}
|
|
88
|
+
if (digits === hexLength) {
|
|
89
|
+
return id.slice(0, index + 1);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return id;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Neighbors in sorted UUID order determine the shortest globally unique left prefix. */
|
|
96
|
+
export function abbreviateActivityIds(ids: Array<string>) {
|
|
97
|
+
const sorted = ids
|
|
98
|
+
.map((id) => ({ id, hex: id.replaceAll("-", "").toLowerCase() }))
|
|
99
|
+
.sort((left, right) => left.hex.localeCompare(right.hex));
|
|
100
|
+
const prefixes = new Map<string, string>();
|
|
101
|
+
|
|
102
|
+
for (const [index, entry] of sorted.entries()) {
|
|
103
|
+
const previous = sorted[index - 1]?.hex ?? "";
|
|
104
|
+
const next = sorted[index + 1]?.hex ?? "";
|
|
105
|
+
const length = Math.max(
|
|
106
|
+
minimumActivityIdLength,
|
|
107
|
+
commonPrefixLength(entry.hex, previous) + 1,
|
|
108
|
+
commonPrefixLength(entry.hex, next) + 1,
|
|
109
|
+
);
|
|
110
|
+
prefixes.set(entry.id, canonicalPrefix(entry.id, length));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return prefixes;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function getTaskNavigation(events: Array<Activity>, taskKey?: string) {
|
|
117
|
+
if (taskKey !== undefined) {
|
|
118
|
+
return activity.getTaskIdentities(taskKey);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const identities = new Map<string, ActivityTaskIdentity>();
|
|
122
|
+
if (events.some((event) => event.entity_type === "task")) {
|
|
123
|
+
for (const item of task.getAll({ archive: "all" })) {
|
|
124
|
+
identities.set(item.id, {
|
|
125
|
+
id: item.id,
|
|
126
|
+
key: item.key,
|
|
127
|
+
title: item.title,
|
|
128
|
+
status: item.archived_at == null ? "active" : "archived",
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const historicalKeys = new Set<string>();
|
|
134
|
+
function addKey(key: unknown) {
|
|
135
|
+
const parsed = activityTaskKeySchema.safeParse(key);
|
|
136
|
+
if (parsed.success) {
|
|
137
|
+
historicalKeys.add(parsed.data);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
for (const event of events) {
|
|
142
|
+
if (event.action !== "delete") {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (event.entity_type === "task") {
|
|
147
|
+
addKey(event.changes?.key);
|
|
148
|
+
} else if (event.entity_type === "project" && Array.isArray(event.changes?.tasks)) {
|
|
149
|
+
for (const snapshot of event.changes.tasks) {
|
|
150
|
+
if (isRecord(snapshot)) {
|
|
151
|
+
addKey(snapshot.key);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const key of historicalKeys) {
|
|
157
|
+
for (const identity of activity.getTaskIdentities(key)) {
|
|
158
|
+
identities.set(identity.id, identity);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return [...identities.values()];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function presentActivities(
|
|
166
|
+
events: Array<Activity>,
|
|
167
|
+
options: { taskKey?: string; showsFullIds?: boolean } = {},
|
|
168
|
+
): Array<PresentedActivity> {
|
|
169
|
+
if (events.length === 0) {
|
|
170
|
+
return [];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const prefixes = options.showsFullIds
|
|
174
|
+
? new Map<string, string>()
|
|
175
|
+
: abbreviateActivityIds(activity.getIds());
|
|
176
|
+
const tasks = getTaskNavigation(events, options.taskKey);
|
|
177
|
+
const tasksById = new Map(tasks.map((identity) => [identity.id, identity]));
|
|
178
|
+
const taskIdsByKey = new Map<string, number>();
|
|
179
|
+
|
|
180
|
+
for (const identity of tasks) {
|
|
181
|
+
taskIdsByKey.set(identity.key, (taskIdsByKey.get(identity.key) ?? 0) + 1);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const projectsById = new Map(project.getAll().map((item) => [item.id, item]));
|
|
185
|
+
const labelsById = events.some((event) => event.entity_type === "label")
|
|
186
|
+
? new Map(label.getAll().map((item) => [item.id, item]))
|
|
187
|
+
: new Map();
|
|
188
|
+
const navigationByEntity = new Map<string, NavigationIdentity | undefined>();
|
|
189
|
+
|
|
190
|
+
return events.map((event) => {
|
|
191
|
+
const entityKey = `${event.entity_type}:${event.entity_id}`;
|
|
192
|
+
let navigation = navigationByEntity.get(entityKey);
|
|
193
|
+
|
|
194
|
+
if (!navigationByEntity.has(entityKey)) {
|
|
195
|
+
switch (event.entity_type) {
|
|
196
|
+
case "task": {
|
|
197
|
+
const identity = tasksById.get(event.entity_id);
|
|
198
|
+
if (identity !== undefined) {
|
|
199
|
+
navigation = {
|
|
200
|
+
name: identity.key,
|
|
201
|
+
status: identity.status === "active" ? "current" : identity.status,
|
|
202
|
+
distinguishesTaskId: taskIdsByKey.get(identity.key)! > 1,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
case "project": {
|
|
208
|
+
const current = projectsById.get(event.entity_id);
|
|
209
|
+
if (current !== undefined) {
|
|
210
|
+
navigation = { name: current.key, status: "current" };
|
|
211
|
+
}
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
case "column": {
|
|
215
|
+
const current = column.getById(event.entity_id);
|
|
216
|
+
if (current != null) {
|
|
217
|
+
navigation = { name: current.name, status: "current" };
|
|
218
|
+
}
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
case "label": {
|
|
222
|
+
const current = labelsById.get(event.entity_id);
|
|
223
|
+
if (current !== undefined) {
|
|
224
|
+
navigation = { name: current.name, status: "current" };
|
|
225
|
+
}
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
navigationByEntity.set(entityKey, navigation);
|
|
230
|
+
}
|
|
231
|
+
const isSnapshot = event.action === "create" || event.action === "delete";
|
|
232
|
+
const recordedName = event.changes?.key ?? event.changes?.name;
|
|
233
|
+
|
|
234
|
+
if (navigation === undefined && isSnapshot && typeof recordedName === "string") {
|
|
235
|
+
navigation = { name: recordedName, status: "recorded" };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const removedTaskIds =
|
|
239
|
+
event.entity_type === "project" &&
|
|
240
|
+
event.action === "delete" &&
|
|
241
|
+
Array.isArray(event.changes?.tasks)
|
|
242
|
+
? new Set(event.changes.tasks.filter(isRecord).map((snapshot) => snapshot.id))
|
|
243
|
+
: new Set();
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
event,
|
|
247
|
+
displayId: options.showsFullIds ? event.id : prefixes.get(event.id)!,
|
|
248
|
+
navigation,
|
|
249
|
+
removedTasks:
|
|
250
|
+
options.taskKey === undefined
|
|
251
|
+
? []
|
|
252
|
+
: tasks.filter((identity) => removedTaskIds.has(identity.id)),
|
|
253
|
+
};
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function formatNavigation(entry: PresentedActivity, options: { showsFullNames?: boolean } = {}) {
|
|
258
|
+
const { event, navigation } = entry;
|
|
259
|
+
if (navigation === undefined) {
|
|
260
|
+
return `${event.entity_type} ${event.entity_id}`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const identity = navigation.distinguishesTaskId ? `; ${event.entity_id}` : "";
|
|
264
|
+
const name = options.showsFullNames ? navigation.name : formatDescriptionPreview(navigation.name);
|
|
265
|
+
return `${event.entity_type} ${name} [${navigation.status}${identity}]`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function summarizeValue(value: unknown): string {
|
|
269
|
+
if (typeof value === "string") {
|
|
270
|
+
return value;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (isRecord(value)) {
|
|
274
|
+
const name = value.key ?? value.name ?? value.title ?? value.id;
|
|
275
|
+
if (typeof name === "string") {
|
|
276
|
+
return name;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return JSON.stringify(value);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function isRecordedChangePair(event: Activity, value: unknown): value is [unknown, unknown] {
|
|
284
|
+
return (
|
|
285
|
+
["edit", "move", "comment_edit"].includes(event.action) &&
|
|
286
|
+
Array.isArray(value) &&
|
|
287
|
+
value.length === 2
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function formatRemovedTasks(entry: PresentedActivity) {
|
|
292
|
+
return entry.removedTasks.map((identity) => `removed ${identity.key} (${identity.id})`);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function formatChangeSummary(
|
|
296
|
+
entry: PresentedActivity,
|
|
297
|
+
options: { showsRemovedTasks?: boolean; omitsIdentity?: boolean } = {},
|
|
298
|
+
) {
|
|
299
|
+
const { event } = entry;
|
|
300
|
+
const changes = event.changes;
|
|
301
|
+
let summary = "-";
|
|
302
|
+
|
|
303
|
+
if (changes !== null) {
|
|
304
|
+
summary =
|
|
305
|
+
Object.entries(changes)
|
|
306
|
+
.map(([field, value]) => {
|
|
307
|
+
const isSnapshot = event.action === "create" || event.action === "delete";
|
|
308
|
+
if (isSnapshot && value === null) {
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
const repeatsIdentity =
|
|
312
|
+
options.omitsIdentity &&
|
|
313
|
+
isSnapshot &&
|
|
314
|
+
(field === "key" || field === "name") &&
|
|
315
|
+
value === entry.navigation?.name;
|
|
316
|
+
if (repeatsIdentity) {
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
if (!RECORDED_FIELDS.has(field)) {
|
|
320
|
+
return `${field}: ${JSON.stringify(value)}`;
|
|
321
|
+
}
|
|
322
|
+
if (field === "description") {
|
|
323
|
+
return event.action === "edit" ? "description changed" : "description recorded";
|
|
324
|
+
}
|
|
325
|
+
if (field === "title" && options.omitsIdentity && isSnapshot) {
|
|
326
|
+
return summarizeValue(value);
|
|
327
|
+
}
|
|
328
|
+
if (isRecordedChangePair(event, value)) {
|
|
329
|
+
return `${field}: ${summarizeValue(value[0])} → ${summarizeValue(value[1])}`;
|
|
330
|
+
}
|
|
331
|
+
if (Array.isArray(value)) {
|
|
332
|
+
if (field === "labels" && value.length === 0) {
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
return `${field}: ${value.length}`;
|
|
336
|
+
}
|
|
337
|
+
if (field === "labels" && isRecord(value)) {
|
|
338
|
+
const additions =
|
|
339
|
+
Array.isArray(value.added) && value.added.length > 0
|
|
340
|
+
? `added labels ${value.added.map(summarizeValue).join(", ")}`
|
|
341
|
+
: "";
|
|
342
|
+
const removals =
|
|
343
|
+
Array.isArray(value.removed) && value.removed.length > 0
|
|
344
|
+
? `removed labels ${value.removed.map(summarizeValue).join(", ")}`
|
|
345
|
+
: "";
|
|
346
|
+
return (
|
|
347
|
+
[additions, removals].filter((part) => part.length > 0).join("; ") ||
|
|
348
|
+
`labels: ${JSON.stringify(value)}`
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
if (field === "comment" && isRecord(value)) {
|
|
352
|
+
return `comment ${summarizeValue(value)}${typeof value.text === "string" ? `: ${value.text}` : ""}`;
|
|
353
|
+
}
|
|
354
|
+
return `${field}: ${summarizeValue(value)}`;
|
|
355
|
+
})
|
|
356
|
+
.filter((part) => part !== null)
|
|
357
|
+
.join("; ") || "-";
|
|
358
|
+
}
|
|
359
|
+
const removed = options.showsRemovedTasks === false ? "" : formatRemovedTasks(entry).join("; ");
|
|
360
|
+
return [formatDescriptionPreview(summary), removed].filter((part) => part.length > 0).join("; ");
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function formatActivityContent(entry: PresentedActivity) {
|
|
364
|
+
const { event, navigation } = entry;
|
|
365
|
+
const name = navigation?.name ?? event.entity_id;
|
|
366
|
+
const shortName = formatDescriptionPreview(name, { maxLength: ENTITY_PREVIEW_LENGTH });
|
|
367
|
+
const entityType =
|
|
368
|
+
event.entity_type === "task" && navigation !== undefined ? "" : `${event.entity_type} `;
|
|
369
|
+
const isMutableCurrentName =
|
|
370
|
+
navigation?.status === "current" &&
|
|
371
|
+
(event.entity_type === "label" || event.entity_type === "column");
|
|
372
|
+
const provenance =
|
|
373
|
+
navigation?.status === "recorded"
|
|
374
|
+
? " [recorded]"
|
|
375
|
+
: isMutableCurrentName
|
|
376
|
+
? " [current name]"
|
|
377
|
+
: "";
|
|
378
|
+
const action = ACTION_LABELS[event.action] ?? event.action;
|
|
379
|
+
const summary = formatChangeSummary(entry, { showsRemovedTasks: false, omitsIdentity: true });
|
|
380
|
+
const lines = [
|
|
381
|
+
`${entityType}${shortName}${provenance} · ${action}${summary === "-" ? "" : `: ${summary}`}`,
|
|
382
|
+
];
|
|
383
|
+
|
|
384
|
+
if (shortName !== name) {
|
|
385
|
+
const nameLabel =
|
|
386
|
+
navigation === undefined
|
|
387
|
+
? "Entity ID"
|
|
388
|
+
: event.entity_type === "task" || event.entity_type === "project"
|
|
389
|
+
? `Full ${event.entity_type} key`
|
|
390
|
+
: `Full ${event.entity_type} name`;
|
|
391
|
+
lines.push(`${nameLabel}: ${name}`);
|
|
392
|
+
}
|
|
393
|
+
if (navigation?.distinguishesTaskId) {
|
|
394
|
+
lines.push(`Task identity: ${navigation.status}; ${event.entity_id}`);
|
|
395
|
+
}
|
|
396
|
+
lines.push(...formatRemovedTasks(entry));
|
|
397
|
+
return lines.join("\n");
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function formatRecordedValue(value: unknown): string {
|
|
401
|
+
if (typeof value === "string") {
|
|
402
|
+
return value;
|
|
403
|
+
}
|
|
404
|
+
if (Array.isArray(value)) {
|
|
405
|
+
if (value.length === 0) {
|
|
406
|
+
return "[]";
|
|
407
|
+
}
|
|
408
|
+
return value
|
|
409
|
+
.map((item, index) => formatSection(`Item ${index + 1}`, formatRecordedValue(item)))
|
|
410
|
+
.join("\n\n");
|
|
411
|
+
}
|
|
412
|
+
if (isRecord(value)) {
|
|
413
|
+
return (
|
|
414
|
+
Object.entries(value)
|
|
415
|
+
.map(([field, item]) =>
|
|
416
|
+
formatSection(
|
|
417
|
+
field,
|
|
418
|
+
RECORDED_FIELDS.has(field) ? formatRecordedValue(item) : JSON.stringify(item, null, 2),
|
|
419
|
+
),
|
|
420
|
+
)
|
|
421
|
+
.join("\n\n") || "{}"
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
return JSON.stringify(value);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function formatRecordedChanges(event: Activity) {
|
|
428
|
+
if (event.changes === null) {
|
|
429
|
+
return "null";
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const sections: Array<string> = [];
|
|
433
|
+
const unknownFields: Record<string, unknown> = {};
|
|
434
|
+
for (const [field, value] of Object.entries(event.changes)) {
|
|
435
|
+
if (!RECORDED_FIELDS.has(field)) {
|
|
436
|
+
unknownFields[field] = value;
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
const body = isRecordedChangePair(event, value)
|
|
440
|
+
? `${formatSection("From", formatRecordedValue(value[0]))}\n\n${formatSection("To", formatRecordedValue(value[1]))}`
|
|
441
|
+
: formatRecordedValue(value);
|
|
442
|
+
sections.push(formatSection(field, body));
|
|
443
|
+
}
|
|
444
|
+
if (Object.keys(unknownFields).length > 0) {
|
|
445
|
+
sections.push(formatSection("Raw JSON", JSON.stringify(unknownFields, null, 2)));
|
|
446
|
+
}
|
|
447
|
+
return sections.join("\n\n") || "{}";
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export function formatActivityList(
|
|
451
|
+
entries: Array<PresentedActivity>,
|
|
452
|
+
options: { showsDetails?: boolean } = {},
|
|
453
|
+
) {
|
|
454
|
+
const rows = entries.map((entry) => {
|
|
455
|
+
const metadata = [
|
|
456
|
+
{ text: entry.displayId },
|
|
457
|
+
{ text: formatLocalTime(entry.event.created_at), style: "dim" as const },
|
|
458
|
+
{ text: `${entry.event.actor_name} (${entry.event.actor_kind})` },
|
|
459
|
+
];
|
|
460
|
+
if (!options.showsDetails) {
|
|
461
|
+
return [...metadata, { text: formatActivityContent(entry) }];
|
|
462
|
+
}
|
|
463
|
+
return [
|
|
464
|
+
...metadata,
|
|
465
|
+
{ text: formatNavigation(entry, { showsFullNames: true }) },
|
|
466
|
+
{ text: entry.event.action },
|
|
467
|
+
{ text: formatChangeSummary(entry) },
|
|
468
|
+
];
|
|
469
|
+
});
|
|
470
|
+
if (!options.showsDetails) {
|
|
471
|
+
return formatRows(rows, { indentsContinuationLines: true });
|
|
472
|
+
}
|
|
473
|
+
return entries
|
|
474
|
+
.map((entry, index) =>
|
|
475
|
+
[
|
|
476
|
+
formatRows([rows[index]!]),
|
|
477
|
+
formatSection("Entity", `${entry.event.entity_type} ${entry.event.entity_id}`),
|
|
478
|
+
...(entry.navigation === undefined
|
|
479
|
+
? []
|
|
480
|
+
: [formatSection("Navigation", formatNavigation(entry, { showsFullNames: true }))]),
|
|
481
|
+
formatSection(
|
|
482
|
+
"Actor",
|
|
483
|
+
`${entry.event.actor_name}\nKind: ${entry.event.actor_kind}\nEmail: ${entry.event.actor_email ?? "null"}`,
|
|
484
|
+
),
|
|
485
|
+
formatSection("Changes", formatRecordedChanges(entry.event)),
|
|
486
|
+
].join("\n\n"),
|
|
487
|
+
)
|
|
488
|
+
.join("\n\n");
|
|
489
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { styleText } from "node:util";
|
|
2
2
|
|
|
3
|
-
type StyleFormat = Parameters<typeof styleText>[0];
|
|
3
|
+
export type StyleFormat = Parameters<typeof styleText>[0];
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Bun's styleText emits ANSI even when stdout is piped or NO_COLOR is set, so Zinn decides when to color.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { RowCell } from "./row-layout";
|
|
2
|
+
|
|
3
|
+
import { formatRows } from "./row-layout";
|
|
4
|
+
|
|
5
|
+
export type PresentedColumn = { name: string; project?: string; taskCount?: number };
|
|
6
|
+
export type PresentedColumnTaskCount = PresentedColumn & { taskCount: number };
|
|
7
|
+
|
|
8
|
+
/** Archived tasks are off the board, so they share one row below the columns instead of a column of their own. */
|
|
9
|
+
const ARCHIVED_ROW_LABEL = "Archived";
|
|
10
|
+
|
|
11
|
+
export function columnNameCell(columnName: string): RowCell {
|
|
12
|
+
// TODO: Derive column colors from column roles once roles exist instead of relying on names.
|
|
13
|
+
if (columnName === "Backlog") {
|
|
14
|
+
return { text: columnName, style: "gray" };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (columnName === "Done") {
|
|
18
|
+
return { text: columnName, style: "green" };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return { text: columnName };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The archived row is left out when there are no archived tasks. */
|
|
25
|
+
export function formatColumnList(
|
|
26
|
+
presentedColumns: Array<PresentedColumn>,
|
|
27
|
+
options: { showsProject?: boolean; showsTaskCounts?: boolean; archivedTaskCount?: number } = {},
|
|
28
|
+
) {
|
|
29
|
+
const rows: Array<Array<RowCell>> = presentedColumns.map((presentedColumn) => {
|
|
30
|
+
const projectCells: Array<RowCell> =
|
|
31
|
+
options.showsProject && presentedColumn.project != null
|
|
32
|
+
? [{ text: presentedColumn.project }]
|
|
33
|
+
: [];
|
|
34
|
+
|
|
35
|
+
const taskCountCells: Array<RowCell> =
|
|
36
|
+
options.showsTaskCounts && presentedColumn.taskCount != null
|
|
37
|
+
? [{ text: String(presentedColumn.taskCount) }]
|
|
38
|
+
: [];
|
|
39
|
+
|
|
40
|
+
return [...projectCells, columnNameCell(presentedColumn.name), ...taskCountCells];
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
if (options.archivedTaskCount != null && options.archivedTaskCount > 0) {
|
|
44
|
+
rows.push([{ text: ARCHIVED_ROW_LABEL }, { text: String(options.archivedTaskCount) }]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return formatRows(rows);
|
|
48
|
+
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
export function formatDescriptionPreview(
|
|
2
|
-
|
|
1
|
+
export function formatDescriptionPreview(
|
|
2
|
+
description: string | null,
|
|
3
|
+
options: { maxLength?: number } = {},
|
|
4
|
+
) {
|
|
5
|
+
const previewLength = options.maxLength ?? 80;
|
|
3
6
|
|
|
4
7
|
if (description == null) {
|
|
5
8
|
return "";
|
|
@@ -7,9 +10,9 @@ export function formatDescriptionPreview(description: string | null) {
|
|
|
7
10
|
|
|
8
11
|
const normalizedDescription = description.replace(/\s+/g, " ").trim();
|
|
9
12
|
|
|
10
|
-
if (normalizedDescription.length <=
|
|
13
|
+
if (normalizedDescription.length <= previewLength) {
|
|
11
14
|
return normalizedDescription;
|
|
12
15
|
}
|
|
13
16
|
|
|
14
|
-
return `${normalizedDescription.slice(0,
|
|
17
|
+
return `${normalizedDescription.slice(0, previewLength - 1).trimEnd()}…`;
|
|
15
18
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { StyleFormat } from "./color";
|
|
2
|
+
|
|
3
|
+
import { styleOutputText } from "./color";
|
|
4
|
+
|
|
5
|
+
export type RowCell = { text: string; style?: StyleFormat };
|
|
6
|
+
|
|
7
|
+
const CELL_SEPARATOR = " | ";
|
|
8
|
+
const LINE_BREAK_PATTERN = /\r?\n/;
|
|
9
|
+
|
|
10
|
+
export function formatCell(cell: RowCell, width = cell.text.length) {
|
|
11
|
+
const paddedText = cell.text.padEnd(width);
|
|
12
|
+
|
|
13
|
+
if (cell.style == null) {
|
|
14
|
+
return paddedText;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return styleOutputText(cell.style, paddedText);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Rows that join extra text onto their last cell, such as a task's labels, put one there too, which is why it is exported.
|
|
22
|
+
*/
|
|
23
|
+
export function formatCellSeparator() {
|
|
24
|
+
return styleOutputText("dim", CELL_SEPARATOR);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function indentContinuationLines(text: string, indentWidth: number) {
|
|
28
|
+
return text.split(LINE_BREAK_PATTERN).join(`\n${" ".repeat(indentWidth)}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Lines rows up by padding each cell's text to its column's **widest** text.
|
|
33
|
+
* Widths use `String.length`, so emoji and CJK misalign.
|
|
34
|
+
* A row's last cell holds free text such as a title and is left unpadded.
|
|
35
|
+
*
|
|
36
|
+
* With `indentsContinuationLines`, a last cell that spans several lines starts each later line
|
|
37
|
+
* where the cell starts, below the padded cells before it. Without it, those lines are left as they are.
|
|
38
|
+
* Only a last cell may span lines: the widths do not account for line breaks.
|
|
39
|
+
*/
|
|
40
|
+
export function formatRows(
|
|
41
|
+
rows: Array<Array<RowCell>>,
|
|
42
|
+
options: { indentsContinuationLines?: boolean } = {},
|
|
43
|
+
) {
|
|
44
|
+
const columnWidths: Array<number> = [];
|
|
45
|
+
|
|
46
|
+
for (const row of rows) {
|
|
47
|
+
row.forEach((cell, columnIndex) => {
|
|
48
|
+
columnWidths[columnIndex] = Math.max(columnWidths[columnIndex] ?? 0, cell.text.length);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return rows
|
|
53
|
+
.map((row) => {
|
|
54
|
+
const lastCellIndex = row.length - 1;
|
|
55
|
+
const lastCellStartColumn = columnWidths
|
|
56
|
+
.slice(0, lastCellIndex)
|
|
57
|
+
.reduce((total, width) => total + width + CELL_SEPARATOR.length, 0);
|
|
58
|
+
|
|
59
|
+
return row
|
|
60
|
+
.map((cell, columnIndex) => {
|
|
61
|
+
if (columnIndex !== lastCellIndex) {
|
|
62
|
+
return formatCell(cell, columnWidths[columnIndex]!);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!options.indentsContinuationLines) {
|
|
66
|
+
return formatCell(cell);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return formatCell({
|
|
70
|
+
...cell,
|
|
71
|
+
text: indentContinuationLines(cell.text, lastCellStartColumn),
|
|
72
|
+
});
|
|
73
|
+
})
|
|
74
|
+
.join(formatCellSeparator());
|
|
75
|
+
})
|
|
76
|
+
.join("\n");
|
|
77
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { styleOutputText } from "./color";
|
|
2
|
+
|
|
3
|
+
/** A bold heading above its body, optionally preceded by a dim horizontal rule. */
|
|
4
|
+
export function formatSection(
|
|
5
|
+
heading: string,
|
|
6
|
+
body: string,
|
|
7
|
+
options: { separatorWidth?: number } = {},
|
|
8
|
+
) {
|
|
9
|
+
const section = `${styleOutputText("bold", `${heading}:`)}\n${body}`;
|
|
10
|
+
|
|
11
|
+
if (options.separatorWidth === undefined) {
|
|
12
|
+
return section;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return `${styleOutputText("dim", "-".repeat(options.separatorWidth))}\n${section}`;
|
|
16
|
+
}
|