From 39c2c81191915eafd1acd8cc9cece49cd500930d Mon Sep 17 00:00:00 2001 From: FieldofClay <7278759+FieldofClay@users.noreply.github.com> Date: Sat, 17 Feb 2024 14:27:40 +1100 Subject: [PATCH 1/5] include initial title in advanced output --- README.md | 4 ++-- src/main.rs | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cf60575..bef6354 100644 --- a/README.md +++ b/README.md @@ -60,9 +60,9 @@ Pass the wildcard "_" as the only argument and it will follow all monitors and o ``` ./hyprland-activewindow _ ``` -The output will be a json array of each monitors name and active window title. +The output will be a json array of each monitors name and the active window's title & initial title. ```json -[{"name":"eDP-1","title":"Alacritty"},{"name":"DP-1","title":"main.rs - hyprland-activewindow (Workspace) - VSCodium"}] +[{"initial_title":"Alacritty","name":"eDP-1","title":"~/hyprland-activewindow"},{"initial_title":"VSCodium","name":"DP-1","title":"main.rs - hyprland-activewindow (Workspace) - VSCodium"}] ``` This allows simplified Eww config similar to this: ```yuck diff --git a/src/main.rs b/src/main.rs index 3fa41e9..803e145 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use hyprland::data::{Monitors, Workspaces}; +use hyprland::data::{Clients, Monitors, Workspaces}; use hyprland::event_listener::EventListenerMutable as EventListener; use hyprland::shared::HyprData; use hyprland::Result; @@ -25,6 +25,7 @@ ARGS: struct MonitorCustom { pub name: String, pub title: String, + pub initial_title: String, } struct WindowPrinter { @@ -63,14 +64,19 @@ impl WindowPrinter { let monitors = Monitors::get().expect("unable to get monitors"); let mut out_monitors: Vec = Vec::new(); for monitor in monitors { - let title = Workspaces::get() + let workspace = Workspaces::get() .expect("unable to get workspaces") .find(|w| w.id == monitor.active_workspace.id) - .unwrap() - .last_window_title; + .unwrap(); + let client = Clients::get() + .expect("unable to get clients") + .find(|c| c.address == workspace.last_window) + .unwrap(); + //.last_window_title;title let mc: MonitorCustom = MonitorCustom { name: monitor.name, - title, + title: client.title, + initial_title: client.initial_title, }; out_monitors.push(mc); } From 1496d26affbdfa735489f93bc7eaea5490a6f725 Mon Sep 17 00:00:00 2001 From: FieldofClay <7278759+FieldofClay@users.noreply.github.com> Date: Sat, 17 Feb 2024 14:27:49 +1100 Subject: [PATCH 2/5] bump hyprland version --- Cargo.lock | 30 +++++++++++++++--------------- Cargo.toml | 4 ++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2bc4bab..3e4f879 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,9 +183,9 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "hyprland" -version = "0.3.12" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfa94ae7899f3f1bdcad21dcd50366a60f323375a25610889246f276d7f9fb06" +checksum = "f87a8f1cc065d451894dd3916c0bc3fcf9b67b276126c05f27b1db912688dde8" dependencies = [ "async-trait", "derive_more", @@ -206,7 +206,7 @@ dependencies = [ [[package]] name = "hyprland-activewindow" -version = "0.5.0" +version = "1.0.0" dependencies = [ "hyprland", "serde", @@ -262,9 +262,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.5.0" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "mio" @@ -367,9 +367,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.3" +version = "1.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" dependencies = [ "aho-corasick", "memchr", @@ -379,9 +379,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.6" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" +checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" dependencies = [ "aho-corasick", "memchr", @@ -390,9 +390,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.4" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rustc_version" @@ -505,18 +505,18 @@ dependencies = [ [[package]] name = "strum" -version = "0.25.0" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" +checksum = "723b93e8addf9aa965ebe2d11da6d7540fa2283fcea14b3371ff055f7ba13f5f" dependencies = [ "strum_macros", ] [[package]] name = "strum_macros" -version = "0.25.1" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6069ca09d878a33f883cc06aaa9718ede171841d3832450354410b718b097232" +checksum = "7a3417fc93d76740d974a01654a09777cb500428cc874ca9f45edfe0c4d4cd18" dependencies = [ "heck", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 33c6c82..c4a86bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,13 +6,13 @@ repository = "https://github.com/FieldofClay/hyprland-activewindow" readme = "README.md" keywords = ["hyprland", "eww"] categories = ["command-line-utilities"] -version = "0.5.0" +version = "1.0.0" edition = "2021" license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -hyprland = "0.3.12" +hyprland = "0.3.13" serde_json = "1.0" serde = "1.0" \ No newline at end of file From 2f003c21813db4dfe30358c30accc11c7899ef71 Mon Sep 17 00:00:00 2001 From: FieldofClay <7278759+FieldofClay@users.noreply.github.com> Date: Sat, 17 Feb 2024 16:39:22 +1100 Subject: [PATCH 3/5] improve Result and Option handling --- src/main.rs | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 803e145..9fef7a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use hyprland::data::{Clients, Monitors, Workspaces}; use hyprland::event_listener::EventListenerMutable as EventListener; -use hyprland::shared::HyprData; +use hyprland::shared::{HyprData, HyprError}; use hyprland::Result; use serde::Serialize; use serde_json::json; @@ -39,48 +39,47 @@ impl WindowPrinter { pub fn print(&self) { if self.mon == "_" { - self.print_all(); + self.print_all().unwrap_or_default(); } else { - self.print_single(); + self.print_single().unwrap_or_default(); } } - fn print_single(&self) { - let active_workspace_id = Monitors::get() - .expect("unable to get monitors") + fn print_single(&self) -> Result<()> { + let active_workspace_id = Monitors::get()? .find(|m| m.name == self.mon.to_string()) - .unwrap() + .ok_or_else(|| HyprError::NotOkDispatch("No monitor found".to_string()))? .active_workspace .id; - let title = Workspaces::get() - .expect("unable to get workspaces") + let title = Workspaces::get()? .find(|w| w.id == active_workspace_id) - .unwrap() + .ok_or_else(|| HyprError::NotOkDispatch("No workspace found".to_string()))? .last_window_title; println!("{}", title); + Ok(()) } - fn print_all(&self) { - let monitors = Monitors::get().expect("unable to get monitors"); + fn print_all(&self) -> Result<()> { + let monitors = Monitors::get()?; let mut out_monitors: Vec = Vec::new(); for monitor in monitors { - let workspace = Workspaces::get() - .expect("unable to get workspaces") + let workspace = Workspaces::get()? .find(|w| w.id == monitor.active_workspace.id) - .unwrap(); - let client = Clients::get() - .expect("unable to get clients") + .ok_or_else(|| HyprError::NotOkDispatch("No active workspace found".to_string()))?; + + let client = Clients::get()? .find(|c| c.address == workspace.last_window) - .unwrap(); - //.last_window_title;title + .ok_or_else(|| HyprError::NotOkDispatch("Unable to get last window".to_string()))?; + let mc: MonitorCustom = MonitorCustom { name: monitor.name, title: client.title, initial_title: client.initial_title, }; - out_monitors.push(mc); + out_monitors.push(mc); } println!("{}", json!(out_monitors).to_string()); + Ok(()) } } @@ -92,8 +91,7 @@ fn main() -> Result<()> { std::process::exit(0); } let mon = args[1].to_string(); - let mon_object = Monitors::get() - .expect("unable to get monitors") + let mon_object = Monitors::get()? .find(|m| m.name == mon); if mon_object.is_none() && mon != "_" { println!("Unable to find monitor {mon}"); From 9ba7a2f429c323a1b770ac476066a118ef1de1ea Mon Sep 17 00:00:00 2001 From: FieldofClay <7278759+FieldofClay@users.noreply.github.com> Date: Sun, 18 Feb 2024 13:56:01 +1100 Subject: [PATCH 4/5] Added logging --- Cargo.lock | 373 ++++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 4 +- src/main.rs | 49 ++++++- 3 files changed, 408 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3e4f879..407c2dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "async-trait" version = "0.1.63" @@ -34,24 +49,57 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bumpalo" +version = "3.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32a994c2b3ca201d9b263612a374263f05e7adde37c4707f693dcd375076d1f" + [[package]] name = "bytes" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfb24e866b15a1af2a1b663f10c6b6b8f397a84aadb828f12e5b289ec23a3a3c" +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "windows-targets 0.52.0", +] + [[package]] name = "convert_case" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + [[package]] name = "derive_more" version = "0.99.17" @@ -71,6 +119,22 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "flexi_logger" +version = "0.27.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "469e584c031833564840fb0cdbce99bdfe946fd45480a188545e73a76f45461c" +dependencies = [ + "chrono", + "glob", + "is-terminal", + "lazy_static", + "log", + "nu-ansi-term", + "regex", + "thiserror", +] + [[package]] name = "futures" version = "0.3.28" @@ -160,6 +224,12 @@ dependencies = [ "slab", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "heck" version = "0.4.1" @@ -175,6 +245,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd5256b483761cd23699d0da46cc6fd2ee3be420bbe6d020ae4a091e70b7e9fd" + [[package]] name = "hex" version = "0.4.3" @@ -208,7 +284,9 @@ dependencies = [ name = "hyprland-activewindow" version = "1.0.0" dependencies = [ + "flexi_logger", "hyprland", + "log", "serde", "serde_json", ] @@ -223,12 +301,55 @@ dependencies = [ "syn 2.0.39", ] +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "is-terminal" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" +dependencies = [ + "hermit-abi 0.3.6", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "itoa" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" +[[package]] +name = "js-sys" +version = "0.3.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -275,7 +396,16 @@ dependencies = [ "libc", "log", "wasi", - "windows-sys", + "windows-sys 0.42.0", +] + +[[package]] +name = "nu-ansi-term" +version = "0.49.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c073d3c1930d0751774acf49e66653acecb416c3a54c6ec095a9b11caddb5a68" +dependencies = [ + "windows-sys 0.48.0", ] [[package]] @@ -293,10 +423,16 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" dependencies = [ - "hermit-abi", + "hermit-abi 0.2.6", "libc", ] +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + [[package]] name = "parking_lot" version = "0.12.1" @@ -317,7 +453,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-sys", + "windows-sys 0.42.0", ] [[package]] @@ -547,6 +683,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e3de26b0965292219b4287ff031fcba86837900fe9cd2b34ea8ad893c0953d2" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "268026685b2be38d7103e9e507c938a1fcb3d7e6eb15e87870b617bf37b6d581" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", +] + [[package]] name = "tokio" version = "1.24.2" @@ -564,7 +720,7 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys", + "windows-sys 0.42.0", ] [[package]] @@ -590,6 +746,60 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.39", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" + [[package]] name = "winapi" version = "0.3.9" @@ -612,19 +822,76 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.0", +] + [[package]] name = "windows-sys" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.42.1", + "windows_aarch64_msvc 0.42.1", + "windows_i686_gnu 0.42.1", + "windows_i686_msvc 0.42.1", + "windows_x86_64_gnu 0.42.1", + "windows_x86_64_gnullvm 0.42.1", + "windows_x86_64_msvc 0.42.1", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", ] [[package]] @@ -633,38 +900,122 @@ version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + [[package]] name = "windows_aarch64_msvc" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + [[package]] name = "windows_i686_gnu" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + [[package]] name = "windows_i686_msvc" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + [[package]] name = "windows_x86_64_gnu" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + [[package]] name = "windows_x86_64_msvc" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" diff --git a/Cargo.toml b/Cargo.toml index c4a86bf..57fab53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,6 @@ license = "MIT" [dependencies] hyprland = "0.3.13" serde_json = "1.0" -serde = "1.0" \ No newline at end of file +serde = "1.0" +flexi_logger = "0.27" +log = "0.4" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9fef7a1..7ce9aad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ use serde::Serialize; use serde_json::json; use std::env; use std::sync::Arc; +use flexi_logger::{Logger, FileSpec}; +use log; const HELP: &str = "\ hyprland-activewindow: a multi monitor aware active hyprland window title reporter, designed to be used with eww. @@ -48,12 +50,18 @@ impl WindowPrinter { fn print_single(&self) -> Result<()> { let active_workspace_id = Monitors::get()? .find(|m| m.name == self.mon.to_string()) - .ok_or_else(|| HyprError::NotOkDispatch("No monitor found".to_string()))? + .ok_or_else(|| { + log::error!("No monitor found with name: {}", self.mon); + HyprError::NotOkDispatch("No monitor found".to_string()) + })? .active_workspace .id; let title = Workspaces::get()? .find(|w| w.id == active_workspace_id) - .ok_or_else(|| HyprError::NotOkDispatch("No workspace found".to_string()))? + .ok_or_else(|| { + log::warn!("No workspace found with ID: {}", active_workspace_id); + HyprError::NotOkDispatch("No workspace found".to_string()) + })? .last_window_title; println!("{}", title); Ok(()) @@ -65,11 +73,17 @@ impl WindowPrinter { for monitor in monitors { let workspace = Workspaces::get()? .find(|w| w.id == monitor.active_workspace.id) - .ok_or_else(|| HyprError::NotOkDispatch("No active workspace found".to_string()))?; + .ok_or_else(|| { + log::error!("No active workspace found. ID: {}", monitor.active_workspace.id); + HyprError::NotOkDispatch("No active workspace found".to_string()) + })?; let client = Clients::get()? .find(|c| c.address == workspace.last_window) - .ok_or_else(|| HyprError::NotOkDispatch("Unable to get last window".to_string()))?; + .ok_or_else(|| { + log::warn!("Unable to get last window"); + HyprError::NotOkDispatch("Unable to get last window".to_string())} + )?; let mc: MonitorCustom = MonitorCustom { name: monitor.name, @@ -83,21 +97,44 @@ impl WindowPrinter { } } -fn main() -> Result<()> { +fn main() -> Result<()>{ let args: Vec = env::args().collect(); //check args if args.len() != 2 || args[1].eq("-h") || args[1].eq("--help") { println!("{HELP}"); std::process::exit(0); } + + let _logger = match Logger::try_with_str("info") { + Ok(logger) => { + match logger.log_to_file(FileSpec::default() + .directory("/tmp") + .basename("hyprland-activewindow")) + .start() { + Ok(logger) => { + logger + }, + Err(e) => { + println!("Unable to start logger: {}", e); + std::process::exit(1) + } + }; + }, + Err(e) => { + println!("Unable to initialise logger: {}", e); + std::process::exit(1) + } + }; + let mon = args[1].to_string(); let mon_object = Monitors::get()? .find(|m| m.name == mon); if mon_object.is_none() && mon != "_" { - println!("Unable to find monitor {mon}"); + log::error!("Unable to find monitor {mon}"); std::process::exit(0); } + log::info!("Started with arg {}", mon); let wp = Arc::new(WindowPrinter::new(mon)); wp.print(); From c874e81bff9971bdd66d7cfb2519ba6848f25277 Mon Sep 17 00:00:00 2001 From: FieldofClay <7278759+FieldofClay@users.noreply.github.com> Date: Sun, 10 Mar 2024 18:32:38 +1100 Subject: [PATCH 5/5] removed windowprinter --- src/main.rs | 170 ++++++++++++++++++++++++++-------------------------- 1 file changed, 84 insertions(+), 86 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7ce9aad..aee5854 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ +use flexi_logger::{FileSpec, Logger}; use hyprland::data::{Clients, Monitors, Workspaces}; use hyprland::event_listener::EventListenerMutable as EventListener; use hyprland::shared::{HyprData, HyprError}; use hyprland::Result; +use log; use serde::Serialize; use serde_json::json; use std::env; use std::sync::Arc; -use flexi_logger::{Logger, FileSpec}; -use log; const HELP: &str = "\ hyprland-activewindow: a multi monitor aware active hyprland window title reporter, designed to be used with eww. @@ -30,131 +30,129 @@ struct MonitorCustom { pub initial_title: String, } -struct WindowPrinter { - mon: String, -} - -impl WindowPrinter { - pub(crate) fn new(mon: String) -> Self { - Self { mon } +fn print(mon: &str) { + if mon == "_" { + print_all().unwrap_or_else(|err| log::error!("Error printing all: {}", err)); + } else { + print_single(mon).unwrap_or_else(|err| log::error!("Error printing single: {}", err)); } +} - pub fn print(&self) { - if self.mon == "_" { - self.print_all().unwrap_or_default(); - } else { - self.print_single().unwrap_or_default(); - } - } +fn print_single(mon: &str) -> Result<()> { + let active_workspace_id = Monitors::get()? + .find(|m| m.name == mon.to_string()) + .ok_or_else(|| { + log::error!("No monitor found with name: {}", mon); + HyprError::NotOkDispatch("No monitor found".to_string()) + })? + .active_workspace + .id; + let title = Workspaces::get()? + .find(|w| w.id == active_workspace_id) + .ok_or_else(|| { + log::warn!("No workspace found with ID: {}", active_workspace_id); + HyprError::NotOkDispatch("No workspace found".to_string()) + })? + .last_window_title; + println!("{}", title); + Ok(()) +} - fn print_single(&self) -> Result<()> { - let active_workspace_id = Monitors::get()? - .find(|m| m.name == self.mon.to_string()) +fn print_all() -> Result<()> { + let monitors = Monitors::get()?; + let mut out_monitors: Vec = Vec::new(); + for monitor in monitors { + let workspace = Workspaces::get()? + .find(|w| w.id == monitor.active_workspace.id) .ok_or_else(|| { - log::error!("No monitor found with name: {}", self.mon); - HyprError::NotOkDispatch("No monitor found".to_string()) - })? - .active_workspace - .id; - let title = Workspaces::get()? - .find(|w| w.id == active_workspace_id) + log::error!( + "No active workspace found. ID: {}", + monitor.active_workspace.id + ); + HyprError::NotOkDispatch("No active workspace found".to_string()) + })?; + + let client = Clients::get()? + .find(|c| c.address == workspace.last_window) .ok_or_else(|| { - log::warn!("No workspace found with ID: {}", active_workspace_id); - HyprError::NotOkDispatch("No workspace found".to_string()) - })? - .last_window_title; - println!("{}", title); - Ok(()) - } - - fn print_all(&self) -> Result<()> { - let monitors = Monitors::get()?; - let mut out_monitors: Vec = Vec::new(); - for monitor in monitors { - let workspace = Workspaces::get()? - .find(|w| w.id == monitor.active_workspace.id) - .ok_or_else(|| { - log::error!("No active workspace found. ID: {}", monitor.active_workspace.id); - HyprError::NotOkDispatch("No active workspace found".to_string()) - })?; - - let client = Clients::get()? - .find(|c| c.address == workspace.last_window) - .ok_or_else(|| { - log::warn!("Unable to get last window"); - HyprError::NotOkDispatch("Unable to get last window".to_string())} - )?; - - let mc: MonitorCustom = MonitorCustom { - name: monitor.name, - title: client.title, - initial_title: client.initial_title, - }; - out_monitors.push(mc); - } - println!("{}", json!(out_monitors).to_string()); - Ok(()) + log::warn!("Unable to get last window"); + HyprError::NotOkDispatch("Unable to get last window".to_string()) + })?; + + let mc: MonitorCustom = MonitorCustom { + name: monitor.name, + title: client.title, + initial_title: client.initial_title, + }; + out_monitors.push(mc); } + println!("{}", json!(out_monitors).to_string()); + Ok(()) } -fn main() -> Result<()>{ +fn main() -> Result<()> { let args: Vec = env::args().collect(); //check args if args.len() != 2 || args[1].eq("-h") || args[1].eq("--help") { println!("{HELP}"); std::process::exit(0); } - + let _logger = match Logger::try_with_str("info") { Ok(logger) => { - match logger.log_to_file(FileSpec::default() - .directory("/tmp") - .basename("hyprland-activewindow")) - .start() { - Ok(logger) => { - logger - }, + match logger + .log_to_file( + FileSpec::default() + .directory("/tmp") + .basename("hyprland-activewindow"), + ) + .start() + { + Ok(logger) => logger, Err(e) => { println!("Unable to start logger: {}", e); std::process::exit(1) } }; - }, + } Err(e) => { println!("Unable to initialise logger: {}", e); std::process::exit(1) } }; - - let mon = args[1].to_string(); - let mon_object = Monitors::get()? - .find(|m| m.name == mon); - if mon_object.is_none() && mon != "_" { + + let mon = Arc::new(String::from(args[1].to_string())); + let mon_object = Monitors::get() + .unwrap_or_else(|err| { + log::error!("Unable to get monitors: {}", err); + std::process::exit(1) + }) + .find(|m| m.name == mon.to_string()); + if mon_object.is_none() && mon.to_string() != "_" { log::error!("Unable to find monitor {mon}"); std::process::exit(0); } log::info!("Started with arg {}", mon); - let wp = Arc::new(WindowPrinter::new(mon)); - wp.print(); + print(&mon); // Create a event listener let mut event_listener = EventListener::new(); - let wp_clone = Arc::clone(&wp); + let mon_clone = Arc::clone(&mon); event_listener.add_active_window_change_handler(move |_, _| { - wp_clone.print(); + print(&mon_clone); }); - let wp_clone = Arc::clone(&wp); + let mon_clone = Arc::clone(&mon); event_listener.add_window_close_handler(move |_, _| { - wp_clone.print(); + print(&mon_clone); }); - let wp_clone = Arc::clone(&wp); + let mon_clone = Arc::clone(&mon); event_listener.add_workspace_change_handler(move |_, _| { - wp_clone.print(); + print(&mon_clone); }); - let wp_clone = Arc::clone(&wp); + let mon_clone = Arc::clone(&mon); event_listener.add_window_moved_handler(move |_, _| { - wp_clone.print(); + print(&mon_clone); }); event_listener.start_listener()