hyprshell_core_lib/config/
explain.rs

1use crate::config::{Config, load_and_migrate_config};
2use crate::daemon_running;
3use std::path::Path;
4use tracing::error;
5
6pub fn explain_config(config_path: &Path) -> anyhow::Result<()> {
7    let config = load_and_migrate_config(config_path)
8        .inspect_err(|err| error!("{BOLD}{RED}Config is invalid:{RESET} {err:?}\n"))?;
9    let info = explain(config);
10    println!("{info}");
11
12    if daemon_running() {
13        println!("Daemon {GREEN}running{RESET}")
14    } else {
15        println!(
16            "Daemon {RED}not running{RESET}, start it with `hyprshell run` or `systemctl --user enable --now hyprshell`"
17        );
18    }
19    Ok(())
20}
21
22const BOLD: &str = "\x1b[1m";
23const BLUE: &str = "\x1b[34m";
24const RED: &str = "\x1b[31m";
25const GREEN: &str = "\x1b[32m";
26const RESET: &str = "\x1b[0m";
27
28pub fn explain(config: Config) -> String {
29    let mut builder = format!(
30        "{BOLD}{GREEN}Config is valid{RESET}\n{BOLD}Explanation{RESET} ({BLUE}blue{RESET} are keys, {BOLD}{BLUE}bold blue{RESET} keys can be configured in config):{RESET}\n\n"
31    );
32
33    if let Some(windows) = &config.windows {
34        if let Some(overview) = &windows.overview {
35            builder.push_str(&format!(
36                "Use {BOLD}{BLUE}{} + {}{RESET} to open the Overview. Use {BLUE}tab{RESET} and {BLUE}grave{RESET} / {BLUE}shift + tab{RESET} to select a different window, press {BLUE}return{RESET} to switch\n\
37                You can also use the {BLUE}arrow keys{RESET} to navigate the workspaces. Use {BLUE}Esc{RESET} to close the overview.\n",
38                overview.modifier,
39                overview.key,
40            ));
41            builder.push_str("After opening the Overview the launcher is available.\n");
42            if let Some(_applications) = overview.launcher.plugins.applications.as_ref() {
43                builder.push_str(&format!("Start typing to search through applications (sorted by how often they were opened).\n\
44                    Press {BLUE}return{RESET} to launch the first app, use {BLUE}Ctrl + 1/2/3/...{RESET} to open the second, third, etc.\n"));
45            }
46            if overview.launcher.plugins.terminal.is_some() {
47                builder.push_str(&format!(
48                    "Press {BLUE}Ctrl + t{RESET} to run the typed command in a terminal.\n"
49                ));
50            }
51            if overview.launcher.plugins.shell.is_some() {
52                builder.push_str(&format!(
53                    "Press {BLUE}Ctrl + r{RESET} to run the typed command in the background.\n",
54                ));
55            }
56            if let Some(engines) = &overview.launcher.plugins.websearch {
57                builder.push_str(&format!("Press {BLUE}Ctrl + {BOLD}{BLUE}<key>{RESET} to search the typed text in any of the configured SearchEngines: {}.\n",
58                                              engines.engines.iter().map(|e| e.name.to_string()).collect::<Vec<_>>().join(", ")));
59            }
60            if overview.launcher.plugins.calc.is_some() {
61                builder.push_str(
62                    "Typing a mathematical expression will calculate it and display the result in the launcher.\n",
63                );
64            }
65        } else {
66            builder.push_str("<Overview disabled>\n");
67        };
68    };
69
70    builder.push('\n');
71
72    if let Some(windows) = &config.windows {
73        if let Some(switch) = &windows.switch {
74            builder.push_str(&format!(
75                "Press {BOLD}{BLUE}{}{RESET} + {BLUE}tab{RESET} and hold {BOLD}{BLUE}{}{RESET} to view recently used applications. Press {BLUE}tab{RESET} and {BLUE}grave{RESET}/{BLUE}shift + tab{RESET} to select a different window,\n\
76                release {BOLD}{BLUE}{}{RESET} to close the window. You can also use the {BLUE}arrow keys{RESET} to navigate the clients.\n",
77                switch.modifier,
78                switch.modifier,
79                switch.modifier,
80            ));
81        } else {
82            builder.push_str("<Recent Apps disabled>\n");
83        };
84    }
85
86    builder
87}