listen method

listen stdin

Implementation

StreamSubscription<List<int>> listen() {
  _printPrompt();
  final sub = stdin.asBroadcastStream().listen(
    (_) async {
      if (_submitInProgress) return;

      String str;
      try {
        str = ascii.decode(_);
      } on FormatException {
        str = utf8.decode(_);
      }

      if (_handlers.containsKey(str)) {
        final res = _handlers[str]!(str);
        if (res is Future) {
          await res;
        }
        return;
      } else if (str.contains('\x1b')) {
        // print(_);
      } else {
        // print(_);
        _handleInsert(str);
      }
    },
    cancelOnError: true,
  );

  return sub;
}