diff options
author | Joe Wilm <joe@jwilm.com> | 2016-12-10 23:32:12 -0800 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2016-12-11 20:23:41 -0800 |
commit | 4df29bb37700ee9f2b8e09374c6db513daae89b3 (patch) | |
tree | 94189e2ea834578ed7130699c6d11e770086200f /src/config.rs | |
parent | ed0b1cfff04903fe26f586340e036c38bbf30b33 (diff) | |
download | alacritty-4df29bb37700ee9f2b8e09374c6db513daae89b3.tar.gz alacritty-4df29bb37700ee9f2b8e09374c6db513daae89b3.zip |
Finish main.rs cleanup
* config::Monitor is more ergonomic and self-contained
* Fixed an issue with macOS resize. Previously, the terminal was marked
as dirty in the window resize handler, but the display can't do that.
Instead, the event processor returns a flag whether it was requested
to wakeup.
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 92 |
1 files changed, 59 insertions, 33 deletions
diff --git a/src/config.rs b/src/config.rs index 2c21428c..23010350 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1074,57 +1074,83 @@ impl Default for Font { } } -pub struct Watcher(::std::thread::JoinHandle<()>); +pub struct Monitor { + _thread: ::std::thread::JoinHandle<()>, + rx: mpsc::Receiver<Config>, +} pub trait OnConfigReload { - fn on_config_reload(&mut self, Config); + fn on_config_reload(&mut self); +} + +impl OnConfigReload for ::display::Notifier { + fn on_config_reload(&mut self) { + self.notify(); + } } -impl Watcher { - pub fn new<H, P>(path: P, mut handler: H) -> Watcher +impl Monitor { + /// Get pending config changes + pub fn pending_config(&self) -> Option<Config> { + let mut config = None; + while let Ok(new) = self.rx.try_recv() { + config = Some(new); + } + + config + } + pub fn new<H, P>(path: P, mut handler: H) -> Monitor where H: OnConfigReload + Send + 'static, P: Into<PathBuf> { let path = path.into(); - Watcher(::util::thread::spawn_named("config watcher", move || { - let (tx, rx) = mpsc::channel(); - let mut watcher = FileWatcher::new(tx).unwrap(); - watcher.watch(&path).expect("watch alacritty yml"); + let (config_tx, config_rx) = mpsc::channel(); - let config_path = path.as_path(); + Monitor { + _thread: ::util::thread::spawn_named("config watcher", move || { + let (tx, rx) = mpsc::channel(); + let mut watcher = FileWatcher::new(tx).unwrap(); + watcher.watch(&path).expect("watch alacritty yml"); - loop { - let event = rx.recv().expect("watcher event"); - let ::notify::Event { path, op } = event; + let config_path = path.as_path(); - if let Ok(op) = op { - // Skip events that are just a rename - if op.contains(op::RENAME) && !op.contains(op::WRITE) { - continue; - } + loop { + let event = rx.recv().expect("watcher event"); + let ::notify::Event { path, op } = event; - // Need to handle ignore for linux - if op.contains(op::IGNORED) { - if let Some(path) = path.as_ref() { - if let Err(err) = watcher.watch(&path) { - err_println!("failed to establish watch on {:?}: {:?}", path, err); - } + if let Ok(op) = op { + // Skip events that are just a rename + if op.contains(op::RENAME) && !op.contains(op::WRITE) { + continue; } - } - // Reload file - path.map(|path| { - if path == config_path { - match Config::load() { - Ok(config) => handler.on_config_reload(config), - Err(err) => err_println!("Ignoring invalid config: {}", err), + // Need to handle ignore for linux + if op.contains(op::IGNORED) { + if let Some(path) = path.as_ref() { + if let Err(err) = watcher.watch(&path) { + err_println!("failed to establish watch on {:?}: {:?}", path, err); + } } } - }); + + // Reload file + path.map(|path| { + if path == config_path { + match Config::load() { + Ok(config) => { + let _ = config_tx.send(config); + handler.on_config_reload(); + }, + Err(err) => err_println!("Ignoring invalid config: {}", err), + } + } + }); + } } - } - })) + }), + rx: config_rx, + } } } |