enableRawVirtualTerminal static method

WindowsConsoleModes? enableRawVirtualTerminal()

Switches the console into raw, virtual-terminal input mode and ensures the output supports virtual-terminal processing.

Returns the previous modes so they can be passed to restore, or null when not on Windows or the console mode could not be changed (e.g. output is redirected), in which case the caller should fall back to other handling.

Implementation

static WindowsConsoleModes? enableRawVirtualTerminal() {
  if (!Platform.isWindows) return null;
  try {
    final inHandle = _getStdHandle(_stdInputHandle);
    final outHandle = _getStdHandle(_stdOutputHandle);

    final inMode = _getConsoleModeOrNull(inHandle);
    final outMode = _getConsoleModeOrNull(outHandle);
    if (inMode == null || outMode == null) return null;

    // Disable line buffering and echo (like Unix raw mode) and enable VT input
    // so arrow keys, Escape, etc. arrive as ANSI escape sequences. Leave
    // ENABLE_PROCESSED_INPUT untouched so Ctrl+C keeps raising a signal.
    final newInMode =
        (inMode & ~_enableLineInput & ~_enableEchoInput) |
        _enableVirtualTerminalInput;
    _setConsoleMode(inHandle, newInMode);
    _setConsoleMode(outHandle, outMode | _enableVirtualTerminalProcessing);

    return WindowsConsoleModes(inMode, outMode);
  } on Object {
    return null;
  }
}