diff --git a/Cargo.lock b/Cargo.lock index 2748303..e471d23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,89 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "1.0.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wlstreamer" version = "0.2.0" +dependencies = [ + "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)", +] +[metadata] +"checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +"checksum proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +"checksum quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +"checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +"checksum serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" +"checksum serde_derive 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" +"checksum serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)" = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" +"checksum syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)" = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac" +"checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" diff --git a/Cargo.toml b/Cargo.toml index bd374b4..5ab7e07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "wlstreamer" -version = "0.2.0" +version = "0.3.0" authors = ["Benjamin Bädorf "] edition = "2018" [dependencies] +serde = { version = "1.0", features = ["derive"]} +serde_json = "1.0" diff --git a/src/main.rs b/src/main.rs index 4be5240..a0ffd12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,27 @@ +use serde::{Deserialize, Serialize}; use std::env; -use std::io::{BufRead, BufReader, Error, ErrorKind, Write}; +use std::io::{BufRead, BufReader, Error}; use std::process::{Child, Command, Stdio}; +#[derive(Serialize, Deserialize, Debug)] +struct SwayWorkspace { + id: u32, + name: String, + focus: Vec, + output: String, + focused: bool, + visible: bool, + num: u32, + #[serde(rename = "type")] + type_name: String, + representation: String, +} + struct Config { current_screen: String, output: String, screen_blacklist: Vec, - workspace_blacklist: Vec, + workspace_blacklist: Vec, verbose: bool, } @@ -63,14 +78,15 @@ fn record_screen(config: &mut Config, valid_screens: &Vec) -> Result) -> Result) -> Result Vec { - let mut command = - "swaymsg -t get_workspaces | jq -r 'sort_by(.focused != true) | map(select(.visible" - .to_string(); - for screen in &config.screen_blacklist { - command.push_str(" and .output != \""); - command.push_str(screen.as_str()); - command.push_str("\""); - } - for workspace in &config.workspace_blacklist { - command.push_str(" and .num != "); - command.push_str(workspace.as_str()); - } - command.push_str(")) | map(.output) | .[]'"); + let mut command = "swaymsg -t get_workspaces"; let output = Command::new("sh") - .args(&["-c", command.as_str()]) + .args(&["-c", command]) .output() .expect("Couldn't get current focus"); - let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 from get_workspaces"); - let valid_screens = stdout - .split('\n') - .map(|s| s.to_string()) - .filter(|s| s != "") + let stdout_string = + String::from_utf8(output.stdout).expect("Invalid UTF-8 from get_workspaces"); + let mut workspaces: Vec = + serde_json::from_str(stdout_string.as_str()).expect("Invalid json from get_workspaces"); + + if config.verbose { + println!("Found workspaces"); + for elem in workspaces.iter() { + println!("{:?}", elem); + } + } + + workspaces = workspaces + .into_iter() + .filter(|w| { + w.visible + && !config + .screen_blacklist + .iter() + .any(|screen| screen.eq(&w.output)) + && !config.workspace_blacklist.iter().any(|&num| num == w.num) + }) .collect(); - println!("Found recordable screens: {:?}", valid_screens); + if config.verbose { + println!("Filtered workspaces"); + for elem in workspaces.iter() { + println!("{:?}", elem); + } + } - return valid_screens; -} - -fn get_screen_index(screen: &String) -> String { - let mut command = "swaymsg -t get_outputs | jq -r 'map(.name==\"".to_string(); - command.push_str(screen.as_str()); - command.push_str("\") | index(true) + 1'"); - let output = Command::new("sh") - .args(&["-c", command.as_str()]) - .output() - .expect("Couldn't get screen index"); - - let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 from get_workspaces"); - return stdout; + workspaces.sort_by_key(|w| w.focused); + return workspaces.into_iter().map(|w| w.output).collect(); } fn main() -> Result<(), Box> { @@ -160,7 +169,9 @@ fn main() -> Result<(), Box> { let arg = &args[i]; if arg == "--not-ws" { i += 1; - config.workspace_blacklist.push(args[i].clone()); + config + .workspace_blacklist + .push(args[i].clone().parse::().unwrap()); } else if arg == "--not-screen" { i += 1; config.screen_blacklist.push(args[i].clone());