runAndListenOutput method

  1. @protected
Future<ProcessResult> runAndListenOutput(
  1. String executable,
  2. List<String> arguments, {
  3. void onOut(
    1. String out
    )?,
  4. void onErr(
    1. String err
    )?,
  5. String? workingDir,
})

Run command and add listeners onOut/onErr on std and err output.

Implementation

@protected
Future<ProcessResult> runAndListenOutput(
  String executable,
  List<String> arguments, {
  void Function(String out)? onOut,
  void Function(String err)? onErr,
  String? workingDir,
}) async {
  final stdout = StringBuffer();
  final stderr = StringBuffer();
  final process = await Process.start(executable, arguments,
      workingDirectory: workingDir);

  systemEncoding.decoder.bind(process.stdout).listen((event) {
    stdout.write(event);
    if (onOut != null) onOut(event);
  });
  systemEncoding.decoder.bind(process.stderr).listen((event) {
    stderr.write(event);
    if (onErr != null) onErr(event);
  });

  final exitCode = await process.exitCode;

  return ProcessResult(
      process.pid, exitCode, stdout.toString(), stderr.toString());
}