watchForInput method

void watchForInput()

Implementation

void watchForInput() {
  _inputSubscription ??= io.stdin.listen((event) {
    var key = utf8.decode(event).toLowerCase();
    if (key.isEmpty && event.length == 1) {
      key = '${event[0]}';
    }
    logger.detail('key: $key');

    final _ = switch (key) {
      'r' => _reload().ignore(),
      'R' => _reload().ignore(),
      'q' => stop().ignore(),
      'Q' => stop().ignore(),
      _ => null,
    };
  });

  logger.detail('Watching for kill signal');

  var attemptsToKill = 0;
  final stream = Platform.isWindows
      ? ProcessSignal.sigint.watch()
      : StreamGroup.merge(
          [
            ProcessSignal.sigterm.watch(),
            ProcessSignal.sigint.watch(),
          ],
        );

  _killSubscription ??= stream.listen((event) {
    logger.detail('Received SIGINT');
    if (attemptsToKill > 0) {
      exit(1);
    } else if (attemptsToKill == 0) {
      stop().ignore();
    }

    attemptsToKill++;
  });
}