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>
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
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';
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:xterm/core.dart';
|
||||
|
||||
import 'chat_shell_terminal_input.dart';
|
||||
import 'chat_shell_terminal_keymap.dart';
|
||||
|
||||
/// The terminal's input handler, chained before xterm.dart's defaults: it
|
||||
/// encodes every key [Terminal.keyInput] receives (the key toolbar, the soft
|
||||
/// keyboard's keys, hardware keys the view did not encode itself) as xterm
|
||||
/// does. A printing key's character comes from the US layout here; the view
|
||||
/// encodes hardware keys with their real character first
|
||||
/// ([chatShellKeyEventBytes]).
|
||||
class ChatShellInputHandler implements TerminalInputHandler {
|
||||
const ChatShellInputHandler({required this.optionAsMeta});
|
||||
|
||||
/// Whether Option is Meta on Apple platforms (read at each key).
|
||||
final bool Function() optionAsMeta;
|
||||
|
||||
@override
|
||||
String? call(TerminalKeyboardEvent event) {
|
||||
final alt = event.alt && chatShellAltIsMeta(optionAsMeta: optionAsMeta());
|
||||
final special = chatShellSpecialKeyBytes(
|
||||
event.key,
|
||||
shift: event.shift,
|
||||
alt: event.alt,
|
||||
meta: alt,
|
||||
ctrl: event.ctrl,
|
||||
state: event.state,
|
||||
);
|
||||
if (special != null) return special;
|
||||
final char = chatShellTerminalKeyChar(event.key);
|
||||
if (char == null) return null;
|
||||
return chatShellCharBytes(
|
||||
event.shift ? chatShellUsShifted(char) : char,
|
||||
alt: alt,
|
||||
ctrl: event.ctrl,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
bool _printable(String? text) {
|
||||
if (text == null || text.runes.length != 1) return false;
|
||||
final code = text.runes.single;
|
||||
return code >= 0x20 && code != 0x7f;
|
||||
}
|
||||
|
||||
bool _ascii(String text) {
|
||||
final code = text.codeUnitAt(0);
|
||||
return code >= 0x20 && code < 0x7f;
|
||||
}
|
||||
|
||||
/// The character a key types with Shift but without Ctrl or Alt: the event's
|
||||
/// own character where it is plain ASCII (so the layout counts), else the
|
||||
/// logical key's, else the physical key's on a US layout. On Apple platforms
|
||||
/// Option's own symbol (Option+B types ∫) is never the character.
|
||||
String? _baseChar(KeyEvent event, {required bool shift, required bool option}) {
|
||||
final character = event.character;
|
||||
if (_printable(character) && _ascii(character!) && !option) {
|
||||
return character;
|
||||
}
|
||||
final id = event.logicalKey.keyId;
|
||||
if (id >= 0x20 && id < 0x7f) {
|
||||
final char = String.fromCharCode(id);
|
||||
return shift ? chatShellUsShifted(char) : char;
|
||||
}
|
||||
final physical = chatShellPhysicalChar(event.physicalKey);
|
||||
if (physical == null) return null;
|
||||
return shift ? chatShellUsShifted(physical) : physical;
|
||||
}
|
||||
|
||||
/// The bytes a hardware key event sends, or null to leave it to the
|
||||
/// emulator's own handling (plain typing, Cmd chords, IME text).
|
||||
///
|
||||
/// Non-printing keys are encoded as xterm does, honouring application cursor
|
||||
/// keys and keypad mode in [state]. Ctrl with a character sends its control
|
||||
/// code; Alt (Meta, see [chatShellAltIsMeta]) sends ESC and the character it
|
||||
/// types with Shift and the layout. AltGr (Ctrl+Alt typing a symbol outside
|
||||
/// Apple platforms) types the symbol.
|
||||
String? chatShellKeyEventBytes(
|
||||
KeyEvent event,
|
||||
HardwareKeyboard keyboard,
|
||||
TerminalState state, {
|
||||
required bool optionAsMeta,
|
||||
}) {
|
||||
if (event is KeyUpEvent || keyboard.isMetaPressed) return null;
|
||||
final apple =
|
||||
defaultTargetPlatform == TargetPlatform.macOS ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS;
|
||||
final shift = keyboard.isShiftPressed;
|
||||
final ctrl = keyboard.isControlPressed;
|
||||
final alt =
|
||||
keyboard.isAltPressed && chatShellAltIsMeta(optionAsMeta: optionAsMeta);
|
||||
final special = chatShellSpecialKeys[event.logicalKey];
|
||||
if (special != null) {
|
||||
final bytes = chatShellSpecialKeyBytes(
|
||||
special,
|
||||
shift: shift,
|
||||
alt: keyboard.isAltPressed,
|
||||
meta: alt,
|
||||
ctrl: ctrl,
|
||||
state: state,
|
||||
);
|
||||
if (bytes != null) return bytes;
|
||||
}
|
||||
if (!alt && !ctrl) return null;
|
||||
final base = _baseChar(
|
||||
event,
|
||||
shift: shift,
|
||||
option: apple && keyboard.isAltPressed,
|
||||
);
|
||||
if (base == null) return null;
|
||||
if (!apple && ctrl && keyboard.isAltPressed) {
|
||||
// AltGr arrives as Ctrl+Alt: a symbol the layout composes is typed.
|
||||
final character = event.character;
|
||||
final id = event.logicalKey.keyId;
|
||||
final plain = id >= 0x20 && id < 0x7f
|
||||
? String.fromCharCode(id)
|
||||
: chatShellPhysicalChar(event.physicalKey);
|
||||
if (_printable(character) &&
|
||||
character!.toLowerCase() != plain &&
|
||||
chatShellUsShifted(plain ?? '') != character) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return chatShellCharBytes(base, alt: alt, ctrl: ctrl);
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
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: ' ',
|
||||
};
|
||||
@@ -1,6 +1,9 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:xterm/core.dart';
|
||||
|
||||
import '../../terminal/terminal_layout.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
import 'chat_shell_terminal_key_input.dart';
|
||||
|
||||
/// Lines of scrollback one card's terminal keeps.
|
||||
const int chatShellTerminalScrollback = 5000;
|
||||
@@ -16,7 +19,14 @@ class ChatShellTerminalSession {
|
||||
ChatShellRun run, {
|
||||
required this.onInput,
|
||||
required this.onResize,
|
||||
}) : terminal = _ChatShellTerminal() {
|
||||
bool Function()? optionAsMeta,
|
||||
}) : optionAsMeta =
|
||||
optionAsMeta ?? (() => TerminalLayout.instance.optionAsMeta),
|
||||
terminal = _ChatShellTerminal() {
|
||||
terminal.inputHandler = CascadeInputHandler([
|
||||
ChatShellInputHandler(optionAsMeta: this.optionAsMeta),
|
||||
defaultInputHandler,
|
||||
]);
|
||||
// Drawn at the process's own size until a view lays it out at its own.
|
||||
if (run.cols > 0 && run.rows > 0) terminal.resize(run.cols, run.rows);
|
||||
terminal.onOutput = _output;
|
||||
@@ -26,6 +36,9 @@ class ChatShellTerminalSession {
|
||||
|
||||
final Terminal terminal;
|
||||
|
||||
/// Whether a Mac's Option key is Meta (Settings → Appearance → Terminal).
|
||||
final bool Function() optionAsMeta;
|
||||
|
||||
/// Receives input for the process; only called while the run is live.
|
||||
final void Function(String data) onInput;
|
||||
|
||||
@@ -47,6 +60,7 @@ class ChatShellTerminalSession {
|
||||
/// queries in that output are not sent: the process asked them long ago.
|
||||
void replay(ChatShellRun run) {
|
||||
_live = run.running && run.terminal;
|
||||
terminal.reflowEnabled = !_live;
|
||||
_replaying = true;
|
||||
try {
|
||||
terminal.useMainBuffer();
|
||||
@@ -86,6 +100,7 @@ class ChatShellTerminalSession {
|
||||
void finish(ChatShellRun run) {
|
||||
if (!_live) return;
|
||||
_live = false;
|
||||
terminal.reflowEnabled = true;
|
||||
terminal.setCursorVisibleMode(false);
|
||||
}
|
||||
|
||||
@@ -125,21 +140,48 @@ class ChatShellTerminalSession {
|
||||
/// The emulator with the scrollback the cards keep. It answers a cursor
|
||||
/// position query (DSR 6) with the one-based row and column the protocol
|
||||
/// specifies; xterm.dart 4.0.0 itself answers zero-based.
|
||||
///
|
||||
/// A live terminal does not reflow on resize: xterm.dart 4.0.0's reflow
|
||||
/// leaves the cursor on a row other than its line's, so the shell's redraw
|
||||
/// lands on old output. Without it a resize behaves as xterm's: narrowing
|
||||
/// cuts rows at the new width, fewer rows scroll the top into scrollback
|
||||
/// (keeping the cursor's row at the bottom), more rows bring it back.
|
||||
/// Finished output, which nothing writes to any more, reflows to fit.
|
||||
class _ChatShellTerminal extends Terminal {
|
||||
_ChatShellTerminal() : super(maxLines: chatShellTerminalScrollback);
|
||||
_ChatShellTerminal()
|
||||
: super(maxLines: chatShellTerminalScrollback, platform: _platform());
|
||||
|
||||
/// The package's own Alt handler (a fallback after the app's) leaves
|
||||
/// Option alone only when it knows it is on a Mac.
|
||||
static TerminalTargetPlatform _platform() =>
|
||||
defaultTargetPlatform == TargetPlatform.macOS ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS
|
||||
? TerminalTargetPlatform.macos
|
||||
: TerminalTargetPlatform.unknown;
|
||||
|
||||
@override
|
||||
void sendCursorPosition() {
|
||||
onOutput?.call('\x1b[${buffer.cursorY + 1};${buffer.cursorX + 1}R');
|
||||
}
|
||||
|
||||
/// Narrowing rewraps long lines, and xterm.dart 4.0.0 keeps every blank
|
||||
/// row below the cursor while doing so: the extra rows push the top of
|
||||
/// the output into scrollback. Blank rows below the cursor are dropped
|
||||
/// instead, keeping the cursor on its line.
|
||||
@override
|
||||
void resize(int width, int height, [int? pixelWidth, int? pixelHeight]) {
|
||||
final oldWidth = viewWidth;
|
||||
if (!reflowEnabled && width >= 1 && width < oldWidth) {
|
||||
// A cut row keeps its cells past the new width, which a later widening
|
||||
// would show again beside what was written since: they are erased.
|
||||
for (final buffer in [mainBuffer, altBuffer]) {
|
||||
buffer.lines.forEach((line) {
|
||||
if (line.length > width) {
|
||||
line.eraseRange(width, line.length, CursorStyle.empty);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
super.resize(width, height, pixelWidth, pixelHeight);
|
||||
if (!reflowEnabled) return;
|
||||
// Rewrapping keeps every blank row below the cursor, and the extra rows
|
||||
// push the top of finished output into scrollback: they are dropped.
|
||||
final lines = buffer.lines;
|
||||
while (buffer.height > viewHeight &&
|
||||
buffer.height - 1 > buffer.absoluteCursorY &&
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:xterm/xterm.dart';
|
||||
import '../conversation/chat_composer_paste_focus.dart';
|
||||
import '../ui/transcript/chat_link_launcher.dart';
|
||||
import 'chat_shell_metrics.dart';
|
||||
import 'chat_shell_terminal_key_input.dart';
|
||||
import 'chat_shell_terminal_links.dart';
|
||||
import 'chat_shell_terminal_session.dart';
|
||||
import 'chat_shell_terminal_theme.dart';
|
||||
@@ -94,6 +95,9 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
final TerminalController _controller = TerminalController();
|
||||
final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal');
|
||||
final FocusNode _outputFocus = FocusNode(debugLabel: 'chat-shell-output');
|
||||
final ScrollController _scroll = ScrollController();
|
||||
final Map<ShortcutActivator, Intent> _shortcuts =
|
||||
chatShellTerminalShortcuts();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -109,6 +113,7 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
_controller.dispose();
|
||||
_focus.dispose();
|
||||
_outputFocus.dispose();
|
||||
_scroll.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -123,11 +128,30 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Ctrl-C with a selection copies; anything else is the terminal's.
|
||||
KeyEventResult _key(FocusNode node, KeyEvent event) =>
|
||||
chatShellCopyKey(event, HardwareKeyboard.instance) && _copySelection()
|
||||
? KeyEventResult.handled
|
||||
: KeyEventResult.ignored;
|
||||
/// Ctrl-C with a selection copies; the terminal's own shortcuts (copy,
|
||||
/// paste) are left to it; any other key xterm encodes differently from
|
||||
/// xterm.dart (Alt as Meta, Ctrl symbols, modified special keys) is sent
|
||||
/// here with its real character. The rest is the emulator's.
|
||||
KeyEventResult _key(FocusNode node, KeyEvent event) {
|
||||
final keyboard = HardwareKeyboard.instance;
|
||||
if (chatShellCopyKey(event, keyboard) && _copySelection()) {
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
if (_shortcuts.keys.any((key) => key.accepts(event, keyboard))) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
final session = widget.session;
|
||||
final bytes = chatShellKeyEventBytes(
|
||||
event,
|
||||
keyboard,
|
||||
session.terminal,
|
||||
optionAsMeta: session.optionAsMeta(),
|
||||
);
|
||||
if (bytes == null) return KeyEventResult.ignored;
|
||||
session.terminal.textInput(bytes);
|
||||
if (_scroll.hasClients) _scroll.jumpTo(_scroll.position.maxScrollExtent);
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
|
||||
/// A finished run's output takes no keys of its own, so the copy keys are
|
||||
/// answered here while it has the keyboard.
|
||||
@@ -163,6 +187,7 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
child: TerminalView(
|
||||
session.terminal,
|
||||
controller: _controller,
|
||||
scrollController: _scroll,
|
||||
focusNode: _focus,
|
||||
autofocus: widget.autofocus && live,
|
||||
autoResize: widget.autoResize,
|
||||
@@ -177,7 +202,7 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
hardwareKeyboardOnly: !chatShellTouchFirst(),
|
||||
// No autocorrect or suggestions rewriting what is sent to a shell.
|
||||
keyboardType: TextInputType.visiblePassword,
|
||||
shortcuts: chatShellTerminalShortcuts(),
|
||||
shortcuts: _shortcuts,
|
||||
cursorType: TerminalCursorType.block,
|
||||
onTapUp: _tapped,
|
||||
onKeyEvent: _key,
|
||||
|
||||
Reference in New Issue
Block a user