feat: use keybindings in all selectors and lists

Update all selector/list components to use EditorKeybindingsManager:
- model-selector, session-selector, oauth-selector, user-message-selector
- hook-selector, hook-input, hook-editor, tree-selector
- select-list, settings-list, cancellable-loader

This allows users to configure selectUp, selectDown, selectConfirm,
selectCancel actions in keybindings.json
This commit is contained in:
Helmut Januschka 2026-01-03 02:07:28 +01:00
parent 8f2682578b
commit 7574fed2f2
11 changed files with 73 additions and 64 deletions

View file

@ -7,7 +7,7 @@ import { spawnSync } from "node:child_process";
import * as fs from "node:fs"; import * as fs from "node:fs";
import * as os from "node:os"; import * as os from "node:os";
import * as path from "node:path"; import * as path from "node:path";
import { Container, Editor, matchesKey, Spacer, Text, type TUI } from "@mariozechner/pi-tui"; import { Container, Editor, getEditorKeybindings, matchesKey, Spacer, Text, type TUI } from "@mariozechner/pi-tui";
import { getEditorTheme, theme } from "../theme/theme.js"; import { getEditorTheme, theme } from "../theme/theme.js";
import { DynamicBorder } from "./dynamic-border.js"; import { DynamicBorder } from "./dynamic-border.js";
@ -67,13 +67,14 @@ export class HookEditorComponent extends Container {
return; return;
} }
const kb = getEditorKeybindings();
// Escape or Ctrl+C to cancel // Escape or Ctrl+C to cancel
if (matchesKey(keyData, "escape") || matchesKey(keyData, "ctrl+c")) { if (kb.matches(keyData, "selectCancel")) {
this.onCancelCallback(); this.onCancelCallback();
return; return;
} }
// Ctrl+G for external editor // Ctrl+G for external editor (keep matchesKey for this app-specific action)
if (matchesKey(keyData, "ctrl+g")) { if (matchesKey(keyData, "ctrl+g")) {
this.openExternalEditor(); this.openExternalEditor();
return; return;

View file

@ -2,7 +2,7 @@
* Simple text input component for hooks. * Simple text input component for hooks.
*/ */
import { Container, Input, matchesKey, Spacer, Text } from "@mariozechner/pi-tui"; import { Container, getEditorKeybindings, Input, Spacer, Text } from "@mariozechner/pi-tui";
import { theme } from "../theme/theme.js"; import { theme } from "../theme/theme.js";
import { DynamicBorder } from "./dynamic-border.js"; import { DynamicBorder } from "./dynamic-border.js";
@ -46,14 +46,15 @@ export class HookInputComponent extends Container {
} }
handleInput(keyData: string): void { handleInput(keyData: string): void {
const kb = getEditorKeybindings();
// Enter // Enter
if (matchesKey(keyData, "enter") || keyData === "\n") { if (kb.matches(keyData, "selectConfirm") || keyData === "\n") {
this.onSubmitCallback(this.input.getValue()); this.onSubmitCallback(this.input.getValue());
return; return;
} }
// Escape or Ctrl+C to cancel // Escape or Ctrl+C to cancel
if (matchesKey(keyData, "escape") || matchesKey(keyData, "ctrl+c")) { if (kb.matches(keyData, "selectCancel")) {
this.onCancelCallback(); this.onCancelCallback();
return; return;
} }

View file

@ -3,7 +3,7 @@
* Displays a list of string options with keyboard navigation. * Displays a list of string options with keyboard navigation.
*/ */
import { Container, matchesKey, Spacer, Text } from "@mariozechner/pi-tui"; import { Container, getEditorKeybindings, Spacer, Text } from "@mariozechner/pi-tui";
import { theme } from "../theme/theme.js"; import { theme } from "../theme/theme.js";
import { DynamicBorder } from "./dynamic-border.js"; import { DynamicBorder } from "./dynamic-border.js";
@ -66,25 +66,26 @@ export class HookSelectorComponent extends Container {
} }
handleInput(keyData: string): void { handleInput(keyData: string): void {
const kb = getEditorKeybindings();
// Up arrow or k // Up arrow or k
if (matchesKey(keyData, "up") || keyData === "k") { if (kb.matches(keyData, "selectUp") || keyData === "k") {
this.selectedIndex = Math.max(0, this.selectedIndex - 1); this.selectedIndex = Math.max(0, this.selectedIndex - 1);
this.updateList(); this.updateList();
} }
// Down arrow or j // Down arrow or j
else if (matchesKey(keyData, "down") || keyData === "j") { else if (kb.matches(keyData, "selectDown") || keyData === "j") {
this.selectedIndex = Math.min(this.options.length - 1, this.selectedIndex + 1); this.selectedIndex = Math.min(this.options.length - 1, this.selectedIndex + 1);
this.updateList(); this.updateList();
} }
// Enter // Enter
else if (matchesKey(keyData, "enter") || keyData === "\n") { else if (kb.matches(keyData, "selectConfirm") || keyData === "\n") {
const selected = this.options[this.selectedIndex]; const selected = this.options[this.selectedIndex];
if (selected) { if (selected) {
this.onSelectCallback(selected); this.onSelectCallback(selected);
} }
} }
// Escape or Ctrl+C // Escape or Ctrl+C
else if (matchesKey(keyData, "escape") || matchesKey(keyData, "ctrl+c")) { else if (kb.matches(keyData, "selectCancel")) {
this.onCancelCallback(); this.onCancelCallback();
} }
} }

View file

@ -1,5 +1,5 @@
import { type Model, modelsAreEqual } from "@mariozechner/pi-ai"; import { type Model, modelsAreEqual } from "@mariozechner/pi-ai";
import { Container, Input, matchesKey, Spacer, Text, type TUI } from "@mariozechner/pi-tui"; import { Container, getEditorKeybindings, Input, Spacer, Text, type TUI } from "@mariozechner/pi-tui";
import type { ModelRegistry } from "../../../core/model-registry.js"; import type { ModelRegistry } from "../../../core/model-registry.js";
import type { SettingsManager } from "../../../core/settings-manager.js"; import type { SettingsManager } from "../../../core/settings-manager.js";
import { fuzzyFilter } from "../../../utils/fuzzy.js"; import { fuzzyFilter } from "../../../utils/fuzzy.js";
@ -205,27 +205,28 @@ export class ModelSelectorComponent extends Container {
} }
handleInput(keyData: string): void { handleInput(keyData: string): void {
const kb = getEditorKeybindings();
// Up arrow - wrap to bottom when at top // Up arrow - wrap to bottom when at top
if (matchesKey(keyData, "up")) { if (kb.matches(keyData, "selectUp")) {
if (this.filteredModels.length === 0) return; if (this.filteredModels.length === 0) return;
this.selectedIndex = this.selectedIndex === 0 ? this.filteredModels.length - 1 : this.selectedIndex - 1; this.selectedIndex = this.selectedIndex === 0 ? this.filteredModels.length - 1 : this.selectedIndex - 1;
this.updateList(); this.updateList();
} }
// Down arrow - wrap to top when at bottom // Down arrow - wrap to top when at bottom
else if (matchesKey(keyData, "down")) { else if (kb.matches(keyData, "selectDown")) {
if (this.filteredModels.length === 0) return; if (this.filteredModels.length === 0) return;
this.selectedIndex = this.selectedIndex === this.filteredModels.length - 1 ? 0 : this.selectedIndex + 1; this.selectedIndex = this.selectedIndex === this.filteredModels.length - 1 ? 0 : this.selectedIndex + 1;
this.updateList(); this.updateList();
} }
// Enter // Enter
else if (matchesKey(keyData, "enter")) { else if (kb.matches(keyData, "selectConfirm")) {
const selectedModel = this.filteredModels[this.selectedIndex]; const selectedModel = this.filteredModels[this.selectedIndex];
if (selectedModel) { if (selectedModel) {
this.handleSelect(selectedModel.model); this.handleSelect(selectedModel.model);
} }
} }
// Escape or Ctrl+C // Escape or Ctrl+C
else if (matchesKey(keyData, "escape") || matchesKey(keyData, "ctrl+c")) { else if (kb.matches(keyData, "selectCancel")) {
this.onCancelCallback(); this.onCancelCallback();
} }
// Pass everything else to search input // Pass everything else to search input

View file

@ -1,5 +1,5 @@
import { getOAuthProviders, type OAuthProviderInfo } from "@mariozechner/pi-ai"; import { getOAuthProviders, type OAuthProviderInfo } from "@mariozechner/pi-ai";
import { Container, matchesKey, Spacer, TruncatedText } from "@mariozechner/pi-tui"; import { Container, getEditorKeybindings, Spacer, TruncatedText } from "@mariozechner/pi-tui";
import type { AuthStorage } from "../../../core/auth-storage.js"; import type { AuthStorage } from "../../../core/auth-storage.js";
import { theme } from "../theme/theme.js"; import { theme } from "../theme/theme.js";
import { DynamicBorder } from "./dynamic-border.js"; import { DynamicBorder } from "./dynamic-border.js";
@ -95,25 +95,26 @@ export class OAuthSelectorComponent extends Container {
} }
handleInput(keyData: string): void { handleInput(keyData: string): void {
const kb = getEditorKeybindings();
// Up arrow // Up arrow
if (matchesKey(keyData, "up")) { if (kb.matches(keyData, "selectUp")) {
this.selectedIndex = Math.max(0, this.selectedIndex - 1); this.selectedIndex = Math.max(0, this.selectedIndex - 1);
this.updateList(); this.updateList();
} }
// Down arrow // Down arrow
else if (matchesKey(keyData, "down")) { else if (kb.matches(keyData, "selectDown")) {
this.selectedIndex = Math.min(this.allProviders.length - 1, this.selectedIndex + 1); this.selectedIndex = Math.min(this.allProviders.length - 1, this.selectedIndex + 1);
this.updateList(); this.updateList();
} }
// Enter // Enter
else if (matchesKey(keyData, "enter")) { else if (kb.matches(keyData, "selectConfirm")) {
const selectedProvider = this.allProviders[this.selectedIndex]; const selectedProvider = this.allProviders[this.selectedIndex];
if (selectedProvider?.available) { if (selectedProvider?.available) {
this.onSelectCallback(selectedProvider.id); this.onSelectCallback(selectedProvider.id);
} }
} }
// Escape or Ctrl+C // Escape or Ctrl+C
else if (matchesKey(keyData, "escape") || matchesKey(keyData, "ctrl+c")) { else if (kb.matches(keyData, "selectCancel")) {
this.onCancelCallback(); this.onCancelCallback();
} }
} }

View file

@ -1,4 +1,12 @@
import { type Component, Container, Input, matchesKey, Spacer, Text, truncateToWidth } from "@mariozechner/pi-tui"; import {
type Component,
Container,
getEditorKeybindings,
Input,
Spacer,
Text,
truncateToWidth,
} from "@mariozechner/pi-tui";
import type { SessionInfo } from "../../../core/session-manager.js"; import type { SessionInfo } from "../../../core/session-manager.js";
import { fuzzyFilter } from "../../../utils/fuzzy.js"; import { fuzzyFilter } from "../../../utils/fuzzy.js";
import { theme } from "../theme/theme.js"; import { theme } from "../theme/theme.js";
@ -114,31 +122,28 @@ class SessionList implements Component {
} }
handleInput(keyData: string): void { handleInput(keyData: string): void {
const kb = getEditorKeybindings();
// Up arrow // Up arrow
if (matchesKey(keyData, "up")) { if (kb.matches(keyData, "selectUp")) {
this.selectedIndex = Math.max(0, this.selectedIndex - 1); this.selectedIndex = Math.max(0, this.selectedIndex - 1);
} }
// Down arrow // Down arrow
else if (matchesKey(keyData, "down")) { else if (kb.matches(keyData, "selectDown")) {
this.selectedIndex = Math.min(this.filteredSessions.length - 1, this.selectedIndex + 1); this.selectedIndex = Math.min(this.filteredSessions.length - 1, this.selectedIndex + 1);
} }
// Enter // Enter
else if (matchesKey(keyData, "enter")) { else if (kb.matches(keyData, "selectConfirm")) {
const selected = this.filteredSessions[this.selectedIndex]; const selected = this.filteredSessions[this.selectedIndex];
if (selected && this.onSelect) { if (selected && this.onSelect) {
this.onSelect(selected.path); this.onSelect(selected.path);
} }
} }
// Escape - cancel // Escape - cancel
else if (matchesKey(keyData, "escape")) { else if (kb.matches(keyData, "selectCancel")) {
if (this.onCancel) { if (this.onCancel) {
this.onCancel(); this.onCancel();
} }
} }
// Ctrl+C - exit
else if (matchesKey(keyData, "ctrl+c")) {
this.onExit();
}
// Pass everything else to search input // Pass everything else to search input
else { else {
this.searchInput.handleInput(keyData); this.searchInput.handleInput(keyData);

View file

@ -1,6 +1,7 @@
import { import {
type Component, type Component,
Container, Container,
getEditorKeybindings,
Input, Input,
matchesKey, matchesKey,
Spacer, Spacer,
@ -655,30 +656,29 @@ class TreeList implements Component {
} }
handleInput(keyData: string): void { handleInput(keyData: string): void {
if (matchesKey(keyData, "up")) { const kb = getEditorKeybindings();
if (kb.matches(keyData, "selectUp")) {
this.selectedIndex = this.selectedIndex === 0 ? this.filteredNodes.length - 1 : this.selectedIndex - 1; this.selectedIndex = this.selectedIndex === 0 ? this.filteredNodes.length - 1 : this.selectedIndex - 1;
} else if (matchesKey(keyData, "down")) { } else if (kb.matches(keyData, "selectDown")) {
this.selectedIndex = this.selectedIndex === this.filteredNodes.length - 1 ? 0 : this.selectedIndex + 1; this.selectedIndex = this.selectedIndex === this.filteredNodes.length - 1 ? 0 : this.selectedIndex + 1;
} else if (matchesKey(keyData, "left")) { } else if (kb.matches(keyData, "cursorLeft")) {
// Page up // Page up
this.selectedIndex = Math.max(0, this.selectedIndex - this.maxVisibleLines); this.selectedIndex = Math.max(0, this.selectedIndex - this.maxVisibleLines);
} else if (matchesKey(keyData, "right")) { } else if (kb.matches(keyData, "cursorRight")) {
// Page down // Page down
this.selectedIndex = Math.min(this.filteredNodes.length - 1, this.selectedIndex + this.maxVisibleLines); this.selectedIndex = Math.min(this.filteredNodes.length - 1, this.selectedIndex + this.maxVisibleLines);
} else if (matchesKey(keyData, "enter")) { } else if (kb.matches(keyData, "selectConfirm")) {
const selected = this.filteredNodes[this.selectedIndex]; const selected = this.filteredNodes[this.selectedIndex];
if (selected && this.onSelect) { if (selected && this.onSelect) {
this.onSelect(selected.node.entry.id); this.onSelect(selected.node.entry.id);
} }
} else if (matchesKey(keyData, "escape")) { } else if (kb.matches(keyData, "selectCancel")) {
if (this.searchQuery) { if (this.searchQuery) {
this.searchQuery = ""; this.searchQuery = "";
this.applyFilter(); this.applyFilter();
} else { } else {
this.onCancel?.(); this.onCancel?.();
} }
} else if (matchesKey(keyData, "ctrl+c")) {
this.onCancel?.();
} else if (matchesKey(keyData, "shift+ctrl+o")) { } else if (matchesKey(keyData, "shift+ctrl+o")) {
// Cycle filter backwards // Cycle filter backwards
const modes: FilterMode[] = ["default", "no-tools", "user-only", "labeled-only", "all"]; const modes: FilterMode[] = ["default", "no-tools", "user-only", "labeled-only", "all"];
@ -691,7 +691,7 @@ class TreeList implements Component {
const currentIndex = modes.indexOf(this.filterMode); const currentIndex = modes.indexOf(this.filterMode);
this.filterMode = modes[(currentIndex + 1) % modes.length]; this.filterMode = modes[(currentIndex + 1) % modes.length];
this.applyFilter(); this.applyFilter();
} else if (matchesKey(keyData, "backspace")) { } else if (kb.matches(keyData, "deleteCharBackward")) {
if (this.searchQuery.length > 0) { if (this.searchQuery.length > 0) {
this.searchQuery = this.searchQuery.slice(0, -1); this.searchQuery = this.searchQuery.slice(0, -1);
this.applyFilter(); this.applyFilter();
@ -759,10 +759,11 @@ class LabelInput implements Component {
} }
handleInput(keyData: string): void { handleInput(keyData: string): void {
if (matchesKey(keyData, "enter")) { const kb = getEditorKeybindings();
if (kb.matches(keyData, "selectConfirm")) {
const value = this.input.getValue().trim(); const value = this.input.getValue().trim();
this.onSubmit?.(this.entryId, value || undefined); this.onSubmit?.(this.entryId, value || undefined);
} else if (matchesKey(keyData, "escape")) { } else if (kb.matches(keyData, "selectCancel")) {
this.onCancel?.(); this.onCancel?.();
} else { } else {
this.input.handleInput(keyData); this.input.handleInput(keyData);

View file

@ -1,4 +1,4 @@
import { type Component, Container, matchesKey, Spacer, Text, truncateToWidth } from "@mariozechner/pi-tui"; import { type Component, Container, getEditorKeybindings, Spacer, Text, truncateToWidth } from "@mariozechner/pi-tui";
import { theme } from "../theme/theme.js"; import { theme } from "../theme/theme.js";
import { DynamicBorder } from "./dynamic-border.js"; import { DynamicBorder } from "./dynamic-border.js";
@ -78,29 +78,24 @@ class UserMessageList implements Component {
} }
handleInput(keyData: string): void { handleInput(keyData: string): void {
const kb = getEditorKeybindings();
// Up arrow - go to previous (older) message, wrap to bottom when at top // Up arrow - go to previous (older) message, wrap to bottom when at top
if (matchesKey(keyData, "up")) { if (kb.matches(keyData, "selectUp")) {
this.selectedIndex = this.selectedIndex === 0 ? this.messages.length - 1 : this.selectedIndex - 1; this.selectedIndex = this.selectedIndex === 0 ? this.messages.length - 1 : this.selectedIndex - 1;
} }
// Down arrow - go to next (newer) message, wrap to top when at bottom // Down arrow - go to next (newer) message, wrap to top when at bottom
else if (matchesKey(keyData, "down")) { else if (kb.matches(keyData, "selectDown")) {
this.selectedIndex = this.selectedIndex === this.messages.length - 1 ? 0 : this.selectedIndex + 1; this.selectedIndex = this.selectedIndex === this.messages.length - 1 ? 0 : this.selectedIndex + 1;
} }
// Enter - select message and branch // Enter - select message and branch
else if (matchesKey(keyData, "enter")) { else if (kb.matches(keyData, "selectConfirm")) {
const selected = this.messages[this.selectedIndex]; const selected = this.messages[this.selectedIndex];
if (selected && this.onSelect) { if (selected && this.onSelect) {
this.onSelect(selected.id); this.onSelect(selected.id);
} }
} }
// Escape - cancel // Escape - cancel
else if (matchesKey(keyData, "escape")) { else if (kb.matches(keyData, "selectCancel")) {
if (this.onCancel) {
this.onCancel();
}
}
// Ctrl+C - cancel
else if (matchesKey(keyData, "ctrl+c")) {
if (this.onCancel) { if (this.onCancel) {
this.onCancel(); this.onCancel();
} }

View file

@ -1,4 +1,4 @@
import { matchesKey } from "../keys.js"; import { getEditorKeybindings } from "../keybindings.js";
import { Loader } from "./loader.js"; import { Loader } from "./loader.js";
/** /**
@ -27,7 +27,8 @@ export class CancellableLoader extends Loader {
} }
handleInput(data: string): void { handleInput(data: string): void {
if (matchesKey(data, "escape")) { const kb = getEditorKeybindings();
if (kb.matches(data, "selectCancel")) {
this.abortController.abort(); this.abortController.abort();
this.onAbort?.(); this.onAbort?.();
} }

View file

@ -1,4 +1,4 @@
import { matchesKey } from "../keys.js"; import { getEditorKeybindings } from "../keybindings.js";
import type { Component } from "../tui.js"; import type { Component } from "../tui.js";
import { truncateToWidth } from "../utils.js"; import { truncateToWidth } from "../utils.js";
@ -145,25 +145,26 @@ export class SelectList implements Component {
} }
handleInput(keyData: string): void { handleInput(keyData: string): void {
const kb = getEditorKeybindings();
// Up arrow - wrap to bottom when at top // Up arrow - wrap to bottom when at top
if (matchesKey(keyData, "up")) { if (kb.matches(keyData, "selectUp")) {
this.selectedIndex = this.selectedIndex === 0 ? this.filteredItems.length - 1 : this.selectedIndex - 1; this.selectedIndex = this.selectedIndex === 0 ? this.filteredItems.length - 1 : this.selectedIndex - 1;
this.notifySelectionChange(); this.notifySelectionChange();
} }
// Down arrow - wrap to top when at bottom // Down arrow - wrap to top when at bottom
else if (matchesKey(keyData, "down")) { else if (kb.matches(keyData, "selectDown")) {
this.selectedIndex = this.selectedIndex === this.filteredItems.length - 1 ? 0 : this.selectedIndex + 1; this.selectedIndex = this.selectedIndex === this.filteredItems.length - 1 ? 0 : this.selectedIndex + 1;
this.notifySelectionChange(); this.notifySelectionChange();
} }
// Enter // Enter
else if (matchesKey(keyData, "enter")) { else if (kb.matches(keyData, "selectConfirm")) {
const selectedItem = this.filteredItems[this.selectedIndex]; const selectedItem = this.filteredItems[this.selectedIndex];
if (selectedItem && this.onSelect) { if (selectedItem && this.onSelect) {
this.onSelect(selectedItem); this.onSelect(selectedItem);
} }
} }
// Escape or Ctrl+C // Escape or Ctrl+C
else if (matchesKey(keyData, "escape") || matchesKey(keyData, "ctrl+c")) { else if (kb.matches(keyData, "selectCancel")) {
if (this.onCancel) { if (this.onCancel) {
this.onCancel(); this.onCancel();
} }

View file

@ -1,4 +1,4 @@
import { matchesKey } from "../keys.js"; import { getEditorKeybindings } from "../keybindings.js";
import type { Component } from "../tui.js"; import type { Component } from "../tui.js";
import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "../utils.js"; import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "../utils.js";
@ -145,13 +145,14 @@ export class SettingsList implements Component {
} }
// Main list input handling // Main list input handling
if (matchesKey(data, "up")) { const kb = getEditorKeybindings();
if (kb.matches(data, "selectUp")) {
this.selectedIndex = this.selectedIndex === 0 ? this.items.length - 1 : this.selectedIndex - 1; this.selectedIndex = this.selectedIndex === 0 ? this.items.length - 1 : this.selectedIndex - 1;
} else if (matchesKey(data, "down")) { } else if (kb.matches(data, "selectDown")) {
this.selectedIndex = this.selectedIndex === this.items.length - 1 ? 0 : this.selectedIndex + 1; this.selectedIndex = this.selectedIndex === this.items.length - 1 ? 0 : this.selectedIndex + 1;
} else if (matchesKey(data, "enter") || data === " ") { } else if (kb.matches(data, "selectConfirm") || data === " ") {
this.activateItem(); this.activateItem();
} else if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) { } else if (kb.matches(data, "selectCancel")) {
this.onCancel(); this.onCancel();
} }
} }