Skip to content

Commit 299df82

Browse files
committed
feat: createInput({ detection }) surfaces CapabilityEvents from scan()
1 parent c68b752 commit 299df82

3 files changed

Lines changed: 385 additions & 23 deletions

File tree

‎input.ts‎

Lines changed: 141 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
*/
88

99
import {
10+
CAP_BACKGROUND_COLOR,
11+
CAP_COLORDEPTH,
12+
CAP_CURSOR_COLOR,
13+
CAP_FOREGROUND_COLOR,
14+
CAP_KITTY_GRAPHICS,
15+
CAP_KITTY_KEYBOARD,
16+
CAP_POINTER_SHAPE,
17+
CAP_SYNC_OUTPUT,
18+
COLORDEPTH_TRUECOLOR,
1019
createInputNative,
20+
EVENT_CAPABILITY,
1121
EVENT_CURSOR,
1222
EVENT_KEY,
1323
EVENT_MOUSE,
@@ -77,7 +87,6 @@ import {
7787
KEY_SUPER_LEFT,
7888
KEY_SUPER_RIGHT,
7989
KEY_TAB,
80-
MAX_TERMINFO,
8190
MOD_ALT,
8291
MOD_CTRL,
8392
MOD_MOTION,
@@ -87,6 +96,8 @@ import {
8796
readEvent,
8897
SCAN_BUFFER_SIZE,
8998
} from "./input-native.ts";
99+
import type { Detection, Rgb } from "./terminfo.ts";
100+
import { rgbOf } from "./terminfo.ts";
90101

91102
/**
92103
* Modifier keys held during a key or mouse event.
@@ -371,6 +382,57 @@ export interface CursorEvent {
371382
column: number;
372383
}
373384

385+
/** Color depth tier reported by the terminal's XTGETTCAP probe response. */
386+
export type ColorDepth = "truecolor" | "256" | "16";
387+
388+
/**
389+
* A probe-response event emitted by scan() when the terminal answers one
390+
* of the capability queries in Detection.probe. Route to term.update().
391+
*/
392+
export type CapabilityEvent =
393+
| {
394+
readonly type: "capability";
395+
readonly key: "foreground-color";
396+
readonly value: Rgb;
397+
}
398+
| {
399+
readonly type: "capability";
400+
readonly key: "background-color";
401+
readonly value: Rgb;
402+
}
403+
| {
404+
readonly type: "capability";
405+
readonly key: "cursor-color";
406+
readonly value: Rgb;
407+
}
408+
| {
409+
readonly type: "capability";
410+
readonly key: "colordepth";
411+
readonly value: ColorDepth;
412+
}
413+
| {
414+
readonly type: "capability";
415+
readonly key: "sync-output";
416+
readonly value: boolean;
417+
}
418+
| {
419+
readonly type: "capability";
420+
readonly key: "kitty-keyboard";
421+
readonly value: boolean;
422+
}
423+
| {
424+
readonly type: "capability";
425+
readonly key: "kitty-graphics";
426+
readonly value: boolean;
427+
}
428+
| {
429+
readonly type: "capability";
430+
readonly key: "pointer-shape";
431+
readonly value: boolean;
432+
};
433+
434+
export type { Rgb };
435+
374436
import type { PointerEvent } from "./term.ts";
375437

376438
export type InputEvent =
@@ -381,6 +443,7 @@ export type InputEvent =
381443
| WheelEvent
382444
| ResizeEvent
383445
| CursorEvent
446+
| CapabilityEvent
384447
| PointerEvent;
385448

386449
/**
@@ -426,38 +489,31 @@ export interface Input {
426489
export interface InputOptions {
427490
/**
428491
* Milliseconds to wait before resolving a lone ESC byte as the Escape
429-
* key rather than the start of an escape sequence. Lower values feel
430-
* snappier but risk misinterpreting sequences on slow connections.
431-
*
432-
* For reference, Vim's `ttimeoutlen` defaults to 100ms and ncurses
433-
* `ESCDELAY` defaults to 1000ms. The default of 25ms is tuned for
434-
* local terminals where escape sequences arrive within microseconds.
492+
* key rather than the start of an escape sequence.
435493
*
436494
* @default 25
437495
*/
438496
escLatency?: number;
439497

440498
/**
441-
* Compiled terminfo binary to load terminal-specific escape sequences.
442-
*
443-
* This is the format used by files like /usr/lib/terminfo/78/xterm-256color
444-
* and they can be directly loaded from disk into this option.
445-
*
446-
* If no terminfo is provided it will use xterm capabilities as the default
499+
* Detection from detectTerminal(). Seeds the key trie from
500+
* detection.keys and uses detection.capabilities.colors for
501+
* colordepth denial events. When omitted, the parser uses xterm
502+
* default key sequences and a 256-color baseline.
447503
*/
448-
terminfo?: Uint8Array;
504+
detection?: Detection;
449505
}
450506

451507
export async function createInput(options: InputOptions = {}): Promise<Input> {
452-
let { escLatency = 25, terminfo } = options;
508+
let { escLatency = 25, detection } = options;
453509

454-
if (terminfo && terminfo.byteLength > MAX_TERMINFO) {
455-
throw new RangeError(
456-
`terminfo exceeds ${MAX_TERMINFO} byte limit (got ${terminfo.byteLength})`,
457-
);
458-
}
510+
let native = await createInputNative(
511+
escLatency,
512+
detection?.keys,
513+
detection?.capabilities.colors,
514+
);
459515

460-
let native = await createInputNative(escLatency);
516+
let initialColors = detection?.capabilities.colors ?? 256;
461517

462518
return {
463519
scan(bytes: Uint8Array = new Uint8Array(0)): ScanResult {
@@ -483,7 +539,7 @@ export async function createInput(options: InputOptions = {}): Promise<Input> {
483539
for (let i = 0; i < count; i++) {
484540
let ptr = native.event(native.state, i);
485541
if (ptr !== 0) {
486-
events.push(mapEvent(readEvent(view, ptr)));
542+
events.push(mapEvent(readEvent(view, ptr), initialColors));
487543
}
488544
}
489545

@@ -632,8 +688,70 @@ function mapKeyEvent(native: NativeInputEvent): KeyEvent {
632688
return ev;
633689
}
634690

635-
function mapEvent(native: NativeInputEvent): InputEvent {
691+
function mapCapEvent(
692+
native: NativeInputEvent,
693+
initialColors: number,
694+
): CapabilityEvent {
695+
switch (native.key) {
696+
case CAP_FOREGROUND_COLOR:
697+
return {
698+
type: "capability",
699+
key: "foreground-color",
700+
value: rgbOf(native.ch),
701+
};
702+
case CAP_BACKGROUND_COLOR:
703+
return {
704+
type: "capability",
705+
key: "background-color",
706+
value: rgbOf(native.ch),
707+
};
708+
case CAP_CURSOR_COLOR:
709+
return {
710+
type: "capability",
711+
key: "cursor-color",
712+
value: rgbOf(native.ch),
713+
};
714+
case CAP_COLORDEPTH: {
715+
let value: ColorDepth = native.ch === COLORDEPTH_TRUECOLOR
716+
? "truecolor"
717+
: initialColors <= 16
718+
? "16"
719+
: "256";
720+
return { type: "capability", key: "colordepth", value };
721+
}
722+
case CAP_SYNC_OUTPUT:
723+
return { type: "capability", key: "sync-output", value: native.ch !== 0 };
724+
case CAP_KITTY_KEYBOARD:
725+
return {
726+
type: "capability",
727+
key: "kitty-keyboard",
728+
value: native.ch !== 0,
729+
};
730+
case CAP_KITTY_GRAPHICS:
731+
return {
732+
type: "capability",
733+
key: "kitty-graphics",
734+
value: native.ch !== 0,
735+
};
736+
case CAP_POINTER_SHAPE:
737+
return {
738+
type: "capability",
739+
key: "pointer-shape",
740+
value: native.ch !== 0,
741+
};
742+
default:
743+
return { type: "capability", key: "pointer-shape", value: false };
744+
}
745+
}
746+
747+
function mapEvent(
748+
native: NativeInputEvent,
749+
initialColors: number,
750+
): InputEvent {
636751
switch (native.type) {
752+
case EVENT_CAPABILITY: {
753+
return mapCapEvent(native, initialColors);
754+
}
637755
case EVENT_KEY: {
638756
return mapKeyEvent(native);
639757
}

‎test/caps.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type DetectOptions, detectTerminal } from "../terminfo.ts";
2+
3+
export function offlineDetect(options: Partial<DetectOptions> = {}) {
4+
return detectTerminal({ env: {}, ...options });
5+
}
6+
7+
/** A Detection with truecolor granted via COLORTERM evidence. */
8+
export function trueColorDetect() {
9+
return offlineDetect({ env: { COLORTERM: "truecolor" } });
10+
}

0 commit comments

Comments
 (0)