Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

3.7 KB
Raw Blame
import 'package:xterm/xterm.dart';

/// Makes ⌥⏎ a newline inside the engine's prompt instead of a submit.
///
/// Claude Code, Codex and every other Ink/readline prompt read Meta+Enter — `ESC` followed by the
/// ordinary Return bytes — as "add a line, do not send this turn yet". A real terminal produces
/// that only when it is configured to treat ⌥ as Meta (iTerm's "Esc+", Terminal.app's "Use Option
/// as Meta Key"); xterm.dart has no such setting, and its [AltInputHandler] deliberately does
/// nothing on macOS so ⌥ stays the compose key for `å`/`ø`/`¬`. So ⌥⏎ fell through to the keytab,
/// which matches on `Return` alone and answers `\r` — the submit. The key the desktop's shortcuts
/// sheet promises (its `kTerminalOwnedKeys`) sent the turn instead of breaking the line.
///
/// ⌥ is Meta for THIS ONE KEY, not for the keyboard: Enter carries no character to compose, so
/// prefixing it costs nothing that ⌥-as-compose needs, and the rest of the alphabet is untouched.
///
/// The base bytes are asked of [_inner] rather than written out here, because Return is not always
/// `\r`: under LNM (`lineFeedMode`) the keytab answers `\r\n`, and a caller may pass a keytab of
/// its own. Alt and Shift are cleared for that question — Alt so the keytab does not see the very
/// modifier being translated, Shift so ⌥⇧⏎ yields a prefixed Return rather than a prefixed `\EOM`,
/// which no prompt would recognise.
class MetaEnterInputHandler implements TerminalInputHandler {
  const MetaEnterInputHandler(this._inner);

  final TerminalInputHandler _inner;

  @override
  String? call(TerminalKeyboardEvent event) {
    final isEnter =
        event.key == TerminalKey.enter || event.key == TerminalKey.numpadEnter;
    // Ctrl+Alt+Enter is left alone: that chord belongs to whatever is running, not to this.
    if (!isEnter || !event.alt || event.ctrl) return _inner(event);
    final base = _inner(event.copyWith(alt: false, shift: false));
    if (base == null) return null;
    return '\x1b$base';
  }
}

/// Keeps ⇧⏎ as CSI-u (`CSI 13;2u`) instead of losing Shift in the default keytab — the desktop's
/// `ShiftEnterInputHandler`, so a hardware keyboard on a phone sends what it sends on the laptop.
///
/// The keytab answers ⇧⏎ with `\EOM`, the keypad's Enter, which no engine's prompt reads as a
/// newline. As CSI-u a prompt can tell the newline it binds to ⇧⏎ from the plain Return that
/// submits. One that does not read CSI-u is no worse off than with `\EOM`, and ⌥⏎
/// ([MetaEnterInputHandler]) is the newline every Ink/readline prompt reads — which is why the key
/// bar's own newline sends that one.
///
/// Fixed bytes rather than asked of [_inner], as on the desktop: CSI-u has one spelling, LNM
/// (`lineFeedMode`) included.
class ShiftEnterInputHandler implements TerminalInputHandler {
  const ShiftEnterInputHandler(this._inner);

  final TerminalInputHandler _inner;

  @override
  String? call(TerminalKeyboardEvent event) {
    final isEnter =
        event.key == TerminalKey.enter || event.key == TerminalKey.numpadEnter;
    // ⌥⇧⏎ is [MetaEnterInputHandler]'s, ⌃⇧⏎ the keytab's: only Shift alone is this.
    if (isEnter && event.shift && !event.alt && !event.ctrl) {
      return '\x1b[13;2u';
    }
    return _inner(event);
  }
}

/// The handler every pane's [Terminal] is built with — xterm's default behaviour, plus ⌥⏎ and ⇧⏎,
/// in the desktop's order: ⌥ is translated first, so ⌥⇧⏎ asks the keytab for a plain Return and
/// stays a Meta-prefixed Return rather than a prefixed CSI-u.
const TerminalInputHandler harnessInputHandler = MetaEnterInputHandler(
  ShiftEnterInputHandler(defaultInputHandler),
);