@bismawy/pi-arnative 0.8.2 → 0.9.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/CHANGELOG.md +81 -63
- package/README.md +52 -83
- package/extensions/arnative.ts +27 -4
- package/extensions/autocompact.ts +1 -1
- package/extensions/footer.ts +5 -2
- package/extensions/section-headers.ts +126 -44
- package/extensions/todos.ts +489 -0
- package/extensions/tools.ts +308 -100
- package/lib/box.ts +77 -12
- package/lib/list-picker.ts +0 -3
- package/lib/preset-store.ts +83 -4
- package/lib/shortcuts.ts +8 -0
- package/lib/todos.ts +473 -0
- package/package.json +3 -3
package/lib/todos.ts
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Data model and painted rows for the arnative task list shown by the extension widget and
|
|
3
|
+
* the `/todos` screen. No pi API and no I/O, so `node lib/todos.ts` checks it while
|
|
4
|
+
* `extensions/todos.ts` only wires it to the tool, the widget and the command.
|
|
5
|
+
*
|
|
6
|
+
* Glyphs follow the widget spec: `✓` done, `◉` in progress, `◌` queued. The heading icon is
|
|
7
|
+
* U+F03A (Font Awesome list), so the terminal needs a Nerd Font.
|
|
8
|
+
*/
|
|
9
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
10
|
+
import { softOf, thinkingColorOf } from "./ansi.ts";
|
|
11
|
+
import { renderBoxLines, renderBoxRows } from "./box.ts";
|
|
12
|
+
import { assert, isMain } from "./check.ts";
|
|
13
|
+
|
|
14
|
+
export type TodoStatus = "pending" | "in_progress" | "completed";
|
|
15
|
+
export const LIST_STATUSES = ["pending", "in_progress", "completed"] as const;
|
|
16
|
+
|
|
17
|
+
export interface Task {
|
|
18
|
+
id: number;
|
|
19
|
+
subject: string;
|
|
20
|
+
/** One line of "what is happening now", shown only while the task is in progress. */
|
|
21
|
+
note?: string;
|
|
22
|
+
status: TodoStatus;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface TodoState {
|
|
26
|
+
tasks: Task[];
|
|
27
|
+
nextId: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type FgTheme = { fg?(color: string, text: string): string; style?(text: string, options: { fg: unknown }): string; colors?: Record<string, { kind?: string; c?: number; h?: number }> } | null;
|
|
31
|
+
|
|
32
|
+
export const LIST_ICON = "\uf03a";
|
|
33
|
+
/** Task rows the expanded widget shows when the terminal height is unknown. */
|
|
34
|
+
export const LIST_ROWS = 5;
|
|
35
|
+
/** Bounds of the adaptive window: fewer than three rows says nothing, more than nine eats the
|
|
36
|
+
* chat area even on a tall terminal. */
|
|
37
|
+
export const MIN_LIST_ROWS = 3;
|
|
38
|
+
export const MAX_LIST_ROWS = 9;
|
|
39
|
+
/** Lines the box spends on everything that is not a task: frame, heading, two counters. */
|
|
40
|
+
const BOX_CHROME = 5;
|
|
41
|
+
/** Lines kept for the editor, the footer and a usable chat area above the box. */
|
|
42
|
+
const SCREEN_RESERVE = 14;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Task rows a terminal of `terminalRows` lines can show: the box is `rows + BOX_CHROME` lines
|
|
46
|
+
* tall, and what is left has to hold the editor, the footer and a chat area. Unknown height
|
|
47
|
+
* (a pipe, a test) falls back to {@link LIST_ROWS}, so the widget never guesses wildly.
|
|
48
|
+
*/
|
|
49
|
+
export function listRowsFor(terminalRows: number | undefined): number {
|
|
50
|
+
if (!terminalRows || !Number.isFinite(terminalRows) || terminalRows <= 0) return LIST_ROWS;
|
|
51
|
+
const fit = Math.floor(terminalRows) - BOX_CHROME - SCREEN_RESERVE;
|
|
52
|
+
return Math.min(MAX_LIST_ROWS, Math.max(MIN_LIST_ROWS, fit));
|
|
53
|
+
}
|
|
54
|
+
export const STATUS_GLYPH: Record<TodoStatus, string> = { pending: "◌", in_progress: "◉", completed: "✓" };
|
|
55
|
+
/** Token for the task text itself. Queued recedes to dim and a done task reads as ordinary
|
|
56
|
+
* text; the task in progress takes the soft accent, which is derived rather than a token (see
|
|
57
|
+
* `softOf`), so it is painted separately. */
|
|
58
|
+
export const STATUS_TEXT: Record<TodoStatus, string> = { pending: "dim", in_progress: "text", completed: "text" };
|
|
59
|
+
/** Token per status. In progress borrows the footer's model-name token (`mdHeading`), so the
|
|
60
|
+
* running task is the brightest row in the box, the way the model is the brightest text in
|
|
61
|
+
* the footer. */
|
|
62
|
+
export const STATUS_COLOR: Record<TodoStatus, string> = { pending: "dim", in_progress: "mdHeading", completed: "accent" };
|
|
63
|
+
/** Tried when the theme has no such token; pi throws on an unknown color. */
|
|
64
|
+
const STATUS_FALLBACK: Record<TodoStatus, string> = { pending: "dim", in_progress: "accent", completed: "accent" };
|
|
65
|
+
export const HINT_EXPAND = "[click to expand]";
|
|
66
|
+
export const HINT_COLLAPSE = "[click to collapse]";
|
|
67
|
+
/** Longest subject or note kept; longer input is cut so one task cannot fill the box. */
|
|
68
|
+
const MAX_FIELD = 120;
|
|
69
|
+
|
|
70
|
+
/** `theme.fg(color, text)`, or the plain text when the theme is absent or rejects the token. */
|
|
71
|
+
function fg(theme: FgTheme, color: string, text: string): string {
|
|
72
|
+
try {
|
|
73
|
+
return theme?.fg ? theme.fg(color, text) : text;
|
|
74
|
+
} catch {
|
|
75
|
+
return text;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The first token the theme knows. A theme that omits `mdHeading` still gets a painted
|
|
80
|
+
* glyph instead of a plain one, the same way the footer falls back to accent. */
|
|
81
|
+
function fgOr(theme: FgTheme, colors: string[], text: string): string {
|
|
82
|
+
for (const color of colors) {
|
|
83
|
+
try {
|
|
84
|
+
if (theme?.fg) return theme.fg(color, text);
|
|
85
|
+
} catch {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return text;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Every task from a stored file, or `null` when the file is not one. Tolerant on fields,
|
|
93
|
+
* strict on the shape: a hand-edited or half-written file must not break the widget. */
|
|
94
|
+
export function normalizeState(raw: unknown): TaskState | null {
|
|
95
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
96
|
+
const tasks = (raw as { tasks?: unknown }).tasks;
|
|
97
|
+
if (!Array.isArray(tasks)) return null;
|
|
98
|
+
const kept: Task[] = [];
|
|
99
|
+
for (const item of tasks) {
|
|
100
|
+
if (!item || typeof item !== "object") continue;
|
|
101
|
+
const record = item as Record<string, unknown>;
|
|
102
|
+
const id = typeof record.id === "number" && Number.isInteger(record.id) && record.id > 0 ? record.id : null;
|
|
103
|
+
const subject = typeof record.subject === "string" ? tidy(record.subject) : "";
|
|
104
|
+
if (id === null || !subject) continue;
|
|
105
|
+
const status = LIST_STATUSES.includes(record.status as TodoStatus) ? (record.status as TodoStatus) : "pending";
|
|
106
|
+
const note = typeof record.note === "string" && record.note.trim() ? tidy(record.note) : undefined;
|
|
107
|
+
kept.push(note ? { id, subject, status, note } : { id, subject, status });
|
|
108
|
+
}
|
|
109
|
+
return { tasks: kept, nextId: kept.reduce((max, task) => Math.max(max, task.id), 0) + 1 };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** One row's worth of text: control characters (an ANSI escape in a subject would break the
|
|
113
|
+
* frame) become spaces, runs collapse, and the tail is cut. */
|
|
114
|
+
export function tidy(text: string): string {
|
|
115
|
+
const clean = text
|
|
116
|
+
.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ")
|
|
117
|
+
.replace(/\s+/g, " ")
|
|
118
|
+
.trim();
|
|
119
|
+
return clean.length > MAX_FIELD ? `${clean.slice(0, MAX_FIELD - 1)}…` : clean;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export const emptyTodos = (): TodoState => ({ tasks: [], nextId: 1 });
|
|
123
|
+
|
|
124
|
+
/** Append a task to the queue. Returns the new state and the id the tool reports back. */
|
|
125
|
+
export function addTodo(state: TodoState, subject: string, note?: string): { state: TodoState; id: number } {
|
|
126
|
+
const task: Task = { id: state.nextId, subject: tidy(subject), status: "pending" };
|
|
127
|
+
if (note) task.note = tidy(note);
|
|
128
|
+
return { state: { tasks: [...state.tasks, task], nextId: state.nextId + 1 }, id: task.id };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface TodoPatch {
|
|
132
|
+
status?: TodoStatus;
|
|
133
|
+
subject?: string;
|
|
134
|
+
/** `null` removes the note; a string replaces it. */
|
|
135
|
+
note?: string | null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Change one task, or `null` when the id is unknown (the tool turns that into an error). */
|
|
139
|
+
export function patchTodo(state: TodoState, id: number, patch: TodoPatch): TodoState | null {
|
|
140
|
+
const index = state.tasks.findIndex((task) => task.id === id);
|
|
141
|
+
if (index === -1) return null;
|
|
142
|
+
const current = state.tasks[index]!;
|
|
143
|
+
const next: Task = { ...current };
|
|
144
|
+
if (patch.status) next.status = patch.status;
|
|
145
|
+
if (patch.subject !== undefined) next.subject = tidy(patch.subject);
|
|
146
|
+
if (patch.note === null) delete next.note;
|
|
147
|
+
else if (patch.note !== undefined) next.note = tidy(patch.note);
|
|
148
|
+
const tasks = [...state.tasks];
|
|
149
|
+
tasks[index] = next;
|
|
150
|
+
return { ...state, tasks };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function removeTodo(state: TodoState, id: number): TodoState | null {
|
|
154
|
+
if (!state.tasks.some((task) => task.id === id)) return null;
|
|
155
|
+
return { ...state, tasks: state.tasks.filter((task) => task.id !== id) };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export const clearTodos = (state: TodoState): TodoState => ({ tasks: [], nextId: state.nextId });
|
|
159
|
+
|
|
160
|
+
export function countTodos(state: TodoState): { done: number; total: number; doing: number; queued: number } {
|
|
161
|
+
const done = state.tasks.filter((task) => task.status === "completed").length;
|
|
162
|
+
const doing = state.tasks.filter((task) => task.status === "in_progress").length;
|
|
163
|
+
return { done, total: state.tasks.length, doing, queued: state.tasks.length - done - doing };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The task whose work is happening now, so the collapsed row can name it. */
|
|
167
|
+
export const activeTodo = (state: TodoState): Task | undefined => state.tasks.find((task) => task.status === "in_progress");
|
|
168
|
+
|
|
169
|
+
/** True when every task is done, the state that folds the box back to one row. */
|
|
170
|
+
export const allDone = (state: TodoState): boolean => state.tasks.length > 0 && state.tasks.every((task) => task.status === "completed");
|
|
171
|
+
|
|
172
|
+
/** What the collapsed row says after the count: the task in progress, or that nothing is left. */
|
|
173
|
+
export const COLLAPSED_DONE = "All tasks finished";
|
|
174
|
+
|
|
175
|
+
export const headingText = (state: TodoState): string => {
|
|
176
|
+
const { done, total } = countTodos(state);
|
|
177
|
+
return `Todos (${done}/${total})`;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/** ` · Tulis widget`, or ` · All tasks finished` once nothing is left (empty when idle). */
|
|
181
|
+
export function collapsedSuffix(state: TodoState): string {
|
|
182
|
+
if (allDone(state)) return ` · ${COLLAPSED_DONE}`;
|
|
183
|
+
const active = activeTodo(state);
|
|
184
|
+
return active ? ` · ${active.subject}` : "";
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** `Todos (3/7) · Tulis widget` — the collapsed box, without the hint. */
|
|
188
|
+
export const collapsedText = (state: TodoState): string => `${headingText(state)}${collapsedSuffix(state)}`;
|
|
189
|
+
|
|
190
|
+
export interface TodoWindow {
|
|
191
|
+
visible: Task[];
|
|
192
|
+
/** Tasks hidden above the window. */
|
|
193
|
+
above: number;
|
|
194
|
+
/** Tasks hidden below the window. */
|
|
195
|
+
below: number;
|
|
196
|
+
/** Index of the first visible task, the anchor a scroll moves. */
|
|
197
|
+
start: number;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The {@link LIST_ROWS} rows the expanded box shows. The window follows the in-progress task
|
|
202
|
+
* and centres it, so the active work is always on screen; with no task in progress it starts
|
|
203
|
+
* at the top. `top` overrides that start after a scroll; it is clamped to the list, so a click
|
|
204
|
+
* past either end parks the window at the edge. `above`/`below` feed the `↑ n more` /
|
|
205
|
+
* `↓ n more` rows.
|
|
206
|
+
*/
|
|
207
|
+
export function todoWindow(state: TodoState, rows = LIST_ROWS, top: number | null = null): TodoWindow {
|
|
208
|
+
const total = state.tasks.length;
|
|
209
|
+
if (total <= rows) return { visible: state.tasks, above: 0, below: 0, start: 0 };
|
|
210
|
+
const active = state.tasks.findIndex((task) => task.status === "in_progress");
|
|
211
|
+
const centre = Math.floor((rows - 1) / 2);
|
|
212
|
+
const follow = active === -1 ? 0 : Math.min(Math.max(active - centre, 0), total - rows);
|
|
213
|
+
const start = top === null ? follow : Math.min(Math.max(Math.trunc(top), 0), total - rows);
|
|
214
|
+
return { visible: state.tasks.slice(start, start + rows), above: start, below: total - start - rows, start };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** How the widget draws itself: what a scroll moved, how many task rows fit, and the thinking
|
|
218
|
+
* level whose slot paints the frame. Every field is optional, so a caller that only cares
|
|
219
|
+
* about the layout can omit it. */
|
|
220
|
+
export interface TodoView {
|
|
221
|
+
/** First visible task after a scroll; null (the default) follows the active task. */
|
|
222
|
+
top?: number | null;
|
|
223
|
+
/** Task rows to show. Defaults to {@link LIST_ROWS}; the widget passes
|
|
224
|
+
* `listRowsFor(process.stdout.rows)`, so a tall terminal shows more of the list. */
|
|
225
|
+
rows?: number;
|
|
226
|
+
/** Thinking level name (`low`, `high`…), for the frame colour. */
|
|
227
|
+
thinking?: string;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Content rows of the widget box (no frame): one row collapsed, heading + tasks expanded. */
|
|
231
|
+
export function widgetRows(theme: FgTheme, state: TodoState, expanded: boolean, width: number, view: TodoView = {}): string[] {
|
|
232
|
+
const fit = (line: string) => truncateToWidth(line, Math.max(0, width - 4), "…");
|
|
233
|
+
// The hint is how the box gets opened, so it survives and the text before it gives way.
|
|
234
|
+
const withHint = (text: string, hint: string): string => {
|
|
235
|
+
const painted = fg(theme, "dim", hint);
|
|
236
|
+
const room = Math.max(0, width - 4 - visibleWidth(painted) - 1);
|
|
237
|
+
return fit(`${truncateToWidth(text, room, "…")} ${painted}`);
|
|
238
|
+
};
|
|
239
|
+
const icon = fg(theme, "accent", LIST_ICON);
|
|
240
|
+
// The count is a label, so it takes the soft accent tier the footer labels use, while the
|
|
241
|
+
// task name after the dot stays in the text colour.
|
|
242
|
+
const soft = softOf(theme);
|
|
243
|
+
const heading = (state: TodoState): string => `${icon} ${soft(headingText(state))}`;
|
|
244
|
+
if (!expanded) return [withHint(`${heading(state)}${collapsedSuffix(state)}`, HINT_EXPAND)];
|
|
245
|
+
|
|
246
|
+
const { visible, above, below } = todoWindow(state, view.rows ?? LIST_ROWS, view.top ?? null);
|
|
247
|
+
const rows = [withHint(heading(state), HINT_COLLAPSE)];
|
|
248
|
+
if (above > 0) rows.push(fg(theme, "dim", `↑ ${above} more`));
|
|
249
|
+
visible.forEach((task, index) => {
|
|
250
|
+
// The last row closes the branch only when nothing is summarized below it.
|
|
251
|
+
const last = index === visible.length - 1 && below === 0;
|
|
252
|
+
const branch = fg(theme, "dim", last ? "└─" : "├─");
|
|
253
|
+
const glyph = fgOr(theme, [STATUS_COLOR[task.status], STATUS_FALLBACK[task.status]], STATUS_GLYPH[task.status]);
|
|
254
|
+
// The subject carries the state too: the work in progress takes the soft accent, queued
|
|
255
|
+
// recedes into dim, and a finished task reads as plain text.
|
|
256
|
+
const subject = task.status === "in_progress" ? soft(task.subject) : fg(theme, STATUS_TEXT[task.status], task.subject);
|
|
257
|
+
const note = task.note && task.status === "in_progress" ? ` ${fg(theme, "dim", `· ${task.note}`)}` : "";
|
|
258
|
+
rows.push(fit(`${branch} ${glyph} ${subject}${note}`));
|
|
259
|
+
});
|
|
260
|
+
if (below > 0) rows.push(fg(theme, "dim", `↓ ${below} more`));
|
|
261
|
+
return rows;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** The widget as pi renders it: the arnative box around {@link widgetRows}. The border follows
|
|
265
|
+
* the session's thinking level, like every other arnative box. Expanded it is at most 10
|
|
266
|
+
* lines (frame 2 + heading 1 + {@link LIST_ROWS} rows + both counters), the size pi caps a
|
|
267
|
+
* plain string widget at; a component widget is not truncated, so this is the size that keeps
|
|
268
|
+
* the chat area usable on a 24-row terminal, not a limit pi enforces. */
|
|
269
|
+
export const renderTodoBox = (theme: FgTheme, state: TodoState, expanded: boolean, width: number, view: TodoView = {}): string[] =>
|
|
270
|
+
renderBoxLines(theme, width, widgetRows(theme, state, expanded, width, view), undefined, thinkingColorOf(theme, view.thinking));
|
|
271
|
+
|
|
272
|
+
export interface TodoLayout {
|
|
273
|
+
/** The whole widget, frame included: what pi paints above the editor. */
|
|
274
|
+
lines: string[];
|
|
275
|
+
/** Row of the `↑ n more` line, or null when nothing is hidden above it. The counters are
|
|
276
|
+
* the click targets that scroll the five-row window. */
|
|
277
|
+
up: number | null;
|
|
278
|
+
/** Row of the `↓ n more` line, or null when nothing is hidden below it. */
|
|
279
|
+
down: number | null;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** {@link renderTodoBox} plus the row numbers of the two counter lines. A mouse handler needs
|
|
283
|
+
* them, since those lines are the click targets that scroll the five-row window; the numbers
|
|
284
|
+
* come from the box renderer, so a wrapped row cannot shift the hit area. */
|
|
285
|
+
export function todoLayout(
|
|
286
|
+
theme: FgTheme,
|
|
287
|
+
state: TodoState,
|
|
288
|
+
expanded: boolean,
|
|
289
|
+
width: number,
|
|
290
|
+
view: TodoView = {},
|
|
291
|
+
): TodoLayout {
|
|
292
|
+
const { above, below } = todoWindow(state, view.rows ?? LIST_ROWS, view.top ?? null);
|
|
293
|
+
const rows = widgetRows(theme, state, expanded, width, view);
|
|
294
|
+
const { lines, starts } = renderBoxRows(theme, width, rows, undefined, thinkingColorOf(theme, view.thinking));
|
|
295
|
+
// Content row 1 is `↑ n more`; the last content row is `↓ n more`.
|
|
296
|
+
const at = (contentRow: number): number => starts[contentRow] ?? Math.max(0, lines.length - 1);
|
|
297
|
+
return {
|
|
298
|
+
lines,
|
|
299
|
+
up: expanded && above > 0 ? at(1) : null,
|
|
300
|
+
down: expanded && below > 0 ? at(rows.length - 1) : null,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The word the widget and the recap use for a status (`pending` reads as `queued`,
|
|
305
|
+
* the word the summary line counts). */
|
|
306
|
+
export const statusText = (status: TodoStatus): string =>
|
|
307
|
+
status === "in_progress" ? "in progress" : status === "pending" ? "queued" : status;
|
|
308
|
+
|
|
309
|
+
/** The word after `todo ·` in the tool box title: the status a task moved to, or the action when
|
|
310
|
+
* the call changed no status (`created`, `removed`, `cleared`, `list`, `updated`). Empty when
|
|
311
|
+
* neither is known, so the title keeps a clean `todo`. */
|
|
312
|
+
export function actionWord(action: unknown, status?: unknown): string {
|
|
313
|
+
if (typeof status === "string" && LIST_STATUSES.includes(status as TodoStatus)) return status;
|
|
314
|
+
switch (action) {
|
|
315
|
+
case "create":
|
|
316
|
+
return "created";
|
|
317
|
+
case "update":
|
|
318
|
+
return "updated";
|
|
319
|
+
case "delete":
|
|
320
|
+
return "removed";
|
|
321
|
+
case "clear":
|
|
322
|
+
return "cleared";
|
|
323
|
+
case "list":
|
|
324
|
+
return "list";
|
|
325
|
+
default:
|
|
326
|
+
return "";
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** `/todos` recap: in progress first, then the queue, then what is done. */
|
|
331
|
+
export function recapRows(state: TodoState): { label: string; note: string }[] {
|
|
332
|
+
const order: Record<TodoStatus, number> = { in_progress: 0, pending: 1, completed: 2 };
|
|
333
|
+
return [...state.tasks]
|
|
334
|
+
.sort((a, b) => order[a.status] - order[b.status] || a.id - b.id)
|
|
335
|
+
.map((task) => ({
|
|
336
|
+
label: `${STATUS_GLYPH[task.status]} ${task.subject}`,
|
|
337
|
+
note: task.note && task.status === "in_progress" ? `${statusText(task.status)} · ${task.note}` : statusText(task.status),
|
|
338
|
+
}));
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** One line for the recap screen: `1 in progress · 3 queued · 3 done`. */
|
|
342
|
+
export function recapSummary(state: TodoState): string {
|
|
343
|
+
const { done, total, doing, queued } = countTodos(state);
|
|
344
|
+
return [`${total} tasks`, `${doing} in progress`, `${queued} queued`, `${done} done`].join(" · ");
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Self-check: `node lib/todos.ts`
|
|
348
|
+
if (isMain(import.meta.url)) {
|
|
349
|
+
const th: FgTheme = { fg: (_color, text) => text };
|
|
350
|
+
// Writes the token down, so the self-check can name the colour a piece was painted with.
|
|
351
|
+
const marked: FgTheme = { fg: (color, text) => `[${color}]${text}` };
|
|
352
|
+
|
|
353
|
+
let state = emptyTodos();
|
|
354
|
+
assert(headingText(state) === "Todos (0/0)", "empty heading");
|
|
355
|
+
assert(collapsedText(state) === "Todos (0/0)", "empty collapsed row carries no task name");
|
|
356
|
+
assert(widgetRows(marked, state, false, 80)[0]!.startsWith(`[accent]${LIST_ICON} [accentSoft]Todos (0/0)`), "icon accent, count soft accent");
|
|
357
|
+
|
|
358
|
+
let added = addTodo(state, "Siapkan rencana");
|
|
359
|
+
state = added.state;
|
|
360
|
+
assert(added.id === 1 && state.nextId === 2, "first id is 1, next id advances");
|
|
361
|
+
for (const subject of ["Tulis widget", "Tulis layar /todos", "Uji fullscreen", "Rilis"]) state = addTodo(state, subject).state;
|
|
362
|
+
assert(countTodos(state).total === 5 && countTodos(state).queued === 5, "five queued tasks");
|
|
363
|
+
|
|
364
|
+
const patched = patchTodo(state, 2, { status: "in_progress", note: "Memproses bug ABC.txt" });
|
|
365
|
+
assert(patched !== null, "patch a known id");
|
|
366
|
+
state = patched!;
|
|
367
|
+
assert(activeTodo(state)?.subject === "Tulis widget", "the active task is the in-progress one");
|
|
368
|
+
assert(collapsedText(state) === "Todos (0/5) · Tulis widget", `collapsed names the active task, got ${collapsedText(state)}`);
|
|
369
|
+
|
|
370
|
+
state = patchTodo(state, 2, { status: "completed" })!;
|
|
371
|
+
assert(activeTodo(state) === undefined, "no active task after completing the only one");
|
|
372
|
+
// The extension folds the widget away once the list is done, yet the renderer still
|
|
373
|
+
// describes that state: it is a pure function of the list, not of who paints it.
|
|
374
|
+
const doneState = { ...state, tasks: state.tasks.map((task) => ({ ...task, status: "completed" as const })) };
|
|
375
|
+
assert(allDone(doneState) && collapsedText(doneState).endsWith(COLLAPSED_DONE), "a finished list names its end");
|
|
376
|
+
assert(headingText(state) === "Todos (1/5)", "heading counts completed over total");
|
|
377
|
+
assert(patchTodo(state, 99, { status: "completed" }) === null, "an unknown id is rejected");
|
|
378
|
+
assert(removeTodo(state, 99) === null, "removing an unknown id is rejected");
|
|
379
|
+
state = removeTodo(state, 5)!;
|
|
380
|
+
assert(countTodos(state).total === 4, "remove drops one task");
|
|
381
|
+
assert(clearTodos(state).tasks.length === 0, "clear empties the list");
|
|
382
|
+
|
|
383
|
+
// Five-row window: the active task is centred, and the hidden counts add up.
|
|
384
|
+
state = emptyTodos();
|
|
385
|
+
for (let i = 1; i <= 7; i++) state = addTodo(state, `Task ${i}`).state;
|
|
386
|
+
// Adaptive height: the window grows with the terminal, bounded at three and nine.
|
|
387
|
+
assert(listRowsFor(24) === 5 && listRowsFor(30) === 9 && listRowsFor(10) === 3, "listRowsFor bounds");
|
|
388
|
+
assert(listRowsFor(undefined) === LIST_ROWS, "unknown height falls back to the default");
|
|
389
|
+
|
|
390
|
+
assert(todoWindow(state).visible.length === 5, "five rows when more exist");
|
|
391
|
+
assert(todoWindow(state).above === 0 && todoWindow(state).below === 2, "nothing hidden above the first five");
|
|
392
|
+
state = patchTodo(state, 5, { status: "in_progress" })!;
|
|
393
|
+
const mid = todoWindow(state);
|
|
394
|
+
assert(mid.visible.map((t) => t.id).join(",") === "3,4,5,6,7", "the window follows the active task");
|
|
395
|
+
assert(mid.above === 2 && mid.below === 0, "two tasks hidden above");
|
|
396
|
+
assert(mid.above + mid.visible.length + mid.below === 7, "the window accounts for every task");
|
|
397
|
+
state = patchTodo(state, 7, { status: "in_progress" })!;
|
|
398
|
+
assert(todoWindow(state).above === 2, "the window stops at the end of the list instead of sliding past it");
|
|
399
|
+
|
|
400
|
+
// The box: collapsed 3 lines, expanded at most 10 (frame, heading, five rows, two counters).
|
|
401
|
+
state = emptyTodos();
|
|
402
|
+
for (let i = 1; i <= 20; i++) state = addTodo(state, `Task ${i}`).state;
|
|
403
|
+
state = patchTodo(state, 10, { status: "in_progress", note: "sedang menulis" })!;
|
|
404
|
+
const collapsedBox = renderTodoBox(th, state, false, 80);
|
|
405
|
+
assert(collapsedBox.length === 3, `collapsed box is frame+row+frame, got ${collapsedBox.length}`);
|
|
406
|
+
assert(collapsedBox[1]!.includes("Todos (0/20)"), "collapsed box carries the heading");
|
|
407
|
+
assert(collapsedBox[1]!.includes("Task 10"), "collapsed box names the active task");
|
|
408
|
+
assert(collapsedBox[1]!.includes(HINT_EXPAND), "collapsed box carries the expand hint");
|
|
409
|
+
// Narrow terminal: the task name gives way, the hint that opens the box does not.
|
|
410
|
+
const narrow = renderTodoBox(th, state, false, 40);
|
|
411
|
+
assert(narrow[1]!.includes(HINT_EXPAND), "the expand hint survives a narrow box");
|
|
412
|
+
assert(narrow.every((line) => line.length > 0), "a narrow box still renders every line");
|
|
413
|
+
|
|
414
|
+
const expandedBox = renderTodoBox(th, state, true, 40);
|
|
415
|
+
assert(expandedBox.length <= 10, `expanded box stays within the widget budget, got ${expandedBox.length}`);
|
|
416
|
+
assert(expandedBox.some((line) => line.includes("↑ 7 more")), "hidden tasks above are counted");
|
|
417
|
+
assert(expandedBox.some((line) => line.includes("↓ 8 more")), "hidden tasks below are counted");
|
|
418
|
+
assert(expandedBox.some((line) => line.includes("· sedang menulis")), "the in-progress note is rendered");
|
|
419
|
+
assert(!expandedBox.some((line) => line.includes("└─")), "an open branch stays ├─ while rows remain below");
|
|
420
|
+
// With nothing below the window the last row closes the branch.
|
|
421
|
+
const tail = renderTodoBox(th, patchTodo(patchTodo(state, 10, { status: "pending" })!, 20, { status: "in_progress" })!, true, 80);
|
|
422
|
+
assert(tail.some((line) => line.includes("└─")), "the last row closes the branch at the end of the list");
|
|
423
|
+
assert(
|
|
424
|
+
expandedBox.every((line) => line.startsWith("│") || line.startsWith("╭") || line.startsWith("╰")),
|
|
425
|
+
"every line belongs to the box",
|
|
426
|
+
);
|
|
427
|
+
|
|
428
|
+
// A note only belongs to the task being worked on.
|
|
429
|
+
state = patchTodo(state, 10, { status: "pending" })!;
|
|
430
|
+
assert(!renderTodoBox(th, state, true, 40).some((line) => line.includes("sedang menulis")), "a queued task hides its note");
|
|
431
|
+
|
|
432
|
+
// Hostile input: an escape sequence in a subject must not break the frame.
|
|
433
|
+
state = addTodo(emptyTodos(), "bad \u001b[31mred\u001b[0m subject").state;
|
|
434
|
+
const hostile = renderTodoBox(th, state, true, 40);
|
|
435
|
+
assert(!hostile.some((line) => line.includes("\u001b[31m")), "control characters are stripped from a subject");
|
|
436
|
+
assert(hostile.every((line) => line.length > 0), "a stripped subject still renders a row");
|
|
437
|
+
|
|
438
|
+
// Recap order and summary.
|
|
439
|
+
state = emptyTodos();
|
|
440
|
+
state = addTodo(state, "Antre A").state;
|
|
441
|
+
state = addTodo(state, "Jalan B").state;
|
|
442
|
+
state = patchTodo(state, 2, { status: "in_progress" })!;
|
|
443
|
+
state = addTodo(state, "Selesai C").state;
|
|
444
|
+
state = patchTodo(state, 3, { status: "completed" })!;
|
|
445
|
+
assert(recapRows(state).map((r) => r.label).join(" | ") === "◉ Jalan B | ◌ Antre A | ✓ Selesai C", "recap groups by status");
|
|
446
|
+
assert(recapSummary(state) === "3 tasks · 1 in progress · 1 queued · 1 done", `recap summary, got ${recapSummary(state)}`);
|
|
447
|
+
|
|
448
|
+
// A stored file: broken rows go, fields are tidied, nextId follows the highest id.
|
|
449
|
+
const loaded = normalizeState({
|
|
450
|
+
tasks: [
|
|
451
|
+
{ id: 2, subject: " Kedua ", status: "in_progress", note: "sedang jalan" },
|
|
452
|
+
{ id: 5, subject: "Kelima", status: "selesai" },
|
|
453
|
+
{ id: "x", subject: "rusak" },
|
|
454
|
+
{ id: 7, subject: " " },
|
|
455
|
+
{ id: 9, subject: "Ketiga", status: "completed" },
|
|
456
|
+
],
|
|
457
|
+
nextId: 1,
|
|
458
|
+
});
|
|
459
|
+
assert(loaded !== null, "a valid record loads");
|
|
460
|
+
assert(normalizeState(null) === null && normalizeState({ tasks: "no" }) === null, "a broken record is rejected");
|
|
461
|
+
assert(loaded!.tasks.map((task) => task.id).join(",") === "2,5,9", "broken rows are dropped");
|
|
462
|
+
assert(loaded!.tasks[0]!.subject === "Kedua" && loaded!.tasks[0]!.note === "sedang jalan", "fields are tidied");
|
|
463
|
+
assert(loaded!.tasks[1]!.status === "pending", "an unknown status falls back to pending");
|
|
464
|
+
assert(loaded!.nextId === 10, "nextId follows the highest stored id");
|
|
465
|
+
|
|
466
|
+
// The tool box title: status when a task moved, action word otherwise.
|
|
467
|
+
assert(actionWord("update", "completed") === "completed", "a known status wins over the action word");
|
|
468
|
+
assert(actionWord("update", "selesai") === "updated", "an unknown status falls back to the action");
|
|
469
|
+
assert(actionWord("create") === "created" && actionWord("delete") === "removed", "create and delete read as past tense");
|
|
470
|
+
assert(actionWord("list") === "list" && actionWord("nonsense") === "", "list keeps its name, an unknown action says nothing");
|
|
471
|
+
|
|
472
|
+
console.log("todos.ts self-check OK");
|
|
473
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bismawy/pi-arnative",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Refined aesthetics. Cohesive tools. Built for Pi. Warm themes, rounded tool boxes, tabbed header, compact footer and a /usage dashboard.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"@earendil-works/pi-tui": "*"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
|
-
"dev": "pi --extension ./extensions/tools.ts --extension ./extensions/section-headers.ts --extension ./extensions/footer.ts --extension ./extensions/timestamps.ts --extension ./extensions/ui-render-tweaks.ts --extension ./extensions/usage.ts --extension ./extensions/animations.ts --extension ./extensions/autocompact.ts --extension ./extensions/model-heal.ts --extension ./extensions/arnative.ts",
|
|
44
|
-
"test": "node lib/box.ts && node lib/patch.ts && node lib/ansi.ts && node lib/color.ts && node lib/shortcuts.ts && node lib/status-widget.ts && node lib/preset-store.ts && node lib/spinner.ts && node themes/gen-themes.mjs --check && node extensions/tools.ts && node lib/list-picker.ts && node lib/compat.ts && node lib/provider.ts && node lib/model-heal.ts && node extensions/footer.ts && node extensions/timestamps.ts && node extensions/ui-render-tweaks.ts && node extensions/usage.ts && node extensions/animations.ts && node extensions/autocompact.ts && node extensions/model-heal.ts && node extensions/arnative.ts && node scripts/reporter.mjs --self-check && node --test --test-reporter=./scripts/reporter.mjs",
|
|
43
|
+
"dev": "pi --extension ./extensions/tools.ts --extension ./extensions/section-headers.ts --extension ./extensions/footer.ts --extension ./extensions/timestamps.ts --extension ./extensions/ui-render-tweaks.ts --extension ./extensions/usage.ts --extension ./extensions/animations.ts --extension ./extensions/autocompact.ts --extension ./extensions/model-heal.ts --extension ./extensions/todos.ts --extension ./extensions/arnative.ts",
|
|
44
|
+
"test": "node lib/box.ts && node lib/patch.ts && node lib/ansi.ts && node lib/color.ts && node lib/shortcuts.ts && node lib/status-widget.ts && node lib/preset-store.ts && node lib/spinner.ts && node lib/todos.ts && node themes/gen-themes.mjs --check && node extensions/tools.ts && node lib/list-picker.ts && node lib/compat.ts && node lib/provider.ts && node lib/model-heal.ts && node extensions/footer.ts && node extensions/timestamps.ts && node extensions/ui-render-tweaks.ts && node extensions/usage.ts && node extensions/animations.ts && node extensions/autocompact.ts && node extensions/model-heal.ts && node extensions/todos.ts && node extensions/arnative.ts && node scripts/reporter.mjs --self-check && node --test --test-reporter=./scripts/reporter.mjs",
|
|
45
45
|
"preview": "node scripts/preview.mjs",
|
|
46
46
|
"themes": "node themes/gen-themes.mjs",
|
|
47
47
|
"release:check": "node scripts/release-check.mjs pre-tag",
|