1use std::path::PathBuf;
2
3use clap::{Parser, Subcommand};
4use console::style;
5use dialoguer::{Select, theme::ColorfulTheme};
6use kairo_core::{Result, Url, UrlHandlerApp};
7
8#[derive(Parser, Debug)]
10#[command(version, author, about, long_about = None)]
11#[command(next_line_help = true)]
12pub struct Cli {
13 #[command(subcommand)]
14 command: Commands,
15
16 #[arg(long, default_value = None, global = true)]
18 search_paths: Option<Vec<std::path::PathBuf>>,
19
20 #[command(flatten)]
21 verbose: clap_verbosity::Verbosity<clap_verbosity::WarnLevel>,
22}
23
24impl Cli {
25 pub fn new() -> Self {
26 Cli::parse()
27 }
28
29 pub fn run(&self) -> Result<()> {
30 pretty_env_logger::formatted_builder()
31 .filter_level(self.verbose.log_level_filter())
32 .init();
33
34 self.command.process()
35 }
36}
37
38impl Default for Cli {
39 fn default() -> Self {
40 Self::new()
41 }
42}
43
44#[derive(Subcommand, Debug)]
45enum Commands {
46 List {
48 #[arg(
50 short,
51 long,
52 conflicts_with("scheme"),
53 required_unless_present("scheme")
54 )]
55 url: Option<Url>,
56
57 #[arg(short, long, conflicts_with("url"), required_unless_present("url"))]
59 scheme: Option<String>,
60 },
61
62 Open {
64 url: Url,
66
67 #[arg(long, default_value = "false")]
69 no_prompt: bool,
70 },
71}
72
73impl Commands {
74 fn process(&self) -> Result<()> {
75 match self {
76 Commands::List { url, scheme } => Self::list(url.clone(), scheme.clone(), None),
77 Commands::Open { url, no_prompt } => Self::open(url.clone(), None, *no_prompt),
78 }
79 }
80
81 fn list(
82 url: Option<Url>,
83 scheme: Option<String>,
84 search_paths: Option<Vec<PathBuf>>,
85 ) -> Result<()> {
86 let scheme = match (url, scheme) {
87 (Some(url), _) => url.scheme().to_string(),
88 (_, Some(scheme)) => scheme,
89 _ => unreachable!(),
90 };
91 let apps = UrlHandlerApp::handlers_for_scheme(&scheme, None, search_paths)?;
92
93 println!(
94 "{: <16} {}",
95 style("App ID").bold().green(),
96 style("Name").bold().green()
97 );
98
99 for app in apps {
100 println!("{:<16} {}", app.appid, app.name);
101 }
102
103 Ok(())
104 }
105
106 fn open(url: Url, search_paths: Option<Vec<PathBuf>>, no_prompt: bool) -> Result<()> {
107 let apps = UrlHandlerApp::handlers_for_scheme(url.scheme(), None, search_paths)?;
108
109 if no_prompt || apps.len() == 1 {
110 return Self::open_with_app(&apps[0], url);
111 }
112
113 let app_names: Vec<String> = apps
114 .iter()
115 .map(|app| format!("{:<16} {}", app.appid, app.name))
116 .collect();
117 let selection = Select::with_theme(&ColorfulTheme::default())
118 .with_prompt("Select an application to open the URL with (press ESC or q to cancel)")
119 .report(false)
120 .default(0)
122 .items(&app_names)
123 .interact_opt()
124 .unwrap();
125
126 if let Some(selection) = selection {
127 return Self::open_with_app(&apps[selection], url);
128 }
129
130 Ok(())
131 }
132
133 fn open_with_app(app: &UrlHandlerApp, url: Url) -> Result<()> {
134 println!("Opening URL with {}...", style(&app.name).bold().green());
135 app.open_url(url)
136 }
137}
138
139pub fn run() -> Result<()> {
140 let cli = Cli::new();
141 cli.run()
142}