This commit is contained in:
filipriec
2025-02-23 09:40:14 +01:00
parent 9fa63f4467
commit d6b465fe46

View File

@@ -1,6 +1,8 @@
// src/client/config.rs // client/src/config.rs
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::Path;
use crossterm::event::{KeyCode, KeyModifiers}; use crossterm::event::{KeyCode, KeyModifiers};
#[derive(Debug, Deserialize, Default)] #[derive(Debug, Deserialize, Default)]
@@ -21,13 +23,23 @@ pub struct Config {
} }
impl Config { impl Config {
/// Loads the configuration from "config.toml" in the client crate directory.
pub fn load() -> Result<Self, Box<dyn std::error::Error>> { pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
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)?; let config: Config = toml::from_str(&config_str)?;
Ok(config) 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 { for (action, bindings) in &self.keybindings {
// Skip mode toggle actions // Skip mode toggle actions
if action == "enter_edit_mode" || action == "exit_edit_mode" { if action == "enter_edit_mode" || action == "exit_edit_mode" {
@@ -42,7 +54,11 @@ impl Config {
None 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 parts: Vec<&str> = binding.split('+').collect();
let mut expected_modifiers = KeyModifiers::empty(); let mut expected_modifiers = KeyModifiers::empty();
let mut expected_key = None; let mut expected_key = None;
@@ -83,6 +99,7 @@ impl Config {
false false
} }
} }
pub fn is_exit_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool { pub fn is_exit_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool {
if let Some(bindings) = self.keybindings.get("exit_edit_mode") { if let Some(bindings) = self.keybindings.get("exit_edit_mode") {
bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers)) bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers))