Feature MAX_GROUP_WS constant instead of hardcoded 10

This commit is contained in:
LluisE 2023-06-05 20:37:19 +02:00
parent c24d02e284
commit 91f697cb42

View file

@ -14,6 +14,13 @@ use std::path::Path;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
// Maximum workspaces per group. This will determine the naming.
// Examples:
// 10 → 17, 27
// 100 → 107, 207
// 15 → 22, 37
const MAX_GROUP_WS: usize = 10;
const RUN_COMMAND: u32 = 0;
const GET_WORKSPACES: u32 = 1;
// const SUBSCRIBE: u32 = 2;
@ -240,7 +247,7 @@ fn get_current_workspace(stream: &UnixStream) -> Workspace {
}
fn move_container_to_workspace(stream: &UnixStream, workspace_index: usize) {
if workspace_index < 10 {
if workspace_index < MAX_GROUP_WS {
move_container_to_workspace_relative(stream, workspace_index);
} else {
move_container_to_workspace_absolute(stream, workspace_index);
@ -249,18 +256,18 @@ fn move_container_to_workspace(stream: &UnixStream, workspace_index: usize) {
fn move_container_to_workspace_group(stream: &UnixStream, target_group: usize) {
let current_workspace_index = get_current_workspace(stream).num;
let current_workspace_index_relative = (current_workspace_index % 10) as usize;
let current_workspace_index_relative = (current_workspace_index % MAX_GROUP_WS) as usize;
move_container_to_workspace_absolute(
stream,
current_workspace_index_relative + target_group * 10,
current_workspace_index_relative + target_group * MAX_GROUP_WS,
);
}
fn move_container_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
let group_index = (workspace_index / 10) as usize;
let group_index = (workspace_index / MAX_GROUP_WS) as usize;
let outputs = get_outputs(stream);
let workspaces = get_workspaces(stream);
let full_ws_name = format!("{}{}", group_index, workspace_index % 10);
let full_ws_name = format!("{}", group_index * MAX_GROUP_WS + workspace_index % MAX_GROUP_WS);
// If the workspace already exists
match workspaces.iter().find(|w| w.num == workspace_index) {
@ -270,8 +277,8 @@ fn move_container_to_workspace_absolute(stream: &UnixStream, workspace_index: us
send_command(stream, &focus_cmd);
}
None => {
let target_group = workspace_index / 10;
let target_screen_index = match workspaces.iter().find(|w| w.num / 10 == target_group) {
let target_group = workspace_index / MAX_GROUP_WS;
let target_screen_index = match workspaces.iter().find(|w| w.num / MAX_GROUP_WS == target_group) {
// If other workspaces on the same group exists
Some(other_workspace) => Some(
outputs
@ -347,16 +354,16 @@ fn move_container_to_workspace_absolute(stream: &UnixStream, workspace_index: us
fn move_container_to_workspace_relative(stream: &UnixStream, workspace_index: usize) {
let current_workspace_index: usize = get_current_workspace(stream).num;
let focused_output_index = current_workspace_index / 10;
let focused_output_index = current_workspace_index / MAX_GROUP_WS;
let mut cmd: String = "move container to workspace number ".to_string();
let full_ws_name = format!("{}{}", focused_output_index, workspace_index);
let full_ws_name = format!("{}", focused_output_index * MAX_GROUP_WS + workspace_index);
cmd.push_str(&full_ws_name);
send_command(stream, &cmd);
}
fn focus_to_workspace(stream: &UnixStream, workspace_index: usize) {
if workspace_index < 10 {
if workspace_index < MAX_GROUP_WS {
focus_to_workspace_relative(stream, workspace_index);
} else {
focus_to_workspace_absolute(stream, workspace_index);
@ -364,7 +371,7 @@ fn focus_to_workspace(stream: &UnixStream, workspace_index: usize) {
}
fn focus_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
let output_index = (workspace_index / 10) as usize;
let output_index = (workspace_index / MAX_GROUP_WS) as usize;
let outputs = get_outputs(stream);
let workspaces = get_workspaces(stream);
@ -376,8 +383,8 @@ fn focus_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
send_command(stream, &focus_cmd);
}
None => {
let target_group = workspace_index / 10;
let target_screen_index = match workspaces.iter().find(|w| w.num / 10 == target_group) {
let target_group = workspace_index / MAX_GROUP_WS;
let target_screen_index = match workspaces.iter().find(|w| w.num / MAX_GROUP_WS == target_group) {
// If other workspaces on the same group exists
Some(other_workspace) => Some(
outputs
@ -418,10 +425,10 @@ fn focus_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) {
fn focus_to_workspace_relative(stream: &UnixStream, workspace_index: usize) {
let current_workspace_index: usize = get_current_workspace(stream).num;
let focused_output_index = current_workspace_index / 10;
let focused_output_index = current_workspace_index / MAX_GROUP_WS;
let mut cmd: String = "workspace number ".to_string();
let full_ws_name = format!("{}{}", focused_output_index, &workspace_index);
let full_ws_name = format!("{}", focused_output_index * MAX_GROUP_WS + workspace_index);
cmd.push_str(&full_ws_name);
send_command(stream, &cmd);
}
@ -431,10 +438,10 @@ fn focus_to_group(stream: &UnixStream, group_index: usize) {
let workspaces = get_workspaces(stream);
let current_workspace_index: usize = get_current_workspace(stream).num;
let target_workspace_relative_index = current_workspace_index % 10;
let target_workspace_relative_index = current_workspace_index % MAX_GROUP_WS;
let target_workspace_index = group_index * 10 + target_workspace_relative_index;
let full_ws_name = format!("{}{}", group_index, target_workspace_relative_index);
let target_workspace_index = group_index * MAX_GROUP_WS + target_workspace_relative_index;
let full_ws_name = format!("{}", group_index * MAX_GROUP_WS + target_workspace_relative_index);
// If the workspace already exists
match workspaces.iter().find(|w| w.num == target_workspace_index) {
@ -444,7 +451,7 @@ fn focus_to_group(stream: &UnixStream, group_index: usize) {
send_command(stream, &focus_cmd);
}
None => {
let target_screen_index = match workspaces.iter().find(|w| w.num / 10 == group_index) {
let target_screen_index = match workspaces.iter().find(|w| w.num / MAX_GROUP_WS == group_index) {
// If other workspaces on the same group exists
Some(other_workspace) => Some(
outputs
@ -457,7 +464,7 @@ fn focus_to_group(stream: &UnixStream, group_index: usize) {
),
None => {
// Or if the targeted output is currently connected
if group_index != 0 && group_index <= outputs.len() {
if group_index > 0 && group_index <= outputs.len() {
Some(group_index)
} else {
None
@ -526,8 +533,8 @@ fn move_container_to_next_or_prev_output(stream: &UnixStream, go_to_prev: bool)
.iter()
.find(|x| x.output == target_output.name && x.visible)
.unwrap();
let group_index = (target_workspace.num / 10) as usize;
let full_ws_name = format!("{}{}", group_index, target_workspace.num % 10);
let group_index = (target_workspace.num / MAX_GROUP_WS) as usize;
let full_ws_name = format!("{}", group_index * MAX_GROUP_WS + target_workspace.num % MAX_GROUP_WS);
// Move container to target workspace
let mut cmd: String = "move container to workspace number ".to_string();
@ -550,7 +557,7 @@ fn focus_container_to_prev_group(stream: &UnixStream) {
fn focus_container_to_next_or_prev_group(stream: &UnixStream, go_to_prev: bool) {
let current_workspace_index: usize = get_current_workspace(stream).num;
let focused_group_index = current_workspace_index / 10;
let focused_group_index = current_workspace_index / MAX_GROUP_WS;
if go_to_prev {
focus_to_group(stream, focused_group_index - 1);
@ -570,9 +577,8 @@ fn init_workspaces(stream: &UnixStream, workspace_index: usize) {
let mut cmd: String = "workspace number ".to_string();
let full_ws_name = format!(
"{}{}",
get_current_output_index(stream) + 1,
&workspace_index
"{}",
(get_current_output_index(stream) + 1) * MAX_GROUP_WS + workspace_index
);
cmd.push_str(&full_ws_name);
send_command(stream, &cmd);
@ -590,7 +596,7 @@ fn rearrange_workspaces(stream: &UnixStream) {
focus_cmd.push_str(&workspace.num.to_string());
send_command(stream, &focus_cmd);
let group_index = workspace.num / 10;
let group_index = workspace.num / MAX_GROUP_WS;
if group_index <= outputs.len() - 1 {
let mut move_cmd = move_cmd_prefix.clone();
move_cmd.push_str(&outputs[group_index.max(1) - 1].name);