liveShell method

Future<void> liveShell(
  1. VoidCallback refresh, {
  2. Function? customCommand,
  3. VoidCallback? postLoad,
  4. dynamic live = false,
  5. ScrollController? scrollController,
  6. dynamic resetOutput = true,
})

Implementation

Future<void> liveShell(
  VoidCallback refresh, {
  Function? customCommand,
  VoidCallback? postLoad,
  live = false,
  ScrollController? scrollController,
  resetOutput = true,
  // bool scroll = false,
}) async {
  setShell(live, resetOutput);
  refresh();
  if (live) {
    controller.stream.listen((event) {
      output.add(event);
      refresh();
      if (scrollController != null) {
        debugPrint("Scrolling");
        scrollController.animateTo(
          scrollController.position.maxScrollExtent,
          duration: const Duration(milliseconds: AppConst.scrollDuration),
          curve: Curves.ease,
        );
      }
      // debugPrint("Event running: $event");
      if (AppConst.logShell) {
        debugPrint(
            "Output ${output.map((item) => item.toString()).toList()}");
      }
    });
  }
  try {
    if (customCommand != null) {
      command = customCommand();
    }
    if (path != null) {
      final dir = SystemUtils.pathFormatter(path!);
      debugPrint("Changing directory to: $dir");
      // command = "cd && cd $dir && \n$command";
      shell = shell.cd(dir);
    }
    debugPrint("Running command: $command ${output.length}");
    final result = await shell.run(command);
    if (result.isEmpty) return;
    List<String> str = result.map((item) => item.stdout.toString()).toList();
    if (!live) {
      output.addAll(str);
    }
  } on ShellException catch (ex) {
    debugPrint("Shell Exception");
    if (status != ShellStatus.stopped) {
      status = ShellStatus.failed;
    }
    if (ex.result != null && ex.result?.stderr != null) {
      debugPrint("Error: ${ex.result?.stderr}");
      errorOutput.add(ex.result?.stderr);
      // Toast.show("Failed to run command - $command",
      //     message: errorOutput.join("\n").toString());
    }
  } catch (ex) {
    errorOutput.add(ex.toString());
    status = ShellStatus.failed;
    // Toast.show("Failed to run command - $command", message: ex.toString());
  } finally {
    shell.kill();
    if (status == ShellStatus.running) {
      status = ShellStatus.successful;
    }
    if (postLoad != null) {
      postLoad();
    }
    refresh();
  }
}