added config file and deserialization

This commit is contained in:
Janis 2021-11-28 21:42:22 +01:00
parent d3b4fcbf18
commit 3a56102ec2
5 changed files with 32 additions and 4 deletions

View file

@ -16,3 +16,5 @@ indexmap = "1.6.2"
thiserror = "1.0.30" thiserror = "1.0.30"
bitflags = "1.3.2" bitflags = "1.3.2"
derivative = "2.2.0" derivative = "2.2.0"
serde = { version = "1.0", features = ["derive"] }
toml = "0.5"

4
nowm.toml Normal file
View file

@ -0,0 +1,4 @@
num_virtualscreens = 10
mod_key = "Super"
gap = 5
kill_clients_on_exit = false

View file

@ -24,7 +24,9 @@ pub enum KeyState {
Released, Released,
} }
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] #[derive(
Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, serde::Deserialize,
)]
#[repr(u8)] #[repr(u8)]
pub enum ModifierKey { pub enum ModifierKey {
Shift, Shift,

View file

@ -1,5 +1,7 @@
#![allow(dead_code, unused_variables)] #![allow(dead_code, unused_variables)]
use std::io::Read;
use log::{debug, error, info, trace, warn}; use log::{debug, error, info, trace, warn};
use log4rs::{ use log4rs::{
append::{console::ConsoleAppender, file::FileAppender}, append::{console::ConsoleAppender, file::FileAppender},
@ -67,8 +69,23 @@ fn main() {
log_prologue(); log_prologue();
state::WindowManager::<backends::xlib::XLib>::new(WMConfig::default()) let mut config_path = std::path::PathBuf::from(env!("HOME"));
.run(); 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::<WMConfig>(&content)?))
.unwrap_or_else(|e| {
warn!("error parsing config file: {}", e);
info!("falling back to default config.");
WMConfig::default()
});
state::WindowManager::<backends::xlib::XLib>::new(config).run();
} }
fn log_prologue() { fn log_prologue() {

View file

@ -22,10 +22,13 @@ use crate::{
clients::{Client, ClientEntry, ClientKey, ClientState}, clients::{Client, ClientEntry, ClientKey, ClientState},
}; };
use serde::Deserialize;
/** /**
Contains static config data for the window manager, the sort of stuff you might want to Contains static config data for the window manager, the sort of stuff you might want to
be able to configure in a config file. be able to configure in a config file.
*/ */
#[derive(Debug, Deserialize)]
pub struct WMConfig { pub struct WMConfig {
num_virtualscreens: usize, num_virtualscreens: usize,
mod_key: ModifierKey, mod_key: ModifierKey,