Use more strongly typed JSON for Output and Workspace

This commit is contained in:
Skia 2021-11-07 00:53:16 +01:00
parent ae9eb161fe
commit aa2a232320
3 changed files with 91 additions and 24 deletions

74
Cargo.lock generated
View file

@ -30,9 +30,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "byteorder"
version = "1.4.2"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "clap"
@ -60,15 +60,33 @@ dependencies = [
[[package]]
name = "itoa"
version = "0.4.7"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
[[package]]
name = "libc"
version = "0.2.101"
version = "0.2.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cb00336871be5ed2c8ed44b60ae9959dc5b9f08539422ed43f09e34ecaeba21"
checksum = "a60553f9a9e039a333b4e9b20573b9e9b9c0bb3a11e201ccc48ef4283456d673"
[[package]]
name = "proc-macro2"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
@ -78,15 +96,29 @@ checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "serde"
version = "1.0.123"
version = "1.0.130"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae"
checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.130"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.64"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
checksum = "e466864e431129c7e0d3476b92f20458e5879919a0596c6472738d9fa2d342f8"
dependencies = [
"itoa",
"ryu",
@ -105,9 +137,21 @@ version = "1.1.2"
dependencies = [
"byteorder",
"clap",
"serde",
"serde_json",
]
[[package]]
name = "syn"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "textwrap"
version = "0.11.0"
@ -119,9 +163,15 @@ dependencies = [
[[package]]
name = "unicode-width"
version = "0.1.8"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "vec_map"

View file

@ -12,5 +12,6 @@ homepage = "https://gitlab.com/hyask/swaysome"
[dependencies]
byteorder = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
clap = "2"

View file

@ -2,6 +2,8 @@ extern crate byteorder;
extern crate clap;
extern crate serde_json;
use serde::{Deserialize, Serialize};
use clap::{App, Arg, SubCommand};
use std::env;
use std::io::Cursor;
@ -103,7 +105,14 @@ fn check_success(stream: &UnixStream) {
};
}
fn get_outputs(stream: &UnixStream) -> Vec<serde_json::Value> {
#[derive(Serialize, Deserialize)]
struct Output {
name: String,
focused: bool,
active: bool,
}
fn get_outputs(stream: &UnixStream) -> Vec<Output> {
send_msg(&stream, GET_OUTPUTS, "");
let o = match read_msg(&stream) {
Ok(msg) => msg,
@ -112,7 +121,14 @@ fn get_outputs(stream: &UnixStream) -> Vec<serde_json::Value> {
serde_json::from_str(&o).unwrap()
}
fn get_workspaces(stream: &UnixStream) -> Vec<serde_json::Value> {
#[derive(Serialize, Deserialize)]
struct Workspace {
num: u32,
output: String,
visible: bool,
}
fn get_workspaces(stream: &UnixStream) -> Vec<Workspace> {
send_msg(&stream, GET_WORKSPACES, "");
let ws = match read_msg(&stream) {
Ok(msg) => msg,
@ -126,7 +142,7 @@ fn get_current_output_index(stream: &UnixStream) -> String {
let focused_output_index = match outputs
.iter()
.position(|x| x["focused"] == serde_json::Value::Bool(true))
.position(|x| x.focused)
{
Some(i) => i,
None => panic!("WTF! No focused output???"),
@ -140,9 +156,9 @@ fn get_current_output_name(stream: &UnixStream) -> String {
let focused_output_index = match outputs
.iter()
.find(|x| x["focused"] == serde_json::Value::Bool(true))
.find(|x| x.focused)
{
Some(i) => i["name"].as_str().unwrap(),
Some(i) => i.name.as_str(),
None => panic!("WTF! No focused output???"),
};
@ -174,7 +190,7 @@ fn focus_all_outputs_to_workspace(stream: &UnixStream, workspace_name: &String)
let outputs = get_outputs(&stream);
for output in outputs.iter() {
let mut cmd: String = "focus output ".to_string();
cmd.push_str(&output["name"].as_str().unwrap());
cmd.push_str(&output.name.as_str());
send_command(&stream, &cmd);
focus_to_workspace(&stream, &workspace_name);
@ -198,7 +214,7 @@ fn move_container_to_next_or_prev_output(stream: &UnixStream, go_to_prev: bool)
let outputs = get_outputs(&stream);
let focused_output_index = match outputs
.iter()
.position(|x| x["focused"] == serde_json::Value::Bool(true))
.position(|x| x.focused)
{
Some(i) => i,
None => panic!("WTF! No focused output???"),
@ -215,19 +231,19 @@ fn move_container_to_next_or_prev_output(stream: &UnixStream, go_to_prev: bool)
let target_workspace = workspaces
.iter()
.filter(|x| {
x["output"] == target_output["name"] && x["visible"] == serde_json::Value::Bool(true)
x.output == target_output.name && x.visible
})
.next()
.unwrap();
// Move container to target workspace
let mut cmd: String = "move container to workspace number ".to_string();
cmd.push_str(&target_workspace["num"].to_string());
cmd.push_str(&target_workspace.num.to_string());
send_command(&stream, &cmd);
// Focus that workspace to follow the container
let mut cmd: String = "workspace number ".to_string();
cmd.push_str(&target_workspace["num"].to_string());
cmd.push_str(&target_workspace.num.to_string());
send_command(&stream, &cmd);
}
@ -235,9 +251,9 @@ fn init_workspaces(stream: &UnixStream, workspace_name: &String) {
let outputs = get_outputs(&stream);
let cmd_prefix: String = "focus output ".to_string();
for output in outputs.iter().filter(|x| x["active"] == true).rev() {
for output in outputs.iter().filter(|x| x.active).rev() {
let mut cmd = cmd_prefix.clone();
cmd.push_str(&output["name"].as_str().unwrap());
cmd.push_str(&output.name.as_str());
send_command(&stream, &cmd);
focus_to_workspace(&stream, &workspace_name);
}