stop method

Future<void> stop()

Stops the terminal session and restores the original state.

This restores terminal modes, exits the alternate screen if active, and cleans up event subscriptions.

Implementation

Future<void> stop() async {
  if (!_running) return;
  _running = false;

  // Restore terminal state in reverse order.
  if (_keyboardEnhancements != 0) disableKeyboardEnhancements();
  if (_mouseEnabled) disableMouse();
  if (_bracketedPasteEnabled) disableBracketedPaste();
  if (_focusReportingEnabled) disableFocusReporting();
  if (_cursorHidden) showCursor();

  if (_inAltScreen) {
    exitAltScreen();
  } else {
    // Go to the bottom of the screen.
    _renderer.moveTo(0, _buf.height() - 1);
    _renderer.writeString('\r${UvAnsi.eraseScreenBelow}');
    _renderer.flush();
  }

  // Restore terminal mode.
  if (_input == stdin) {
    try {
      if (stdin.hasTerminal) {
        stdin.echoMode = true;
        stdin.lineMode = true;
      }
    } catch (_) {}
  }

  await _readerSubscription?.cancel();
  _readerSubscription = null;
  await _winchSubscription?.cancel();
  _winchSubscription = null;
  await _sigintSubscription?.cancel();
  _sigintSubscription = null;
  await _winch.stop();
  await _reader.close();
}