config: hot reloading

This commit is contained in:
Alex Janka 2024-07-30 16:27:09 +10:00
parent 943f371cf1
commit 8322bbc051
4 changed files with 199 additions and 8 deletions

160
Cargo.lock generated
View file

@ -87,6 +87,12 @@ dependencies = [
"rustc-demangle",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.6.0"
@ -120,6 +126,15 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.20"
@ -203,6 +218,27 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a"
[[package]]
name = "filetime"
version = "0.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ee447700ac8aa0b2f2bd7bc4462ad686ba06baa6727ac149a2d6277f0d240fd"
dependencies = [
"cfg-if",
"libc",
"redox_syscall 0.4.1",
"windows-sys 0.52.0",
]
[[package]]
name = "fsevent-sys"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
dependencies = [
"libc",
]
[[package]]
name = "futures-core"
version = "0.3.30"
@ -310,6 +346,26 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "inotify"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff"
dependencies = [
"bitflags 1.3.2",
"inotify-sys",
"libc",
]
[[package]]
name = "inotify-sys"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
dependencies = [
"libc",
]
[[package]]
name = "is-terminal"
version = "0.4.12"
@ -327,6 +383,26 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "kqueue"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7447f1ca1b7b563588a205fe93dea8df60fd981423a768bc1c0ded35ed147d0c"
dependencies = [
"kqueue-sys",
"libc",
]
[[package]]
name = "kqueue-sys"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b"
dependencies = [
"bitflags 1.3.2",
"libc",
]
[[package]]
name = "lab"
version = "0.11.0"
@ -345,7 +421,7 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"
dependencies = [
"bitflags",
"bitflags 2.6.0",
"libc",
]
@ -386,6 +462,18 @@ dependencies = [
"adler",
]
[[package]]
name = "mio"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"log",
"wasi",
"windows-sys 0.48.0",
]
[[package]]
name = "mio"
version = "1.0.1"
@ -398,6 +486,36 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "notify"
version = "6.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d"
dependencies = [
"bitflags 2.6.0",
"crossbeam-channel",
"filetime",
"fsevent-sys",
"inotify",
"kqueue",
"libc",
"log",
"mio 0.8.11",
"walkdir",
"windows-sys 0.48.0",
]
[[package]]
name = "notify-debouncer-mini"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d40b221972a1fc5ef4d858a2f671fb34c75983eb385463dff3780eeff6a9d43"
dependencies = [
"crossbeam-channel",
"log",
"notify",
]
[[package]]
name = "object"
version = "0.36.2"
@ -437,7 +555,7 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"redox_syscall 0.5.3",
"smallvec",
"windows-targets 0.52.6",
]
@ -497,13 +615,22 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "redox_syscall"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4"
dependencies = [
"bitflags",
"bitflags 2.6.0",
]
[[package]]
@ -558,7 +685,7 @@ version = "0.38.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f"
dependencies = [
"bitflags",
"bitflags 2.6.0",
"errno",
"libc",
"linux-raw-sys",
@ -571,6 +698,15 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
@ -653,12 +789,14 @@ dependencies = [
[[package]]
name = "sway-flash-indicator"
version = "0.3.1"
version = "0.4.0"
dependencies = [
"directories",
"futures-util",
"lab",
"log",
"notify",
"notify-debouncer-mini",
"pretty_env_logger",
"serde",
"swayipc-async",
@ -741,7 +879,7 @@ dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"mio 1.0.1",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
@ -817,6 +955,16 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View file

@ -1,6 +1,6 @@
[package]
name = "sway-flash-indicator"
version = "0.3.1"
version = "0.4.0"
edition = "2021"
[dependencies]
@ -14,3 +14,5 @@ directories = "5.0.1"
toml = "0.8.15"
serde = { version = "1.0.204", features = ["derive"] }
thiserror = "1.0.63"
notify-debouncer-mini = "0.4.1"
notify = "6.1.1"

View file

@ -1,7 +1,7 @@
# Maintainer: Alex Janka <alex@alexjanka.com>
pkgname=sway-flash-indicator
pkgver=0.3.1
pkgver=0.4.0
pkgrel=1
pkgdesc="flashes sway indicator border rather than always showing it"
arch=('x86_64' 'aarch64')

View file

@ -40,6 +40,10 @@ impl ConfigHandle {
*self.inner.write().await = Some(config)
}
fn set_blocking(&self, config: Config) {
*self.inner.blocking_write() = Some(config);
}
pub async fn get(&self) -> ConfigHandleHandle {
ConfigHandleHandle(self.inner.read().await)
}
@ -67,12 +71,49 @@ pub async fn parse_config() -> Res<()> {
log::info!("wrote default to {config_path:?}");
c
};
let path = config_path.clone();
tokio::task::spawn(async move {
let mut debouncer = notify_debouncer_mini::new_debouncer(
std::time::Duration::from_secs(1),
move |res: notify_debouncer_mini::DebounceEventResult| match res {
Ok(events) => events.iter().for_each(|e| {
if e.kind == notify_debouncer_mini::DebouncedEventKind::Any {
if let Some(config) = read_config(&path) {
CONFIG.set_blocking(config);
} else {
log::error!("failed to parse config");
}
}
}),
Err(e) => println!("Error {:?}", e),
},
)
.unwrap();
debouncer
.watcher()
.watch(&config_path, notify::RecursiveMode::Recursive)
.unwrap();
loop {
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
}
});
crate::CONFIG.set(config).await;
Ok(())
}
fn read_config(path: &std::path::PathBuf) -> Option<Config> {
if path.try_exists().is_ok_and(|v| v) {
let v = toml::from_str(&std::fs::read_to_string(path).ok()?).ok()?;
log::info!("read config from {path:?}");
Some(v)
} else {
None
}
}
impl Default for Config {
fn default() -> Self {
Self {