readLine method

Future<String?> readLine(
  1. String prompt
)

Prints prompt and reads one line.

Returns null when input ends, or when Ctrl+D is pressed on an empty line. A line that was partly typed when input ends is returned as it is. Lines that aren't blank are added to history, unless they repeat the previous line.

Implementation

Future<String?> readLine(String prompt) {
  if (_reading != null) {
    throw StateError('A line is already being read.');
  }
  if (_closed) return Future<String?>.value(null);

  final Completer<String?> reading = Completer<String?>();
  _reading = reading;
  _prompt = prompt;
  _interactive = _terminal.hasTerminal;
  _ansi = _interactive && _terminal.supportsAnsi;
  _line.clear();
  _cursor = 0;
  _historyIndex = _history.length;
  _draft = null;
  _completing = false;
  _generation++;

  if (_interactive) {
    _terminal.enableRawMode();
    _rawMode = true;
  }
  _output.write(_interactive && !_ansi ? _stripAnsi(prompt) : prompt);

  _listen();
  _process();
  return reading.future;
}