use crate::utils::{exec, get_pwd, get_reader};
use serde::{Deserialize, Serialize};
use std::io::BufRead;
#[derive(Debug, Serialize, Deserialize)]
pub struct Workspace {
pub id: i32,
pub status: u8,
pub monitor: String,
}
impl Workspace {}
#[derive(Debug, Deserialize)]
struct HyprWorkspace {
id: i32,
monitor: String,
windows: i32,
}
pub fn print_workspaces() {
let input = get_reader();
for line in input.lines() {
match line {
Ok(line) => {
let mut line_iterator = line.splitn(2, ">>");
let verb = line_iterator.next().unwrap();
let _info = line_iterator.next().unwrap();
let workspace_verbs = ["workspace", "focusedmon"];
if workspace_verbs.contains(&verb) {
print_workspace();
}
}
Err(_) => {}
}
}
}
fn print_workspace() {
let workspaces = &get_workspace().unwrap();
let workspaces_json = match serde_json::to_string(workspaces) {
Ok(w) => w,
Err(err) => format!("Could not parse workspaces: {}", err),
};
println!("{}", workspaces_json);
}
fn get_workspace() -> Result<Vec<Workspace>, std::io::Error> {
match exec("hyprctl workspaces -j", get_pwd(None).as_path()) {
Ok(o) => {
let output = match String::from_utf8(o.stdout) {
Ok(output) => output,
Err(err) => {
println!("Error parsing hyprctl output: {err}");
std::process::exit(1);
}
};
let active_id: i32 = match exec("hyprctl activeworkspace -j", get_pwd(None).as_path()) {
Ok(o) => match String::from_utf8(o.stdout) {
Ok(output) => {
match serde_json::from_str::<HyprWorkspace>(&output.to_string()) {
Ok(workspace) => workspace.id,
Err(err) => {
println!("Error parsing hyprctl output: {err}");
std::process::exit(1);
}
}
}
Err(err) => {
println!("Error getting hyprctl output: {err}");
std::process::exit(1);
}
},
Err(err) => {
println!("Error: {err}");
std::process::exit(1);
}
};
let hypr_list: Vec<HyprWorkspace> = match serde_json::from_str(&output.to_string()) {
Ok(vec) => vec,
Err(err) => {
println!("Couldn't parse input from hyprctl: {err}");
std::process::exit(1);
}
};
let mut list: Vec<Workspace> = hypr_list
.iter()
.map(|w| Workspace {
id: w.id,
status: get_status(w.id, w.windows, active_id),
monitor: w.monitor.clone(),
})
.collect();
list.sort_by(|a, b| a.id.cmp(&b.id));
return Ok(list);
}
Err(err) => {
println!("Error executing hyprctl: {err}");
std::process::exit(1);
}
}
}
fn get_status(id: i32, windows: i32, active_id: i32) -> u8 {
if id == active_id {
return 2;
} else if windows > 0 {
return 1;
} else {
return 0;
}
}