repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(notes): render widget at column zero

63e92dc89bd61d475d6b30d4c4ecb20d3a9c9258

Review <review@example.invalid> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

CHANGELOG.md

Modified
2 2 # Changelog
4 4 ## Unreleased
6 +- Fix notes widget rendering so Pi displays it at column zero with width-aware
7 + compact and expanded layouts
6 8 - Add the `gestalt` skill for visual-organization audits of UIs, diagrams,
7 9 screenshots, dashboards, hierarchy, grouping, and scan-path issues
8 10 - Fix goal compaction reentrancy by pausing continuations during session

extensions/notes/__tests__/harness.test.ts

Modified
1 1 import { fileURLToPath } from "node:url";
2 +import { Text } from "@earendil-works/pi-tui";
2 3 import { afterEach, describe, expect, it } from "vitest";
3 4 import { createTestSession, type TestSession } from "../../../test/harness";
4 5 import { __test } from "../index.ts";
32 -function latestWidget(): string[] | undefined {
33 +function latestWidget(): unknown {
33 34 return uiArgs("setWidget")
34 35 .filter((args) => args[0] === "notes")
35 - .map((args) => args[1] as string[] | undefined)
36 + .map((args) => args[1])
36 37 .at(-1);
40 +function renderLatestWidget(width = 80): string[] | undefined {
41 + const widget = uiArgs("setWidget")
42 + .filter((args) => args[0] === "notes")
43 + .map((args) => args[1])
44 + .at(-1);
45 + if (!widget) return undefined;
46 + if (Array.isArray(widget)) {
47 + return widget.flatMap((line) => new Text(line, 1, 0).render(width));
48 + }
49 + if (typeof widget === "function") {
50 + return (widget({}, {}) as { render(width: number): string[] }).render(
51 + width,
52 + );
53 + }
54 + throw new Error("unknown widget shape");
39 57 async function noteCommand(args: string): Promise<void> {
40 58 const command = t?.session.extensionRunner.extensions[0].commands.get("note");
41 59 if (!command || !t) throw new Error("missing note command");
63 81 await noteCommand("add first note");
64 82 await noteCommand("add second note");
66 - expect(latestWidget()).toEqual([
84 + expect(renderLatestWidget()).toEqual([
67 85 "󰎚 2 notes · alt+n expand · last: second note",
89 + it("renders widget at column zero in Pi", async () => {
90 + t = await createTestSession({ extensions: [extensionPath()] });
91 + await noteCommand("add first note");
93 + expect(renderLatestWidget()?.[0]).toMatch(/^󰎚/u);
94 + });
71 96 it("toggles expanded widget with slash command and shortcut", async () => {
72 97 t = await createTestSession({ extensions: [extensionPath()] });
73 98 await noteCommand("add first note");
74 99 await noteCommand("add second note");
75 100 await noteCommand("");
77 - expect(latestWidget()).toEqual([
102 + expect(renderLatestWidget()).toEqual([
78 103 "󰎚 notes · 2",
79 104 " 1 first note",
80 105 " 2 second note",
84 109 t.session.extensionRunner.extensions[0].shortcuts.get("alt+n");
85 110 await shortcut.handler(t.session.extensionRunner.createCommandContext());
87 - expect(latestWidget()).toEqual([
112 + expect(renderLatestWidget()).toEqual([
88 113 "󰎚 2 notes · alt+n expand · last: second note",
103 128 reason: "resume",
106 - expect(latestWidget()).toEqual([
131 + expect(renderLatestWidget()).toEqual([
107 132 "󰎚 1 notes · alt+n expand · last: restored note",
158 183 await noteCommand("clear");
160 185 expect(customEntries("notes-state")).toHaveLength(1);
161 - expect(latestWidget()).toEqual([
186 + expect(renderLatestWidget()).toEqual([
162 187 "󰎚 1 notes · alt+n expand · last: first note",
178 203 text: "edited note",
179 204 updatedAt: expect.any(Number),
181 - expect(latestWidget()).toEqual([
206 + expect(renderLatestWidget()).toEqual([
182 207 "󰎚 1 notes · alt+n expand · last: edited note",

extensions/notes/index.ts

Modified
3 3 import type {
4 4 ExtensionAPI,
5 5 ExtensionContext,
6 6 } from "@earendil-works/pi-coding-agent";
7 7 import type { AutocompleteItem } from "@earendil-works/pi-tui";
8 +import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
9 10 const ENTRY_TYPE = "notes-state";
10 11 const WIDGET_KEY = "notes";
12 +const NOTES_ICON = "󰎚";
13 +const WIDGET_DEFAULT_WIDTH = 80;
12 15 type Note = { id: number; text: string };
13 16 type NotesEvent =
147 150 : oneLine;
150 -function compactWidgetLine(state: NotesState): string {
151 - const notes = pendingNotes(state);
152 - const last = notes.at(-1);
153 - return `󰎚 ${notes.length} notes · alt+n expand · last: ${preview(
154 - last?.text ?? "",
155 - )}`;
153 +function fitVisible(text: string, width: number): string {
154 + return truncateToWidth(text, Math.max(0, width), "…");
158 -function expandedWidgetLines(state: NotesState): string[] {
157 +function wrapVisibleWords(text: string, width: number): string[] {
158 + if (width <= 0) return [""];
159 + const words = text.replace(/\s+/g, " ").trim().split(" ").filter(Boolean);
160 + if (words.length === 0) return [""];
161 + const lines: string[] = [];
162 + let line = "";
163 + for (const word of words) {
164 + if (visibleWidth(word) > width) {
165 + if (line) {
166 + lines.push(line);
167 + line = "";
169 + let rest = word;
170 + while (visibleWidth(rest) > width) {
171 + const part = truncateToWidth(rest, width, "");
172 + if (!part) break;
173 + lines.push(part);
174 + rest = rest.slice(part.length);
176 + line = rest;
177 + continue;
179 + const candidate = line ? `${line} ${word}` : word;
180 + if (visibleWidth(candidate) <= width) line = candidate;
181 + else {
182 + lines.push(line);
183 + line = word;
186 + if (line) lines.push(line);
187 + return lines;
190 +function iconWrap(rows: string[], width: number): string[] {
191 + const safeWidth = Math.max(1, width);
192 + const firstPrefix = `${NOTES_ICON} `;
193 + const nextPrefix = " ";
194 + const lines: string[] = [];
195 + for (const row of rows) {
196 + const prefix = lines.length === 0 ? firstPrefix : nextPrefix;
197 + const textWidth = Math.max(1, safeWidth - visibleWidth(prefix));
198 + for (const part of wrapVisibleWords(row, textWidth)) {
199 + lines.push(`${lines.length === 0 ? firstPrefix : nextPrefix}${part}`);
202 + return lines;
205 +function compactWidgetLine(
206 + state: NotesState,
207 + width = WIDGET_DEFAULT_WIDTH,
208 +): string {
159 209 const notes = pendingNotes(state);
160 - return [
161 - `󰎚 notes · ${notes.length}`,
162 - ...notes.map((note) => ` ${note.id} ${note.text}`),
163 - ];
210 + const last = notes.at(-1);
211 + return fitVisible(
212 + `${NOTES_ICON} ${notes.length} notes · alt+n expand · last: ${preview(
213 + last?.text ?? "",
214 + )}`,
215 + width,
216 + );
219 +function expandedWidgetLines(
220 + state: NotesState,
221 + width = WIDGET_DEFAULT_WIDTH,
222 +): string[] {
223 + const notes = pendingNotes(state);
224 + const lines = iconWrap([`notes · ${notes.length}`], width);
225 + for (const note of notes) {
226 + const prefix = ` ${note.id} `;
227 + const textWidth = Math.max(1, width - visibleWidth(prefix));
228 + for (const [index, part] of wrapVisibleWords(
229 + note.text,
230 + textWidth,
231 + ).entries()) {
232 + lines.push(`${index === 0 ? prefix : " "}${part}`);
235 + return lines;
166 238 function widgetLines(
167 239 state: NotesState,
168 240 expanded: boolean,
241 + width = WIDGET_DEFAULT_WIDTH,
169 242 ): string[] | undefined {
170 243 if (state.notes.size === 0) return undefined;
171 - return expanded ? expandedWidgetLines(state) : [compactWidgetLine(state)];
244 + return expanded
245 + ? expandedWidgetLines(state, width)
246 + : [compactWidgetLine(state, width)];
174 249 function splitCommand(args: string): { command: string; rest: string } {
230 305 function refreshWidget(ctx: ExtensionContext): void {
231 306 if (!ctx.hasUI) return;
232 - ctx.ui.setWidget(WIDGET_KEY, widgetLines(state, expanded));
307 + if (state.notes.size === 0) {
308 + ctx.ui.setWidget(WIDGET_KEY, undefined);
309 + return;
311 + ctx.ui.setWidget(WIDGET_KEY, () => ({
312 + invalidate() {},
313 + render(width: number): string[] {
314 + return widgetLines(state, expanded, width) ?? [];
315 + },
316 + }));
235 319 function toggleWidget(ctx: ExtensionContext): void {