This repository has been archived on 2026-10-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
maestro/web/src/keys.ts
T
Miguel Palhas 3ce1a6f331
ci / web (push) Successful in 1m22s
ci / rust (push) Successful in 7m24s
ci / flake (push) Successful in 8m50s
feat(maestro): add paging and symbol keys to the mobile key bar
PgUp/PgDn and the `- / | ~` symbols the soft keyboard buries behind a page
switch join the bar, in an 8-column grid that keeps the cursor inverted-T.
Columns hold a thumb's width, so a narrow phone scrolls the bar sideways
instead of shrinking the caps.

Closes #7
2026-09-02 16:01:08 +01:00

41 lines
1.7 KiB
TypeScript

// Terminal key encoding: control characters, meta prefixes, and cursor escape
// sequences. Table-driven and easy to get subtly wrong, so it lives apart from
// the DOM and is tested directly.
export function toCtrl(ch: string): string {
if (ch.length !== 1) return ch;
const upper = ch.toUpperCase().codePointAt(0) ?? 0;
if (upper >= 0x40 && upper <= 0x5f) return String.fromCodePoint(upper & 0x1f);
if (ch === " ") return "\x00";
if (ch === "?") return "\x7f";
return ch;
}
/** Apply pending modifiers to a single typed character: Ctrl → control code,
* Alt → ESC prefix (xterm meta). Both compose (Ctrl+Alt = ESC + control). */
export function applyModsToChar(ch: string, ctrl: boolean, alt: boolean): string {
let out = ctrl ? toCtrl(ch) : ch;
if (alt) out = "\x1b" + out;
return out;
}
/** Cursor/nav-key sequence, promoted to the modified CSI form when Ctrl/Alt are
* pending — `\x1b[1;<n><final>`, n = 1 + alt(2) + ctrl(4) (xterm convention). */
export function cursorSeq(final: string, ctrl: boolean, alt: boolean): string {
if (!ctrl && !alt) return `\x1b[${final}`;
return `\x1b[1;${modParam(ctrl, alt)}${final}`;
}
/** Modifier parameter of the xterm CSI forms: n = 1 + alt(2) + ctrl(4). */
function modParam(ctrl: boolean, alt: boolean): number {
return 1 + (alt ? 2 : 0) + (ctrl ? 4 : 0);
}
/** Sequence for the `~`-terminated navigation keys (PgUp `5`, PgDn `6`), which
* carry modifiers as `\x1b[<code>;<n>~` rather than the cursor keys' `1;<n>`
* form. */
export function tildeSeq(code: number, ctrl: boolean, alt: boolean): string {
if (!ctrl && !alt) return `\x1b[${code}~`;
return `\x1b[${code};${modParam(ctrl, alt)}~`;
}