added another utility for next/prev commands

This commit is contained in:
Nickiel12 2024-08-17 14:35:32 -07:00
parent 90aa9bd469
commit 24d6642981
2 changed files with 118 additions and 28 deletions

View file

@ -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<dyn std::error::Error>> {
ProcessType::WorkspaceSelector => {
run_workspace_selector(&args);
}
ProcessType::PrevNextWorkspace => {
prev_or_next_workspace_selector(&args);
}
}
Ok(())
}

View file

@ -5,8 +5,9 @@ use serde::{Deserialize, Serialize};
use crate::CliArgs;
pub fn run_workspace_selector(cli: &CliArgs) {
let output = {
static MAX_WORKSPACES: i32 = 10;
fn get_monitors_info() -> Vec<MonitorInfo> {
let std_out = match Command::new("hyprctl").arg("monitors").arg("-j").output() {
Ok(val) => val,
Err(e) => {
@ -19,8 +20,8 @@ pub fn run_workspace_selector(cli: &CliArgs) {
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");
error!("Could not parse hyprctl monitors output to string: {e}");
panic!("Could not parse hyprctl monitors output to string");
}
};
@ -29,13 +30,86 @@ pub fn run_workspace_selector(cli: &CliArgs) {
let json_output: Vec<MonitorInfo> = 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");
error!("Could not parse monitors stdout to json: {e}");
panic!("Could not parse monitors stdout to json");
}
};
json_output
}
fn _get_workspaces_info() -> Vec<WorkspaceInfo> {
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<WorkspaceInfo> = 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::<Vec<i32>>()
.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
}