From b76d325d60b0f4ac59a805cb4e56ff459eb7aede Mon Sep 17 00:00:00 2001 From: redxef Date: Sun, 15 Oct 2023 00:33:55 +0200 Subject: [PATCH] Add logging. --- Cargo.toml | 3 +++ src/config.rs | 14 +++++++++++++- src/main.rs | 21 ++++++++++++++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 97dcbe9..7c18d2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,10 @@ edition = "2021" [dependencies] anyhow = "1.0.75" byteorder = "1.5.0" +clap = { version = "4.4.6", features = ["derive"] } +env_logger = "0.10.0" futures = "0.3.28" +log = "0.4.20" rust_lisp = { git = "https://github.com/brundonsmith/rust_lisp.git", branch = "arc-feature-addition", features = ["arc"] } serde = { version = "1.0.188", features = ["std", "derive", "serde_derive"] } serde_json = "1.0.107" diff --git a/src/config.rs b/src/config.rs index 1a9ff75..00b60be 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,4 @@ +use std::fmt::{Display, Formatter}; use serde::{Serialize, Deserialize, Serializer, Deserializer}; use rust_lisp::model::Value as RValue; @@ -18,6 +19,18 @@ impl Into> for Value { } } +impl Display for Value { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + let mut s = String::new(); + s.push_str("(begin\n"); + for i in &self.0 { + s.push_str(&format!("{}\n", i)); + } + s.push_str(")"); + write!(f, "{}", &s) + } +} + impl<'de> Deserialize<'de> for Value { fn deserialize(deserializer: D) -> Result where @@ -29,7 +42,6 @@ impl<'de> Deserialize<'de> for Value { } } - #[derive(Clone, Debug)] #[derive(Deserialize)] pub struct Program { diff --git a/src/main.rs b/src/main.rs index b2a1634..f633986 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use anyhow::Result; use tokio::time::{timeout, Duration}; use tokio::io::AsyncReadExt; +use clap::Parser; +use log::{info, debug, warn}; mod config; mod i3ipc; @@ -9,25 +11,35 @@ mod lisp; use config::Config; use i3ipc::{Connection, MessageType}; + +#[derive(Debug, Clone)] +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Args { +} + fn new_window_cb( b: MessageType, c: serde_json::Value, config: &Config, + args: &Args, ) -> futures::future::BoxFuture<'static, Vec<(MessageType, Vec)>> { let config_ = config.clone(); Box::pin(async move { + debug!("Received window event: {}", &c); for p in config_.programs.iter() { + debug!("Evaluating program: {}", &p.match_); let e = lisp::env(&c); let init: Vec = config_.init.clone().into(); let prog: Vec = p.match_.clone().into(); let m = init.into_iter().chain(prog.into_iter()); let result = rust_lisp::interpreter::eval_block(rust_lisp::model::reference::new(e), m); - println!("{:?}", result); if let Ok(rust_lisp::model::Value::True) = result { + debug!("Match found"); return vec![(MessageType::Command, p.cmd.clone().into_bytes())]; } } - + debug!("No match found"); Vec::new() }) } @@ -47,6 +59,8 @@ async fn run<'a>(connection: &mut Connection<'a>, config: &Config) -> Result<(), #[tokio::main] async fn main() -> Result<()> { + let args = std::sync::Arc::new(Args::parse()); + env_logger::init_from_env(env_logger::Env::new().filter("I3TOOLWAIT_LOG").write_style("I3TOOLWAIT_LOG_STYLE")); let mut connection = Connection::connect((i3ipc::get_socket_path().await?).as_ref())?; let mut sub_connection = connection.clone(); @@ -56,7 +70,8 @@ async fn main() -> Result<()> { let config = std::sync::Arc::new(config); let cb_config = config.clone(); - let cb = move |a, b| {new_window_cb(a, b, &cb_config)}; + let cb_args = args.clone(); + let cb = move |a, b| {new_window_cb(a, b, &cb_config, &cb_args)}; sub_connection .subscribe(&[MessageType::SubWindow], &cb) .await?;