sendCommand method
Send a command to the process
Returns the output lines collected between sending the command and the timeout or a matching end condition
Implementation
Future<List<String>> sendCommand(
String command, {
bool Function(String)? endCondition,
int? customTimeoutMs,
}) async {
final responseBuffer = <String>[];
final completer = Completer<List<String>>();
// Set up a buffer to collect response lines
late StreamSubscription<String> subscription;
subscription = _responseController.stream.listen((line) {
responseBuffer.add(line);
// Check if we've reached the end condition
if (endCondition != null && endCondition(line)) {
subscription.cancel();
completer.complete(responseBuffer);
}
});
// Set a timeout
final timeout = customTimeoutMs ?? _responseTimeoutMs;
Future.delayed(Duration(milliseconds: timeout)).then((_) {
if (!completer.isCompleted) {
subscription.cancel();
completer.complete(responseBuffer);
}
});
// Send the command
_process.stdin.writeln(command);
await _process.stdin.flush();
return completer.future;
}