From 24d6642981648d968ee1dd9aa190c3cfd0042ff7 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 17 Aug 2024 14:35:32 -0700 Subject: [PATCH] added another utility for next/prev commands --- ewwtilities/src/main.rs | 6 +- ewwtilities/src/workspace_switcher.rs | 140 +++++++++++++++++++++----- 2 files changed, 118 insertions(+), 28 deletions(-) diff --git a/ewwtilities/src/main.rs b/ewwtilities/src/main.rs index 4ec4d65..0990946 100644 --- a/ewwtilities/src/main.rs +++ b/ewwtilities/src/main.rs @@ -4,12 +4,13 @@ mod calendar_background; use calendar_background::run_calendar; mod workspace_switcher; -use workspace_switcher::run_workspace_selector; +use workspace_switcher::{prev_or_next_workspace_selector, run_workspace_selector}; #[derive(ValueEnum, Debug, Clone)] enum ProcessType { CalendarBackground, WorkspaceSelector, + PrevNextWorkspace, } #[derive(Parser, Debug)] @@ -47,6 +48,9 @@ fn main() -> Result<(), Box> { ProcessType::WorkspaceSelector => { run_workspace_selector(&args); } + ProcessType::PrevNextWorkspace => { + prev_or_next_workspace_selector(&args); + } } Ok(()) } diff --git a/ewwtilities/src/workspace_switcher.rs b/ewwtilities/src/workspace_switcher.rs index d3ebac2..b46f458 100644 --- a/ewwtilities/src/workspace_switcher.rs +++ b/ewwtilities/src/workspace_switcher.rs @@ -5,37 +5,111 @@ use serde::{Deserialize, Serialize}; use crate::CliArgs; -pub fn run_workspace_selector(cli: &CliArgs) { - let output = { - let std_out = match Command::new("hyprctl").arg("monitors").arg("-j").output() { - Ok(val) => val, - Err(e) => { - error!("Error getting the focused monitor: {e}"); - panic!("Error getting focused monitor"); - } +static MAX_WORKSPACES: i32 = 10; + +fn get_monitors_info() -> Vec { + let std_out = match Command::new("hyprctl").arg("monitors").arg("-j").output() { + Ok(val) => val, + Err(e) => { + error!("Error getting the focused monitor: {e}"); + panic!("Error getting focused monitor"); } - .stdout; + } + .stdout; - let std_string = match String::from_utf8(std_out) { - Ok(v) => v, - Err(e) => { - error!("Could not parse hyprctl output to string: {e}"); - panic!("Could not parse hyprctl output to string"); - } - }; - - debug!("hyprctl command output: {std_string}"); - - let json_output: Vec = match serde_json::from_str(&std_string) { - Ok(val) => val, - Err(e) => { - error!("Could not parse stdout to json: {e}"); - panic!("Could not parse stdout to json"); - } - }; - json_output + let std_string = match String::from_utf8(std_out) { + Ok(v) => v, + Err(e) => { + error!("Could not parse hyprctl monitors output to string: {e}"); + panic!("Could not parse hyprctl monitors output to string"); + } }; + debug!("hyprctl command output: {std_string}"); + + let json_output: Vec = match serde_json::from_str(&std_string) { + Ok(val) => val, + Err(e) => { + error!("Could not parse monitors stdout to json: {e}"); + panic!("Could not parse monitors stdout to json"); + } + }; + json_output +} + +fn _get_workspaces_info() -> Vec { + let std_out = match Command::new("hyprctl").arg("workspaces").arg("-j").output() { + Ok(val) => val, + Err(e) => { + error!("Error getting the current workspaces: {e}"); + panic!("Error getting current workspaces"); + } + } + .stdout; + + let std_string = match String::from_utf8(std_out) { + Ok(v) => v, + Err(e) => { + error!("Could not parse hyprctl workspaces output to string: {e}"); + panic!("Could not parse hyprctl workspaces output to string"); + } + }; + + debug!("hyprctl command output: {std_string}"); + + let json_output: Vec = match serde_json::from_str(&std_string) { + Ok(val) => val, + Err(e) => { + error!("Could not parse workspaces stdout to json: {e}"); + panic!("Could not parse workspaces stdout to json"); + } + }; + json_output + +} + +pub fn prev_or_next_workspace_selector(cli: &CliArgs) { + let monitors = get_monitors_info(); + + let active_workspace = monitors + .iter() + .filter(|x| x.focused) + .map(|x| x.active_workspace.id.clone()) + .collect::>() + .first() + .unwrap_or(&1) + .to_owned(); + + debug!("Active workspace: {:#?}", active_workspace); + + + let mut target_workspace: i32; + if cli.source_workspace.unwrap_or(1) > 0 { + target_workspace = active_workspace + monitors.len() as i32; + + if target_workspace > MAX_WORKSPACES { + target_workspace -= MAX_WORKSPACES; + assert!(target_workspace > 0, "target workspace cannot be less than zero!"); + } + } else { + target_workspace = active_workspace - monitors.len() as i32; + + if target_workspace < 0 { + target_workspace += MAX_WORKSPACES; + assert!(target_workspace <= MAX_WORKSPACES, "target workspace cannot be larger than MAX_WORKSPACES!"); + } + } + + debug!("Target workspace: {}", target_workspace); + + debug!("Calling hyprctl and closing"); + + Command::new("hyprctl").arg("dispatch").arg("workspace").arg(target_workspace.to_string()).output().unwrap(); +} + +pub fn run_workspace_selector(cli: &CliArgs) { + let output = get_monitors_info(); + debug!("Parsed monitor json: {:#?}", output); let id: u32 = match output @@ -113,3 +187,15 @@ struct SpecialWorkspace { } +#[derive(Serialize, Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +struct WorkspaceInfo { + id: u32, + name: String, + monitor: String, + monitor_id: u32, + windows: u32, + hasfullscree: bool, + lastwindow: String, + lastwindowtitle: String +}