diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-04-09 04:06:44 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-04-09 04:07:14 +0300 |
| commit | b5b61c14841fda60f289cfd9c782580ea099a2a2 (patch) | |
| tree | f38fcf143d5dd99e3bc1540ac1f8f9ca83468031 /src/config.rs | |
| parent | a3ecb4e3b4151e840ad237e073d8282e1181c34a (diff) | |
| parent | 74a6161bff998c311d0d43bf6c44de5e193c23e6 (diff) | |
merge: reload keybinds
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs index bd6b93b9..b57d67f4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -90,6 +90,12 @@ pub struct LoadedConfig { pub diagnostics: Vec<String>, } +#[derive(Debug)] +pub struct LiveKeybindConfig { + pub prefix: (KeyCode, KeyModifiers), + pub keybinds: Keybinds, +} + #[derive(Debug, Deserialize)] #[serde(default)] pub struct KeysConfig { @@ -425,6 +431,16 @@ impl Config { .chain(self.ui.sound.diagnostics()) .collect() } + + pub fn live_keybinds(&self) -> Result<LiveKeybindConfig, Vec<String>> { + let (prefix_diag, prefix, keybind_diags, keybinds) = self.validated_keybinds(); + let diagnostics: Vec<String> = prefix_diag.into_iter().chain(keybind_diags).collect(); + if diagnostics.is_empty() { + Ok(LiveKeybindConfig { prefix, keybinds }) + } else { + Err(diagnostics) + } + } } fn resolve_config_relative_path(path: &Path) -> PathBuf { @@ -819,6 +835,25 @@ pub fn config_path() -> PathBuf { config_dir().join("config.toml") } +pub fn load_live_keybinds() -> Result<LiveKeybindConfig, Vec<String>> { + let path = config_path(); + if !path.exists() { + return Config::default().live_keybinds(); + } + + let content = std::fs::read_to_string(&path).map_err(|err| { + vec![format!( + "config read error: {err}; keeping current keybinds" + )] + })?; + let config = toml::from_str::<Config>(&content).map_err(|err| { + vec![format!( + "config parse error: {err}; keeping current keybinds" + )] + })?; + config.live_keybinds() +} + fn upsert_top_level_bool(content: &str, key: &str, value: bool) -> String { let replacement = format!("{key} = {value}"); let mut lines: Vec<String> = content.lines().map(|line| line.to_string()).collect(); @@ -1286,6 +1321,42 @@ rename_workspace = "g" } #[test] + fn live_keybinds_reject_invalid_keybinding() { + let config: Config = toml::from_str( + r#" +[keys] +rename_workspace = "wat" +"#, + ) + .unwrap(); + + let diagnostics = config.live_keybinds().unwrap_err(); + assert!(diagnostics + .iter() + .any(|d| d.contains("keys.rename_workspace"))); + } + + #[test] + fn live_keybinds_ignore_non_key_diagnostics() { + let config: Config = toml::from_str( + r#" +[keys] +new_workspace = "g" + +[ui.sound] +done_path = "sounds/missing.mp3" +"#, + ) + .unwrap(); + + let live = config.live_keybinds().unwrap(); + assert_eq!( + live.keybinds.new_workspace, + (KeyCode::Char('g'), KeyModifiers::empty()) + ); + } + + #[test] fn sound_table_config_parses() { let toml = r#" [ui.sound] |
