@ftrdotdev/pi-tracker 0.2.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/LICENSE +21 -0
- package/README.md +164 -0
- package/package.json +71 -0
- package/src/domain.test.ts +157 -0
- package/src/domain.ts +62 -0
- package/src/index.ts +455 -0
- package/src/persistence.test.ts +192 -0
- package/src/persistence.ts +47 -0
- package/src/store.test.ts +809 -0
- package/src/store.ts +517 -0
- package/src/tool-metadata.test.ts +213 -0
- package/src/tool-metadata.ts +302 -0
- package/src/ui.test.ts +204 -0
- package/src/ui.ts +468 -0
package/src/ui.test.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { TodoItem, TodoList, TrackerState } from "./domain.ts";
|
|
4
|
+
import {
|
|
5
|
+
makeTrackerOverlay,
|
|
6
|
+
makeTrackerWidget,
|
|
7
|
+
renderTrackerWidget,
|
|
8
|
+
type TrackerOverlayHandle,
|
|
9
|
+
type TrackerUiAction,
|
|
10
|
+
} from "./ui.ts";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Identity theme: styles pass text through unchanged, so render output is
|
|
14
|
+
* plain text that assertions can search. The overlay and widget only use
|
|
15
|
+
* `fg`, `bold` and `strikethrough`, which this stub covers.
|
|
16
|
+
*/
|
|
17
|
+
const identityTheme = {
|
|
18
|
+
fg: (_color: string, text: string): string => text,
|
|
19
|
+
bold: (text: string): string => text,
|
|
20
|
+
strikethrough: (text: string): string => text,
|
|
21
|
+
} as unknown as Theme;
|
|
22
|
+
|
|
23
|
+
/** A realistic two-list state: Work is the active list. */
|
|
24
|
+
const twoListState = (): TrackerState =>
|
|
25
|
+
new TrackerState({
|
|
26
|
+
lists: [
|
|
27
|
+
new TodoList({
|
|
28
|
+
id: 1,
|
|
29
|
+
name: "Work",
|
|
30
|
+
items: [
|
|
31
|
+
new TodoItem({ text: "write plan", done: true }),
|
|
32
|
+
new TodoItem({ text: "implement tracker", done: false }),
|
|
33
|
+
],
|
|
34
|
+
}),
|
|
35
|
+
new TodoList({
|
|
36
|
+
id: 2,
|
|
37
|
+
name: "Home",
|
|
38
|
+
items: [new TodoItem({ text: "water plants", done: false })],
|
|
39
|
+
}),
|
|
40
|
+
],
|
|
41
|
+
activeListId: 1,
|
|
42
|
+
nextListId: 3,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/** Assert `text` does not contain `needle` (@effect/vitest lacks doesNotMatch). */
|
|
46
|
+
const notContain = (text: string, needle: string): void => {
|
|
47
|
+
assert.equal(
|
|
48
|
+
text.includes(needle),
|
|
49
|
+
false,
|
|
50
|
+
`expected text not to contain ${JSON.stringify(needle)}`,
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
interface OverlayHarness {
|
|
55
|
+
readonly overlay: TrackerOverlayHandle;
|
|
56
|
+
readonly actions: TrackerUiAction[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Build an overlay over a scripted state. `onAction` mirrors the real
|
|
61
|
+
* bridge's `set_active` handling (the only action the tests exercise), so
|
|
62
|
+
* the snapshot passed to `getState` follows the same object-identity rules
|
|
63
|
+
* as the live mirror in `src/index.ts`.
|
|
64
|
+
*/
|
|
65
|
+
const makeOverlay = (initial: TrackerState): OverlayHarness => {
|
|
66
|
+
let current = initial;
|
|
67
|
+
const actions: TrackerUiAction[] = [];
|
|
68
|
+
const overlay = makeTrackerOverlay({
|
|
69
|
+
getState: () => current,
|
|
70
|
+
theme: identityTheme,
|
|
71
|
+
requestRender: () => {},
|
|
72
|
+
onAction: async (action) => {
|
|
73
|
+
actions.push(action);
|
|
74
|
+
if (action.type === "setActive") {
|
|
75
|
+
current = new TrackerState({ ...current, activeListId: action.listId });
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
},
|
|
79
|
+
onClose: () => {},
|
|
80
|
+
});
|
|
81
|
+
return { overlay, actions };
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** Flush the microtask queue so fire-and-forget `runAction` continuations land. */
|
|
85
|
+
const flushAsync = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0));
|
|
86
|
+
|
|
87
|
+
// --------------------------------------------------------------------------
|
|
88
|
+
// Interactive overlay: items pane follows the focused list
|
|
89
|
+
// --------------------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
it("previews the focused list's items while navigating the lists pane", () => {
|
|
92
|
+
const h = makeOverlay(twoListState());
|
|
93
|
+
const text = () => h.overlay.render(80).join("\n");
|
|
94
|
+
|
|
95
|
+
// The cursor starts on the active list (Work) and previews its items.
|
|
96
|
+
assert.match(text(), /Items — Work/);
|
|
97
|
+
assert.match(text(), /write plan/);
|
|
98
|
+
|
|
99
|
+
// Moving the cursor to Home previews Home's items instead of the active
|
|
100
|
+
// list's — the reported bug (items pane stuck on the active list).
|
|
101
|
+
h.overlay.handleInput("\x1b[B");
|
|
102
|
+
assert.match(text(), /Items — Home/);
|
|
103
|
+
assert.match(text(), /water plants/);
|
|
104
|
+
notContain(text(), "write plan");
|
|
105
|
+
|
|
106
|
+
// And back.
|
|
107
|
+
h.overlay.handleInput("\x1b[A");
|
|
108
|
+
assert.match(text(), /Items — Work/);
|
|
109
|
+
assert.match(text(), /write plan/);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("enter commits the focused list as active and opens its items", async () => {
|
|
113
|
+
const h = makeOverlay(twoListState());
|
|
114
|
+
h.overlay.handleInput("\x1b[B"); // focus Home
|
|
115
|
+
h.overlay.handleInput("\r"); // commit
|
|
116
|
+
await flushAsync();
|
|
117
|
+
|
|
118
|
+
assert.deepStrictEqual(h.actions, [{ type: "setActive", listId: 2 }]);
|
|
119
|
+
const text = h.overlay.render(80).join("\n");
|
|
120
|
+
assert.match(text, /Items — Home/);
|
|
121
|
+
assert.match(text, /water plants/);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("items pane follows the active list in items mode (preview is not a commit)", () => {
|
|
125
|
+
const h = makeOverlay(twoListState());
|
|
126
|
+
h.overlay.handleInput("\x1b[B"); // preview Home
|
|
127
|
+
h.overlay.handleInput("\t"); // switch to items without committing
|
|
128
|
+
const text = h.overlay.render(80).join("\n");
|
|
129
|
+
|
|
130
|
+
// No enter was pressed, so the items pane still targets the active list.
|
|
131
|
+
assert.match(text, /Items — Work/);
|
|
132
|
+
assert.match(text, /write plan/);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// --------------------------------------------------------------------------
|
|
136
|
+
// Render caching (tui.md "Performance" / "Invalidation and Theme Changes")
|
|
137
|
+
// --------------------------------------------------------------------------
|
|
138
|
+
|
|
139
|
+
it("overlay caches render output and clears it on invalidate (theme change)", () => {
|
|
140
|
+
const h = makeOverlay(twoListState());
|
|
141
|
+
const first = h.overlay.render(80);
|
|
142
|
+
|
|
143
|
+
// Same width, unchanged inputs: the exact cached lines are returned.
|
|
144
|
+
assert.strictEqual(h.overlay.render(80), first);
|
|
145
|
+
|
|
146
|
+
// Width change: recomputed.
|
|
147
|
+
assert.notStrictEqual(h.overlay.render(120), first);
|
|
148
|
+
|
|
149
|
+
// invalidate() (called by the TUI on theme changes) clears the cache.
|
|
150
|
+
h.overlay.invalidate();
|
|
151
|
+
assert.notStrictEqual(h.overlay.render(80), first);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("overlay recomputes when the state snapshot changes", () => {
|
|
155
|
+
const h = makeOverlay(twoListState());
|
|
156
|
+
const first = h.overlay.render(80);
|
|
157
|
+
|
|
158
|
+
// Space toggles the active list; the bridge swaps in a fresh snapshot.
|
|
159
|
+
// Same width and overlay signature, but new state identity: recompute.
|
|
160
|
+
h.overlay.handleInput(" ");
|
|
161
|
+
assert.notStrictEqual(h.overlay.render(80), first);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("overlay recomputes when the cursor moves", () => {
|
|
165
|
+
const h = makeOverlay(twoListState());
|
|
166
|
+
const first = h.overlay.render(80);
|
|
167
|
+
|
|
168
|
+
h.overlay.handleInput("\x1b[B"); // listIndex changed → signature changed
|
|
169
|
+
assert.notStrictEqual(h.overlay.render(80), first);
|
|
170
|
+
assert.match(h.overlay.render(80).join("\n"), /Items — Home/);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// --------------------------------------------------------------------------
|
|
174
|
+
// Widget pane
|
|
175
|
+
// --------------------------------------------------------------------------
|
|
176
|
+
|
|
177
|
+
it("widget renders the active list's items", () => {
|
|
178
|
+
const lines = renderTrackerWidget(twoListState(), identityTheme, 40);
|
|
179
|
+
const text = lines.join("\n");
|
|
180
|
+
assert.match(text, /Work \(1\/2\)/);
|
|
181
|
+
assert.match(text, /write plan/);
|
|
182
|
+
notContain(text, "water plants");
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("widget renders nothing when no list is active (bridge hides it)", () => {
|
|
186
|
+
const state = new TrackerState({ ...twoListState(), activeListId: null });
|
|
187
|
+
assert.deepStrictEqual(renderTrackerWidget(state, identityTheme, 40), []);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("widget survives the setWidget bridge wrapper (render/invalidate handed off by reference)", () => {
|
|
191
|
+
// Mirrors the tracker bridge: `return { render: widget.render, invalidate: widget.invalidate }`.
|
|
192
|
+
// pi calls render as a method of the wrapper; closure components carry no
|
|
193
|
+
// `this`, so the hand-off is safe by construction (resume-crash regression).
|
|
194
|
+
// oxlint-disable typescript/unbound-method -- intentional seam regression:
|
|
195
|
+
// closure components must survive detached hand-off.
|
|
196
|
+
const widget = makeTrackerWidget(twoListState(), identityTheme);
|
|
197
|
+
const wrapper = { render: widget.render, invalidate: widget.invalidate };
|
|
198
|
+
// oxlint-enable typescript/unbound-method
|
|
199
|
+
const lines = wrapper.render(40);
|
|
200
|
+
assert.match(lines.join("\n"), /Work \(1\/2\)/);
|
|
201
|
+
wrapper.invalidate();
|
|
202
|
+
assert.match(wrapper.render(40).join("\n"), /Work \(1\/2\)/);
|
|
203
|
+
assert.deepStrictEqual(wrapper.render(40), lines);
|
|
204
|
+
});
|
package/src/ui.ts
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
3
|
+
import { Key, matchesKey, truncateToWidth } from "@earendil-works/pi-tui";
|
|
4
|
+
import { makeBorderedBox } from "@ftrdotdev/pi-tui";
|
|
5
|
+
import type { TodoList, TrackerState } from "./domain.ts";
|
|
6
|
+
import type { UpdateItemPatch } from "./store.ts";
|
|
7
|
+
|
|
8
|
+
const MAX_LISTS = 8;
|
|
9
|
+
const MAX_ITEMS = 12;
|
|
10
|
+
|
|
11
|
+
const clamp = (value: number, min: number, max: number): number =>
|
|
12
|
+
Math.min(Math.max(value, min), max);
|
|
13
|
+
|
|
14
|
+
// --------------------------------------------------------------------------
|
|
15
|
+
// Widget pane (ctx.ui.setWidget)
|
|
16
|
+
// --------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
/** The active list, or undefined when the widget should render nothing. */
|
|
19
|
+
const activeList = (state: TrackerState): TodoList | undefined =>
|
|
20
|
+
state.lists.find((list) => list.id === state.activeListId);
|
|
21
|
+
|
|
22
|
+
/** The label embedded in the widget's top border, derived per render so it follows theme changes. */
|
|
23
|
+
const widgetLabel =
|
|
24
|
+
(list: TodoList) =>
|
|
25
|
+
(theme: Theme): string => {
|
|
26
|
+
const done = list.items.filter((item) => item.done).length;
|
|
27
|
+
return (
|
|
28
|
+
theme.fg("accent", theme.bold(list.name)) + theme.fg("dim", ` (${done}/${list.items.length})`)
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** The item lines inside the widget frame, rendered per width so they stay flush. */
|
|
33
|
+
const widgetRows =
|
|
34
|
+
(list: TodoList, theme: Theme) =>
|
|
35
|
+
(_width: number): string[] => {
|
|
36
|
+
const rows: string[] = [];
|
|
37
|
+
for (const item of list.items.slice(0, MAX_ITEMS)) {
|
|
38
|
+
const check = item.done ? theme.fg("success", "✓ ") : theme.fg("dim", "○ ");
|
|
39
|
+
const text = item.done ? theme.fg("muted", theme.strikethrough(item.text)) : item.text;
|
|
40
|
+
rows.push(` ${check}${text}`);
|
|
41
|
+
}
|
|
42
|
+
if (list.items.length > MAX_ITEMS) {
|
|
43
|
+
rows.push(theme.fg("dim", ` ... ${list.items.length - MAX_ITEMS} more`));
|
|
44
|
+
}
|
|
45
|
+
return rows;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Component for the always-visible widget pane above the editor: the active
|
|
50
|
+
* list framed in the house rounded box (`makeBorderedBox`), with the list
|
|
51
|
+
* name and counts embedded in the top border. The same frame the tui, inquiry,
|
|
52
|
+
* and preview surfaces use, so the widget reads as part of the house UI.
|
|
53
|
+
*
|
|
54
|
+
* Closure component: the returned object carries no `this`, so the bridge can
|
|
55
|
+
* hand it to pi-tui through any wrapper without losing state.
|
|
56
|
+
*/
|
|
57
|
+
export const makeTrackerWidget = (state: TrackerState, theme: Theme): Component => {
|
|
58
|
+
const list = activeList(state);
|
|
59
|
+
if (!list) {
|
|
60
|
+
// No list is active: render nothing. The bridge hides the widget in this
|
|
61
|
+
// case; an empty component keeps the seam safe regardless.
|
|
62
|
+
return { render: () => [], invalidate: () => {} };
|
|
63
|
+
}
|
|
64
|
+
// makeBorderedBox caches per width; a fresh component is registered on
|
|
65
|
+
// every state change (refreshWidget), and invalidate() clears it on theme
|
|
66
|
+
// changes so stale themed output is never served.
|
|
67
|
+
return makeBorderedBox({ render: widgetRows(list, theme), invalidate: () => {} }, theme, {
|
|
68
|
+
label: widgetLabel(list),
|
|
69
|
+
color: "border",
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Lines for the widget pane, or an empty array when no list is active — the
|
|
75
|
+
* bridge hides the widget in that case. Kept as a convenience for tests.
|
|
76
|
+
*/
|
|
77
|
+
export const renderTrackerWidget = (state: TrackerState, theme: Theme, width: number): string[] =>
|
|
78
|
+
makeTrackerWidget(state, theme).render(Math.max(4, width));
|
|
79
|
+
|
|
80
|
+
// --------------------------------------------------------------------------
|
|
81
|
+
// Interactive overlay (/tracker command)
|
|
82
|
+
// --------------------------------------------------------------------------
|
|
83
|
+
|
|
84
|
+
/** A mutation requested by the interactive UI. The bridge runs it on the store. */
|
|
85
|
+
export type TrackerUiAction =
|
|
86
|
+
| { readonly type: "createList"; readonly name: string }
|
|
87
|
+
| { readonly type: "deleteList"; readonly listId: number }
|
|
88
|
+
| { readonly type: "setActive"; readonly listId: number | null }
|
|
89
|
+
| { readonly type: "addItem"; readonly listId: number; readonly text: string }
|
|
90
|
+
| { readonly type: "updateItem"; readonly itemId: string; readonly patch: UpdateItemPatch }
|
|
91
|
+
| { readonly type: "removeItem"; readonly itemId: string };
|
|
92
|
+
|
|
93
|
+
type InputMode =
|
|
94
|
+
| { readonly kind: "newList"; buffer: string }
|
|
95
|
+
| { readonly kind: "addItem"; readonly listId: number; buffer: string }
|
|
96
|
+
| { readonly kind: "editItem"; readonly itemId: string; buffer: string };
|
|
97
|
+
|
|
98
|
+
export interface TrackerOverlayOptions {
|
|
99
|
+
/** Latest state snapshot (re-read on every render). */
|
|
100
|
+
readonly getState: () => TrackerState;
|
|
101
|
+
readonly theme: Theme;
|
|
102
|
+
/** Ask the TUI to re-render after state changes. */
|
|
103
|
+
readonly requestRender: () => void;
|
|
104
|
+
/**
|
|
105
|
+
* Run a mutation through the bridge. Resolves with an error message on
|
|
106
|
+
* failure, or null on success (state already updated).
|
|
107
|
+
*/
|
|
108
|
+
readonly onAction: (action: TrackerUiAction) => Promise<string | null>;
|
|
109
|
+
/** Close the overlay. */
|
|
110
|
+
readonly onClose: () => void;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The interactive overlay surface: render + input + cache invalidation. */
|
|
114
|
+
export interface TrackerOverlayHandle {
|
|
115
|
+
readonly render: (width: number) => string[];
|
|
116
|
+
readonly invalidate: () => void;
|
|
117
|
+
readonly handleInput: (data: string) => void;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Interactive two-pane tracker: lists on top, items of the focused list
|
|
122
|
+
* below. Moving the cursor in the lists pane previews that list's items;
|
|
123
|
+
* `enter` commits it as the active list and opens the items pane. `tab`
|
|
124
|
+
* switches panes. Inline text input for names/tasks (printable chars,
|
|
125
|
+
* backspace, enter confirm, esc cancel).
|
|
126
|
+
*
|
|
127
|
+
* Closure factory: all mutable state (mode, cursors, input buffer, render
|
|
128
|
+
* cache) lives in the factory closure, so the returned handle uses no `this`
|
|
129
|
+
* and survives any wrapper-style hand-off into pi.
|
|
130
|
+
*/
|
|
131
|
+
export const makeTrackerOverlay = (options: TrackerOverlayOptions): TrackerOverlayHandle => {
|
|
132
|
+
let mode: "lists" | "items" = "lists";
|
|
133
|
+
let listIndex = 0;
|
|
134
|
+
let itemIndex = 0;
|
|
135
|
+
let input: InputMode | null = null;
|
|
136
|
+
let error: string | null = null;
|
|
137
|
+
|
|
138
|
+
// Render cache (tui.md "Performance"): lines are reused while the inputs
|
|
139
|
+
// (width, state snapshot, overlay state) are unchanged.
|
|
140
|
+
let cachedWidth: number | undefined;
|
|
141
|
+
let cachedLines: string[] | undefined;
|
|
142
|
+
let cachedState: TrackerState | undefined;
|
|
143
|
+
let cachedSignature: string | undefined;
|
|
144
|
+
|
|
145
|
+
const handleInput = (data: string): void => {
|
|
146
|
+
if (input) {
|
|
147
|
+
handleInputMode(data);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (matchesKey(data, Key.escape)) {
|
|
151
|
+
options.onClose();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (matchesKey(data, Key.tab) || matchesKey(data, Key.shift("tab"))) {
|
|
155
|
+
mode = mode === "lists" ? "items" : "lists";
|
|
156
|
+
error = null;
|
|
157
|
+
syncCursors();
|
|
158
|
+
options.requestRender();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const state = options.getState();
|
|
162
|
+
const handled = mode === "lists" ? handleListsKey(data, state) : handleItemsKey(data, state);
|
|
163
|
+
if (handled) {
|
|
164
|
+
error = null;
|
|
165
|
+
options.requestRender();
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const render = (width: number): string[] => {
|
|
170
|
+
const theme = options.theme;
|
|
171
|
+
const state = options.getState();
|
|
172
|
+
const signature = thisSignature();
|
|
173
|
+
if (
|
|
174
|
+
cachedLines !== undefined &&
|
|
175
|
+
cachedWidth === width &&
|
|
176
|
+
cachedState === state &&
|
|
177
|
+
cachedSignature === signature
|
|
178
|
+
) {
|
|
179
|
+
return cachedLines;
|
|
180
|
+
}
|
|
181
|
+
const lines: string[] = [];
|
|
182
|
+
|
|
183
|
+
// Lists pane
|
|
184
|
+
const listsHeader =
|
|
185
|
+
mode === "lists" ? theme.fg("accent", theme.bold("Lists")) : theme.fg("muted", "Lists");
|
|
186
|
+
lines.push(truncateToWidth(` ${listsHeader}`, width));
|
|
187
|
+
|
|
188
|
+
if (state.lists.length === 0) {
|
|
189
|
+
lines.push(
|
|
190
|
+
truncateToWidth(` ${theme.fg("dim", "No lists yet — press n to create one")}`, width),
|
|
191
|
+
);
|
|
192
|
+
} else {
|
|
193
|
+
for (const [index, list] of state.lists.slice(0, MAX_LISTS).entries()) {
|
|
194
|
+
const selected = mode === "lists" && index === listIndex;
|
|
195
|
+
const isActive = list.id === state.activeListId;
|
|
196
|
+
const counts = theme.fg(
|
|
197
|
+
"dim",
|
|
198
|
+
` (${list.items.filter((i) => i.done).length}/${list.items.length})`,
|
|
199
|
+
);
|
|
200
|
+
const name = isActive ? `${theme.fg("success", "● ")}${list.name}` : list.name;
|
|
201
|
+
if (selected) {
|
|
202
|
+
lines.push(
|
|
203
|
+
truncateToWidth(
|
|
204
|
+
` ${theme.fg("accent", "→ ")}${theme.fg("accent", name)}${counts}`,
|
|
205
|
+
width,
|
|
206
|
+
),
|
|
207
|
+
);
|
|
208
|
+
} else {
|
|
209
|
+
lines.push(truncateToWidth(` ${theme.fg("text", name)}${counts}`, width));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (state.lists.length > MAX_LISTS) {
|
|
213
|
+
lines.push(
|
|
214
|
+
truncateToWidth(
|
|
215
|
+
` ${theme.fg("dim", `... ${state.lists.length - MAX_LISTS} more`)}`,
|
|
216
|
+
width,
|
|
217
|
+
),
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
lines.push("");
|
|
223
|
+
|
|
224
|
+
// Items pane
|
|
225
|
+
const activeListFor = activeList(state);
|
|
226
|
+
const itemsHeader = theme.fg(
|
|
227
|
+
mode === "items" ? "accent" : "muted",
|
|
228
|
+
mode === "items"
|
|
229
|
+
? theme.bold(`Items — ${activeListFor?.name ?? "(no list)"}`)
|
|
230
|
+
: `Items — ${activeListFor?.name ?? "(no list)"}`,
|
|
231
|
+
);
|
|
232
|
+
lines.push(truncateToWidth(` ${itemsHeader}`, width));
|
|
233
|
+
|
|
234
|
+
if (!activeListFor) {
|
|
235
|
+
const hint =
|
|
236
|
+
state.lists.length === 0
|
|
237
|
+
? "No lists yet — press n to create one"
|
|
238
|
+
: "No active list — select one in the lists pane (enter)";
|
|
239
|
+
lines.push(truncateToWidth(` ${theme.fg("dim", hint)}`, width));
|
|
240
|
+
} else if (activeListFor.items.length === 0) {
|
|
241
|
+
lines.push(truncateToWidth(` ${theme.fg("dim", "No items — press a to add")}`, width));
|
|
242
|
+
} else {
|
|
243
|
+
for (const [index, item] of activeListFor.items.slice(0, MAX_ITEMS).entries()) {
|
|
244
|
+
const selected = mode === "items" && index === itemIndex;
|
|
245
|
+
const prefix = selected ? theme.fg("accent", "→ ") : " ";
|
|
246
|
+
const check = item.done ? theme.fg("success", "✓") : theme.fg("dim", "○");
|
|
247
|
+
const text = item.done ? theme.fg("muted", theme.strikethrough(item.text)) : item.text;
|
|
248
|
+
lines.push(truncateToWidth(`${prefix}${check} ${text}`, width));
|
|
249
|
+
}
|
|
250
|
+
if (activeListFor.items.length > MAX_ITEMS) {
|
|
251
|
+
lines.push(
|
|
252
|
+
truncateToWidth(
|
|
253
|
+
` ${theme.fg("dim", `... ${activeListFor.items.length - MAX_ITEMS} more`)}`,
|
|
254
|
+
width,
|
|
255
|
+
),
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Input prompt / error line
|
|
261
|
+
if (input) {
|
|
262
|
+
lines.push("");
|
|
263
|
+
const prompt =
|
|
264
|
+
input.kind === "newList"
|
|
265
|
+
? "New list name"
|
|
266
|
+
: input.kind === "addItem"
|
|
267
|
+
? "Item text"
|
|
268
|
+
: "Edit item text";
|
|
269
|
+
lines.push(truncateToWidth(` ${theme.fg("accent", `${prompt}:`)} ${input.buffer}▌`, width));
|
|
270
|
+
lines.push(truncateToWidth(` ${theme.fg("dim", "enter confirm · esc cancel")}`, width));
|
|
271
|
+
} else if (error) {
|
|
272
|
+
lines.push("");
|
|
273
|
+
lines.push(truncateToWidth(` ${theme.fg("warning", `! ${error}`)}`, width));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Help
|
|
277
|
+
lines.push("");
|
|
278
|
+
const help =
|
|
279
|
+
mode === "lists"
|
|
280
|
+
? "[tab] items · [n] new · [d] delete · [space] toggle · [↑↓] move · [enter] active · [esc] close"
|
|
281
|
+
: "[tab] lists · [a] add · [x] toggle · [e] edit · [r] remove · [↑↓] move · [esc] close";
|
|
282
|
+
lines.push(truncateToWidth(` ${theme.fg("dim", help)}`, width));
|
|
283
|
+
|
|
284
|
+
cachedWidth = width;
|
|
285
|
+
cachedLines = lines;
|
|
286
|
+
cachedState = state;
|
|
287
|
+
cachedSignature = signature;
|
|
288
|
+
return lines;
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const invalidate = (): void => {
|
|
292
|
+
cachedWidth = undefined;
|
|
293
|
+
cachedLines = undefined;
|
|
294
|
+
cachedState = undefined;
|
|
295
|
+
cachedSignature = undefined;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Fingerprint of the overlay's own mutable state, used as part of the
|
|
300
|
+
* render-cache key. The state snapshot is compared by object identity:
|
|
301
|
+
* the store is immutable, so any mutation produces a fresh snapshot.
|
|
302
|
+
*/
|
|
303
|
+
const thisSignature = (): string => {
|
|
304
|
+
const inputPart = input === null ? "" : `${input.kind}:${input.buffer}`;
|
|
305
|
+
return `${mode}:${listIndex}:${itemIndex}:${error ?? ""}:${inputPart}`;
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
const activeList = (state: TrackerState): TodoList | undefined => {
|
|
309
|
+
// The items pane previews the list under the cursor while browsing the
|
|
310
|
+
// lists pane; in items mode it follows the explicitly active list (the
|
|
311
|
+
// one committed with enter), so item edits always target the active list.
|
|
312
|
+
if (mode === "lists") {
|
|
313
|
+
return state.lists[listIndex];
|
|
314
|
+
}
|
|
315
|
+
return state.lists.find((list) => list.id === state.activeListId);
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const handleListsKey = (data: string, state: TrackerState): boolean => {
|
|
319
|
+
const lists = state.lists;
|
|
320
|
+
if (lists.length === 0) {
|
|
321
|
+
if (data === "n") {
|
|
322
|
+
input = { kind: "newList", buffer: "" };
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
if (matchesKey(data, Key.up)) {
|
|
328
|
+
listIndex = (listIndex - 1 + lists.length) % lists.length;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
if (matchesKey(data, Key.down)) {
|
|
332
|
+
listIndex = (listIndex + 1) % lists.length;
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
if (matchesKey(data, Key.enter)) {
|
|
336
|
+
const list = lists[listIndex]!;
|
|
337
|
+
void runAction({ type: "setActive", listId: list.id }, () => {
|
|
338
|
+
mode = "items";
|
|
339
|
+
itemIndex = 0;
|
|
340
|
+
});
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
if (data === "n") {
|
|
344
|
+
input = { kind: "newList", buffer: "" };
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
if (data === "d") {
|
|
348
|
+
const list = lists[listIndex]!;
|
|
349
|
+
void runAction({ type: "deleteList", listId: list.id });
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
if (data === " " || matchesKey(data, Key.space)) {
|
|
353
|
+
// Toggle the highlighted list: selecting another list makes it active,
|
|
354
|
+
// toggling the already-active list deselects it (hiding the widget).
|
|
355
|
+
const list = lists[listIndex]!;
|
|
356
|
+
const target = state.activeListId === list.id ? null : list.id;
|
|
357
|
+
void runAction({ type: "setActive", listId: target });
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
return false;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const handleItemsKey = (data: string, state: TrackerState): boolean => {
|
|
364
|
+
const list = activeList(state);
|
|
365
|
+
if (!list) {
|
|
366
|
+
if (data === "n") {
|
|
367
|
+
input = { kind: "newList", buffer: "" };
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
const items = list.items;
|
|
373
|
+
if (matchesKey(data, Key.up)) {
|
|
374
|
+
if (items.length > 0) itemIndex = (itemIndex - 1 + items.length) % items.length;
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
if (matchesKey(data, Key.down)) {
|
|
378
|
+
if (items.length > 0) itemIndex = (itemIndex + 1) % items.length;
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
if (matchesKey(data, Key.enter) || data === "x") {
|
|
382
|
+
const item = items[itemIndex];
|
|
383
|
+
if (!item) return true;
|
|
384
|
+
void runAction({
|
|
385
|
+
type: "updateItem",
|
|
386
|
+
itemId: `${list.name}:${itemIndex + 1}`,
|
|
387
|
+
patch: { done: !item.done },
|
|
388
|
+
});
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
if (data === "a") {
|
|
392
|
+
input = { kind: "addItem", listId: list.id, buffer: "" };
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
395
|
+
if (data === "e") {
|
|
396
|
+
const item = items[itemIndex];
|
|
397
|
+
if (!item) return true;
|
|
398
|
+
input = {
|
|
399
|
+
kind: "editItem",
|
|
400
|
+
itemId: `${list.name}:${itemIndex + 1}`,
|
|
401
|
+
buffer: item.text,
|
|
402
|
+
};
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
if (data === "r") {
|
|
406
|
+
const item = items[itemIndex];
|
|
407
|
+
if (!item) return true;
|
|
408
|
+
void runAction({ type: "removeItem", itemId: `${list.name}:${itemIndex + 1}` });
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
if (data === "n") {
|
|
412
|
+
input = { kind: "newList", buffer: "" };
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
return false;
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
const handleInputMode = (data: string): void => {
|
|
419
|
+
const current = input;
|
|
420
|
+
if (!current) return;
|
|
421
|
+
if (matchesKey(data, Key.escape)) {
|
|
422
|
+
input = null;
|
|
423
|
+
error = null;
|
|
424
|
+
} else if (matchesKey(data, Key.enter)) {
|
|
425
|
+
input = null;
|
|
426
|
+
const action: TrackerUiAction =
|
|
427
|
+
current.kind === "newList"
|
|
428
|
+
? { type: "createList", name: current.buffer }
|
|
429
|
+
: current.kind === "addItem"
|
|
430
|
+
? { type: "addItem", listId: current.listId, text: current.buffer }
|
|
431
|
+
: {
|
|
432
|
+
type: "updateItem",
|
|
433
|
+
itemId: current.itemId,
|
|
434
|
+
patch: { text: current.buffer },
|
|
435
|
+
};
|
|
436
|
+
void runAction(action);
|
|
437
|
+
} else if (matchesKey(data, Key.backspace)) {
|
|
438
|
+
current.buffer = current.buffer.slice(0, -1);
|
|
439
|
+
} else if (data.length === 1 && data.charCodeAt(0) >= 32) {
|
|
440
|
+
// Printable character (single UTF-16 code unit).
|
|
441
|
+
current.buffer += data;
|
|
442
|
+
}
|
|
443
|
+
options.requestRender();
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
const runAction = async (action: TrackerUiAction, afterSuccess?: () => void): Promise<void> => {
|
|
447
|
+
const actionError = await options.onAction(action);
|
|
448
|
+
if (actionError) {
|
|
449
|
+
error = actionError;
|
|
450
|
+
} else {
|
|
451
|
+
error = null;
|
|
452
|
+
afterSuccess?.();
|
|
453
|
+
syncCursors();
|
|
454
|
+
}
|
|
455
|
+
options.requestRender();
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
const syncCursors = (): void => {
|
|
459
|
+
const state = options.getState();
|
|
460
|
+
const lists = state.lists;
|
|
461
|
+
listIndex = lists.length === 0 ? 0 : clamp(listIndex, 0, lists.length - 1);
|
|
462
|
+
const list = activeList(state);
|
|
463
|
+
const items = list?.items ?? [];
|
|
464
|
+
itemIndex = items.length === 0 ? 0 : clamp(itemIndex, 0, items.length - 1);
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
return { render, invalidate, handleInput };
|
|
468
|
+
};
|