From d6b465fe46e7d00d7823a95b3229a83ea7569803 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 23 Feb 2025 09:40:14 +0100 Subject: [PATCH] working --- client/src/config.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/client/src/config.rs b/client/src/config.rs index 0395742..7be38f9 100644 --- a/client/src/config.rs +++ b/client/src/config.rs @@ -1,6 +1,8 @@ -// src/client/config.rs +// client/src/config.rs + use serde::Deserialize; use std::collections::HashMap; +use std::path::Path; use crossterm::event::{KeyCode, KeyModifiers}; #[derive(Debug, Deserialize, Default)] @@ -21,13 +23,23 @@ pub struct Config { } impl Config { + /// Loads the configuration from "config.toml" in the client crate directory. pub fn load() -> Result> { - let config_str = std::fs::read_to_string("config.toml")?; + // env!("CARGO_MANIFEST_DIR") resolves to the directory where the Cargo.toml of + // the client crate is located. + let manifest_dir = env!("CARGO_MANIFEST_DIR"); + let config_path = Path::new(manifest_dir).join("config.toml"); + let config_str = std::fs::read_to_string(&config_path) + .map_err(|e| format!("Failed to read config file at {:?}: {}", config_path, e))?; let config: Config = toml::from_str(&config_str)?; Ok(config) } - pub fn get_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> { + pub fn get_action_for_key( + &self, + key: KeyCode, + modifiers: KeyModifiers, + ) -> Option<&str> { for (action, bindings) in &self.keybindings { // Skip mode toggle actions if action == "enter_edit_mode" || action == "exit_edit_mode" { @@ -42,7 +54,11 @@ impl Config { None } - fn matches_keybinding(binding: &str, key: KeyCode, modifiers: KeyModifiers) -> bool { + fn matches_keybinding( + binding: &str, + key: KeyCode, + modifiers: KeyModifiers, + ) -> bool { let parts: Vec<&str> = binding.split('+').collect(); let mut expected_modifiers = KeyModifiers::empty(); let mut expected_key = None; @@ -83,6 +99,7 @@ impl Config { false } } + pub fn is_exit_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool { if let Some(bindings) = self.keybindings.get("exit_edit_mode") { bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers))