watchForInput method
void
watchForInput()
Implementation
void watchForInput() {
try {
// Create broadcast stream controller once from stdin
if (_stdinController == null) {
_stdinController = StreamController<List<int>>.broadcast();
_stdinSourceSubscription = io.stdin.listen(
(event) => _stdinController?.add(event),
onDone: () => _stdinController?.close(),
);
}
// Cancel existing subscription if any
_inputSubscription?.cancel();
// Re-lock input to ensure single-key mode is maintained
lockInput();
_inputSubscription = _stdinController?.stream.listen((event) {
var key = utf8.decode(event).toLowerCase().trim();
if (key.isEmpty && event.length == 1) {
key = '${event[0]}';
}
logger.detail('key: $key');
final _ = switch (key) {
'r' || 'R' => _reload().ignore(),
'c' || 'C' => _cleanConsole(),
'q' || 'Q' => stop().ignore(),
_ => null,
};
});
} catch (e) {
logger
..err('Failed to connect to stdin, cannot watch for input')
..detail('$e');
return;
}
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++;
});
}