mirror of
https://github.com/earendil-works/pi.git
synced 2026-06-18 15:54:04 +08:00
All keybindings configurable via ~/.pi/agent/keybindings.json
Editor actions:
- cursorUp, cursorDown, cursorLeft, cursorRight
- cursorWordLeft, cursorWordRight, cursorLineStart, cursorLineEnd
- deleteCharBackward, deleteCharForward, deleteWordBackward
- deleteToLineStart, deleteToLineEnd
- newLine, submit, tab
- selectUp, selectDown, selectConfirm, selectCancel
App actions:
- interrupt, clear, exit, suspend
- cycleThinkingLevel, cycleModelForward, cycleModelBackward
- selectModel, expandTools, toggleThinking, externalEditor
Also adds support for numpad Enter key (Kitty protocol codepoint 57414
and SS3 M sequence)
Example emacs-style keybindings.json:
{
"cursorUp": ["up", "ctrl+p"],
"cursorDown": ["down", "ctrl+n"],
"cursorLeft": ["left", "ctrl+b"],
"cursorRight": ["right", "ctrl+f"],
"deleteCharForward": ["delete", "ctrl+d"],
"cycleModelForward": "ctrl+o"
}
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { Editor, type EditorTheme } from "@mariozechner/pi-tui";
|
|
import type { AppAction, KeybindingsManager } from "../../../core/keybindings.js";
|
|
|
|
/**
|
|
* Custom editor that handles app-level keybindings for coding-agent.
|
|
*/
|
|
export class CustomEditor extends Editor {
|
|
private keybindings: KeybindingsManager;
|
|
private actionHandlers: Map<AppAction, () => void> = new Map();
|
|
|
|
// Special handlers that can be dynamically replaced
|
|
public onEscape?: () => void;
|
|
public onCtrlD?: () => void;
|
|
|
|
constructor(theme: EditorTheme, keybindings: KeybindingsManager) {
|
|
super(theme);
|
|
this.keybindings = keybindings;
|
|
}
|
|
|
|
/**
|
|
* Register a handler for an app action.
|
|
*/
|
|
onAction(action: AppAction, handler: () => void): void {
|
|
this.actionHandlers.set(action, handler);
|
|
}
|
|
|
|
handleInput(data: string): void {
|
|
// Check app keybindings first
|
|
|
|
// Escape/interrupt - only if autocomplete is NOT active
|
|
if (this.keybindings.matches(data, "interrupt")) {
|
|
if (!this.isShowingAutocomplete()) {
|
|
// Use dynamic onEscape if set, otherwise registered handler
|
|
const handler = this.onEscape ?? this.actionHandlers.get("interrupt");
|
|
if (handler) {
|
|
handler();
|
|
return;
|
|
}
|
|
}
|
|
// Let parent handle escape for autocomplete cancellation
|
|
super.handleInput(data);
|
|
return;
|
|
}
|
|
|
|
// Exit (Ctrl+D) - only when editor is empty
|
|
if (this.keybindings.matches(data, "exit")) {
|
|
if (this.getText().length === 0) {
|
|
const handler = this.onCtrlD ?? this.actionHandlers.get("exit");
|
|
if (handler) handler();
|
|
}
|
|
return; // Always consume
|
|
}
|
|
|
|
// Check all other app actions
|
|
for (const [action, handler] of this.actionHandlers) {
|
|
if (action !== "interrupt" && action !== "exit" && this.keybindings.matches(data, action)) {
|
|
handler();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Pass to parent for editor handling
|
|
super.handleInput(data);
|
|
}
|
|
}
|