diff options
| author | Ivan Molodetskikh <yalterz@gmail.com> | 2024-01-18 11:02:15 +0400 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2024-01-18 11:13:36 +0400 |
| commit | 0f85c7954888b436359af1f76e3eb800817c085e (patch) | |
| tree | c09a0b6ea50fb867889dd9c5a6f616003bea126b | |
| parent | 6beef26662e4aab28bc7928ad5f3e2878a2199bd (diff) | |
| download | niri-0f85c7954888b436359af1f76e3eb800817c085e.tar.gz niri-0f85c7954888b436359af1f76e3eb800817c085e.tar.bz2 niri-0f85c7954888b436359af1f76e3eb800817c085e.zip | |
Watch config path even if it didn't exist at startup
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | niri-config/Cargo.toml | 1 | ||||
| -rw-r--r-- | niri-config/src/lib.rs | 22 | ||||
| -rw-r--r-- | src/main.rs | 37 | ||||
| -rw-r--r-- | src/niri.rs | 4 |
5 files changed, 36 insertions, 29 deletions
@@ -1744,7 +1744,6 @@ name = "niri-config" version = "0.1.0-alpha.3" dependencies = [ "bitflags 2.4.1", - "directories", "knuffel", "miette", "smithay", diff --git a/niri-config/Cargo.toml b/niri-config/Cargo.toml index 1d6acab9..3f3c61a4 100644 --- a/niri-config/Cargo.toml +++ b/niri-config/Cargo.toml @@ -9,7 +9,6 @@ repository.workspace = true [dependencies] bitflags.workspace = true -directories.workspace = true knuffel = "3.2.0" miette = "5.10.0" smithay.workspace = true diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index b9742df8..f61180f0 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -1,11 +1,10 @@ #[macro_use] extern crate tracing; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::str::FromStr; use bitflags::bitflags; -use directories::ProjectDirs; use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler}; use smithay::input::keyboard::keysyms::KEY_NoSymbol; use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE}; @@ -479,30 +478,19 @@ impl Default for DebugConfig { } impl Config { - pub fn load(path: Option<PathBuf>) -> miette::Result<(Self, PathBuf)> { + pub fn load(path: &Path) -> miette::Result<Self> { let _span = tracy_client::span!("Config::load"); Self::load_internal(path).context("error loading config") } - fn load_internal(path: Option<PathBuf>) -> miette::Result<(Self, PathBuf)> { - let path = if let Some(path) = path { - path - } else { - let mut path = ProjectDirs::from("", "", "niri") - .ok_or_else(|| miette!("error retrieving home directory"))? - .config_dir() - .to_owned(); - path.push("config.kdl"); - path - }; - - let contents = std::fs::read_to_string(&path) + fn load_internal(path: &Path) -> miette::Result<Self> { + let contents = std::fs::read_to_string(path) .into_diagnostic() .with_context(|| format!("error reading {path:?}"))?; let config = Self::parse("config.kdl", &contents).context("error parsing")?; debug!("loaded config from {path:?}"); - Ok((config, path)) + Ok(config) } pub fn parse(filename: &str, text: &str) -> Result<Self, knuffel::Error> { diff --git a/src/main.rs b/src/main.rs index b4de4656..8a8096b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,7 @@ use std::process::Command; use std::{env, mem}; use clap::{Parser, Subcommand}; +use directories::ProjectDirs; #[cfg(not(feature = "xdp-gnome-screencast"))] use dummy_pw_utils as pw_utils; use git_version::git_version; @@ -132,7 +133,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { if let Some(subcommand) = cli.subcommand { match subcommand { Sub::Validate { config } => { - Config::load(config)?; + let path = config + .or_else(default_config_path) + .expect("error getting config path"); + Config::load(&path)?; info!("config is valid"); return Ok(()); } @@ -151,13 +155,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { ); // Load the config. - let (mut config, path) = match Config::load(cli.config) { - Ok((config, path)) => (config, Some(path)), - Err(err) => { - warn!("{err:?}"); - (Config::default(), None) - } - }; + let path = cli.config.or_else(default_config_path); + + let mut config = path + .as_deref() + .and_then(|path| match Config::load(path) { + Ok(config) => Some(config), + Err(err) => { + warn!("{err:?}"); + None + } + }) + .unwrap_or_default(); + animation::ANIMATION_SLOWDOWN.store(config.debug.animation_slowdown, Ordering::Relaxed); let spawn_at_startup = mem::take(&mut config.spawn_at_startup); @@ -264,3 +274,14 @@ fn import_env_to_systemd() { } } } + +fn default_config_path() -> Option<PathBuf> { + let Some(dirs) = ProjectDirs::from("", "", "niri") else { + warn!("error retrieving home directory"); + return None; + }; + + let mut path = dirs.config_dir().to_owned(); + path.push("config.kdl"); + Some(path) +} diff --git a/src/niri.rs b/src/niri.rs index d0a7490f..e0762ae7 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -536,8 +536,8 @@ impl State { pub fn reload_config(&mut self, path: PathBuf) { let _span = tracy_client::span!("State::reload_config"); - let config = match Config::load(Some(path)) { - Ok((config, _)) => config, + let config = match Config::load(&path) { + Ok(config) => config, Err(err) => { warn!("{:?}", err.context("error loading config")); return; |
