ghosttyTerminalShellLaunches function

List<GhosttyTerminalShellLaunch> ghosttyTerminalShellLaunches({
  1. required GhosttyTerminalShellProfile profile,
  2. Map<String, String>? platformEnvironment,
  3. Map<String, String> environmentOverrides = const <String, String>{'TERM' : 'xterm-256color'},
  4. GhosttyTerminalShellEnvironmentOptions environmentOptions = const GhosttyTerminalShellEnvironmentOptions(),
  5. bool includeSetupCommands = true,
})

Resolves one or more native shell launch plans for a given profile.

The returned launches already include a normalized environment via ghosttyTerminalShellEnvironment.

Implementation

List<GhosttyTerminalShellLaunch> ghosttyTerminalShellLaunches({
  required GhosttyTerminalShellProfile profile,
  Map<String, String>? platformEnvironment,
  Map<String, String> environmentOverrides = const <String, String>{
    'TERM': 'xterm-256color',
  },
  GhosttyTerminalShellEnvironmentOptions environmentOptions =
      const GhosttyTerminalShellEnvironmentOptions(),
  bool includeSetupCommands = true,
}) {
  final effectivePlatformEnvironment =
      platformEnvironment ?? ghosttyTerminalPlatformEnvironment();
  final shellEnvironment = ghosttyTerminalShellEnvironment(
    platformEnvironment: effectivePlatformEnvironment,
    overrides: environmentOverrides,
    options: environmentOptions,
  );

  if (platform_shell.ghosttyTerminalPlatformIsWindows()) {
    final command = effectivePlatformEnvironment['ComSpec'];
    if (command == null || command.isEmpty) {
      return const <GhosttyTerminalShellLaunch>[];
    }
    return <GhosttyTerminalShellLaunch>[
      GhosttyTerminalShellLaunch(
        label: 'cmd.exe',
        shell: command,
        environment: shellEnvironment,
      ),
    ];
  }

  GhosttyTerminalShellLaunch? bashLaunch() {
    final bash = platform_shell.ghosttyTerminalResolveFirstExistingShell(
      const <String>['/bin/bash', '/usr/bin/bash'],
    );
    if (bash == null) {
      return null;
    }
    return GhosttyTerminalShellLaunch(
      label: 'clean bash shell',
      shell: bash,
      arguments: const <String>['--noprofile', '--norc', '-i'],
      environment: shellEnvironment,
      setupCommand: includeSetupCommands ? "export PS1='> '\n" : null,
    );
  }

  GhosttyTerminalShellLaunch? zshLaunch() {
    final zsh = platform_shell.ghosttyTerminalResolveFirstExistingShell(
      const <String>['/bin/zsh', '/usr/bin/zsh'],
    );
    if (zsh == null) {
      return null;
    }
    return GhosttyTerminalShellLaunch(
      label: 'clean zsh shell',
      shell: zsh,
      arguments: const <String>['-f', '-i'],
      environment: shellEnvironment,
      setupCommand: includeSetupCommands
          ? "PROMPT='%# '\nRPROMPT=\nunsetopt TRANSIENT_RPROMPT\n"
                "stty erase '^?'\n"
                "bindkey '^?' backward-delete-char\n"
                "bindkey '^H' backward-delete-char\n"
          : null,
    );
  }

  GhosttyTerminalShellLaunch? shLaunch() {
    final sh = platform_shell.ghosttyTerminalResolveFirstExistingShell(
      const <String>['/bin/sh', '/usr/bin/sh'],
    );
    if (sh == null) {
      return null;
    }
    return GhosttyTerminalShellLaunch(
      label: 'clean sh shell',
      shell: sh,
      arguments: const <String>['-i'],
      environment: shellEnvironment,
      setupCommand: includeSetupCommands ? "PS1='> '\n" : null,
    );
  }

  GhosttyTerminalShellLaunch? userShellLaunch() {
    final shell = effectivePlatformEnvironment['SHELL'];
    if (shell == null || shell.isEmpty) {
      return null;
    }
    return GhosttyTerminalShellLaunch(
      label: 'user shell',
      shell: shell,
      arguments: const <String>['-i'],
      environment: shellEnvironment,
    );
  }

  List<GhosttyTerminalShellLaunch> maybeLaunch(
    GhosttyTerminalShellLaunch? launch,
  ) {
    if (launch == null) {
      return const <GhosttyTerminalShellLaunch>[];
    }
    return <GhosttyTerminalShellLaunch>[launch];
  }

  return switch (profile) {
    GhosttyTerminalShellProfile.auto => <GhosttyTerminalShellLaunch>[
      ...maybeLaunch(bashLaunch()),
      ...maybeLaunch(zshLaunch()),
      ...maybeLaunch(shLaunch()),
    ],
    GhosttyTerminalShellProfile.cleanBash => <GhosttyTerminalShellLaunch>[
      ...maybeLaunch(bashLaunch()),
    ],
    GhosttyTerminalShellProfile.cleanZsh => <GhosttyTerminalShellLaunch>[
      ...maybeLaunch(zshLaunch()),
    ],
    GhosttyTerminalShellProfile.userShell => <GhosttyTerminalShellLaunch>[
      ...maybeLaunch(userShellLaunch()),
    ],
  };
}