Use tokio and add debug logs

This commit is contained in:
Victor Timofei 2023-06-14 16:30:53 +03:00
parent cf32f4d439
commit b3b3793fdf
Signed by: vtimofei
GPG Key ID: B790DCEBE281403A
1 changed files with 53 additions and 34 deletions

View File

@ -1,19 +1,23 @@
#!/usr/bin/env rust-script #!/usr/bin/env rust-script
//! //!
//! ```cargo //! ```cargo
//! [dependencies] //! [dependencies]
//! serde = { version = "1.0", features = ["derive"] } //! serde = { version = "1.0", features = ["derive"] }
//! serde_json = "1.0" //! serde_json = "1.0"
//! anyhow = "1.0" //! anyhow = "1.0"
//! handlebars = "3" //! handlebars = "3"
//! log = "0.4"
//! env_logger = "0.10"
//! tokio = { version = "1", features = ["full"] }
//! ``` //! ```
use serde::Deserialize;
use anyhow::Result; use anyhow::Result;
use handlebars::Handlebars;
use serde::Deserialize;
use std::collections::HashMap;
use std::process::Command; use std::process::Command;
use std::{fs, io}; use std::{fs, io};
use handlebars::Handlebars; use log::{info, debug};
use std::collections::HashMap;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct Monitor { struct Monitor {
@ -25,10 +29,7 @@ struct Monitor {
impl Monitor { impl Monitor {
fn list() -> Result<Vec<Self>> { fn list() -> Result<Vec<Self>> {
let output = Command::new("hyprctl") let output = Command::new("hyprctl").arg("monitors").arg("-j").output()?;
.arg("monitors")
.arg("-j")
.output()?;
assert!(output.status.success()); assert!(output.status.success());
@ -41,17 +42,17 @@ impl Monitor {
let bar_name = format!("bar-{wl_display}-{}", self.id); let bar_name = format!("bar-{wl_display}-{}", self.id);
let dir = format!("/tmp/vbar/{wl_display}/{}", self.id); let dir = format!("/tmp/vbar/{wl_display}/{}", self.id);
info!("Launching bar {bar_name}");
match fs::create_dir_all(&dir) { match fs::create_dir_all(&dir) {
Ok(()) => {}, Ok(()) => {}
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => { Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
let output = Command::new("rm") info!("Directory {dir} already exists. Recreating...");
.arg("-rf") let output = Command::new("rm").arg("-rf").arg(&dir).output()?;
.arg(&dir)
.output()?;
assert!(output.status.success()); assert!(output.status.success());
fs::create_dir_all(&dir)?; fs::create_dir_all(&dir)?;
}, }
Err(e) => return Err(e.into()), Err(e) => return Err(e.into()),
} }
@ -63,30 +64,31 @@ impl Monitor {
&dir, &dir,
)?; )?;
let output = Command::new("eww") Command::new("eww")
.arg("-c")
.arg(&dir)
.arg("reload")
.output()?;
assert!(output.status.success());
let output = Command::new("eww")
.arg("-c") .arg("-c")
.arg(&dir) .arg(&dir)
.arg("open") .arg("open")
.arg(bar_name) .arg("--restart")
.output()?; .arg(&bar_name)
assert!(output.status.success()); .spawn()?;
info!("Successfuly launched bar {bar_name}");
Ok(()) 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)? { for entry in fs::read_dir(source_dir)? {
let entry = entry?; let entry = entry?;
let path = entry.path(); let path = entry.path();
let relpath = path.strip_prefix(&source_dir)? let relpath = path.strip_prefix(&source_dir)?.to_str().unwrap();
.to_str().unwrap();
if path.is_dir() { if path.is_dir() {
fs::create_dir_all(&format!("{dest_dir}/{relpath}"))?; fs::create_dir_all(&format!("{dest_dir}/{relpath}"))?;
@ -129,17 +131,34 @@ impl Monitor {
} }
} }
fn main() -> Result<()> { #[tokio::main]
let wl_display = std::env::var_os("WAYLAND_DISPLAY").unwrap() async fn main() -> Result<()> {
.into_string().unwrap(); 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() let home = std::env::var_os("HOME").unwrap().into_string().unwrap();
.into_string().unwrap();
let monitors = Monitor::list()?; let monitors = Monitor::list()?;
info!("Received monitor list");
debug!("{:?}", monitors);
let mut handles = Vec::new();
for monitor in monitors { 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(()) Ok(())