passthroughSequence function

String passthroughSequence(
  1. String sequence,
  2. Multiplexer multiplexer
)

Returns sequence wrapped so that multiplexer passes it on to the terminal outside; sequence itself for Multiplexer.none.

tmux (with allow-passthrough on, 3.3 and later) takes it in DCS tmux; … ST with every ESC doubled; GNU screen in DCS … ST pieces of at most 252 bytes of UTF-8 (older versions keep no more than 256), whole characters, where ST would end the piece, so the OSCs in it end with BEL. One level only: inside a multiplexer inside another the sequence is lost.

Implementation

String passthroughSequence(String sequence, Multiplexer multiplexer) {
  switch (multiplexer) {
    case Multiplexer.none:
      return sequence;
    case Multiplexer.tmux:
      return '\x1bPtmux;${sequence.replaceAll('\x1b', '\x1b\x1b')}\x1b\\';
    case Multiplexer.screen:
      final sb = StringBuffer();
      final piece = StringBuffer();
      var bytes = 0;
      for (final r in sequence.replaceAll('\x1b\\', '\x07').runes) {
        final n = r < 0x80
            ? 1
            : r < 0x800
            ? 2
            : r < 0x10000
            ? 3
            : 4;
        if (bytes + n > 252) {
          sb.write('\x1bP$piece\x1b\\');
          piece.clear();
          bytes = 0;
        }
        piece.writeCharCode(r);
        bytes += n;
      }
      if (piece.isNotEmpty) {
        sb.write('\x1bP$piece\x1b\\');
      }
      return sb.toString();
  }
}