Skip to content

Your lightweight open-source personal agent.

fix(tui): keep picker overflow choices reachable

chengyongru740e39b1ca42
diff --git a/tui/src/app.test.ts b/tui/src/app.test.ts
index da0491393..acb6a6dc8 100644
--- a/tui/src/app.test.ts
+++ b/tui/src/app.test.ts
@@ -1455,6 +1455,70 @@ describe("NanobotTui layout", () => {
}
})
+ test("selects model presets beyond the runtime menu's visible limit", async () => {
+ const original = globalThis.fetch
+ const sent: string[] = []
+ globalThis.fetch = (async (input: string | URL | Request) => {
+ const url = String(input)
+ if (url.endsWith("/api/settings")) {
+ return new Response(JSON.stringify({
+ model_presets: [
+ { name: "default", model: "test/model" },
+ ...Array.from({ length: 10 }, (_, index) => ({
+ name: `preset-${index}`,
+ model: `test/model-${index}`,
+ })),
+ ],
+ }))
+ }
+ if (url.endsWith("/api/workspaces")) {
+ return new Response(JSON.stringify({ controls: { can_use_full_access: true } }))
+ }
+ return new Response(JSON.stringify({ sessions: [] }))
+ }) as typeof fetch
+ setup = await createRenderer({ width: 96, height: 24, screenMode: "alternate-screen" })
+ const app = NanobotTui.mount(
+ setup.renderer,
+ { ...options, apiUrl: "http://nanobot.test", apiToken: "secret" },
+ client(sent),
+ new MockTreeSitterClient({ autoResolveTimeout: 0 }),
+ )
+ app.accept({ event: "attached", chat_id: "chat" })
+ const ui = app as unknown as {
+ ready: boolean
+ runtimeControls: {
+ modelText: TextRenderable
+ visible: boolean
+ menuRoot: { getChildren(): unknown[] }
+ }
+ }
+
+ try {
+ await waitUntil(() => ui.ready)
+ await setup.renderOnce()
+ await setup.mockMouse.click(
+ ui.runtimeControls.modelText.x + 2,
+ ui.runtimeControls.modelText.y,
+ )
+ await waitUntil(() => ui.runtimeControls.visible)
+ await setup.flush()
+ expect(ui.runtimeControls.menuRoot.getChildren()).toHaveLength(9)
+ expect(setup.captureCharFrame()).toContain("1–8 of 11 · ↓")
+ expect(setup.captureCharFrame()).not.toContain("preset-8")
+
+ for (let index = 0; index < 9; index += 1) setup.mockInput.pressArrow("down")
+ await setup.flush()
+ expect(setup.captureCharFrame()).toContain("› preset-8")
+ expect(setup.captureCharFrame()).toContain("3–10 of 11 · ↑↓")
+
+ setup.mockInput.pressEnter()
+ await waitUntil(() => sent.includes("/model preset-8"))
+ expect(ui.runtimeControls.visible).toBe(false)
+ } finally {
+ globalThis.fetch = original
+ }
+ })
+
test("switches sessions only through the sessions command", async () => {
const original = globalThis.fetch
globalThis.fetch = ((input: string | URL | Request) => {
diff --git a/tui/src/picker-menu.test.ts b/tui/src/picker-menu.test.ts
new file mode 100644
index 000000000..d3fafb5be
--- /dev/null
+++ b/tui/src/picker-menu.test.ts
@@ -0,0 +1,99 @@
+import { afterEach, describe, expect, test } from "bun:test"
+import { TextRenderable } from "@opentui/core"
+import { createTestRenderer, type TestRendererSetup } from "@opentui/core/testing"
+
+import { PickerMenu } from "./picker-menu"
+
+interface Choice {
+ id: string
+ label: string
+}
+
+const theme = {
+ text: "#FFFFFF",
+ muted: "#999999",
+ border: "#555555",
+}
+
+function choices(...ids: string[]): Choice[] {
+ return ids.map((id) => ({ id, label: `Choice ${id}` }))
+}
+
+describe("PickerMenu", () => {
+ let setup: TestRendererSetup | undefined
+
+ afterEach(() => {
+ if (setup && !setup.renderer.isDestroyed) setup.renderer.destroy()
+ setup = undefined
+ })
+
+ test("keeps overflow choices reachable inside a bounded visible window", async () => {
+ setup = await createTestRenderer({ width: 60, height: 14, screenMode: "alternate-screen" })
+ let picked = ""
+ const menu = new PickerMenu<Choice>(setup.renderer, theme, {
+ id: "test-picker",
+ key: (choice) => choice.id,
+ searchText: (choice) => choice.label,
+ render: (choice) => choice.label,
+ onSelect: (choice) => { picked = choice.id },
+ })
+ setup.renderer.root.add(menu.root)
+ menu.show(choices("0", "1", "2", "3", "4"), "", 3)
+ await setup.renderOnce()
+
+ expect(menu.root.getChildren().filter((child) => child.id !== "test-picker-overflow")).toHaveLength(3)
+ expect(setup.captureCharFrame()).toContain("1–3 of 5 · ↓")
+ expect(setup.captureCharFrame()).not.toContain("Choice 3")
+
+ menu.move(1)
+ menu.move(1)
+ menu.move(1)
+ await setup.flush()
+ expect(menu.current()?.id).toBe("3")
+ expect(setup.captureCharFrame()).toContain("2–4 of 5 · ↑↓")
+ expect(setup.captureCharFrame()).toContain("› Choice 3")
+ expect(setup.captureCharFrame()).not.toContain("Choice 0")
+
+ menu.move(1)
+ menu.move(1)
+ expect(menu.current()?.id).toBe("0")
+ menu.move(-1)
+ await setup.flush()
+ expect(menu.current()?.id).toBe("4")
+ expect(setup.captureCharFrame()).toContain("3–5 of 5 · ↑")
+
+ const firstVisible = menu.root.getChildren()[0] as TextRenderable | undefined
+ if (!firstVisible) throw new Error("picker row was not rendered")
+ await setup.mockMouse.click(firstVisible.x + 2, firstVisible.y)
+ expect(picked).toBe("2")
+ expect(menu.current()?.id).toBe("2")
+ })
+
+ test("keeps preserved and filtered selections inside the visible window", async () => {
+ setup = await createTestRenderer({ width: 60, height: 14, screenMode: "alternate-screen" })
+ const menu = new PickerMenu<Choice>(setup.renderer, theme, {
+ id: "test-picker",
+ key: (choice) => choice.id,
+ searchText: (choice) => choice.id,
+ render: (choice) => choice.label,
+ })
+ setup.renderer.root.add(menu.root)
+ menu.show(choices("a", "b", "c", "d", "e"), "", 2)
+ menu.move(1)
+ menu.move(1)
+ menu.move(1)
+ expect(menu.current()?.id).toBe("d")
+
+ menu.replace(choices("e", "d", "a"))
+ await setup.renderOnce()
+ expect(menu.current()?.id).toBe("d")
+ expect(setup.captureCharFrame()).toContain("› Choice d")
+ expect(setup.captureCharFrame()).toContain("2–3 of 3 · ↑")
+
+ menu.update("e", 2)
+ await setup.flush()
+ expect(menu.current()?.id).toBe("e")
+ expect(setup.captureCharFrame()).toContain("› Choice e")
+ expect(menu.root.getChildren()).toHaveLength(1)
+ })
+})
diff --git a/tui/src/picker-menu.ts b/tui/src/picker-menu.ts
index f06e5359b..840fdb1f9 100644
--- a/tui/src/picker-menu.ts
+++ b/tui/src/picker-menu.ts
@@ -35,6 +35,7 @@ export class PickerMenu<T> {
private items: T[] = []
private matches: T[] = []
private selected = 0
+ private windowStart = 0
private query = ""
private limit = 6
@@ -93,14 +94,13 @@ export class PickerMenu<T> {
this.query = query
this.limit = Math.max(1, limit)
const words = query.trim().toLocaleLowerCase().split(/\s+/u).filter(Boolean)
- this.matches = this.items
- .filter((item) => {
- const haystack = this.options.searchText(item).toLocaleLowerCase()
- return words.every((word) => haystack.includes(word))
- })
- .slice(0, this.limit)
+ this.matches = this.items.filter((item) => {
+ const haystack = this.options.searchText(item).toLocaleLowerCase()
+ return words.every((word) => haystack.includes(word))
+ })
if (changed) {
this.selected = 0
+ this.windowStart = 0
} else {
const preserved = previous === undefined
? -1
@@ -111,12 +111,14 @@ export class PickerMenu<T> {
? preserved
: Math.min(this.selected, Math.max(0, this.matches.length - 1))
}
+ this.keepSelectionVisible()
this.render()
}
move(direction: -1 | 1): boolean {
if (!this.visible || this.matches.length < 2) return false
this.selected = (this.selected + direction + this.matches.length) % this.matches.length
+ this.keepSelectionVisible()
this.render()
return true
}
@@ -129,6 +131,7 @@ export class PickerMenu<T> {
this.items = []
this.matches = []
this.selected = 0
+ this.windowStart = 0
this.query = ""
this.root.visible = false
this.clear()
@@ -153,7 +156,9 @@ export class PickerMenu<T> {
}))
return
}
- for (const [index, item] of this.matches.entries()) {
+ const visibleMatches = this.matches.slice(this.windowStart, this.windowStart + this.limit)
+ for (const [visibleIndex, item] of visibleMatches.entries()) {
+ const index = this.windowStart + visibleIndex
const selected = index === this.selected
const rendered = this.options.render(item, selected)
const content = typeof rendered === "string"
@@ -189,6 +194,34 @@ export class PickerMenu<T> {
},
}))
}
+ if (visibleMatches.length < this.matches.length) {
+ const end = this.windowStart + visibleMatches.length
+ const directions = `${this.windowStart > 0 ? "↑" : ""}${end < this.matches.length ? "↓" : ""}`
+ this.root.add(new TextRenderable(this.renderer, {
+ id: `${this.options.id}-overflow`,
+ content: ` ${this.windowStart + 1}–${end} of ${this.matches.length} · ${directions}`,
+ width: "100%",
+ height: 1,
+ wrapMode: "none",
+ fg: this.theme.muted,
+ selectable: false,
+ }))
+ }
+ }
+
+ private keepSelectionVisible(): void {
+ if (this.matches.length === 0) {
+ this.selected = 0
+ this.windowStart = 0
+ return
+ }
+ const maxStart = Math.max(0, this.matches.length - this.limit)
+ if (this.selected < this.windowStart) {
+ this.windowStart = this.selected
+ } else if (this.selected >= this.windowStart + this.limit) {
+ this.windowStart = this.selected - this.limit + 1
+ }
+ this.windowStart = Math.min(Math.max(0, this.windowStart), maxStart)
}
private clear(): void {