/** * bookmark-atlas — a search palette for your starred GitHub repos and saved X posts. * * Usage: /bookmarks [query] * * Opens an overlay modeled on pi-skill-palette: type to search titles and the * captured text of every saved README, post, and article; arrows to * navigate, preview shows post/article text and inline images. * enter insert the bookmark (title, url, content) into the editor * ctrl+y copy the url * ctrl+o open the url in the default browser * ctrl+r fetch new bookmarks (GitHub stars, X posts, READMEs, browsers) * ctrl+b choose which browsers to import from * ctrl+e read the full text * ctrl+n write a note about the selected bookmark * ctrl+t filter by topic; ctrl+a hide archived; ctrl+d only the last 7 days * esc close * * Reads the Bookmark Atlas SQLite database directly (read-only). Override the * location with BOOKMARK_ATLAS_DB. */ import type { ExtensionAPI, ExtensionCommandContext, Theme } from "@earendil-works/pi-coding-agent"; import { Image, Input, fuzzyMatch, getNativeClipboard, matchesKey, truncateToWidth, visibleWidth, type Component, type Focusable, } from "@earendil-works/pi-tui"; import { spawn, spawnSync } from "node:child_process"; import { existsSync, readFileSync, realpathSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { DatabaseSync } from "node:sqlite"; import { fileURLToPath } from "node:url"; // Resolve the real path first: when the extension is symlinked into // ~/.pi/agent/extensions, import.meta.url is the symlink, so relative paths // would resolve against ~/.pi/agent instead of the repo. function resolveRepoRoot(): string { const modulePath = fileURLToPath(import.meta.url); let real = modulePath; try { real = realpathSync(modulePath); } catch { // fall back to the unresolved path } return join(dirname(real), "..", ".."); } /** * Per-user data directory. Mirrors atlasDataDir() in src/db.ts — this module * cannot import it, because when the extension is symlinked into * ~/.pi/agent/extensions a relative import resolves against the symlink and not * the checkout. test/db.test.ts fails if the two ever drift apart. */ export function atlasDataDir(): string { const override = process.env.BOOKMARK_ATLAS_DATA_DIR; if (override) return resolve(override); const home = homedir(); if (process.platform === "darwin") { return join(home, "Library", "Application Support", "bookmark-atlas"); } if (process.platform === "win32") { return join(process.env.LOCALAPPDATA ?? join(home, "AppData", "Local"), "bookmark-atlas"); } return join(process.env.XDG_DATA_HOME ?? join(home, ".local", "share"), "bookmark-atlas"); } const REPO_ROOT = resolveRepoRoot(); const DB_PATH = process.env.BOOKMARK_ATLAS_DB ?? join(atlasDataDir(), "bookmarks.db"); // The compiled CLI is preferred because Node refuses to strip TypeScript types // for files under node_modules, and an installed palette lives there. A source // checkout that has not been built still falls back to the TypeScript entry. const COMPILED_CLI = join(REPO_ROOT, "dist", "cli.js"); const SOURCE_CLI = join(REPO_ROOT, "src", "cli.ts"); const CLI_PATH = existsSync(COMPILED_CLI) ? COMPILED_CLI : SOURCE_CLI; // Rows visible in the list; also the jump size for PageUp/PageDown and Cmd+↑/↓. const LIST_ROWS = 10; // How far back "recent" reaches, and how many topics the picker will list. const RECENT_DAYS = 7; const TOPIC_LIMIT = 60; const EXCERPT_ROWS = 5; // The image is always given these rows, so the overlay keeps one height as you // move down the list. It must match the Image's maxHeightCells: the renderer // returns exactly that many lines, and the kitty/iTerm sequence draws all of // them, so a larger image would be painted over the bottom border. const IMAGE_ROWS = 10; // How many decoded images the palette keeps in memory at once. const IMAGE_CACHE = 24; // The reading pane trades the list and its separator for more text, so the // overlay keeps the same height. The image keeps its rows in both views. const READING_ROWS = LIST_ROWS + 1 + EXCERPT_ROWS; type RecallHit = { id: number; title: string; url: string; whyMatched: string[]; passage: string | null; score: number; }; const LIST_SQL = ` SELECT r.id, r.title, r.canonical_url AS url, r.author, r.description, r.resource_type AS type, r.created_at AS createdAt, (SELECT MAX(s.saved_at) FROM saves s WHERE s.resource_id = r.id AND s.unsaved_at IS NULL) AS savedAt, (SELECT COUNT(*) FROM x_media m WHERE m.resource_id = r.id AND m.type = 'photo') AS photos, (SELECT COUNT(*) FROM x_media m WHERE m.resource_id = r.id AND m.type = 'video') AS videos, n.context AS context, g.stars AS stars, COALESCE(g.archived, 0) AS archived, g.topics AS topics FROM resources r LEFT JOIN resource_notes n ON n.resource_id = r.id LEFT JOIN github_repositories g ON g.resource_id = r.id `; type Bookmark = { id: number; title: string; url: string; author: string | null; description: string | null; type: string; createdAt: string | null; savedAt: string | null; photos: number; videos: number; context: string | null; stars: number | null; archived: number; topics: string | null; }; /** Which source a bookmark came from. */ type SourceFilter = "all" | "github" | "x" | "browsers"; function sourceOf(bookmark: Bookmark): "github" | "x" | "browsers" { if (bookmark.type === "x_post") return "x"; // Only a page saved from a browser is a browser bookmark. Everything else // keeps the meaning it already had, so an unknown type is never counted as // one of the hundreds of browser rows. if (bookmark.type === "web_page") return "browsers"; return "github"; } /** * `bookmark-atlas browsers` reports one entry per browser profile, but the * selection is per browser, so this folds the profiles into one row each. * * `count` is the raw bookmark total across profiles and `pages` is how many * distinct links that comes to. They differ wildly — four profiles of the same * import hold a thousand bookmarks and a couple of hundred pages — and `unique` * is the part that exists in no other browser, which is what switching this one * on is actually worth. */ type BrowserRow = { provider: string; count: number; pages: number; unique: number; installed: boolean; enabled: boolean; }; function parseBrowserList(stdout: string): BrowserRow[] { let parsed: unknown; try { parsed = JSON.parse(stdout); } catch { return []; } if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return []; const sources = (parsed as { sources?: unknown }).sources; if (!Array.isArray(sources)) return []; const contributions = new Map(); const reported = (parsed as { contributions?: unknown }).contributions; if (Array.isArray(reported)) { for (const entry of reported) { if (typeof entry !== "object" || entry === null || Array.isArray(entry)) continue; const { provider, pages, unique } = entry as { provider?: unknown; pages?: unknown; unique?: unknown; }; if (typeof provider !== "string") continue; contributions.set(provider, { pages: typeof pages === "number" ? pages : 0, unique: typeof unique === "number" ? unique : 0, }); } } const byProvider = new Map(); for (const entry of sources) { if (typeof entry !== "object" || entry === null || Array.isArray(entry)) continue; const { provider, bookmarks, enabled, installed } = entry as { provider?: unknown; bookmarks?: unknown; enabled?: unknown; installed?: unknown; }; if (typeof provider !== "string") continue; const contribution = contributions.get(provider); const row = byProvider.get(provider) ?? { provider, count: 0, pages: contribution?.pages ?? 0, unique: contribution?.unique ?? 0, installed: installed !== false, enabled: enabled === true, }; row.count += typeof bookmarks === "number" ? bookmarks : 0; byProvider.set(provider, row); } return [...byProvider.values()].sort((a, b) => a.provider.localeCompare(b.provider)); } /** * When a bookmark was saved. X bookmarks have no save date from TweetXVault, so * they fall back to when this database first saw them — the same fallback the * base ordering uses, which keeps the date sorts and the base order agreeing. */ function savedOn(bookmark: Bookmark): string { return bookmark.savedAt ?? bookmark.createdAt ?? ""; } /** Compact star count for the list column, e.g. 12345 -> "★ 12.3k". */ function formatStars(stars: number | null): string { if (stars === null || stars === undefined) return "★ —"; return stars >= 1000 ? `★ ${(stars / 1000).toFixed(1)}k` : `★ ${stars}`; } /** Topics are stored as a JSON array; never let a malformed value break the list. */ function topicsOf(bookmark: Bookmark): string[] { if (!bookmark.topics) return []; try { const parsed = JSON.parse(bookmark.topics) as unknown; return Array.isArray(parsed) ? parsed.filter((value): value is string => typeof value === "string") : []; } catch { return []; } } type SortMode = "relevance" | "newest" | "oldest" | "stars" | "alpha"; type PaletteOptions = { initialQuery?: string; /** Preset relevance order (from /consult); restricts the list to these ids. */ rankedIds?: number[]; reasons?: Map; /** Repaint hook, so async work (a background refresh) can update the overlay. */ requestRender?: () => void; /** Test seam: how one refresh step is executed. Defaults to the real CLI. */ cliRunner?: CliRunner; }; type Media = { type: string; path: string | null; contentType: string | null }; /** The one picture that stands for a bookmark, whatever kind of bookmark it is. */ type PreviewImage = { path: string; contentType: string }; type BookmarkDetail = Bookmark & { content: string; media: Media[]; previewImage: PreviewImage | null; }; type Action = { action: "cancel" } | { action: "insert"; ids: number[] }; /** The text one bookmark contributes to the editor when inserted. */ function insertText(detail: BookmarkDetail): string { return [detail.title, detail.url, detail.context ?? "", detail.content.trim().slice(0, 3000)] .filter(Boolean) .join("\n\n"); } function openDb(): DatabaseSync { return new DatabaseSync(DB_PATH, { readOnly: true }); } function loadBookmarks(): Bookmark[] { if (!existsSync(DB_PATH)) return []; const db = openDb(); try { return db .prepare( `${LIST_SQL} WHERE EXISTS (SELECT 1 FROM saves s WHERE s.resource_id = r.id AND s.unsaved_at IS NULL) ORDER BY COALESCE(savedAt, r.created_at) DESC, r.id DESC`, ) .all() as Bookmark[]; } finally { db.close(); } } // Content search: the palette filter is fuzzy over metadata, this adds the // captured text so a query can match a README, post, or article body. Kept // identical to STOP_WORDS in src/search.ts — this module cannot import it when // the extension is symlinked into pi's extensions directory, so // test/parity.test.ts fails if the two lists drift apart again. const SEARCH_STOP_WORDS = new Set([ "a", "an", "and", "for", "from", "in", "into", "of", "on", "or", "over", "the", "to", "with", "is", "are", "was", "be", "been", "being", "it", "its", "this", "that", "these", "those", "i", "me", "my", "we", "our", "you", "your", "he", "she", "they", "them", "their", "do", "does", "did", "can", "could", "should", "would", "will", "shall", "may", "might", "what", "which", "who", "whom", "how", "when", "where", "why", "there", "here", "any", "some", "das", "der", "die", "ein", "eine", "für", "im", "mit", "oder", "und", "von", "zu", // Conversational filler that should never drive a search. "ok", "okay", "nice", "cool", "yeah", "yep", "thanks", "thank", "please", "just", "really", "maybe", "sure", "hello", "hey", "hi", "so", "well", "actually", "basically", ]); let searchConnection: DatabaseSync | null = null; let contentCache: { query: string; ids: number[] } | null = null; /** Resource ids whose captured text matches the query, best first. */ function contentMatchIds(query: string): number[] { if (contentCache?.query === query) return contentCache.ids; const tokens = (query.normalize("NFKC").match(/[\p{L}\p{N}]+/gu) ?? []).filter( (token) => !SEARCH_STOP_WORDS.has(token.toLocaleLowerCase()), ); if (tokens.length === 0 || !existsSync(DB_PATH)) return []; const ftsQuery = tokens.map((token) => `"${token.replaceAll('"', '""')}"`).join(" OR "); let ids: number[] = []; try { searchConnection ??= new DatabaseSync(DB_PATH, { readOnly: true }); const rows = searchConnection .prepare(` SELECT resource_id AS id FROM resources_fts WHERE resources_fts MATCH ? -- Same column weights as src/search.ts; see test/parity.test.ts. ORDER BY bm25(resources_fts, 0.0, 10.0, 5.0, 3.0, 1.0, 2.0) LIMIT 200 `) .all(ftsQuery) as Array<{ id: number }>; ids = rows.map((row) => row.id); } catch { ids = []; } contentCache = { query, ids }; return ids; } function loadDetail(id: number): BookmarkDetail | null { const db = openDb(); try { const bookmark = db.prepare(`${LIST_SQL} WHERE r.id = ?`).get(id) as Bookmark | undefined; if (!bookmark) return null; const captures = db .prepare(` SELECT kind, normalized_content AS content FROM captures WHERE resource_id = ? ORDER BY fetched_at DESC, id DESC `) .all(id) as Array<{ kind: string; content: string }>; // An X article post carries both an x_post capture (a bare t.co link, or a // scraped dump of the same article) and an x_article capture with the real // body. The body is the reason the bookmark exists, so it wins. const primary = captures.find((capture) => capture.kind === "x_article") ?? captures.find((capture) => capture.kind === "github_readme") ?? captures.find((capture) => capture.kind === "x_post") ?? captures.find((capture) => capture.content.trim()) ?? captures[0]; const media = db .prepare(` SELECT type, local_path AS path, content_type AS contentType FROM x_media WHERE resource_id = ? ORDER BY position, media_key `) .all(id) as Media[]; // A README's own image or a page's og:image, captured during enrichment. // It wins over X media because it is the picture that describes the page, // where an X photo is one attachment among several. const stored = db .prepare(` SELECT local_path AS path, content_type AS contentType FROM resource_images WHERE resource_id = ? `) .get(id) as PreviewImage | undefined; return { ...bookmark, content: primary?.content ?? "", media, previewImage: stored?.path ? stored : null, }; } finally { db.close(); } } async function copyText(text: string): Promise { const clipboard = getNativeClipboard(); if (clipboard?.setText) { try { await clipboard.setText(text); return true; } catch { // fall through to the platform helper } } if (process.platform !== "darwin") return false; return spawnSync("pbcopy", [], { input: text }).status === 0; } type CliResult = { ok: boolean; stdout: string; stderr: string }; type CliRunner = (args: string[]) => Promise; /** Run the CLI without blocking the TUI. Resolves on exit rather than throwing. */ function runCli(args: string[]): Promise { return new Promise((resolve) => { let stdout = ""; let stderr = ""; const child = spawn(process.execPath, [CLI_PATH, ...args], { stdio: ["ignore", "pipe", "pipe"] }); child.stdout.on("data", (chunk: Buffer) => { stdout += chunk.toString("utf8"); }); child.stderr.on("data", (chunk: Buffer) => { stderr += chunk.toString("utf8"); }); child.on("error", (error) => resolve({ ok: false, stdout, stderr: `${stderr}${error.message}` })); child.on("close", (code) => resolve({ ok: code === 0, stdout, stderr })); }); } /** Pull one named count out of a CLI JSON payload, when it printed one. */ function countFrom(stdout: string, key: string): number | null { try { const value = JSON.parse(stdout) as Record; const count = value[key]; return typeof count === "number" ? count : null; } catch { return null; } } function wrap(text: string, width: number, maxLines: number): string[] { const words = text.replace(/\s+/g, " ").trim().split(" "); const lines: string[] = []; let current = ""; for (const word of words) { const candidate = current ? `${current} ${word}` : word; if (visibleWidth(candidate) > width && current) { lines.push(current); current = word; if (lines.length >= maxLines) break; } else { current = candidate; } } if (current && lines.length < maxLines) lines.push(current); return lines; } export class BookmarkPalette implements Component, Focusable { focused = false; private readonly input: Input; private items: Bookmark[]; private readonly theme: Theme; private readonly done: (action: Action) => void; private readonly reasons: Map; private readonly ranked: boolean; private filtered: Bookmark[]; private selected = 0; private sourceFilter: SourceFilter = "all"; private sortMode: SortMode = "relevance"; private hideArchived = false; private recentOnly = false; private topic: string | null = null; private topicPicker: { entries: Array<{ name: string; count: number }>; index: number } | null = null; /** Which browsers to import from. `count` is across every profile of that browser. */ private browserPicker: { entries: BrowserRow[]; index: number; saving: boolean; } | null = null; private reading = false; /** Bookmarks marked for a combined insert. */ private marked = new Set(); /** The full library, held while the list is pivoted to a bookmark's relations. */ private relatedBase: Bookmark[] | null = null; private noteEdit: { id: number; input: Input } | null = null; private preview: { id: number; offset: number } | null = null; /** Recorded by render so input handling can clamp the scroll without re-wrapping. */ private previewTotal = 0; private readonly requestRender: (() => void) | undefined; private readonly cliRunner: CliRunner; private refreshing = false; private showHelp = false; private notice: string | null = null; private readonly details = new Map(); private readonly images = new Map(); constructor( allItems: Bookmark[], theme: Theme, done: (action: Action) => void, options: PaletteOptions = {}, ) { const byId = new Map(allItems.map((bookmark) => [bookmark.id, bookmark])); this.items = options.rankedIds ? options.rankedIds.flatMap((id) => { const bookmark = byId.get(id); return bookmark ? [bookmark] : []; }) : allItems; this.ranked = Boolean(options.rankedIds); this.reasons = options.reasons ?? new Map(); this.requestRender = options.requestRender; this.cliRunner = options.cliRunner ?? runCli; this.theme = theme; this.done = done; this.input = new Input({ placeholder: "Search title, author, description, url…" }); const initialQuery = options.initialQuery ?? ""; if (initialQuery) this.input.setValue(initialQuery); this.filtered = this.applyFilter(initialQuery); } private applyFilter(query: string): Bookmark[] { let scoped = this.sourceFilter === "all" ? this.items : this.items.filter((bookmark) => sourceOf(bookmark) === this.sourceFilter); if (this.hideArchived) scoped = scoped.filter((bookmark) => bookmark.archived !== 1); if (this.recentOnly) { const cutoff = new Date(Date.now() - RECENT_DAYS * 86_400_000).toISOString(); scoped = scoped.filter((bookmark) => (bookmark.savedAt ?? "") >= cutoff); } if (this.topic) { const wanted = this.topic; scoped = scoped.filter((bookmark) => topicsOf(bookmark).includes(wanted)); } const trimmed = query.trim(); if (!trimmed) return this.sortBookmarks([...scoped]); // fuzzyMatch accepts any in-order subsequence and scans greedily from the left, // so it credits scattered letters over a clean run later in the string. Only a // literal substring hit may outrank a match found in the captured content; a // loose subsequence hit still appears, but beneath it. const tokens = trimmed.toLowerCase().split(/[\s/]+/).filter(Boolean); const literal: Array<{ bookmark: Bookmark; inTitle: boolean; at: number }> = []; const loose: Array<{ bookmark: Bookmark; score: number }> = []; for (const bookmark of scoped) { const raw = `${bookmark.title} ${bookmark.author ?? ""} ${bookmark.description ?? ""} ${bookmark.url}`; const title = bookmark.title.toLowerCase(); const text = raw.toLowerCase(); let at = Number.MAX_SAFE_INTEGER; let everyToken = true; for (const token of tokens) { const found = text.indexOf(token); if (found < 0) { everyToken = false; break; } if (found < at) at = found; } if (everyToken) { literal.push({ bookmark, inTitle: tokens.every((token) => title.includes(token)), at }); continue; } let score = 0; let matchesAll = true; for (const token of tokens) { const match = fuzzyMatch(token, raw); if (!match.matches) { matchesAll = false; break; } score += match.score; } if (matchesAll) loose.push({ bookmark, score }); } literal.sort((a, b) => Number(b.inTitle) - Number(a.inTitle) || a.at - b.at); loose.sort((a, b) => a.score - b.score); const matched = new Set([...literal, ...loose].map((entry) => entry.bookmark.id)); const scopedById = new Map(scoped.map((bookmark) => [bookmark.id, bookmark])); const byContent: Bookmark[] = []; for (const id of contentMatchIds(trimmed)) { const bookmark = scopedById.get(id); if (bookmark && !matched.has(id)) byContent.push(bookmark); } return this.sortBookmarks([ ...literal.map((entry) => entry.bookmark), ...byContent, ...loose.map((entry) => entry.bookmark), ]); } private hasQuery(): boolean { return this.input.getValue().trim() !== ""; } private sortBookmarks(items: Bookmark[]): Bookmark[] { const sorted = [...items]; switch (this.sortMode) { case "newest": return sorted.sort((a, b) => savedOn(b).localeCompare(savedOn(a))); case "oldest": return sorted.sort((a, b) => savedOn(a).localeCompare(savedOn(b))); case "stars": return sorted.sort((a, b) => (b.stars ?? -1) - (a.stars ?? -1)); case "alpha": return sorted.sort((a, b) => a.title.localeCompare(b.title)); default: return sorted; } } private refilter(): void { this.filtered = this.applyFilter(this.input.getValue()); this.selected = 0; } private cycleSort(): void { // "relevance" only means something once there is a query. Without one it is // the base ordering, which is newest-first — so it is not offered, and // starting from it steps to oldest rather than to an identical view. const order: SortMode[] = this.hasQuery() ? ["relevance", "newest", "oldest", "stars", "alpha"] : ["newest", "oldest", "stars", "alpha"]; const current = this.sortMode === "relevance" && !this.hasQuery() ? "newest" : this.sortMode; const index = order.indexOf(current); this.sortMode = order[(index + 1) % order.length] ?? order[0] ?? "newest"; this.filtered = this.applyFilter(this.input.getValue()); this.selected = 0; } private sourceCounts(): { all: number; github: number; x: number; browsers: number } { let github = 0; let x = 0; for (const bookmark of this.items) { const source = sourceOf(bookmark); if (source === "github") github += 1; else if (source === "x") x += 1; } return { all: this.items.length, github, x, browsers: this.items.length - github - x }; } private cycleSource(step: number): void { const order: SourceFilter[] = ["all", "github", "x", "browsers"]; const index = order.indexOf(this.sourceFilter); this.sourceFilter = order[(index + step + order.length) % order.length] ?? "all"; this.filtered = this.applyFilter(this.input.getValue()); this.selected = 0; } private selectedBookmark(): Bookmark | undefined { return this.filtered[this.selected]; } private detail(bookmark: Bookmark): BookmarkDetail | null { if (!this.details.has(bookmark.id)) this.details.set(bookmark.id, loadDetail(bookmark.id)); return this.details.get(bookmark.id) ?? null; } private image(bookmark: Bookmark): Image | null { if (this.images.has(bookmark.id)) return this.images.get(bookmark.id) ?? null; let image: Image | null = null; const detail = this.detail(bookmark); const photo = detail?.media.find( (media) => media.path && (media.contentType ?? "").startsWith("image/"), ); const source = detail?.previewImage ?? (photo?.path ? { path: photo.path, contentType: photo.contentType ?? "image/jpeg" } : null); if (source) { try { const buffer = readFileSync(source.path); if (buffer.byteLength <= 5 * 1024 * 1024) { image = new Image( buffer.toString("base64"), source.contentType, { fallbackColor: (text) => this.theme.fg("dim", text) }, { maxWidthCells: 56, maxHeightCells: IMAGE_ROWS, filename: source.path }, ); } } catch { image = null; } } this.images.set(bookmark.id, image); // An Image keeps its base64 and its rendered rows, so caching every one you // scroll past is megabytes each. The list only ever needs the ones near the // cursor, and a dropped entry costs one file read to rebuild. if (this.images.size > IMAGE_CACHE) this.images.clear(); return image; } /** Marked bookmarks in view order, or just the selection when nothing is marked. */ private insertTargets(): number[] { const marked = this.filtered .filter((bookmark) => this.marked.has(bookmark.id)) .map((bookmark) => bookmark.id); if (marked.length > 0) return marked; const bookmark = this.selectedBookmark(); return bookmark ? [bookmark.id] : []; } /** * Pivot the list to what is related to the selected bookmark, and back again. * The ranking lives in the CLI, so this shells out the same way consult does and * reuses the reasons mechanism to say why each one matched. */ private async toggleRelated(): Promise { if (this.relatedBase) { this.items = this.relatedBase; this.relatedBase = null; this.reasons.clear(); this.notice = null; this.refilter(); this.requestRender?.(); return; } const bookmark = this.selectedBookmark(); if (!bookmark) return; const base = this.items; this.notice = "↳ finding related…"; this.requestRender?.(); const result = await this.cliRunner(["related", String(bookmark.id), "--limit", "40"]); if (!result.ok) { this.notice = "↳ related lookup failed"; this.requestRender?.(); return; } let hits: Array<{ id: number; whyRelated?: string[] }>; try { hits = JSON.parse(result.stdout) as Array<{ id: number; whyRelated?: string[] }>; } catch { this.notice = "↳ related lookup returned invalid data"; this.requestRender?.(); return; } const byId = new Map(base.map((item) => [item.id, item])); const items = hits.flatMap((hit) => { const item = byId.get(hit.id); return item ? [item] : []; }); if (items.length === 0) { this.notice = "↳ nothing related found"; this.requestRender?.(); return; } this.relatedBase = base; this.items = items; this.reasons.clear(); for (const hit of hits) { const item = byId.get(hit.id); if (item && hit.whyRelated) this.reasons.set(hit.id, hit.whyRelated); } // Relation order is the point, so stop any other sort from rearranging it. this.sortMode = "relevance"; this.refilter(); this.notice = `↳ related to ${bookmark.title.slice(0, 28)}`; this.requestRender?.(); } /** Run the refresh steps in order, non-blocking, then reload the list in place. */ async refresh(): Promise { if (this.refreshing) return; if (!existsSync(CLI_PATH)) { this.notice = "refresh unavailable: CLI not found"; this.requestRender?.(); return; } const steps = [ { label: "GitHub", args: ["sync", "github"], key: "imported" }, { label: "X", args: ["collect", "x", "--fast"], key: "imported" }, { label: "READMEs", args: ["enrich", "github-readmes", "--limit", "25"], key: "enriched" }, // Last because it is the cheapest and the only one that can fail per // source: a blocked Safari plist reports itself without stopping the run. { label: "Browsers", args: ["sync", "browsers"], key: "imported" }, ]; this.refreshing = true; const parts: string[] = []; let failed = false; try { for (const step of steps) { this.notice = `↻ refreshing ${step.label}\u2026`; this.requestRender?.(); const result = await this.cliRunner(step.args); if (!result.ok) { failed = true; parts.push(`${step.label} failed`); continue; } const count = countFrom(result.stdout, step.key); parts.push(`${step.label} ${count === null ? "done" : count > 0 ? `+${count}` : "no change"}`); } } finally { this.refreshing = false; contentCache = null; this.items = loadBookmarks(); this.details.clear(); this.images.clear(); this.filtered = this.applyFilter(this.input.getValue()); this.selected = Math.min(this.selected, Math.max(0, this.filtered.length - 1)); this.notice = `${failed ? "⚠" : "✓"} ${parts.join(" · ")}`; this.requestRender?.(); } } /** Topics worth offering, most used first, with an "All topics" entry on top. */ private openTopicPicker(): void { const counts = new Map(); let total = 0; for (const bookmark of this.items) { if (this.sourceFilter !== "all" && sourceOf(bookmark) !== this.sourceFilter) continue; total += 1; for (const name of topicsOf(bookmark)) counts.set(name, (counts.get(name) ?? 0) + 1); } const entries = [ { name: "", count: total }, ...[...counts.entries()] .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])) .slice(0, TOPIC_LIMIT) .map(([name, count]) => ({ name, count })), ]; const current = entries.findIndex((entry) => entry.name === (this.topic ?? "")); this.topicPicker = { entries, index: current >= 0 ? current : 0 }; this.requestRender?.(); } private handleTopicPickerInput(data: string): void { const picker = this.topicPicker; if (!picker) return; const page = LIST_ROWS - 1; const last = Math.max(0, picker.entries.length - 1); if (matchesKey(data, "escape") || matchesKey(data, "ctrl+t") || matchesKey(data, "ctrl+c")) { this.topicPicker = null; } else if (matchesKey(data, "up")) { picker.index = Math.max(0, picker.index - 1); } else if (matchesKey(data, "down")) { picker.index = Math.min(last, picker.index + 1); } else if (matchesKey(data, "pageUp")) { picker.index = Math.max(0, picker.index - page); } else if (matchesKey(data, "pageDown")) { picker.index = Math.min(last, picker.index + page); } else if (matchesKey(data, "home")) { picker.index = 0; } else if (matchesKey(data, "end")) { picker.index = last; } else if (matchesKey(data, "return")) { const name = picker.entries[picker.index]?.name ?? ""; this.topic = name === "" ? null : name; this.topicPicker = null; this.refilter(); } else { return; } this.requestRender?.(); } /** * The browser list comes from the CLI rather than being discovered here, so * the config file has exactly one reader and one writer. */ private async openBrowserPicker(): Promise { if (this.browserPicker) return; if (!existsSync(CLI_PATH)) { this.notice = "browser selection unavailable: CLI not found"; this.requestRender?.(); return; } this.notice = "↻ reading browsers…"; this.requestRender?.(); const result = await this.cliRunner(["browsers"]); if (!result.ok) { this.notice = "⚠ could not read browsers"; this.requestRender?.(); return; } const entries = parseBrowserList(result.stdout); if (entries.length === 0) { this.notice = "no browsers found"; this.requestRender?.(); return; } this.browserPicker = { entries, index: 0, saving: false }; this.notice = null; this.requestRender?.(); } private handleBrowserPickerInput(data: string): void { const picker = this.browserPicker; if (!picker || picker.saving) return; const page = LIST_ROWS - 1; const last = Math.max(0, picker.entries.length - 1); if (matchesKey(data, "escape") || matchesKey(data, "ctrl+b") || matchesKey(data, "ctrl+c")) { this.browserPicker = null; } else if (matchesKey(data, "up")) { picker.index = Math.max(0, picker.index - 1); } else if (matchesKey(data, "down")) { picker.index = Math.min(last, picker.index + 1); } else if (matchesKey(data, "pageUp")) { picker.index = Math.max(0, picker.index - page); } else if (matchesKey(data, "pageDown")) { picker.index = Math.min(last, picker.index + page); } else if (matchesKey(data, "home")) { picker.index = 0; } else if (matchesKey(data, "end")) { picker.index = last; } else if (matchesKey(data, "space") || data === " ") { // Both forms, because pi's own selectable lists compare the raw string // while its examples use the key name. Checking only one leaves the // toggle silently dead: a miss falls through to the return below, so // nothing re-renders and the key looks ignored. const entry = picker.entries[picker.index]; if (entry) entry.enabled = !entry.enabled; } else if (matchesKey(data, "return")) { void this.saveBrowserPicker(); return; } else { return; } this.requestRender?.(); } private async saveBrowserPicker(): Promise { const picker = this.browserPicker; if (!picker) return; picker.saving = true; this.notice = "↻ applying browser selection…"; this.requestRender?.(); const on = picker.entries.filter((entry) => entry.enabled).map((entry) => entry.provider); // Everything on is the default, so it is stored as the absence of a choice // rather than a list that would go stale as browsers are installed. let request = on.join(","); if (on.length === picker.entries.length) request = "all"; else if (on.length === 0) request = "none"; const saved = await this.cliRunner(["browsers", "--enable", request]); if (!saved.ok) { picker.saving = false; this.notice = "⚠ could not save the browser selection"; this.requestRender?.(); return; } // The choice only changes what is in the database once a sync runs, and the // sync is what hides the bookmarks of a browser switched off here. const synced = await this.cliRunner(["sync", "browsers"]); this.browserPicker = null; contentCache = null; this.items = loadBookmarks(); this.details.clear(); this.images.clear(); this.filtered = this.applyFilter(this.input.getValue()); this.selected = Math.min(this.selected, Math.max(0, this.filtered.length - 1)); const hidden = synced.ok ? countFrom(synced.stdout, "removed") : null; let notice = "✓ browsers updated"; if (!synced.ok) notice = "⚠ selection saved, but the sync failed"; else if (hidden !== null && hidden > 0) { notice = `✓ browsers updated · ${hidden} bookmark${hidden === 1 ? "" : "s"} hidden`; } this.notice = notice; this.requestRender?.(); } /** Keys while writing a note: enter saves it, esc cancels, anything else edits. */ private handleNoteInput(data: string): void { const edit = this.noteEdit; if (!edit) return; if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) { this.noteEdit = null; this.requestRender?.(); return; } if (matchesKey(data, "return")) { const text = edit.input.getValue().trim(); this.noteEdit = null; void this.saveNote(edit.id, text); return; } edit.input.handleInput(data); this.requestRender?.(); } /** * Write the note through the CLI, which owns both the note write and the FTS * refresh, so the extension keeps its read-only database connection. */ private async saveNote(id: number, text: string): Promise { const bookmark = this.items.find((item) => item.id === id); const previous = bookmark?.context ?? null; if (bookmark) bookmark.context = text || null; this.notice = "✎ saving note…"; this.requestRender?.(); const result = await this.cliRunner(["note", String(id), text]); if (!result.ok && bookmark) { bookmark.context = previous; this.notice = "✎ note not saved"; } else { this.notice = text ? "✎ note saved" : "✎ note cleared"; contentCache = null; } this.requestRender?.(); } /** Keys while the reading pane is open: scroll the text, esc returns to the list. */ private handleReadingInput(data: string): void { const bookmark = this.selectedBookmark(); const current = this.preview && bookmark && this.preview.id === bookmark.id ? this.preview.offset : 0; const maxOffset = Math.max(0, this.previewTotal - READING_ROWS); const scrollTo = (offset: number): void => { if (!bookmark) return; this.preview = { id: bookmark.id, offset: Math.max(0, Math.min(offset, maxOffset)) }; this.requestRender?.(); }; if (matchesKey(data, "escape") || matchesKey(data, "ctrl+e") || matchesKey(data, "ctrl+c")) { this.reading = false; this.requestRender?.(); return; } if (matchesKey(data, "up")) return scrollTo(current - 1); if (matchesKey(data, "down")) return scrollTo(current + 1); if (matchesKey(data, "pageUp")) return scrollTo(current - (READING_ROWS - 1)); if (matchesKey(data, "pageDown")) return scrollTo(current + (READING_ROWS - 1)); if (matchesKey(data, "home")) return scrollTo(0); if (matchesKey(data, "end")) return scrollTo(maxOffset); if (matchesKey(data, "return") && bookmark) { const ids = this.insertTargets(); if (ids.length > 0) this.done({ action: "insert", ids }); } } handleInput(data: string): void { if (this.showHelp) { this.showHelp = false; this.requestRender?.(); return; } if (this.topicPicker) { this.handleTopicPickerInput(data); return; } if (this.browserPicker) { this.handleBrowserPickerInput(data); return; } if (this.reading) { this.handleReadingInput(data); return; } if (this.noteEdit) { this.handleNoteInput(data); return; } if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) { this.done({ action: "cancel" }); return; } if (matchesKey(data, "tab")) { this.cycleSource(1); return; } if (matchesKey(data, "shift+tab")) { this.cycleSource(-1); return; } if (matchesKey(data, "ctrl+s")) { this.cycleSort(); return; } if (matchesKey(data, "ctrl+r")) { void this.refresh(); return; } if (matchesKey(data, "ctrl+e")) { this.reading = true; this.preview = null; this.requestRender?.(); return; } if (matchesKey(data, "ctrl+n")) { const bookmark = this.selectedBookmark(); if (bookmark) { const input = new Input({ placeholder: "Why does this bookmark matter?" }); input.setValue(bookmark.context ?? ""); this.noteEdit = { id: bookmark.id, input }; this.requestRender?.(); } return; } if (matchesKey(data, "ctrl+a")) { this.hideArchived = !this.hideArchived; this.refilter(); return; } if (matchesKey(data, "ctrl+d")) { this.recentOnly = !this.recentOnly; this.refilter(); return; } if (matchesKey(data, "ctrl+t")) { this.openTopicPicker(); return; } if (matchesKey(data, "ctrl+b")) { void this.openBrowserPicker(); return; } if (matchesKey(data, "ctrl+l")) { void this.toggleRelated(); return; } if (matchesKey(data, "ctrl+x")) { const bookmark = this.selectedBookmark(); if (bookmark) { if (this.marked.has(bookmark.id)) this.marked.delete(bookmark.id); else this.marked.add(bookmark.id); this.requestRender?.(); } return; } // Jump navigation. macOS sends Fn+←/→ as home/end and Fn+↑/↓ as pageUp/pageDown; // Cmd+↑/↓ arrives as super+up/down only when the terminal forwards the modifier. if (matchesKey(data, "home")) { this.selected = 0; return; } if (matchesKey(data, "end")) { this.selected = Math.max(0, this.filtered.length - 1); return; } if (matchesKey(data, "pageUp") || matchesKey(data, "super+up")) { this.selected = Math.max(0, this.selected - LIST_ROWS); return; } if (matchesKey(data, "pageDown") || matchesKey(data, "super+down")) { this.selected = Math.min(Math.max(0, this.filtered.length - 1), this.selected + LIST_ROWS); return; } if (matchesKey(data, "up")) { this.selected = Math.max(0, this.selected - 1); return; } if (matchesKey(data, "down")) { this.selected = Math.min(Math.max(0, this.filtered.length - 1), this.selected + 1); return; } if (matchesKey(data, "return")) { const ids = this.insertTargets(); if (ids.length > 0) this.done({ action: "insert", ids }); return; } const bookmark = this.selectedBookmark(); if (bookmark && matchesKey(data, "ctrl+y")) { void copyText(bookmark.url); return; } if (bookmark && matchesKey(data, "ctrl+o")) { if (process.platform === "darwin") spawnSync("open", [bookmark.url]); this.done({ action: "cancel" }); return; } // `?` only opens help on an empty search box, so it can still be typed in a query. if (matchesKey(data, "f1") || (data === "?" && this.input.getValue().trim() === "")) { this.showHelp = true; this.requestRender?.(); return; } // Everything that reaches the search box. Keys that only move the caret — // left/right, shift+arrows, home/end inside the query — change nothing, and // must not be mistaken for a new query: re-filtering would send the cursor // back to the first result while the list stayed exactly the same. const before = this.input.getValue(); this.input.handleInput(data); const after = this.input.getValue(); if (after !== before) { this.filtered = this.applyFilter(after); this.selected = 0; } } /** The topic picker: topics ranked by how many bookmarks carry them. */ private renderTopics(width: number): string[] { const theme = this.theme; const innerWidth = Math.max(20, width - 4); const pad = (text: string): string => { const line = truncateToWidth(text, innerWidth, "…", true); return line + " ".repeat(Math.max(0, innerWidth - visibleWidth(line))); }; const row = (content: string): string => theme.fg("border", "│ ") + pad(content) + theme.fg("border", " │"); const picker = this.topicPicker; const entries = picker?.entries ?? []; const selected = picker?.index ?? 0; const rows = 20; const maxStart = Math.max(0, entries.length - rows); const start = Math.max(0, Math.min(selected - Math.floor(rows / 2), maxStart)); const lines: string[] = [theme.fg("border", `┌${"─".repeat(width - 2)}┐`)]; lines.push( row( theme.fg("accent", theme.bold("Filter by topic")) + theme.fg("dim", ` ${Math.max(0, entries.length - 1)} topics`), ), ); lines.push(theme.fg("border", `├${"─".repeat(width - 2)}┤`)); for (let offset = 0; offset < rows; offset += 1) { const index = start + offset; const entry = entries[index]; if (!entry) { lines.push(row("")); continue; } const label = (entry.name === "" ? "All topics" : `#${entry.name}`).padEnd(30); const text = `${index === selected ? "▸" : " "} ${label}${String(entry.count).padStart(4)}`; lines.push( row( index === selected ? theme.bg("selectedBg", theme.fg("text", text)) : theme.fg("muted", text), ), ); } lines.push(theme.fg("border", `└${"─".repeat(width - 2)}┘`)); lines.push(theme.fg("dim", " ↑↓ navigate · enter filter · esc cancel")); return lines; } /** Which browsers to import from. `space` toggles, `enter` saves. */ private renderBrowsers(width: number): string[] { const theme = this.theme; const innerWidth = Math.max(20, width - 4); const pad = (text: string): string => { const line = truncateToWidth(text, innerWidth, "…", true); return line + " ".repeat(Math.max(0, innerWidth - visibleWidth(line))); }; const row = (content: string): string => theme.fg("border", "│ ") + pad(content) + theme.fg("border", " │"); const picker = this.browserPicker; const entries = picker?.entries ?? []; const selected = picker?.index ?? 0; const rows = 20; const maxStart = Math.max(0, entries.length - rows); const start = Math.max(0, Math.min(selected - Math.floor(rows / 2), maxStart)); const on = entries.filter((entry) => entry.enabled).length; const lines: string[] = [theme.fg("border", `┌${"─".repeat(width - 2)}┐`)]; lines.push( row( theme.fg("accent", theme.bold("Import bookmarks from")) + theme.fg("dim", ` ${on} of ${entries.length} on`), ), ); lines.push(theme.fg("border", `├${"─".repeat(width - 2)}┤`)); for (let offset = 0; offset < rows; offset += 1) { const index = start + offset; const entry = entries[index]; if (!entry) { lines.push(row("")); continue; } const box = entry.enabled ? "[x]" : "[ ]"; // Pages and unique, not the raw bookmark count: a browser holding four // copies of the same import reads as 258 pages and +4 new, which is the // whole reason someone is looking at this list. const pages = `${entry.pages} pages`.padStart(11); const unique = `+${entry.unique} new`.padStart(9); const note = entry.installed ? "" : " not installed"; const text = `${index === selected ? "▸" : " "} ${box} ${entry.provider.padEnd(11)}${pages}${unique}${note}`; lines.push( row( index === selected ? theme.bg("selectedBg", theme.fg("text", text)) : theme.fg("muted", text), ), ); } lines.push(theme.fg("border", `└${"─".repeat(width - 2)}┘`)); lines.push(theme.fg("dim", " ↑↓ navigate · space toggle · enter save · esc cancel")); lines.push( theme.fg("dim", " pages = distinct links · new = not in any other browser"), ); lines.push( theme.fg("dim", " switching a browser off hides its bookmarks from search and recall"), ); return lines; } /** The `?` / F1 screen: what this is, and every key. */ private renderHelp(width: number): string[] { const theme = this.theme; const innerWidth = Math.max(20, width - 4); const pad = (text: string): string => { const line = truncateToWidth(text, innerWidth, "…", true); return line + " ".repeat(Math.max(0, innerWidth - visibleWidth(line))); }; const row = (content: string): string => theme.fg("border", "│ ") + pad(content) + theme.fg("border", " │"); const key = (keys: string, what: string): string => theme.fg("accent", ` ${keys.padEnd(18)}`) + theme.fg("muted", what); const body = [ theme.fg("accent", theme.bold("Bookmark Atlas")), theme.fg("dim", " GitHub stars, X posts and browser bookmarks, read from a local database."), "", theme.fg("dim", " Type to search titles and captured text, arrows to move, enter to insert."), "", theme.fg("accent", "Navigate"), key("↑ / ↓", "one row"), key("Fn+↑ / Fn+↓", "ten rows (Page Up/Down)"), key("Fn+← / Fn+→", "first / last (Home/End)"), key("Cmd+↑ / Cmd+↓", "ten rows, where the terminal forwards Cmd"), "", theme.fg("accent", "Filter"), key("tab / shift+tab", "All → GitHub → X → Browsers"), key("ctrl+a", "hide archived repositories"), key("ctrl+d", "only what was added in the last 7 days"), key("ctrl+t", "filter by topic"), key("ctrl+b", "choose which browsers to import from"), key("ctrl+l", "pivot the list to what is related to this bookmark"), key("ctrl+x", "mark this bookmark, so enter inserts several at once"), key("ctrl+s", "sort: newest, oldest, stars, A-Z (best match appears once you search)"), "", theme.fg("accent", "Act"), key("enter", "insert title, url and content into the editor"), key("ctrl+e", "read the full README or post text"), key("ctrl+n", "write a note: why this matters to you"), key("ctrl+y", "copy the url"), key("ctrl+o", "open in the browser"), key("ctrl+r", "fetch new bookmarks (GitHub, X, READMEs, browsers)"), "", key("? / f1", "this help"), key("esc", "close"), ]; const lines: string[] = [theme.fg("border", `┌${"─".repeat(width - 2)}┐`)]; for (const line of body) lines.push(row(line)); lines.push(theme.fg("border", `└${"─".repeat(width - 2)}┘`)); lines.push(theme.fg("dim", " press any key to go back")); return lines; } render(width: number): string[] { if (this.showHelp) return this.renderHelp(width); if (this.topicPicker) return this.renderTopics(width); if (this.browserPicker) return this.renderBrowsers(width); this.input.focused = this.focused; if (this.noteEdit) this.noteEdit.input.focused = this.focused; const theme = this.theme; const innerWidth = Math.max(20, width - 4); const pad = (text: string): string => { const line = truncateToWidth(text, innerWidth, "…", true); return line + " ".repeat(Math.max(0, innerWidth - visibleWidth(line))); }; const row = (content: string): string => theme.fg("border", "│ ") + pad(content) + theme.fg("border", " │"); const lines: string[] = []; lines.push(theme.fg("border", `┌${"─".repeat(width - 2)}┐`)); lines.push( row( theme.fg("accent", theme.bold(this.relatedBase ? "Related" : this.ranked ? "Consult" : "Bookmark Atlas")) + theme.fg( "dim", this.relatedBase ? ` ${this.items.length} related · ${this.filtered.length} shown` : this.ranked ? ` ${this.items.length} matches · ${this.filtered.length} shown` : ` ${this.items.length} bookmarks · ${this.filtered.length} shown`, ), ), ); lines.push( row( this.noteEdit ? theme.fg("accent", "✎ note: ") + (this.noteEdit.input.render(innerWidth - 8)[0] ?? "") : (this.input.render(innerWidth)[0] ?? ""), ), ); const counts = this.sourceCounts(); // Without a query (or a consult ranking) there is nothing to be relevant to. const sortLabel = this.relatedBase ? "by relation" : this.sortMode === "relevance" && !this.hasQuery() && !this.ranked ? "newest" : this.sortMode; const chip = (label: string, value: SourceFilter, count: number): string => { const text = ` ${label} ${count} `; return this.sourceFilter === value ? theme.bg("selectedBg", theme.fg("accent", text)) : theme.fg("dim", text); }; const toggle = (label: string, on: boolean): string => on ? theme.bg("selectedBg", theme.fg("accent", ` ${label} `)) : theme.fg("dim", ` ${label} `); lines.push( row( chip("All", "all", counts.all) + chip("GitHub", "github", counts.github) + chip("X", "x", counts.x) + chip("Browsers", "browsers", counts.browsers) + toggle("hide archived", this.hideArchived) + toggle(`recent ${RECENT_DAYS}d`, this.recentOnly) + (this.topic ? toggle(`#${this.topic}`, true) : "") + theme.fg("dim", ` sort:${sortLabel} `) + theme.fg("dim", this.notice ?? "? help"), ), ); lines.push(theme.fg("border", `├${"─".repeat(width - 2)}┤`)); const reading = this.reading; const listRows = reading ? 0 : LIST_ROWS; const maxStart = Math.max(0, this.filtered.length - LIST_ROWS); const start = Math.max(0, Math.min(this.selected - Math.floor(LIST_ROWS / 2), maxStart)); // Numbers are absolute positions in the current view, so they stay stable while scrolling. const ordinalWidth = Math.max(2, String(this.filtered.length).length); // Swap the date column for the star count while sorting by it, so the order is legible. const showStars = this.sortMode === "stars"; for (let offset = 0; offset < listRows; offset += 1) { const index = start + offset; const bookmark = this.filtered[index]; if (!bookmark) { lines.push( row(index === 0 && this.filtered.length === 0 ? theme.fg("warning", "No bookmarks match.") : ""), ); continue; } const marker = index === this.selected ? theme.fg("accent", "▸") : " "; const ordinal = String(index + 1).padStart(ordinalWidth); const date = savedOn(bookmark).slice(0, 10) || "----------"; const leading = showStars ? formatStars(bookmark.stars).padEnd(10) : date; const author = truncateToWidth((bookmark.author ?? "").replace(/^@/, ""), 14, "…"); // A note is the strongest signal recall has, so it is marked and coloured to // stand out from the media badges. const media = [ bookmark.photos ? `📷${bookmark.photos}` : "", bookmark.videos ? `🎬${bookmark.videos}` : "", ] .filter(Boolean) .join(" "); const badge = (media ? theme.fg("dim", ` ${media}`) : "") + (this.marked.has(bookmark.id) ? theme.fg("accent", " ●") : "") + (bookmark.context ? theme.fg("accent", " 📝") : ""); const title = truncateToWidth( bookmark.title.replace(/\s+/g, " "), Math.max(10, innerWidth - 44 - ordinalWidth), "…", ); const text = `${marker} ${ordinal} ${leading} ${author.padEnd(14)} ${title}${badge}`; lines.push( row( index === this.selected ? theme.bg("selectedBg", theme.fg("text", text)) : theme.fg("muted", text), ), ); } if (!reading) lines.push(theme.fg("border", `├${"─".repeat(width - 2)}┤`)); const bookmark = this.selectedBookmark(); const detail = bookmark ? this.detail(bookmark) : null; const note = bookmark?.context ? `📝 ${bookmark.context}` : ""; const body = (detail?.content ?? bookmark?.description ?? "").replace(/\s+/g, " ").trim(); const fullText = [note, body].filter(Boolean).join(" · ").slice(0, 20000); const excerptLines = wrap(fullText, innerWidth, 600); // render() owns the wrap, so it records the total for handleReadingInput to clamp against. this.previewTotal = excerptLines.length; const excerptRows = reading ? READING_ROWS : EXCERPT_ROWS; const maxOffset = Math.max(0, excerptLines.length - excerptRows); const offset = reading && this.preview && bookmark && this.preview.id === bookmark.id ? Math.min(this.preview.offset, maxOffset) : 0; lines.push( row(bookmark ? theme.fg("accent", theme.bold(truncateToWidth(bookmark.title, innerWidth, "…"))) : ""), ); lines.push(row(bookmark ? theme.fg("mdLinkUrl", bookmark.url) : "")); const meta = bookmark ? [ bookmark.author, (bookmark.savedAt ?? "").slice(0, 10), bookmark.type, `${bookmark.photos} photos · ${bookmark.videos} videos`, ] .filter(Boolean) .join(" · ") : ""; lines.push(row(theme.fg("dim", meta))); const why = bookmark ? (this.reasons.get(bookmark.id) ?? []) : []; lines.push(row(why.length > 0 ? theme.fg("accent", `why: ${why.join(" · ")}`) : "")); lines.push( row( reading ? theme.fg( "dim", `${offset + 1}-${Math.min(offset + excerptRows, this.previewTotal)} of ${this.previewTotal} lines · ↑↓ scroll · esc back`, ) : theme.fg("dim", excerptLines.length > EXCERPT_ROWS ? "ctrl+e to read the full text" : ""), ), ); for (let index = 0; index < excerptRows; index += 1) { lines.push(row(theme.fg("text", excerptLines[offset + index] ?? ""))); } lines.push(row("")); // The image stays visible while reading: the rows the list gave up go to // text, and the rows the image gave up stay with the image. const imageLines = bookmark ? (this.image(bookmark)?.render(innerWidth) ?? []) : []; for (let index = 0; index < IMAGE_ROWS; index += 1) { lines.push(row(imageLines[index] ?? "")); } lines.push(theme.fg("border", `└${"─".repeat(width - 2)}┘`)); lines.push( theme.fg( "dim", this.noteEdit ? " enter save note · esc cancel" : reading ? " ↑↓ scroll · enter insert · esc back · ? help" : this.marked.size > 0 ? ` ${this.marked.size} marked · enter insert all · ctrl+x unmark · esc close` : " ↑↓ move · enter insert · ctrl+e read · ctrl+n note · ctrl+l related · esc close", ), ); return lines; } invalidate(): void { this.input.invalidate(); this.images.clear(); } } export default function (pi: ExtensionAPI) { pi.registerCommand("bookmarks", { description: "Search your GitHub stars, X posts, and browser bookmarks", handler: async (args: string, ctx: ExtensionCommandContext) => { if (ctx.mode !== "tui") { if (ctx.hasUI) ctx.ui.notify("/bookmarks requires interactive TUI mode", "warning"); return; } if (!existsSync(DB_PATH)) { ctx.ui.notify(`Bookmark Atlas database not found at ${DB_PATH}`, "error"); return; } const items = loadBookmarks(); if (items.length === 0) { ctx.ui.notify("No bookmarks found in the Bookmark Atlas database.", "warning"); return; } const result = await ctx.ui.custom( (tui, theme, _keybindings, done) => new BookmarkPalette(items, theme, done, { initialQuery: args.trim(), requestRender: () => tui.requestRender(), }), { overlay: true, overlayOptions: { anchor: "center", width: "85%", maxHeight: "90%" } }, ); if (result.action !== "insert") return; const details = result.ids .map((id) => loadDetail(id)) .filter((detail): detail is BookmarkDetail => detail !== null); if (details.length === 0) return; ctx.ui.setEditorText(details.map(insertText).join("\n\n---\n\n")); ctx.ui.notify( details.length === 1 ? `Inserted bookmark #${details[0]?.id}` : `Inserted ${details.length} bookmarks`, "info", ); }, }); pi.registerCommand("consult", { description: "Find saved bookmarks that fit where this project is right now", handler: async (args: string, ctx: ExtensionCommandContext) => { if (ctx.mode !== "tui") { if (ctx.hasUI) ctx.ui.notify("/consult requires interactive TUI mode", "warning"); return; } if (!existsSync(DB_PATH)) { ctx.ui.notify(`Bookmark Atlas database not found at ${DB_PATH}`, "error"); return; } const focus = args.trim(); const result = spawnSync( process.execPath, [ CLI_PATH, "recall", "--stage", ...(focus ? [focus] : []), "--repo", ctx.cwd, "--limit", "20", ], { encoding: "utf8", maxBuffer: 32 * 1024 * 1024 }, ); if (result.status !== 0 || !result.stdout) { ctx.ui.notify(result.stderr?.trim() || "Consult failed", "error"); return; } let hits: RecallHit[]; try { hits = JSON.parse(result.stdout) as RecallHit[]; } catch { ctx.ui.notify("Consult returned invalid data", "error"); return; } if (hits.length === 0) { ctx.ui.notify("Nothing in your bookmarks fits this stage yet.", "info"); return; } const rankedIds = hits.map((hit) => hit.id); const reasons = new Map(hits.map((hit) => [hit.id, hit.whyMatched])); const chosen = await ctx.ui.custom( (tui, theme, _keybindings, done) => new BookmarkPalette(loadBookmarks(), theme, done, { rankedIds, reasons, requestRender: () => tui.requestRender(), }), { overlay: true, overlayOptions: { anchor: "center", width: "85%", maxHeight: "90%" } }, ); if (chosen.action !== "insert") return; const chosenDetails = chosen.ids .map((id) => loadDetail(id)) .filter((detail): detail is BookmarkDetail => detail !== null); if (chosenDetails.length === 0) return; ctx.ui.setEditorText(chosenDetails.map(insertText).join("\n\n---\n\n")); ctx.ui.notify( chosenDetails.length === 1 ? `Consult: inserted bookmark #${chosenDetails[0]?.id}` : `Consult: inserted ${chosenDetails.length} bookmarks`, "info", ); }, }); }