extractCdTarget function

String? extractCdTarget(
  1. String command
)

Extract the target directory from a cd command.

Returns null if the command is not a cd, or if no target is given (bare cd goes to $HOME).

Implementation

String? extractCdTarget(String command) {
  final cmds = extractCommands(command);
  if (cmds.isEmpty) return null;

  final cmd = cmds.first;
  final exe = cmd.executable.split('/').last;

  if (exe != 'cd' && exe != 'pushd') return null;

  // Collect first non-flag argument.
  for (final arg in cmd.arguments) {
    if (!arg.startsWith('-')) {
      return arg;
    }
  }

  // Bare cd with no args means $HOME.
  if (exe == 'cd') return '~';

  return null;
}