use crate::error::CliError;
use clap::Command;
use clap_complete::{generate, Generator};
use std::io;
pub fn generate_completion_for_command<G: Generator>(
generator: G,
cmd: &mut Command,
) -> Result<(), CliError> {
generate(
generator,
cmd,
cmd.get_name().to_string(),
&mut io::stdout(),
);
Ok(())
}
#[must_use]
pub fn print_installation_instructions(shell: &str) -> String {
match shell.to_lowercase().as_str() {
"bash" => {
r#"# Bash completion installation:
# For current session only:
eval "$(hedl completion bash)"
# For persistent installation, add to your ~/.bashrc:
echo 'eval "$(hedl completion bash)"' >> ~/.bashrc
# Or save to completions directory:
hedl completion bash > ~/.local/share/bash-completion/completions/hedl
"#
}
"zsh" => {
r#"# Zsh completion installation:
# For current session only:
eval "$(hedl completion zsh)"
# For persistent installation, add to your ~/.zshrc:
echo 'eval "$(hedl completion zsh)"' >> ~/.zshrc
# Or save to completions directory (ensure directory is in $fpath):
hedl completion zsh > ~/.zsh/completions/_hedl
"#
}
"fish" => {
r"# Fish completion installation:
# Save to fish completions directory:
hedl completion fish > ~/.config/fish/completions/hedl.fish
# Completions will be available in new fish sessions
"
}
"powershell" | "pwsh" => {
r"# PowerShell completion installation:
# For current session only:
hedl completion powershell | Out-String | Invoke-Expression
# For persistent installation, add to your PowerShell profile:
# Find profile location with: $PROFILE
# Then add this line:
hedl completion powershell | Out-String | Invoke-Expression
"
}
"elvish" => {
r"# Elvish completion installation:
# For current session only:
eval (hedl completion elvish)
# For persistent installation, add to your ~/.elvish/rc.elv:
eval (hedl completion elvish)
"
}
_ => "Unsupported shell",
}
.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_installation_instructions_bash() {
let instructions = print_installation_instructions("bash");
assert!(!instructions.is_empty());
assert!(instructions.to_lowercase().contains("bash"));
}
#[test]
fn test_installation_instructions_zsh() {
let instructions = print_installation_instructions("zsh");
assert!(!instructions.is_empty());
assert!(instructions.contains("zsh"));
}
#[test]
fn test_installation_instructions_fish() {
let instructions = print_installation_instructions("fish");
assert!(!instructions.is_empty());
assert!(instructions.contains("fish"));
}
#[test]
fn test_installation_instructions_powershell() {
let instructions = print_installation_instructions("powershell");
assert!(!instructions.is_empty());
assert!(instructions.to_lowercase().contains("powershell"));
}
#[test]
fn test_installation_instructions_elvish() {
let instructions = print_installation_instructions("elvish");
assert!(!instructions.is_empty());
assert!(instructions.contains("elvish"));
}
#[test]
fn test_installation_instructions_case_insensitive() {
let lower = print_installation_instructions("bash");
let upper = print_installation_instructions("BASH");
let mixed = print_installation_instructions("Bash");
assert_eq!(lower, upper);
assert_eq!(lower, mixed);
}
#[test]
fn test_installation_instructions_unsupported() {
let instructions = print_installation_instructions("invalid");
assert_eq!(instructions, "Unsupported shell");
}
}