mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-17 06:04:51 +00:00
fix(tui): add isHome, isEnd, isDelete helpers for Kitty protocol
Extended the Kitty protocol parser to handle functional keys with ~ terminator (Delete, Insert, PageUp, PageDown) and Home/End keys. Updated editor.ts and input.ts to use the new helpers.
This commit is contained in:
parent
716516d61e
commit
cbd20b58a6
4 changed files with 84 additions and 7 deletions
|
|
@ -16,8 +16,11 @@ import {
|
||||||
isCtrlRight,
|
isCtrlRight,
|
||||||
isCtrlU,
|
isCtrlU,
|
||||||
isCtrlW,
|
isCtrlW,
|
||||||
|
isDelete,
|
||||||
|
isEnd,
|
||||||
isEnter,
|
isEnter,
|
||||||
isEscape,
|
isEscape,
|
||||||
|
isHome,
|
||||||
isShiftEnter,
|
isShiftEnter,
|
||||||
isTab,
|
isTab,
|
||||||
} from "../keys.js";
|
} from "../keys.js";
|
||||||
|
|
@ -459,16 +462,13 @@ export class Editor implements Component {
|
||||||
this.handleBackspace();
|
this.handleBackspace();
|
||||||
}
|
}
|
||||||
// Line navigation shortcuts (Home/End keys)
|
// Line navigation shortcuts (Home/End keys)
|
||||||
else if (data === "\x1b[H" || data === "\x1b[1~" || data === "\x1b[7~") {
|
else if (isHome(data)) {
|
||||||
// Home key
|
|
||||||
this.moveToLineStart();
|
this.moveToLineStart();
|
||||||
} else if (data === "\x1b[F" || data === "\x1b[4~" || data === "\x1b[8~") {
|
} else if (isEnd(data)) {
|
||||||
// End key
|
|
||||||
this.moveToLineEnd();
|
this.moveToLineEnd();
|
||||||
}
|
}
|
||||||
// Forward delete (Fn+Backspace or Delete key)
|
// Forward delete (Fn+Backspace or Delete key)
|
||||||
else if (data === "\x1b[3~") {
|
else if (isDelete(data)) {
|
||||||
// Delete key
|
|
||||||
this.handleForwardDelete();
|
this.handleForwardDelete();
|
||||||
}
|
}
|
||||||
// Word navigation (Option/Alt + Arrow or Ctrl + Arrow)
|
// Word navigation (Option/Alt + Arrow or Ctrl + Arrow)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import {
|
||||||
isCtrlK,
|
isCtrlK,
|
||||||
isCtrlU,
|
isCtrlU,
|
||||||
isCtrlW,
|
isCtrlW,
|
||||||
|
isDelete,
|
||||||
isEnter,
|
isEnter,
|
||||||
} from "../keys.js";
|
} from "../keys.js";
|
||||||
import type { Component } from "../tui.js";
|
import type { Component } from "../tui.js";
|
||||||
|
|
@ -118,7 +119,7 @@ export class Input implements Component {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data === "\x1b[3~") {
|
if (isDelete(data)) {
|
||||||
// Delete - delete grapheme at cursor (handles emojis, etc.)
|
// Delete - delete grapheme at cursor (handles emojis, etc.)
|
||||||
if (this.cursor < this.value.length) {
|
if (this.cursor < this.value.length) {
|
||||||
const afterCursor = this.value.slice(this.cursor);
|
const afterCursor = this.value.slice(this.cursor);
|
||||||
|
|
|
||||||
|
|
@ -41,8 +41,11 @@ export {
|
||||||
isCtrlT,
|
isCtrlT,
|
||||||
isCtrlU,
|
isCtrlU,
|
||||||
isCtrlW,
|
isCtrlW,
|
||||||
|
isDelete,
|
||||||
|
isEnd,
|
||||||
isEnter,
|
isEnter,
|
||||||
isEscape,
|
isEscape,
|
||||||
|
isHome,
|
||||||
isShiftEnter,
|
isShiftEnter,
|
||||||
isShiftTab,
|
isShiftTab,
|
||||||
isTab,
|
isTab,
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,16 @@ interface ParsedKittySequence {
|
||||||
*
|
*
|
||||||
* Returns null if not a valid Kitty sequence.
|
* Returns null if not a valid Kitty sequence.
|
||||||
*/
|
*/
|
||||||
|
// Virtual codepoints for functional keys (negative to avoid conflicts)
|
||||||
|
const FUNCTIONAL_CODEPOINTS = {
|
||||||
|
delete: -10,
|
||||||
|
insert: -11,
|
||||||
|
pageUp: -12,
|
||||||
|
pageDown: -13,
|
||||||
|
home: -14,
|
||||||
|
end: -15,
|
||||||
|
} as const;
|
||||||
|
|
||||||
function parseKittySequence(data: string): ParsedKittySequence | null {
|
function parseKittySequence(data: string): ParsedKittySequence | null {
|
||||||
// Match CSI u format: \x1b[<num>u or \x1b[<num>;<mod>u
|
// Match CSI u format: \x1b[<num>u or \x1b[<num>;<mod>u
|
||||||
const csiUMatch = data.match(/^\x1b\[(\d+)(?:;(\d+))?u$/);
|
const csiUMatch = data.match(/^\x1b\[(\d+)(?:;(\d+))?u$/);
|
||||||
|
|
@ -96,6 +106,35 @@ function parseKittySequence(data: string): ParsedKittySequence | null {
|
||||||
return { codepoint, modifier: modValue - 1 };
|
return { codepoint, modifier: modValue - 1 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Match functional keys with ~ terminator: \x1b[<num>~ or \x1b[<num>;<mod>~
|
||||||
|
// DELETE=3, INSERT=2, PAGEUP=5, PAGEDOWN=6, etc.
|
||||||
|
const funcMatch = data.match(/^\x1b\[(\d+)(?:;(\d+))?~$/);
|
||||||
|
if (funcMatch) {
|
||||||
|
const keyNum = parseInt(funcMatch[1]!, 10);
|
||||||
|
const modValue = funcMatch[2] ? parseInt(funcMatch[2], 10) : 1;
|
||||||
|
// Map functional key numbers to virtual codepoints
|
||||||
|
const funcCodes: Record<number, number> = {
|
||||||
|
2: FUNCTIONAL_CODEPOINTS.insert,
|
||||||
|
3: FUNCTIONAL_CODEPOINTS.delete,
|
||||||
|
5: FUNCTIONAL_CODEPOINTS.pageUp,
|
||||||
|
6: FUNCTIONAL_CODEPOINTS.pageDown,
|
||||||
|
7: FUNCTIONAL_CODEPOINTS.home, // Alternative home
|
||||||
|
8: FUNCTIONAL_CODEPOINTS.end, // Alternative end
|
||||||
|
};
|
||||||
|
const codepoint = funcCodes[keyNum];
|
||||||
|
if (codepoint !== undefined) {
|
||||||
|
return { codepoint, modifier: modValue - 1 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match Home/End with modifier: \x1b[1;<mod>H/F
|
||||||
|
const homeEndMatch = data.match(/^\x1b\[1;(\d+)([HF])$/);
|
||||||
|
if (homeEndMatch) {
|
||||||
|
const modValue = parseInt(homeEndMatch[1]!, 10);
|
||||||
|
const codepoint = homeEndMatch[2] === "H" ? FUNCTIONAL_CODEPOINTS.home : FUNCTIONAL_CODEPOINTS.end;
|
||||||
|
return { codepoint, modifier: modValue - 1 };
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -408,3 +447,37 @@ export function isCtrlLeft(data: string): boolean {
|
||||||
export function isCtrlRight(data: string): boolean {
|
export function isCtrlRight(data: string): boolean {
|
||||||
return data === "\x1b[1;5C" || matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.ctrl);
|
return data === "\x1b[1;5C" || matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.ctrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if input matches Home key.
|
||||||
|
* Handles legacy formats and Kitty protocol with lock key modifiers.
|
||||||
|
*/
|
||||||
|
export function isHome(data: string): boolean {
|
||||||
|
return (
|
||||||
|
data === "\x1b[H" ||
|
||||||
|
data === "\x1b[1~" ||
|
||||||
|
data === "\x1b[7~" ||
|
||||||
|
matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, 0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if input matches End key.
|
||||||
|
* Handles legacy formats and Kitty protocol with lock key modifiers.
|
||||||
|
*/
|
||||||
|
export function isEnd(data: string): boolean {
|
||||||
|
return (
|
||||||
|
data === "\x1b[F" ||
|
||||||
|
data === "\x1b[4~" ||
|
||||||
|
data === "\x1b[8~" ||
|
||||||
|
matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, 0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if input matches Delete key (forward delete).
|
||||||
|
* Handles legacy format and Kitty protocol with lock key modifiers.
|
||||||
|
*/
|
||||||
|
export function isDelete(data: string): boolean {
|
||||||
|
return data === "\x1b[3~" || matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, 0);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue