From 093ac43f806d4321674888a7cd48d4c2ecdfa7bf Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Mon, 5 Dec 2016 11:18:02 -0800 Subject: Move config path into Config type This cleans up the Config::load API significantly. Several miscellaneous comments were also added. --- src/config.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 5911fdb9..2c21428c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -194,6 +194,9 @@ pub struct Config { /// Bindings for the mouse #[serde(default)] mouse_bindings: Vec, + + /// Path where config was loaded from + config_path: Option, } impl Default for Config { @@ -206,6 +209,7 @@ impl Default for Config { colors: Default::default(), key_bindings: Vec::new(), mouse_bindings: Vec::new(), + config_path: None, } } } @@ -785,7 +789,7 @@ impl Config { /// /// 1. `$HOME/.config/alacritty.yml` /// 2. `$HOME/.alacritty.yml` - pub fn load() -> Result<(Config, PathBuf)> { + pub fn load() -> Result { let home = env::var("HOME")?; // First path @@ -849,10 +853,19 @@ impl Config { self.render_timer } - fn load_from>(path: P) -> Result<(Config, PathBuf)> { + pub fn path(&self) -> Option<&Path> { + self.config_path + .as_ref() + .map(|p| p.as_path()) + } + + fn load_from>(path: P) -> Result { let path = path.into(); let raw = Config::read_file(path.as_path())?; - Ok((serde_yaml::from_str(&raw[..])?, path)) + let mut config: Config = serde_yaml::from_str(&raw)?; + config.config_path = Some(path); + + Ok(config) } fn read_file>(path: P) -> Result { @@ -1068,7 +1081,12 @@ pub trait OnConfigReload { } impl Watcher { - pub fn new(path: PathBuf, mut handler: H) -> Watcher { + pub fn new(path: P, mut handler: H) -> Watcher + where H: OnConfigReload + Send + 'static, + P: Into + { + let path = path.into(); + Watcher(::util::thread::spawn_named("config watcher", move || { let (tx, rx) = mpsc::channel(); let mut watcher = FileWatcher::new(tx).unwrap(); @@ -1099,7 +1117,7 @@ impl Watcher { path.map(|path| { if path == config_path { match Config::load() { - Ok((config, _)) => handler.on_config_reload(config), + Ok(config) => handler.on_config_reload(config), Err(err) => err_println!("Ignoring invalid config: {}", err), } } -- cgit