config: not oncelock
This commit is contained in:
parent
488f2fb99d
commit
943f371cf1
|
@ -15,7 +15,37 @@ pub struct Config {
|
||||||
pub output_blocklist: Vec<String>,
|
pub output_blocklist: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_config() -> Res<()> {
|
pub struct ConfigHandle {
|
||||||
|
inner: tokio::sync::RwLock<Option<Config>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ConfigHandleHandle<'a>(tokio::sync::RwLockReadGuard<'a, Option<Config>>);
|
||||||
|
|
||||||
|
impl std::ops::Deref for ConfigHandleHandle<'_> {
|
||||||
|
type Target = Config;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
self.0.as_ref().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConfigHandle {
|
||||||
|
pub(super) const fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
inner: tokio::sync::RwLock::const_new(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn set(&self, config: Config) {
|
||||||
|
*self.inner.write().await = Some(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get(&self) -> ConfigHandleHandle {
|
||||||
|
ConfigHandleHandle(self.inner.read().await)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn parse_config() -> Res<()> {
|
||||||
let dirs = directories::ProjectDirs::from("com", "alexjanka", "sway-flash-indicator")
|
let dirs = directories::ProjectDirs::from("com", "alexjanka", "sway-flash-indicator")
|
||||||
.ok_or(Error::NoMatchingConfig)?;
|
.ok_or(Error::NoMatchingConfig)?;
|
||||||
|
|
||||||
|
@ -38,7 +68,7 @@ pub fn parse_config() -> Res<()> {
|
||||||
c
|
c
|
||||||
};
|
};
|
||||||
|
|
||||||
crate::CONFIG.set(config)?;
|
crate::CONFIG.set(config).await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ pub async fn interpolate_task() -> Res<()> {
|
||||||
let mut connection = swayipc_async::Connection::new().await?;
|
let mut connection = swayipc_async::Connection::new().await?;
|
||||||
|
|
||||||
let to = crate::DEFAULT_BORDER.get().ok_or(Error::NoMatchingConfig)?;
|
let to = crate::DEFAULT_BORDER.get().ok_or(Error::NoMatchingConfig)?;
|
||||||
let config = CONFIG.get().ok_or(Error::NoMatchingConfig)?;
|
let config = CONFIG.get().await;
|
||||||
let per_frame = 1.0 / config.frames_anim as f32;
|
let per_frame = 1.0 / config.frames_anim as f32;
|
||||||
let (d_l, d_a, d_b) = (
|
let (d_l, d_a, d_b) = (
|
||||||
(to.l - config.flash_colour.l) * per_frame,
|
(to.l - config.flash_colour.l) * per_frame,
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -1,4 +1,3 @@
|
||||||
use config::Config;
|
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
use lab::Lab;
|
use lab::Lab;
|
||||||
|
|
||||||
|
@ -13,8 +12,7 @@ pub mod prelude {
|
||||||
}
|
}
|
||||||
|
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
pub static CONFIG: config::ConfigHandle = config::ConfigHandle::new();
|
||||||
pub static CONFIG: tokio::sync::OnceCell<Config> = tokio::sync::OnceCell::const_new();
|
|
||||||
|
|
||||||
static DEFAULT_COLOURS: tokio::sync::OnceCell<String> = tokio::sync::OnceCell::const_new();
|
static DEFAULT_COLOURS: tokio::sync::OnceCell<String> = tokio::sync::OnceCell::const_new();
|
||||||
static DEFAULT_COLOURS_NO_INDICATOR: tokio::sync::OnceCell<String> =
|
static DEFAULT_COLOURS_NO_INDICATOR: tokio::sync::OnceCell<String> =
|
||||||
|
@ -28,7 +26,7 @@ async fn main() -> Res<()> {
|
||||||
}
|
}
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
config::parse_config()?;
|
config::parse_config().await?;
|
||||||
|
|
||||||
let mut event_connection = swayipc_async::Connection::new().await?;
|
let mut event_connection = swayipc_async::Connection::new().await?;
|
||||||
let mut autosplit_connection = swayipc_async::Connection::new().await?;
|
let mut autosplit_connection = swayipc_async::Connection::new().await?;
|
||||||
|
@ -84,7 +82,7 @@ async fn main() -> Res<()> {
|
||||||
if let Err(e) = code_trigger(
|
if let Err(e) = code_trigger(
|
||||||
&mut autosplit_connection,
|
&mut autosplit_connection,
|
||||||
id,
|
id,
|
||||||
&CONFIG.get().unwrap().output_blocklist,
|
&CONFIG.get().await.output_blocklist,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
|
@ -101,8 +99,7 @@ async fn main() -> Res<()> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let ratio = (width as f64) / (height as f64);
|
let ratio = (width as f64) / (height as f64);
|
||||||
let autosplit_ratio =
|
let autosplit_ratio = CONFIG.get().await.autosplit_ratio;
|
||||||
CONFIG.get().ok_or(Error::NoMatchingConfig)?.autosplit_ratio;
|
|
||||||
if let Err(e) = if ratio > autosplit_ratio {
|
if let Err(e) = if ratio > autosplit_ratio {
|
||||||
autosplit_connection.run_command("splith").await
|
autosplit_connection.run_command("splith").await
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue