-
Notifications
You must be signed in to change notification settings - Fork 409
/
completions.rs
67 lines (60 loc) 路 1.61 KB
/
completions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use super::command::Command;
use crate::config::FnmConfig;
use crate::shell::{infer_shell, Shell};
use crate::{cli::Cli, shell::Shells};
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::{Generator, Shell as ClapShell};
use thiserror::Error;
#[derive(Parser, Debug)]
pub struct Completions {
/// The shell syntax to use. Infers when missing.
#[clap(long)]
shell: Option<Shells>,
}
impl Command for Completions {
type Error = Error;
fn apply(self, _config: &FnmConfig) -> Result<(), Self::Error> {
let mut stdio = std::io::stdout();
let shell: Box<dyn Shell> = self
.shell
.map(Into::into)
.or_else(|| infer_shell().map(Into::into))
.ok_or(Error::CantInferShell)?;
let shell: ClapShell = shell.into();
let mut app = Cli::command();
app.build();
shell.generate(&app, &mut stdio);
Ok(())
}
}
#[derive(Error, Debug)]
pub enum Error {
#[error(
"{}\n{}\n{}\n{}",
"Can't infer shell!",
"fnm can't infer your shell based on the process tree.",
"Maybe it is unsupported? we support the following shells:",
shells_as_string()
)]
CantInferShell,
}
fn shells_as_string() -> String {
Shells::value_variants()
.iter()
.map(|x| format!("* {x}"))
.collect::<Vec<_>>()
.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(not(windows))]
fn test_smoke() {
let config = FnmConfig::default();
Completions {
shell: Some(Shells::Bash),
}
.call(config);
}
}