7d1ffe8ad3
- 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>
164 lines
5.0 KiB
Dart
164 lines
5.0 KiB
Dart
import 'package:flutter/foundation.dart';
|
||
import 'package:xterm/core.dart';
|
||
|
||
/// The bytes xterm sends for keys, where xterm.dart 4.0.0's own handlers
|
||
/// differ (Alt+letter upper-cased, no Ctrl symbols, the wrong modifier codes,
|
||
/// application cursor keys read from the keypad mode).
|
||
///
|
||
/// Modified special keys use xterm's parameter: 1 + Shift(1) + Alt(2) +
|
||
/// Ctrl(4), so Shift+Up is `CSI 1;2A`, Alt+Left `CSI 1;3D`, Ctrl+Right
|
||
/// `CSI 1;5C`, Ctrl+Shift+Home `CSI 1;6H`, Shift+PgUp `CSI 5;2~`.
|
||
|
||
const String _esc = '\x1b';
|
||
|
||
/// Whether Alt is Meta here: always outside Apple platforms; on them the
|
||
/// Option key is Meta only while [optionAsMeta] (else it types its symbol).
|
||
bool chatShellAltIsMeta({required bool optionAsMeta}) {
|
||
final apple =
|
||
defaultTargetPlatform == TargetPlatform.macOS ||
|
||
defaultTargetPlatform == TargetPlatform.iOS;
|
||
return !apple || optionAsMeta;
|
||
}
|
||
|
||
int _modifier({required bool shift, required bool alt, required bool ctrl}) =>
|
||
1 + (shift ? 1 : 0) + (alt ? 2 : 0) + (ctrl ? 4 : 0);
|
||
|
||
/// Cursor keys and Home/End: their final letter. DECCKM (application cursor
|
||
/// keys) sends them unmodified as SS3.
|
||
const Map<TerminalKey, String> _cursorKeys = {
|
||
TerminalKey.arrowUp: 'A',
|
||
TerminalKey.arrowDown: 'B',
|
||
TerminalKey.arrowRight: 'C',
|
||
TerminalKey.arrowLeft: 'D',
|
||
TerminalKey.home: 'H',
|
||
TerminalKey.end: 'F',
|
||
};
|
||
|
||
/// F1–F4: SS3 unmodified, `CSI 1;m P` modified.
|
||
const Map<TerminalKey, String> _pfKeys = {
|
||
TerminalKey.f1: 'P',
|
||
TerminalKey.f2: 'Q',
|
||
TerminalKey.f3: 'R',
|
||
TerminalKey.f4: 'S',
|
||
};
|
||
|
||
/// Keys sent as `CSI n ~` (`CSI n;m ~` modified).
|
||
const Map<TerminalKey, int> _tildeKeys = {
|
||
TerminalKey.insert: 2,
|
||
TerminalKey.delete: 3,
|
||
TerminalKey.pageUp: 5,
|
||
TerminalKey.pageDown: 6,
|
||
TerminalKey.f5: 15,
|
||
TerminalKey.f6: 17,
|
||
TerminalKey.f7: 18,
|
||
TerminalKey.f8: 19,
|
||
TerminalKey.f9: 20,
|
||
TerminalKey.f10: 21,
|
||
TerminalKey.f11: 23,
|
||
TerminalKey.f12: 24,
|
||
};
|
||
|
||
/// Keypad keys in application keypad mode (DECKPAM). The digits and the
|
||
/// decimal point keep typing their characters, as xterm does while NumLock
|
||
/// is on (a keypad digit only arrives with NumLock on).
|
||
const Map<TerminalKey, String> _keypadKeys = {
|
||
TerminalKey.numpadEnter: 'M',
|
||
TerminalKey.numpadMultiply: 'j',
|
||
TerminalKey.numpadAdd: 'k',
|
||
TerminalKey.numpadSubtract: 'm',
|
||
TerminalKey.numpadDivide: 'o',
|
||
TerminalKey.numpadEqual: 'X',
|
||
};
|
||
|
||
/// The bytes for a non-printing key, or null for a key that types a
|
||
/// character (letters, digits, symbols, Space, the keypad outside its
|
||
/// application mode). [alt] counts in the modifier parameter; [meta] (Alt
|
||
/// as Meta) prefixes ESC to the keys that have no parameter.
|
||
String? chatShellSpecialKeyBytes(
|
||
TerminalKey key, {
|
||
required bool shift,
|
||
required bool alt,
|
||
required bool meta,
|
||
required bool ctrl,
|
||
required TerminalState state,
|
||
}) {
|
||
final m = _modifier(shift: shift, alt: alt, ctrl: ctrl);
|
||
final prefix = meta ? _esc : '';
|
||
final cursor = _cursorKeys[key];
|
||
if (cursor != null) {
|
||
if (m > 1) return '$_esc[1;$m$cursor';
|
||
return state.cursorKeysMode ? '${_esc}O$cursor' : '$_esc[$cursor';
|
||
}
|
||
final pf = _pfKeys[key];
|
||
if (pf != null) return m > 1 ? '$_esc[1;$m$pf' : '${_esc}O$pf';
|
||
final tilde = _tildeKeys[key];
|
||
if (tilde != null) return m > 1 ? '$_esc[$tilde;$m~' : '$_esc[$tilde~';
|
||
if (state.appKeypadMode) {
|
||
final keypad = _keypadKeys[key];
|
||
if (keypad != null) return '${_esc}O$keypad';
|
||
}
|
||
switch (key) {
|
||
case TerminalKey.enter || TerminalKey.numpadEnter || TerminalKey.returnKey:
|
||
return '$prefix${state.lineFeedMode ? '\r\n' : '\r'}';
|
||
case TerminalKey.tab:
|
||
return shift ? '$prefix$_esc[Z' : '$prefix\t';
|
||
case TerminalKey.backtab:
|
||
return '$prefix$_esc[Z';
|
||
case TerminalKey.escape:
|
||
return '$prefix$_esc';
|
||
case TerminalKey.backspace:
|
||
return '$prefix${ctrl ? '\b' : '\x7f'}';
|
||
case TerminalKey.space:
|
||
if (ctrl) return '$prefix\x00';
|
||
return meta ? '$_esc ' : null;
|
||
default:
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// Ctrl with a character: the C0 code xterm sends for it.
|
||
const Map<String, int> _ctrlSymbols = {
|
||
' ': 0x00,
|
||
'@': 0x00,
|
||
'2': 0x00,
|
||
'[': 0x1b,
|
||
'3': 0x1b,
|
||
'\\': 0x1c,
|
||
'4': 0x1c,
|
||
']': 0x1d,
|
||
'5': 0x1d,
|
||
'^': 0x1e,
|
||
'6': 0x1e,
|
||
'_': 0x1f,
|
||
'/': 0x1f,
|
||
'-': 0x1f,
|
||
'7': 0x1f,
|
||
'?': 0x7f,
|
||
'8': 0x7f,
|
||
};
|
||
|
||
/// The control code Ctrl+[char] sends, or null if it has none.
|
||
int? chatShellCtrlCode(String char) {
|
||
if (char.length != 1) return null;
|
||
final code = char.toLowerCase().codeUnitAt(0);
|
||
if (code >= 0x61 && code <= 0x7a) return code - 0x60;
|
||
return _ctrlSymbols[char];
|
||
}
|
||
|
||
/// The bytes for [char] (as typed, Shift applied) with Ctrl and/or Meta
|
||
/// ([alt]): Ctrl maps it to its control code, Meta prefixes ESC. Null with
|
||
/// neither modifier, or Ctrl with a character that has no control code.
|
||
String? chatShellCharBytes(
|
||
String char, {
|
||
required bool alt,
|
||
required bool ctrl,
|
||
}) {
|
||
if (!alt && !ctrl) return null;
|
||
if (ctrl) {
|
||
final code = chatShellCtrlCode(char);
|
||
if (code == null) return null;
|
||
return '${alt ? _esc : ''}${String.fromCharCode(code)}';
|
||
}
|
||
return '$_esc$char';
|
||
}
|