sendCommand method

Future<List<String>> sendCommand(
  1. String command, {
  2. bool endCondition(
    1. String
    )?,
  3. int? customTimeoutMs,
})

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;
}