leadr/
shell.rs

1use crate::{Config, LeadrError, keymap::to_ascii};
2
3const BASH_INIT_TEMPLATE: &str = include_str!("../shell/init.bash");
4const ZSH_INIT_TEMPLATE: &str = include_str!("../shell/init.zsh");
5
6/// Generates a bash script that handles the resulting command and binds it to the leadr key.
7pub fn init_bash(config: &Config) -> Result<String, LeadrError> {
8    let leader_key = to_ascii(&config.leadr_key)?;
9
10    Ok(BASH_INIT_TEMPLATE.replace("{{bind_key}}", &leader_key))
11}
12
13/// Generates a zsh script that handles the resulting command and binds it to the leadr key.
14pub fn init_zsh(config: &Config) -> Result<String, LeadrError> {
15    let leader_key = to_ascii(&config.leadr_key)?;
16
17    Ok(ZSH_INIT_TEMPLATE.replace("{{bind_key}}", &leader_key))
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    use crate::Config;
24
25    #[test]
26    fn test_bash_script_contains_replacements() {
27        let config = Config::default();
28        let result = init_bash(&config).unwrap();
29        assert!(result.contains("\\x07"));
30    }
31
32    #[test]
33    fn test_zsh_script_contains_replacements() {
34        let config = Config::default();
35        let result = init_zsh(&config).unwrap();
36        assert!(result.contains("\\x07"));
37    }
38}