parse config

This commit is contained in:
Alex Janka 2024-07-24 15:29:41 +10:00
parent 981501613a
commit e1e3265e70

View file

@ -1,6 +1,17 @@
use futures_util::StreamExt;
use lab::Lab;
#[derive(Debug)]
struct NoMatchingConfig;
impl std::fmt::Display for NoMatchingConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "no config for client.focused")
}
}
impl std::error::Error for NoMatchingConfig {}
const INTERPOLATE_FROM: Lab = Lab {
l: 53.2,
a: 80.1,
@ -13,6 +24,9 @@ const FRAMES_ANIM: u32 = 20;
const REFRESH_RATE: u64 = 60;
const DUR_PER_FRAME: std::time::Duration = std::time::Duration::from_millis(1000 / REFRESH_RATE);
static DEFAULT_COLOURS: tokio::sync::OnceCell<String> = tokio::sync::OnceCell::const_new();
static DEFAULT_BORDER: tokio::sync::OnceCell<Lab> = tokio::sync::OnceCell::const_new();
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
if std::env::var_os("RUST_LOG").is_none() {
@ -20,7 +34,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
pretty_env_logger::init();
let event_connection = swayipc_async::Connection::new().await?;
let mut event_connection = swayipc_async::Connection::new().await?;
get_config(&mut event_connection).await?;
let mut events = event_connection
.subscribe([swayipc_async::EventType::Binding])
@ -51,10 +67,36 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
async fn get_config(
connection: &mut swayipc_async::Connection,
) -> Result<(), Box<dyn std::error::Error>> {
let config = connection.get_config().await?;
let default_colours = config
.config
.split('\n')
.find(|v| v.contains("client.focused"))
.ok_or(NoMatchingConfig)?
.trim();
let default_border = default_colours
.split_whitespace()
.nth(2)
.and_then(|v| v.strip_prefix('#'))
.ok_or(NoMatchingConfig)?;
let r = u8::from_str_radix(&default_border[..2], 16)?;
let g = u8::from_str_radix(&default_border[2..4], 16)?;
let b = u8::from_str_radix(&default_border[4..], 16)?;
DEFAULT_BORDER.set(Lab::from_rgb(&[r, g, b]))?;
DEFAULT_COLOURS.set(default_colours.to_string())?;
Ok(())
}
async fn interpolate_task() -> Result<(), Box<dyn std::error::Error>> {
let mut connection = swayipc_async::Connection::new().await?;
let to = Lab::from_rgb(&[0x88, 0xc0, 0xd0]);
let to = DEFAULT_BORDER.get().ok_or(NoMatchingConfig)?;
let per_frame = 1.0 / FRAMES_ANIM as f32;
let (d_l, d_a, d_b) = (
(to.l - INTERPOLATE_FROM.l) * per_frame,
@ -74,7 +116,7 @@ async fn interpolate_task() -> Result<(), Box<dyn std::error::Error>> {
tokio::time::sleep(DUR_PER_FRAME).await;
}
connection
.run_command("client.focused #000000 #88c0d0 #000000 #88c0d0")
.run_command(DEFAULT_COLOURS.get().ok_or(NoMatchingConfig)?)
.await?;
Ok(())
}