aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs92
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,
+ }
}
}