Files
pipelines/app/lib/features/chat/shell/chat_shell_terminal_keymap.dart
T
alan 7d1ffe8ad3 Terminal: xterm key encoding, no live reflow on resize, Alt+T/Ctrl+T reach the shell
- Live terminals no longer reflow (xterm.dart 4.0.0 left the cursor on the
  wrong row); narrowing cuts and erases cells past the width, rows scroll
  into scrollback keeping the cursor row; finished output still reflows.
- App-side key encoding before xterm.dart's defaults: Alt as Meta (ESC +
  the typed character), Ctrl symbol table, xterm modifier parameter for
  arrows/Home/End/PgUp/PgDn/Ins/Del/F-keys, DECCKM and DECKPAM, Alt+BS.
- Settings > Appearance > Terminal: Use Option as Meta key (Macs, on).
- Inside a focused terminal only Cmd+T and Ctrl+` toggle it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:32:29 +00:00

140 lines
4.7 KiB
Dart

import 'package:flutter/services.dart';
import 'package:xterm/core.dart';
/// Key tables for terminal input: the non-printing keys by their Flutter
/// logical key, and the US layout's characters, used when a key event
/// carries no usable character (Option's symbol on a Mac, a control code).
/// The non-printing keys the terminal encodes itself.
final Map<LogicalKeyboardKey, TerminalKey> chatShellSpecialKeys = {
LogicalKeyboardKey.arrowUp: TerminalKey.arrowUp,
LogicalKeyboardKey.arrowDown: TerminalKey.arrowDown,
LogicalKeyboardKey.arrowLeft: TerminalKey.arrowLeft,
LogicalKeyboardKey.arrowRight: TerminalKey.arrowRight,
LogicalKeyboardKey.home: TerminalKey.home,
LogicalKeyboardKey.end: TerminalKey.end,
LogicalKeyboardKey.insert: TerminalKey.insert,
LogicalKeyboardKey.delete: TerminalKey.delete,
LogicalKeyboardKey.pageUp: TerminalKey.pageUp,
LogicalKeyboardKey.pageDown: TerminalKey.pageDown,
LogicalKeyboardKey.f1: TerminalKey.f1,
LogicalKeyboardKey.f2: TerminalKey.f2,
LogicalKeyboardKey.f3: TerminalKey.f3,
LogicalKeyboardKey.f4: TerminalKey.f4,
LogicalKeyboardKey.f5: TerminalKey.f5,
LogicalKeyboardKey.f6: TerminalKey.f6,
LogicalKeyboardKey.f7: TerminalKey.f7,
LogicalKeyboardKey.f8: TerminalKey.f8,
LogicalKeyboardKey.f9: TerminalKey.f9,
LogicalKeyboardKey.f10: TerminalKey.f10,
LogicalKeyboardKey.f11: TerminalKey.f11,
LogicalKeyboardKey.f12: TerminalKey.f12,
LogicalKeyboardKey.enter: TerminalKey.enter,
LogicalKeyboardKey.numpadEnter: TerminalKey.numpadEnter,
LogicalKeyboardKey.tab: TerminalKey.tab,
LogicalKeyboardKey.escape: TerminalKey.escape,
LogicalKeyboardKey.backspace: TerminalKey.backspace,
LogicalKeyboardKey.space: TerminalKey.space,
LogicalKeyboardKey.numpadMultiply: TerminalKey.numpadMultiply,
LogicalKeyboardKey.numpadAdd: TerminalKey.numpadAdd,
LogicalKeyboardKey.numpadSubtract: TerminalKey.numpadSubtract,
LogicalKeyboardKey.numpadDivide: TerminalKey.numpadDivide,
LogicalKeyboardKey.numpadEqual: TerminalKey.numpadEqual,
};
/// US layout: each unshifted symbol and its shifted one.
const Map<String, String> _usShifted = {
'1': '!',
'2': '@',
'3': '#',
'4': r'$',
'5': '%',
'6': '^',
'7': '&',
'8': '*',
'9': '(',
'0': ')',
'-': '_',
'=': '+',
'[': '{',
']': '}',
'\\': '|',
';': ':',
"'": '"',
'`': '~',
',': '<',
'.': '>',
'/': '?',
};
/// [char] as Shift types it on a US layout.
String chatShellUsShifted(String char) =>
_usShifted[char] ?? char.toUpperCase();
/// The unshifted character of a printing [TerminalKey] on a US layout.
String? chatShellTerminalKeyChar(TerminalKey key) {
final index = key.index;
if (index >= TerminalKey.keyA.index && index <= TerminalKey.keyZ.index) {
return String.fromCharCode(0x61 + index - TerminalKey.keyA.index);
}
if (index >= TerminalKey.digit1.index && index <= TerminalKey.digit9.index) {
return String.fromCharCode(0x31 + index - TerminalKey.digit1.index);
}
if (index >= TerminalKey.numpad1.index &&
index <= TerminalKey.numpad9.index) {
return String.fromCharCode(0x31 + index - TerminalKey.numpad1.index);
}
return _terminalKeySymbols[key];
}
const Map<TerminalKey, String> _terminalKeySymbols = {
TerminalKey.digit0: '0',
TerminalKey.numpad0: '0',
TerminalKey.minus: '-',
TerminalKey.equal: '=',
TerminalKey.bracketLeft: '[',
TerminalKey.bracketRight: ']',
TerminalKey.backslash: '\\',
TerminalKey.semicolon: ';',
TerminalKey.quote: "'",
TerminalKey.backquote: '`',
TerminalKey.comma: ',',
TerminalKey.period: '.',
TerminalKey.slash: '/',
TerminalKey.numpadDecimal: '.',
TerminalKey.numpadDivide: '/',
TerminalKey.numpadMultiply: '*',
TerminalKey.numpadSubtract: '-',
TerminalKey.numpadAdd: '+',
TerminalKey.numpadEqual: '=',
};
/// The unshifted character a physical key types on a US layout.
String? chatShellPhysicalChar(PhysicalKeyboardKey key) {
final usb = key.usbHidUsage;
// USB HID: keyA (0x70004) … keyZ, digit1 … digit9, digit0.
if (usb >= 0x70004 && usb <= 0x7001d) {
return String.fromCharCode(0x61 + usb - 0x70004);
}
if (usb >= 0x7001e && usb <= 0x70026) {
return String.fromCharCode(0x31 + usb - 0x7001e);
}
return _physicalSymbols[key];
}
final Map<PhysicalKeyboardKey, String> _physicalSymbols = {
PhysicalKeyboardKey.digit0: '0',
PhysicalKeyboardKey.minus: '-',
PhysicalKeyboardKey.equal: '=',
PhysicalKeyboardKey.bracketLeft: '[',
PhysicalKeyboardKey.bracketRight: ']',
PhysicalKeyboardKey.backslash: '\\',
PhysicalKeyboardKey.semicolon: ';',
PhysicalKeyboardKey.quote: "'",
PhysicalKeyboardKey.backquote: '`',
PhysicalKeyboardKey.comma: ',',
PhysicalKeyboardKey.period: '.',
PhysicalKeyboardKey.slash: '/',
PhysicalKeyboardKey.space: ' ',
};