wiki_tui/
cli.rs

1use clap::Parser;
2
3use crate::{
4    action::{Action, ActionPacket, SearchAction},
5    config::{cache_dir, config_dir, CONFIG_FILE_NAME, THEME_FILE_NAME},
6};
7use wiki_api::languages::Language;
8
9#[derive(Parser)]
10#[command(author, version, about, long_about = None)]
11#[command(propagate_version = true)]
12struct Cli {
13    /// Search for an article
14    #[arg(value_name = "QUERY")]
15    search_query: Option<String>,
16
17    /// Override the configured search language of wikipedia. The value can be either the language
18    /// code, the name of the language in english or the native language name
19    #[arg(value_name = "LANGUAGE", short = 'l', long = "language")]
20    language: Option<Language>,
21
22    /// Override the configured logging level
23    #[arg(value_name = "LEVEL", long = "level")]
24    level: Option<tracing::level_filters::LevelFilter>,
25
26    /// Print the path to the cache directory
27    #[arg(long = "cache-dir")]
28    print_cache_dir: bool,
29
30    /// Print the path to the config file
31    #[arg(long = "config-path")]
32    print_config_path: bool,
33
34    /// Print the path to the theme configuration file
35    #[arg(long = "theme-config-path")]
36    print_theme_config_path: bool,
37
38    #[cfg(debug_assertions)]
39    #[arg(value_name = "PATH", long = "page")]
40    load_debug_page: Option<std::path::PathBuf>,
41}
42
43pub struct CliResults {
44    pub actions: Option<ActionPacket>,
45    pub log_level: Option<tracing::level_filters::LevelFilter>,
46}
47
48pub fn match_cli() -> CliResults {
49    let cli = Cli::parse();
50
51    let mut should_quit = false;
52    let mut results = CliResults {
53        actions: None,
54        log_level: None,
55    };
56
57    let mut packet = ActionPacket::default();
58
59    if let Some(language) = cli.language {
60        packet.add_action(Action::Search(SearchAction::ChangeLanguage(language)));
61    }
62
63    if let Some(level) = cli.level {
64        results.log_level = Some(level);
65    }
66
67    if let Some(search_query) = cli.search_query {
68        packet.add_action(Action::ExitSearchBar);
69        packet.add_action(Action::SwitchContextSearch);
70        packet.add_action(Action::Search(SearchAction::StartSearch(search_query)));
71    }
72
73    if cli.print_config_path {
74        let config_path = config_dir().map(|x| x.join(CONFIG_FILE_NAME));
75        println!(
76            "{}",
77            config_path
78                .map(|x| x.to_string_lossy().to_string())
79                .unwrap_or("UNKNOWN".to_string())
80        );
81        should_quit = true;
82    }
83
84    if cli.print_cache_dir {
85        let cache_dir = cache_dir();
86        println!(
87            "{}",
88            cache_dir
89                .map(|x| x.to_string_lossy().to_string())
90                .unwrap_or("UNKNOWN".to_string())
91        );
92        should_quit = true;
93    }
94
95    if cli.print_theme_config_path {
96        let theme_config_path = config_dir().map(|x| x.join(THEME_FILE_NAME));
97        println!(
98            "{}",
99            theme_config_path
100                .map(|x| x.to_string_lossy().to_string())
101                .unwrap_or("UNKNOWN".to_string())
102        );
103        should_quit = true;
104    }
105
106    #[cfg(debug_assertions)]
107    if let Some(ref debug_page) = cli.load_debug_page {
108        if let Some(page) = wiki_api::page::Page::from_path(debug_page) {
109            packet.add_action(Action::SwitchContextPage);
110            packet.add_action(Action::PageViewer(
111                crate::action::PageViewerAction::DisplayPage(page),
112            ));
113        }
114    }
115    results.actions = Some(packet);
116
117    if should_quit {
118        std::process::exit(libc::EXIT_SUCCESS)
119    }
120
121    results
122}