hyprshell_core_lib/
listener.rs

1use crate::WarnWithDetails;
2use notify::event::{DataChange, ModifyKind};
3use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
4use std::path::Path;
5use tracing::{debug, info, trace, warn};
6
7pub fn hyprshell_config_listener<F>(file_path: &Path, callback: F) -> Option<RecommendedWatcher>
8where
9    F: Fn(&'static str) + 'static + Clone + Send,
10{
11    if !file_path.exists() {
12        debug!("unable to watch for file changes as the file doesnt exist");
13        return None;
14    }
15
16    let mut watcher = RecommendedWatcher::new(
17        move |res: notify::Result<Event>| match res {
18            Ok(event) if event.kind == EventKind::Modify(ModifyKind::Data(DataChange::Any)) => {
19                trace!("Event: {:?}", event);
20                callback("hyprshell config change");
21            }
22            Err(err) => {
23                warn!("Watch error: {:?}", err)
24            }
25            Ok(_) => {}
26        },
27        Config::default(),
28    )
29    .expect("Failed to create watcher");
30
31    info!("Starting hyprshell config reload listener");
32    watcher
33        .watch(file_path, RecursiveMode::NonRecursive)
34        .expect("Failed to start hyprshell config reload listener");
35
36    Some(watcher)
37}
38
39pub fn hyprshell_css_listener<F>(file_path: &Path, callback: F) -> Option<RecommendedWatcher>
40where
41    F: Fn(&'static str) + 'static + Clone + Send,
42{
43    if !file_path.exists() {
44        debug!("unable to watch for file changes as the file doesnt exist");
45        return None;
46    }
47
48    let mut watcher = RecommendedWatcher::new(
49        move |res: notify::Result<Event>| match res {
50            Ok(event) if event.kind == EventKind::Modify(ModifyKind::Data(DataChange::Any)) => {
51                trace!("Event: {:?}", event);
52                callback("hyprshell css change");
53            }
54            Err(err) => {
55                warn!("Watch error: {:?}", err)
56            }
57            Ok(_) => {}
58        },
59        Config::default(),
60    )
61    .expect("Failed to create watcher");
62
63    info!("Starting hyprshell css reload listener");
64    watcher
65        .watch(file_path.as_ref(), RecursiveMode::NonRecursive)
66        .expect("Failed to start hyprshell css reload listener");
67
68    Some(watcher)
69}
70
71pub fn hyprshell_config_block(file_path: &Path) {
72    if !file_path.exists() {
73        debug!("unable to watch for file changes as the file doesnt exist, exiting");
74        std::process::exit(1);
75    }
76
77    let (tx, rx) = std::sync::mpsc::channel();
78    let mut watcher = RecommendedWatcher::new(
79        move |res: notify::Result<Event>| match res {
80            Ok(event) if event.kind == EventKind::Modify(ModifyKind::Data(DataChange::Any)) => {
81                trace!("Event: {:?}", event);
82                tx.send(()).warn("Failed to send reload signal");
83            }
84            Err(err) => {
85                warn!("Watch error: {:?}", err)
86            }
87            Ok(_) => {}
88        },
89        Config::default(),
90    )
91    .expect("Failed to create watcher");
92    debug!("Starting hyprshell config reload listener");
93
94    watcher
95        .watch(file_path.as_ref(), RecursiveMode::NonRecursive)
96        .expect("Failed to start hyprshell config reload listener");
97    rx.recv().warn("Failed to receive reload signal");
98}