write method

void write(
  1. String data
)

Writes the data from the underlying program to the terminal. Calling this updates the states of the terminal and emits events such as onBell or onTitleChange when the escape sequences in data request it.

Implementation

void write(String data) {
  // A UTF-8 sequence left incomplete by writeBytes can never be completed
  // by a String chunk; flush it as U+FFFD through the parser to keep the
  // output ordered. Interleaving write/writeBytes is not supported — this
  // only bounds the damage.
  if (_utf8Decoder.hasPendingBytes) {
    _utf8Decoder.reset();
    _parser.writeCodepoints(const [0xFFFD]);
  }

  // Fast path: if the chunk contains nothing the parser would dispatch on
  // (no escape byte, no C0 control characters, no dangling surrogate half)
  // and the parser isn't holding back bytes from a previous chunk, write
  // directly to the buffer, bypassing the parser state machine. This is
  // the common case for plain-text output floods.
  if (!_parser.hasPendingInput && !_requiresParser(data)) {
    // Keep _precedingCodepoint in sync so CSI n b (REP) still works after
    // a fast-path write; writeChar would normally maintain it. Extracted
    // via codeUnitAt instead of data.runes.last, which allocates a Runes
    // iterable and scans the whole chunk to find the last code point.
    if (data.isNotEmpty) {
      _precedingCodepoint = _lastCodepoint(data);
    }
    _buffer.write(data);
    _notifyOutput();
    return;
  }
  _parser.write(data);
  _notifyOutput();
}