From 3a56102ec2f2255281c8c0675e1d6f8a9f4908d9 Mon Sep 17 00:00:00 2001 From: Janis Date: Sun, 28 Nov 2021 21:42:22 +0100 Subject: [PATCH] added config file and deserialization --- Cargo.toml | 2 ++ nowm.toml | 4 ++++ src/backends/window_event.rs | 4 +++- src/main.rs | 21 +++++++++++++++++++-- src/state.rs | 5 ++++- 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 nowm.toml diff --git a/Cargo.toml b/Cargo.toml index f782450..6996d86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,5 @@ indexmap = "1.6.2" thiserror = "1.0.30" bitflags = "1.3.2" derivative = "2.2.0" +serde = { version = "1.0", features = ["derive"] } +toml = "0.5" \ No newline at end of file diff --git a/nowm.toml b/nowm.toml new file mode 100644 index 0000000..5a73047 --- /dev/null +++ b/nowm.toml @@ -0,0 +1,4 @@ +num_virtualscreens = 10 +mod_key = "Super" +gap = 5 +kill_clients_on_exit = false \ No newline at end of file diff --git a/src/backends/window_event.rs b/src/backends/window_event.rs index 89a7867..c99beba 100644 --- a/src/backends/window_event.rs +++ b/src/backends/window_event.rs @@ -24,7 +24,9 @@ pub enum KeyState { Released, } -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] +#[derive( + Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, serde::Deserialize, +)] #[repr(u8)] pub enum ModifierKey { Shift, diff --git a/src/main.rs b/src/main.rs index c290067..4be7753 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ #![allow(dead_code, unused_variables)] +use std::io::Read; + use log::{debug, error, info, trace, warn}; use log4rs::{ append::{console::ConsoleAppender, file::FileAppender}, @@ -67,8 +69,23 @@ fn main() { log_prologue(); - state::WindowManager::::new(WMConfig::default()) - .run(); + let mut config_path = std::path::PathBuf::from(env!("HOME")); + config_path.push(".config/nowm.toml"); + + let config = std::fs::File::open(config_path) + .and_then(|mut file| { + let mut content = String::new(); + file.read_to_string(&mut content)?; + Ok(content) + }) + .and_then(|content| Ok(toml::from_str::(&content)?)) + .unwrap_or_else(|e| { + warn!("error parsing config file: {}", e); + info!("falling back to default config."); + WMConfig::default() + }); + + state::WindowManager::::new(config).run(); } fn log_prologue() { diff --git a/src/state.rs b/src/state.rs index 91927f0..e706e6b 100644 --- a/src/state.rs +++ b/src/state.rs @@ -22,10 +22,13 @@ use crate::{ clients::{Client, ClientEntry, ClientKey, ClientState}, }; +use serde::Deserialize; + /** Contains static config data for the window manager, the sort of stuff you might want to be able to configure in a config file. -*/ + */ +#[derive(Debug, Deserialize)] pub struct WMConfig { num_virtualscreens: usize, mod_key: ModifierKey,