Skip to main content

skim_run/
cli.rs

1use crate::SkimRun;
2#[cfg(feature = "apps")]
3use crate::apps;
4#[cfg(feature = "calc")]
5use crate::calc;
6#[cfg(feature = "hyprland")]
7use crate::hyprctl_clients;
8#[cfg(feature = "hyprland")]
9use crate::hyprctl_hide;
10#[cfg(feature = "paru")]
11use crate::paru;
12#[cfg(feature = "systemd")]
13use crate::systemd_services;
14
15/// Parses the CLI mode and returns the corresponding `SkimRun` implementation.
16///
17/// # Panics
18/// Panics if the mode is not enabled in this build.
19///
20/// # Errors
21/// This function does not return errors, but the returned `SkimRun` may fail at runtime.
22#[must_use]
23pub fn parse_mode(mode: &Mode) -> Box<dyn SkimRun> {
24    match mode {
25        #[cfg(feature = "apps")]
26        Mode::Apps {} => Box::new(apps::Apps),
27        #[cfg(feature = "calc")]
28        Mode::Calc { .. } => Box::new(calc::Calc),
29        #[cfg(feature = "hyprland")]
30        Mode::HyprctlClients {} => Box::new(hyprctl_clients::HyprctlClients),
31        #[cfg(feature = "hyprland")]
32        Mode::HyprctlHide { ignore_class, swap } => Box::new(hyprctl_hide::HyprctlHide {
33            ignore_class: ignore_class.clone(),
34            swap: swap.clone(),
35        }),
36        #[cfg(feature = "systemd")]
37        Mode::SystemdServices {} => Box::new(systemd_services::SystemdServices),
38        #[cfg(feature = "paru")]
39        Mode::Paru {} => Box::new(paru::Paru),
40        #[allow(unreachable_patterns)]
41        _ => panic!("This mode is not enabled in this build. Enable the corresponding feature."),
42    }
43}
44
45#[derive(Debug, clap::Parser)]
46pub struct Cli {
47    #[command(subcommand)]
48    pub mode: Mode,
49    #[arg(short, long, global = true)]
50    pub query: Option<String>,
51    #[arg(short, long, global = true, value_delimiter = ',')]
52    pub modes: Vec<String>,
53}
54#[derive(Debug, Clone, clap::Subcommand)]
55pub enum Mode {
56    #[cfg(feature = "apps")]
57    Apps {},
58    #[cfg(feature = "calc")]
59    Calc {
60        #[arg(long, default_value = "false")]
61        eval: bool,
62        expr: Vec<String>,
63    },
64    #[cfg(feature = "hyprland")]
65    HyprctlClients {},
66    #[cfg(feature = "hyprland")]
67    HyprctlHide {
68        #[arg(long)]
69        ignore_class: Option<String>,
70        #[arg(long)]
71        swap: Option<String>,
72    },
73    #[cfg(feature = "systemd")]
74    SystemdServices {},
75    #[cfg(feature = "paru")]
76    Paru {},
77}
78impl std::fmt::Display for Mode {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        let s = match self {
81            #[cfg(feature = "apps")]
82            Mode::Apps { .. } => "apps",
83            #[cfg(feature = "calc")]
84            Mode::Calc { .. } => "calc",
85            #[cfg(feature = "hyprland")]
86            Mode::HyprctlClients { .. } => "hyprctl-clients",
87            #[cfg(feature = "hyprland")]
88            Mode::HyprctlHide { .. } => "hyprctl-hide",
89            #[cfg(feature = "systemd")]
90            Mode::SystemdServices { .. } => "systemd-services",
91            #[cfg(feature = "paru")]
92            Mode::Paru { .. } => "paru",
93        };
94        write!(f, "{s}")
95    }
96}