diff --git a/src/main.rs b/src/main.rs index a70143b..2bcc12a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; // 10 → 17, 27 // 100 → 107, 207 // 15 → 22, 37 -const MAX_GROUP_WS: usize = 10; +const MAX_GROUP_WS: i64 = 10; const RUN_COMMAND: u32 = 0; const GET_WORKSPACES: u32 = 1; @@ -84,7 +84,7 @@ enum Command { #[derive(Args, Debug)] struct Index { #[clap(value_name = "index", help = "The workspace index to work with")] - index: usize, + index: i64, } struct SwaySome { @@ -106,7 +106,9 @@ struct Output { #[derive(Serialize, Deserialize, Debug)] struct Workspace { - num: usize, + //num: usize, + num: i64, + name: String, output: String, visible: bool, } @@ -262,11 +264,11 @@ impl SwaySome { workspaces } - fn get_current_output_index(&self) -> usize { + fn get_current_output_index(&self) -> i64 { // Do not use `self.outputs`, as the information here could be outdated, especially the `focused` attribute let outputs = self.get_outputs(); match outputs.iter().position(|x| x.focused) { - Some(i) => i, + Some(i) => i as i64, None => panic!("WTF! No focused output???"), } } @@ -282,7 +284,7 @@ impl SwaySome { focused_output_index.to_string() } - fn get_current_workspace_index(&self) -> usize { + fn get_current_workspace_index(&self) -> i64 { // Do not use `self.outputs`, as the information here could be outdated, especially the `focused` attribute let outputs = self.get_outputs(); // Do not use `self.workspaces`, as the information here could be outdated, especially the `visible` attribute @@ -293,7 +295,7 @@ impl SwaySome { .num } - fn move_container_to_workspace(&self, workspace_index: usize) { + fn move_container_to_workspace(&self, workspace_index: i64) { if workspace_index < MAX_GROUP_WS { self.move_container_to_workspace_relative(workspace_index); } else { @@ -301,16 +303,16 @@ impl SwaySome { } } - fn move_container_to_workspace_group(&self, target_group: usize) { + fn move_container_to_workspace_group(&self, target_group: i64) { let current_workspace_index = self.get_current_workspace_index(); - let current_workspace_index_relative = (current_workspace_index % MAX_GROUP_WS) as usize; + let current_workspace_index_relative = current_workspace_index % MAX_GROUP_WS; self.move_container_to_workspace_absolute( current_workspace_index_relative + target_group * MAX_GROUP_WS, ); } - fn move_container_to_workspace_absolute(&self, workspace_index: usize) { - let group_index = (workspace_index / MAX_GROUP_WS) as usize; + fn move_container_to_workspace_absolute(&self, workspace_index: i64) { + let group_index = workspace_index / MAX_GROUP_WS; let full_ws_name = format!( "{}", group_index * MAX_GROUP_WS + workspace_index % MAX_GROUP_WS @@ -341,8 +343,8 @@ impl SwaySome { ), None => { // Or if the targeted output is currently connected - if group_index < self.outputs.len() { - Some(group_index) + if group_index < (self.outputs.len() as i64) { + Some(group_index as usize) } else { None } @@ -403,8 +405,8 @@ impl SwaySome { } } - fn move_container_to_workspace_relative(&self, workspace_index: usize) { - let current_workspace_index: usize = self.get_current_workspace_index(); + fn move_container_to_workspace_relative(&self, workspace_index: i64) { + let current_workspace_index = self.get_current_workspace_index(); let focused_output_index = current_workspace_index / MAX_GROUP_WS; let mut cmd: String = "move container to workspace number ".to_string(); @@ -413,7 +415,7 @@ impl SwaySome { self.send_command(&cmd); } - fn focus_to_workspace(&self, workspace_index: usize) { + fn focus_to_workspace(&self, workspace_index: i64) { if workspace_index < MAX_GROUP_WS { self.focus_to_workspace_relative(workspace_index); } else { @@ -421,8 +423,8 @@ impl SwaySome { } } - fn focus_to_workspace_absolute(&self, workspace_index: usize) { - let output_index = (workspace_index / MAX_GROUP_WS) as usize; + fn focus_to_workspace_absolute(&self, workspace_index: i64) { + let output_index = workspace_index / MAX_GROUP_WS; // If the workspace already exists match self.workspaces.iter().find(|w| w.num == workspace_index) { @@ -449,8 +451,8 @@ impl SwaySome { ), None => { // Or if the targeted output is currently connected - if output_index < self.outputs.len() { - Some(output_index) + if output_index < (self.outputs.len() as i64) { + Some(output_index as usize) } else { None } @@ -476,8 +478,8 @@ impl SwaySome { } } - fn focus_to_workspace_relative(&self, workspace_index: usize) { - let current_workspace_index: usize = self.get_current_workspace_index(); + fn focus_to_workspace_relative(&self, workspace_index: i64) { + let current_workspace_index = self.get_current_workspace_index(); let focused_output_index = current_workspace_index / MAX_GROUP_WS; let mut cmd: String = "workspace number ".to_string(); @@ -486,8 +488,8 @@ impl SwaySome { self.send_command(&cmd); } - fn focus_to_group(&self, group_index: usize) { - let current_workspace_index: usize = self.get_current_workspace_index(); + fn focus_to_group(&self, group_index: i64) { + let current_workspace_index = self.get_current_workspace_index(); let target_workspace_relative_index = current_workspace_index % MAX_GROUP_WS; let target_workspace_index = group_index * MAX_GROUP_WS + target_workspace_relative_index; @@ -525,8 +527,8 @@ impl SwaySome { ), None => { // Or if the targeted output is currently connected - if group_index > 0 && group_index <= self.outputs.len() { - Some(group_index) + if group_index > 0 && group_index <= (self.outputs.len() as i64) { + Some(group_index as usize) } else { None } @@ -552,7 +554,7 @@ impl SwaySome { } } - fn focus_all_outputs_to_workspace(&self, workspace_index: usize) { + fn focus_all_outputs_to_workspace(&self, workspace_index: i64) { let current_output = self.get_current_output_name(); // Iterate on all outputs to focus on the given workspace @@ -582,9 +584,10 @@ impl SwaySome { let focused_output_index = self.get_current_output_index(); let target_output = if go_to_prev { - &self.outputs[(focused_output_index + self.outputs.len() - 1) % self.outputs.len()] + &self.outputs + [((focused_output_index as usize) + self.outputs.len() - 1) % self.outputs.len()] } else { - &self.outputs[(focused_output_index + 1) % self.outputs.len()] + &self.outputs[((focused_output_index + 1) as usize) % self.outputs.len()] }; let workspaces = self.get_workspaces(); @@ -592,21 +595,31 @@ impl SwaySome { .iter() .find(|x| x.output == target_output.name && x.visible) .unwrap(); - 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 - ); + let group_index = target_workspace.num / MAX_GROUP_WS; - // Move container to target workspace - let mut cmd: String = "move container to workspace number ".to_string(); - cmd.push_str(&full_ws_name); - self.send_command(&cmd); + let ws_name_idx: i64 = group_index * MAX_GROUP_WS + target_workspace.num % MAX_GROUP_WS; + if ws_name_idx == -1 { + // Move container to target workspace + let mut cmd: String = "move container to workspace ".to_string(); + cmd.push_str(&target_workspace.name); + self.send_command(&cmd); - // Focus that workspace to follow the container - let mut cmd: String = "workspace number ".to_string(); - cmd.push_str(&full_ws_name); - self.send_command(&cmd); + // Focus that workspace to follow the container + let mut cmd: String = "workspace ".to_string(); + cmd.push_str(&target_workspace.name); + self.send_command(&cmd); + } else { + let full_ws_name = format!("{}", ws_name_idx); + // Move container to target workspace + let mut cmd: String = "move container to workspace number ".to_string(); + cmd.push_str(&full_ws_name); + self.send_command(&cmd); + + // Focus that workspace to follow the container + let mut cmd: String = "workspace number ".to_string(); + cmd.push_str(&full_ws_name); + self.send_command(&cmd); + } } fn move_workspace_group_to_next_output(&self) { @@ -621,12 +634,13 @@ impl SwaySome { let focused_output_index = self.get_current_output_index(); let target_output = if go_to_prev { - &self.outputs[(focused_output_index + self.outputs.len() - 1) % self.outputs.len()] + &self.outputs + [((focused_output_index as usize) + self.outputs.len() - 1) % self.outputs.len()] } else { - &self.outputs[(focused_output_index + 1) % self.outputs.len()] + &self.outputs[((focused_output_index + 1) as usize) % self.outputs.len()] }; let current_workspace = self.get_current_workspace_index(); - let current_group_index = (current_workspace / MAX_GROUP_WS) as usize; + let current_group_index = current_workspace / MAX_GROUP_WS; for workspace in self.get_workspaces() { let ws_index = workspace.num / MAX_GROUP_WS; if ws_index == current_group_index { @@ -649,7 +663,7 @@ impl SwaySome { } fn focus_container_to_next_or_prev_group(&self, go_to_prev: bool) { - let current_workspace_index: usize = self.get_current_workspace_index(); + let current_workspace_index = self.get_current_workspace_index(); let focused_group_index = current_workspace_index / MAX_GROUP_WS; if go_to_prev { @@ -659,7 +673,7 @@ impl SwaySome { }; } - fn init_workspaces(&self, workspace_index: usize) { + fn init_workspaces(&self, workspace_index: i64) { let cmd_prefix: String = "focus output ".to_string(); for output in self.outputs.iter().rev() { let mut cmd = cmd_prefix.clone(); @@ -685,9 +699,9 @@ impl SwaySome { self.send_command(&focus_cmd); let group_index = workspace.num / MAX_GROUP_WS; - if group_index <= self.outputs.len() - 1 { + if group_index <= (self.outputs.len() - 1).try_into().unwrap() { let mut move_cmd = move_cmd_prefix.clone(); - move_cmd.push_str(&self.outputs[group_index.max(1) - 1].name); + move_cmd.push_str(&self.outputs[(group_index.max(1) - 1) as usize].name); self.send_command(&move_cmd); } }