detectShell function

ShellConfig detectShell({
  1. Map<String, String>? env,
})

Detect the user's shell.

Implementation

ShellConfig detectShell({Map<String, String>? env}) {
  final environment = env ?? Platform.environment;

  if (Platform.isWindows) {
    final comspec = environment['COMSPEC'];
    if (comspec != null && comspec.toLowerCase().contains('powershell')) {
      return ShellConfig(type: ShellType.powershell, shellPath: comspec);
    }
    return ShellConfig(type: ShellType.cmd, shellPath: comspec ?? 'cmd.exe');
  }

  final shell = environment['SHELL'] ?? '/bin/sh';
  final shellName = shell.split('/').last;

  final type = switch (shellName) {
    'bash' => ShellType.bash,
    'zsh' => ShellType.zsh,
    'fish' => ShellType.fish,
    'sh' || 'dash' => ShellType.sh,
    _ => ShellType.sh,
  };

  return ShellConfig(type: type, shellPath: shell);
}