From 6676b4bb36159e161b6bb0edbe5b3762f43832f4 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 23 Feb 2026 01:02:54 +0900 Subject: fix(completion): auto-detect shell from $SHELL when argument omitted Composer auto-detects the shell from the $SHELL environment variable when the shell argument is not provided. Mozart previously required the argument and errored without it. Co-Authored-By: Claude Opus 4.6 --- crates/mozart/src/commands/completion.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'crates/mozart/src') diff --git a/crates/mozart/src/commands/completion.rs b/crates/mozart/src/commands/completion.rs index d095ce6..7cae278 100644 --- a/crates/mozart/src/commands/completion.rs +++ b/crates/mozart/src/commands/completion.rs @@ -4,9 +4,9 @@ use clap_complete::aot::Shell; #[derive(Args)] pub struct CompletionArgs { - /// The shell to generate completions for + /// The shell to generate completions for (auto-detected from $SHELL if omitted) #[arg(value_enum)] - pub shell: Shell, + pub shell: Option, } pub async fn execute( @@ -14,7 +14,31 @@ pub async fn execute( _cli: &super::Cli, _console: &mozart_core::console::Console, ) -> anyhow::Result<()> { + let shell = match args.shell { + Some(s) => s, + None => detect_shell()?, + }; let mut cmd = super::Cli::command(); - clap_complete::aot::generate(args.shell, &mut cmd, "mozart", &mut std::io::stdout()); + clap_complete::aot::generate(shell, &mut cmd, "mozart", &mut std::io::stdout()); Ok(()) } + +fn detect_shell() -> anyhow::Result { + let shell_env = std::env::var("SHELL") + .map_err(|_| anyhow::anyhow!("Could not auto-detect shell. Please specify one of: bash, elvish, fish, powershell, zsh"))?; + let basename = std::path::Path::new(&shell_env) + .file_name() + .and_then(|s| s.to_str()) + .unwrap_or(""); + match basename { + "bash" => Ok(Shell::Bash), + "zsh" => Ok(Shell::Zsh), + "fish" => Ok(Shell::Fish), + "elvish" => Ok(Shell::Elvish), + "pwsh" | "powershell" => Ok(Shell::PowerShell), + _ => anyhow::bail!( + "Unrecognized shell '{}'. Please specify one of: bash, elvish, fish, powershell, zsh", + basename + ), + } +} -- cgit v1.3.1