Skip to main content

pretty_please/shell/
mod.rs

1mod bash;
2mod fish;
3mod nu;
4mod pwsh;
5mod zsh;
6
7use std::io;
8
9use clap::CommandFactory;
10use clap_complete::generate;
11
12use crate::cli::Cli;
13
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15pub enum Shell {
16    Bash,
17    Zsh,
18    Fish,
19    Pwsh,
20    Nu,
21}
22
23impl Shell {
24    pub fn init_script(self) -> &'static str {
25        match self {
26            Self::Bash => bash::INIT,
27            Self::Zsh => zsh::INIT,
28            Self::Fish => fish::INIT,
29            Self::Pwsh => pwsh::INIT,
30            Self::Nu => nu::INIT,
31        }
32    }
33
34    pub fn write_completions(self) -> io::Result<()> {
35        let mut command = Cli::command();
36        let bin_name = command.get_name().to_string();
37        let mut stdout = io::stdout();
38
39        match self {
40            Self::Bash => generate(
41                clap_complete::Shell::Bash,
42                &mut command,
43                bin_name,
44                &mut stdout,
45            ),
46            Self::Zsh => generate(
47                clap_complete::Shell::Zsh,
48                &mut command,
49                bin_name,
50                &mut stdout,
51            ),
52            Self::Fish => generate(
53                clap_complete::Shell::Fish,
54                &mut command,
55                bin_name,
56                &mut stdout,
57            ),
58            Self::Pwsh => generate(
59                clap_complete::Shell::PowerShell,
60                &mut command,
61                bin_name,
62                &mut stdout,
63            ),
64            Self::Nu => generate(
65                clap_complete_nushell::Nushell,
66                &mut command,
67                bin_name,
68                &mut stdout,
69            ),
70        };
71
72        Ok(())
73    }
74}