From b3b3793fdfa971d1e9d3986675046094f2bf666a Mon Sep 17 00:00:00 2001 From: Victor Timofei Date: Wed, 14 Jun 2023 16:30:53 +0300 Subject: [PATCH] Use tokio and add debug logs --- .config/eww/scripts/launch.rs | 87 +++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/.config/eww/scripts/launch.rs b/.config/eww/scripts/launch.rs index bc6feeb..ff64ecd 100755 --- a/.config/eww/scripts/launch.rs +++ b/.config/eww/scripts/launch.rs @@ -1,19 +1,23 @@ #!/usr/bin/env rust-script -//! +//! //! ```cargo //! [dependencies] //! serde = { version = "1.0", features = ["derive"] } //! serde_json = "1.0" //! anyhow = "1.0" //! handlebars = "3" +//! log = "0.4" +//! env_logger = "0.10" +//! tokio = { version = "1", features = ["full"] } //! ``` -use serde::Deserialize; use anyhow::Result; +use handlebars::Handlebars; +use serde::Deserialize; +use std::collections::HashMap; use std::process::Command; use std::{fs, io}; -use handlebars::Handlebars; -use std::collections::HashMap; +use log::{info, debug}; #[derive(Deserialize, Debug)] struct Monitor { @@ -25,10 +29,7 @@ struct Monitor { impl Monitor { fn list() -> Result> { - let output = Command::new("hyprctl") - .arg("monitors") - .arg("-j") - .output()?; + let output = Command::new("hyprctl").arg("monitors").arg("-j").output()?; assert!(output.status.success()); @@ -41,17 +42,17 @@ impl Monitor { let bar_name = format!("bar-{wl_display}-{}", self.id); let dir = format!("/tmp/vbar/{wl_display}/{}", self.id); + info!("Launching bar {bar_name}"); + match fs::create_dir_all(&dir) { - Ok(()) => {}, + Ok(()) => {} Err(e) if e.kind() == io::ErrorKind::AlreadyExists => { - let output = Command::new("rm") - .arg("-rf") - .arg(&dir) - .output()?; + info!("Directory {dir} already exists. Recreating..."); + let output = Command::new("rm").arg("-rf").arg(&dir).output()?; assert!(output.status.success()); fs::create_dir_all(&dir)?; - }, + } Err(e) => return Err(e.into()), } @@ -63,30 +64,31 @@ impl Monitor { &dir, )?; - let output = Command::new("eww") - .arg("-c") - .arg(&dir) - .arg("reload") - .output()?; - assert!(output.status.success()); - - let output = Command::new("eww") + Command::new("eww") .arg("-c") .arg(&dir) .arg("open") - .arg(bar_name) - .output()?; - assert!(output.status.success()); + .arg("--restart") + .arg(&bar_name) + .spawn()?; + + info!("Successfuly launched bar {bar_name}"); Ok(()) } - fn generate_bar_config(&self, bar_name: &str, home: &str, base_dir: &str, source_dir: &str, dest_dir: &str) -> Result<()> { + fn generate_bar_config( + &self, + bar_name: &str, + home: &str, + base_dir: &str, + source_dir: &str, + dest_dir: &str, + ) -> Result<()> { for entry in fs::read_dir(source_dir)? { let entry = entry?; let path = entry.path(); - let relpath = path.strip_prefix(&source_dir)? - .to_str().unwrap(); + let relpath = path.strip_prefix(&source_dir)?.to_str().unwrap(); if path.is_dir() { fs::create_dir_all(&format!("{dest_dir}/{relpath}"))?; @@ -129,17 +131,34 @@ impl Monitor { } } -fn main() -> Result<()> { - let wl_display = std::env::var_os("WAYLAND_DISPLAY").unwrap() - .into_string().unwrap(); +#[tokio::main] +async fn main() -> Result<()> { + env_logger::init(); + info!("Starting..."); + let wl_display = std::env::var_os("WAYLAND_DISPLAY") + .unwrap() + .into_string() + .unwrap(); - let home = std::env::var_os("HOME").unwrap() - .into_string().unwrap(); + let home = std::env::var_os("HOME").unwrap().into_string().unwrap(); let monitors = Monitor::list()?; + info!("Received monitor list"); + debug!("{:?}", monitors); + + let mut handles = Vec::new(); for monitor in monitors { - monitor.launch_bar(&wl_display, &home)?; + let home = home.clone(); + let wl_display = wl_display.clone(); + let handle = tokio::spawn(async move { + monitor.launch_bar(&wl_display, &home).unwrap(); + }); + handles.push(handle); + } + + for handle in handles { + handle.await?; } Ok(())