runInteractiveRepl function

Future<int> runInteractiveRepl()

Implementation

Future<int> runInteractiveRepl() async {
  stdout.write('''
Type 'help' for all commands.

Common commands:
  h, help       Show command help
  s, status     Show whether the app is running
  d, describe   Describe the current screen and refs
  tap @1        Tap an element from describe output
  back          Navigate back
  r, reload     Hot reload
  R, restart    Hot restart
  kill          Stop the app and exit
  detach        Exit this REPL and leave the app running
  q, quit       Stop the app and exit

''');

  while (true) {
    stdout.write('fdb> ');
    await stdout.flush();

    final line = stdin.readLineSync(encoding: utf8);
    if (line == null) {
      stdout.writeln();
      return 0;
    }

    final trimmed = line.trim();
    if (trimmed.isEmpty) continue;

    List<String> parts;
    try {
      parts = splitCommandLine(trimmed);
    } on FormatException catch (e) {
      stderr.writeln('ERROR: ${e.message}');
      continue;
    }
    if (parts.isEmpty) continue;

    final commandName = _expandAlias(parts.first);
    final args = parts.sublist(1);

    final metaCommand = _replMetaCommands[commandName];
    if (metaCommand != null) {
      final exitCode = await metaCommand(args);
      if (exitCode != null) return exitCode;
      continue;
    }

    final command = CliCommand.fromWireName(commandName);
    if (command == null) {
      stderr.writeln('ERROR: Unknown command: $commandName');
      continue;
    }
    await _runCommand(command, args);
  }
}