Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
Swaysome
https://docs.rs/swaysome/2.3.2
Admin
A Rust crate providing utilities or features related to the Sway Wayland window manager.
Tokens:
8,833
Snippets:
89
Trust Score:
9.1
Update:
3 months ago
Context
Chat
Benchmark
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Initialize Workspaces Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Initializes workspaces starting from a given workspace index. This is typically used during SwaySome setup. ```rust pub fn init_workspaces(&self, workspace_index: usize) ``` -------------------------------- ### SwaySome::init_workspaces Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Initializes workspaces with a starting index. ```APIDOC ## SwaySome::init_workspaces ### Description Initializes the workspaces, setting the starting workspace index. ### Method `init_workspaces(&self, workspace_index: usize)` ### Parameters #### Path Parameters - **workspace_index** (`usize`): The index for the initial workspace. ``` -------------------------------- ### Focus All Outputs to Workspace Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves all outputs to a specified workspace index. This is useful for multi-monitor setups. ```rust pub fn focus_all_outputs_to_workspace(&self, workspace_index: usize) ``` -------------------------------- ### Get Workspaces Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Fetches all workspaces, parses the JSON response, and sorts them by their number. This is used to retrieve the current workspace configuration. ```rust self.send_msg(GET_WORKSPACES, ""); let ws = match self.read_msg() { Ok(msg) => msg, Err(_) => panic!("Unable to get current workspace"), }; let mut workspaces: Vec<Workspace> = serde_json::from_str(&ws).unwrap(); workspaces.sort_by_key(|x| x.num); workspaces ``` -------------------------------- ### Get Active Outputs Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Fetches all outputs, filters them to include only active ones, and sorts them by name. This function is used to determine the current output configuration. ```rust self.send_msg(GET_OUTPUTS, ""); let o = match self.read_msg() { Ok(msg) => msg, Err(_) => panic!("Unable to get outputs"), }; let mut outputs: Vec<Output> = serde_json::from_str::<Vec<Output>>(&o) .unwrap() .into_iter() .filter(|x| x.active) .collect(); outputs.sort_by(|x, y| x.name.cmp(&y.name)); // sort_by_key doesn't work here (https://stackoverflow.com/a/47126516) outputs ``` -------------------------------- ### Get Tree Structure Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Sends a GET_TREE message and parses the JSON response to get the current tree structure. Expects a valid JSON response and panics if parsing fails or if the message cannot be read. ```rust self.send_msg(GET_TREE, ""); match self.read_msg() { Ok(msg) => serde_json::from_str(&msg).expect("Failed to parse JSON for get_tree"), Err(_) => panic!("Unable to get current workspace"), } ``` -------------------------------- ### Get Sway Tree Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Retrieves the current Sway window tree as a Value. This can be used to inspect the state of windows and workspaces. ```rust pub fn get_tree(&self) -> Value ``` -------------------------------- ### Get Current Output Name Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Retrieves the name of the currently focused output. It fetches the latest output information to ensure accuracy, as cached data might be outdated. ```rust let outputs = self.get_outputs(); let focused_output_index = match outputs.iter().find(|x| x.focused) { Some(i) => i.name.as_str(), None => panic!("WTF! No focused output???"), }; focused_output_index.to_string() ``` -------------------------------- ### Get Type ID Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Gets the `TypeId` of `self`. This is part of the `Any` trait implementation. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Get Current Workspace Index Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Identifies the index of the currently active workspace. It considers both the visibility of workspaces and the focused state of their associated outputs to determine the current context. Avoids using cached `self.workspaces` or `self.outputs`. ```rust let outputs = self.get_outputs(); // Do not use `self.workspaces`, as the information here could be outdated, especially the `visible` attribute self.get_workspaces() .iter() .find(|w| w.visible && outputs.iter().find(|o| o.name == w.output).unwrap().focused) .unwrap() .num ``` -------------------------------- ### Get Current Output Index Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Determines the index of the currently focused output by fetching all outputs and finding the one with the 'focused' attribute set to true. Avoids using cached `self.outputs` to ensure data is up-to-date. ```rust let outputs = self.get_outputs(); match outputs.iter().position(|x| x.focused) { Some(i) => i, None => panic!("WTF! No focused output???"), } ``` -------------------------------- ### Initialize Workspaces Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Initializes workspaces on each output. It iterates through outputs in reverse, focuses each output, and then creates a new workspace with a calculated number. ```rust pub fn init_workspaces(&self, workspace_index: usize) { let cmd_prefix: String = "focus output ".to_string(); for output in self.outputs.iter().rev() { let mut cmd = cmd_prefix.clone(); cmd.push_str(output.name.as_str()); self.send_command(&cmd); let mut cmd: String = "workspace number ".to_string(); let full_ws_name = format!( "{}", (self.get_current_output_index() + 1) * MAX_GROUP_WS + workspace_index ); cmd.push_str(&full_ws_name); self.send_command(&cmd); } } ``` -------------------------------- ### SwaySome::new Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Initializes a new SwaySome client. It attempts to connect to the Sway/i3 socket by checking the SWAYSOCK and I3SOCK environment variables. ```APIDOC ## SwaySome::new ### Description Initializes a new SwaySome client by connecting to the Sway/i3 IPC socket. It automatically detects the socket path from environment variables `SWAYSOCK` or `I3SOCK`. ### Returns A `SwaySome` instance connected to the socket. ### Panics Panics if no Sway/i3 socket can be found in the environment variables or if connection fails. ``` -------------------------------- ### SwaySome::new Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Creates a new instance of SwaySome. ```APIDOC ## SwaySome::new ### Description Creates a new instance of the SwaySome struct. ### Method `new()` ### Returns - `SwaySome`: A new instance of SwaySome. ``` -------------------------------- ### Initialize SwaySome with Default Socket Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Creates a new SwaySome instance by attempting to connect to the default Sway or i3 socket path found in environment variables (SWAYSOCK or I3SOCK). Panics if no socket can be found or connected to. ```rust pub fn new() -> SwaySome { for socket_var in ["SWAYSOCK", "I3SOCK"] { let socket_path = match env::var(socket_var) { Ok(val) => val, Err(_e) => { eprintln!("{} not found in environment", socket_var); continue; } }; let socket = Path::new(&socket_path); match SwaySome::new_from_socket(socket) { Ok(swaysome) => return swaysome, Err(e) => eprintln!("Error with value found in ${}: {}", socket_var, e) } } panic!("couldn't find any i3/sway socket") } ``` -------------------------------- ### init_workspaces Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Initializes workspaces across all outputs. This function iterates through each output, sends a command to focus that output, and then creates a new workspace with a specific numbering scheme. ```APIDOC ## init_workspaces ### Description Initializes workspaces across all configured outputs. For each output, it focuses the output and then creates a new workspace with a calculated number. ### Method `pub fn init_workspaces(&self, workspace_index: usize)` ### Parameters * `workspace_index` (usize) - Required - The base index for the new workspaces. ### Returns None ``` -------------------------------- ### SwaySome::run_command Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Executes a command in Sway. ```APIDOC ## SwaySome::run_command ### Description Executes a given command string within the Sway environment. This method sends the command to the Sway IPC interface and checks for a successful execution. ### Parameters #### Path Parameters - **command** (`&str`) - Required - The command string to execute in Sway (e.g., `"focus output up"`). ### Panics Panics if the command execution fails. ``` -------------------------------- ### Initialize SwaySome from Socket Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Creates a new instance of SwaySome by connecting to a Sway IPC socket at the specified path. Returns a Result indicating success or failure. ```rust pub fn new_from_socket(socket: &Path) -> Result<SwaySome, String> ``` -------------------------------- ### Initialize SwaySome from Specific Socket Path Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Creates a new SwaySome instance by connecting to a specific Unix domain socket path. Returns an error if the connection fails. ```rust pub fn new_from_socket(socket: &Path) -> Result<SwaySome, String> { let stream = match UnixStream::connect(&socket) { Err(_) => { return Err(format!( "counldn't connect to socket '{}'", socket.display() )); } Ok(stream) => { eprintln!("successful connection to socket '{}'", socket.display()); stream } }; let mut swaysome = SwaySome { socket: Cell::new(Some(stream)), outputs: vec![], workspaces: vec![], }; swaysome.outputs = swaysome.get_outputs(); swaysome.workspaces = swaysome.get_workspaces(); Ok(swaysome) } ``` -------------------------------- ### Focus All Outputs to Workspace Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Iterates through all available outputs, focuses each one, and then focuses the specified workspace. Finally, it returns to the originally focused output. ```Rust pub fn focus_all_outputs_to_workspace(&self, workspace_index: usize) { let current_output = self.get_current_output_name(); for output in self.outputs.iter() { let mut cmd: String = "focus output ".to_string(); cmd.push_str(output.name.as_str()); self.send_command(&cmd); self.focus_to_workspace(workspace_index); } } ``` -------------------------------- ### SwaySome::focus_all_outputs_to_workspace Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Focuses all outputs to a specified workspace. ```APIDOC ## SwaySome::focus_all_outputs_to_workspace ### Description Directs all outputs to display the specified workspace. ### Method `focus_all_outputs_to_workspace(&self, workspace_index: usize)` ### Parameters #### Path Parameters - **workspace_index** (`usize`): The index of the workspace to focus all outputs on. ``` -------------------------------- ### Send a Command to Sway/i3 Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html A convenience function to send a command string to the Sway/i3 window manager using the `RUN_COMMAND` message type. It logs the command being sent and then checks for success. ```rust fn send_command(&self, command: &str) { eprint!("Sending command: '{}' - ", &command); self.send_msg(RUN_COMMAND, command); self.check_success(); } ``` -------------------------------- ### Move Workspace to Output Command Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Constructs and sends a command to move the current workspace to a specified output. This is useful for organizing windows across multiple displays. ```rust let mut move_cmd_prefix: String = "move workspace to ".to_string(); for workspace in self.workspaces.iter() { let mut focus_cmd = focus_cmd_prefix.clone(); focus_cmd.push_str(&workspace.num.to_string()); self.send_command(&focus_cmd); let group_index = (workspace.num / MAX_GROUP_WS + self.outputs.len() - 1) % self.outputs.len(); // if group_index <= self.outputs.len() - 1 { let mut move_cmd = move_cmd_prefix.clone(); move_cmd.push_str(&self.outputs[group_index].name); self.send_command(&move_cmd); // } } } ``` -------------------------------- ### SwaySome::get_outputs Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Retrieves a list of all outputs connected to Sway. ```APIDOC ## SwaySome::get_outputs ### Description Fetches and returns a list of all configured outputs in the Sway environment. Each output object contains its name, and whether it is currently focused or active. ### Returns A `Vec<Output>` where each `Output` struct contains `name`, `focused`, and `active` fields. ``` -------------------------------- ### SwaySome::get_workspaces Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Retrieves a list of all workspaces. ```APIDOC ## SwaySome::get_workspaces ### Description Fetches and returns a list of all workspaces. Each workspace object contains its number, associated output, and visibility status. ### Returns A `Vec<Workspace>` where each `Workspace` struct contains `num`, `output`, and `visible` fields. ``` -------------------------------- ### SwaySome::focus_to_workspace Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Focuses on a specified workspace. ```APIDOC ## SwaySome::focus_to_workspace ### Description Sets the focus to the specified workspace index. ### Method `focus_to_workspace(&self, workspace_index: usize)` ### Parameters #### Path Parameters - **workspace_index** (`usize`): The index of the workspace to focus on. ``` -------------------------------- ### SwaySome::new_from_socket Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Creates a new instance of SwaySome from a given socket path. ```APIDOC ## SwaySome::new_from_socket ### Description Creates a new instance of SwaySome by connecting to a sway IPC socket. ### Method `new_from_socket(socket: &Path) -> Result<SwaySome, String>` ### Parameters #### Path Parameters - **socket** (`&Path`): The path to the sway IPC socket. ### Returns - `Result<SwaySome, String>`: Ok containing a new SwaySome instance on success, or Err with a String message on failure. ``` -------------------------------- ### Initialize SwaySome Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Creates a new instance of SwaySome. This is the primary constructor for the struct. ```rust pub fn new() -> SwaySome ``` -------------------------------- ### SwaySome::focus_to_next_group Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Focuses on the next workspace group. ```APIDOC ## SwaySome::focus_to_next_group ### Description Sets the focus to the next workspace group. ### Method `focus_to_next_group(&self)` ``` -------------------------------- ### SwaySome::move_container_to_next_output Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the currently focused container to the next output. ```APIDOC ## SwaySome::move_container_to_next_output ### Description Moves the currently focused container to the next available output display. ### Method `move_container_to_next_output(&self)` ``` -------------------------------- ### move_container_to_next_output Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves the currently focused container to the next output. If there is a visible workspace on the target output, the container will be moved to that workspace. Otherwise, it will be moved to the first available workspace on the target output. ```APIDOC ## move_container_to_next_output ### Description Moves the currently focused container to the next output. This function determines the target output and the corresponding workspace, then sends commands to move the container and focus the target workspace. ### Method `pub fn move_container_to_next_output(&self)` ### Parameters None ### Returns None ``` -------------------------------- ### SwaySome::focus_to_prev_group Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Focuses on the previous workspace group. ```APIDOC ## SwaySome::focus_to_prev_group ### Description Sets the focus to the previous workspace group. ### Method `focus_to_prev_group(&self)` ``` -------------------------------- ### Focus Workspace Relatively Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Navigates to a workspace relative to the current workspace within the same output group. ```Rust fn focus_to_workspace_relative(&self, workspace_index: usize) { let current_workspace_index: usize = self.get_current_workspace_index(); 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 * MAX_GROUP_WS + workspace_index); cmd.push_str(&full_ws_name); self.send_command(&cmd); } ``` -------------------------------- ### move_container_to_prev_output Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves the currently focused container to the previous output. Similar to `move_container_to_next_output`, it handles finding the target workspace and executing the move. ```APIDOC ## move_container_to_prev_output ### Description Moves the currently focused container to the previous output. This function determines the target output and the corresponding workspace, then sends commands to move the container and focus the target workspace. ### Method `pub fn move_container_to_prev_output(&self)` ### Parameters None ### Returns None ``` -------------------------------- ### SwaySome::move_container_to_prev_output Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the currently focused container to the previous output. ```APIDOC ## SwaySome::move_container_to_prev_output ### Description Moves the currently focused container to the previous available output display. ### Method `move_container_to_prev_output(&self)` ``` -------------------------------- ### Move Container to Next Output Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the currently focused container to the next available output monitor. ```rust pub fn move_container_to_next_output(&self) ``` -------------------------------- ### Focus to Next Group Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Sets the focus to the next workspace group. ```rust pub fn focus_to_next_group(&self) ``` -------------------------------- ### Focus Workspace by Index Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Focuses on a specific workspace by its index. If other workspaces exist on the same group, it first focuses on the corresponding output. ```Rust match self .workspaces .iter() .find(|w| w.num / MAX_GROUP_WS == target_group) { Some(other_workspace) => { let target_output = self .outputs .iter() .enumerate() .find(|i| i.1.name == other_workspace.output) .unwrap() .1; let mut focus_cmd: String = "focus output ".to_string(); focus_cmd.push_str(&target_output.name); self.send_command(&focus_cmd); } None => {} }; let mut focus_cmd: String = "workspace number ".to_string(); focus_cmd.push_str(&workspace_index.to_string()); self.send_command(&focus_cmd); } ``` -------------------------------- ### SwaySome::move_workspace_group_to_next_output Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the current workspace group to the next output. ```APIDOC ## SwaySome::move_workspace_group_to_next_output ### Description Moves the current workspace group to the next available output display. ### Method `move_workspace_group_to_next_output(&self)` ``` -------------------------------- ### SwaySome::rearrange_workspaces Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Rearranges the workspaces. ```APIDOC ## SwaySome::rearrange_workspaces ### Description Rearranges the existing workspaces according to sway's internal logic. ### Method `rearrange_workspaces(&self)` ``` -------------------------------- ### Focus to Workspace Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Sets the focus to a specific workspace by its index. ```rust pub fn focus_to_workspace(&self, workspace_index: usize) ``` -------------------------------- ### SwaySome::move_workspace_group_to_prev_output Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the current workspace group to the previous output. ```APIDOC ## SwaySome::move_workspace_group_to_prev_output ### Description Moves the current workspace group to the previous available output display. ### Method `move_workspace_group_to_prev_output(&self)` ``` -------------------------------- ### Focus to Previous Group Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Sets the focus to the previous workspace group. ```rust pub fn focus_to_prev_group(&self) ``` -------------------------------- ### Move Container to Next/Previous Output Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves the currently focused container to a workspace on the next or previous output. It identifies the target output and workspace, then sends commands to move and focus the container. ```rust fn move_container_to_next_or_prev_output(&self, go_to_prev: bool) { 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()] } else { &self.outputs[(focused_output_index + 1) % self.outputs.len()] }; let workspaces = self.get_workspaces(); let target_workspace = workspaces .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 ); // 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); } ``` -------------------------------- ### SwaySome::get_tree Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Retrieves the current sway tree structure. ```APIDOC ## SwaySome::get_tree ### Description Retrieves the current sway window manager tree structure as a JSON Value. ### Method `get_tree(&self) -> Value` ### Returns - `Value`: A JSON Value representing the sway tree. ``` -------------------------------- ### SwaySome::focus_to_group Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Focuses on a specified workspace group. ```APIDOC ## SwaySome::focus_to_group ### Description Sets the focus to the specified workspace group index. ### Method `focus_to_group(&self, group_index: usize)` ### Parameters #### Path Parameters - **group_index** (`usize`): The index of the workspace group to focus on. ``` -------------------------------- ### From Conversion Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Returns the argument unchanged. This is part of the `From` trait implementation. ```rust fn from(t: T) -> T ``` -------------------------------- ### Move Container to Previous Output Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the currently focused container to the previous available output monitor. ```rust pub fn move_container_to_prev_output(&self) ``` -------------------------------- ### TryInto Conversion Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Performs the conversion. This is part of the `TryInto` trait implementation. ```rust fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error> ``` -------------------------------- ### move_workspace_group_to_next_output Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves all workspaces in the current group to the next output. This function iterates through all workspaces in the current group and moves each one to the target output, then returns focus to the original workspace. ```APIDOC ## move_workspace_group_to_next_output ### Description Moves all workspaces belonging to the current group to the next output. It identifies the target output, then iterates through the workspaces, sending commands to move each one and finally refocusing the original workspace. ### Method `pub fn move_workspace_group_to_next_output(&self)` ### Parameters None ### Returns None ``` -------------------------------- ### TryFrom Conversion Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Performs the conversion. This is part of the `TryFrom` trait implementation. ```rust fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error> ``` -------------------------------- ### SwaySome::move_container_to_workspace Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the currently focused container to a specified workspace. ```APIDOC ## SwaySome::move_container_to_workspace ### Description Moves the currently focused container to the specified workspace index. ### Method `move_container_to_workspace(&self, workspace_index: usize)` ### Parameters #### Path Parameters - **workspace_index** (`usize`): The index of the target workspace. ``` -------------------------------- ### move_workspace_group_to_prev_output Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves all workspaces in the current group to the previous output. This function is analogous to `move_workspace_group_to_next_output` but targets the previous output. ```APIDOC ## move_workspace_group_to_prev_output ### Description Moves all workspaces belonging to the current group to the previous output. It identifies the target output, then iterates through the workspaces, sending commands to move each one and finally refocusing the original workspace. ### Method `pub fn move_workspace_group_to_prev_output(&self)` ### Parameters None ### Returns None ``` -------------------------------- ### Move Container to Workspace Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the currently focused container to a specified workspace index. ```rust pub fn move_container_to_workspace(&self, workspace_index: usize) ``` -------------------------------- ### Focus to Next/Previous Workspace Group Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Shifts focus to the next or previous workspace group. It calculates the target group index, wrapping around if necessary, and then focuses on that group. ```rust fn focus_to_next_or_prev_group(&self, go_to_prev: bool) { let current_workspace_index: usize = self.get_current_workspace_index(); let focused_group_index = current_workspace_index / MAX_GROUP_WS; let highest_group = self.workspaces.last().unwrap().num / MAX_GROUP_WS; let target_group; if go_to_prev { if focused_group_index == 0 { target_group = highest_group; } else { target_group = focused_group_index - 1; } } else { if focused_group_index >= highest_group { target_group = 0; } else { target_group = focused_group_index + 1; } }; self.focus_to_group(target_group); } ``` -------------------------------- ### Focus Group by Index Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Focuses on a workspace within a specified group. If the workspace doesn't exist, it attempts to focus on an appropriate output first. ```Rust pub fn focus_to_group(&self, group_index: usize) { let current_workspace_index: usize = 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; let full_ws_name = format!("{}", group_index * MAX_GROUP_WS + target_workspace_relative_index ); match self .workspaces .iter() .find(|w| w.num == target_workspace_index) { Some(_) => { let mut focus_cmd: String = "workspace number ".to_string(); focus_cmd.push_str(&full_ws_name); self.send_command(&focus_cmd); } None => { let target_screen_index = match self .workspaces .iter() .find(|w| w.num / MAX_GROUP_WS == group_index) { Some(other_workspace) => Some( self.outputs .iter() .enumerate() .find(|i| i.1.name == other_workspace.output) .unwrap() .0 + 1, ), None => { if group_index > 0 && group_index <= self.outputs.len() { Some(group_index) } else { None } } }; match target_screen_index { Some(target_screen_index) => { let target_output = &self.outputs[target_screen_index - 1]; let mut focus_cmd: String = "focus output ".to_string(); focus_cmd.push_str(&target_output.name); self.send_command(&focus_cmd); } None => {} }; let mut focus_cmd: String = "workspace number ".to_string(); focus_cmd.push_str(&target_workspace_index.to_string()); self.send_command(&focus_cmd); } } } ``` -------------------------------- ### Move Workspace Group to Next Output Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the current workspace group to the next output monitor. ```rust pub fn move_workspace_group_to_next_output(&self) ``` -------------------------------- ### Rearrange Workspaces Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Rearranges the existing workspaces according to Sway's internal logic or configuration. ```rust pub fn rearrange_workspaces(&self) ``` -------------------------------- ### Focus to Group Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Sets the focus to a specific group by its index. ```rust pub fn focus_to_group(&self, group_index: usize) ``` -------------------------------- ### Into Conversion Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Calls `U::from(self)`. This is part of the `Into` trait implementation. ```rust fn into(self) -> U ``` -------------------------------- ### Move Workspace Group to Previous Output Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the current workspace group to the previous output monitor. ```rust pub fn move_workspace_group_to_prev_output(&self) ``` -------------------------------- ### focus_to_prev_group Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Shifts focus to the previous workspace group. This function handles wrapping around from the first group to the last. ```APIDOC ## focus_to_prev_group ### Description Shifts the focus to the previous workspace group. If the current group is the first one, it wraps around to the last group. ### Method `pub fn focus_to_prev_group(&self)` ### Parameters None ### Returns None ``` -------------------------------- ### focus_to_next_group Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Shifts focus to the next workspace group. This function handles wrapping around from the last group to the first. ```APIDOC ## focus_to_next_group ### Description Shifts the focus to the next workspace group. If the current group is the last one, it wraps around to the first group. ### Method `pub fn focus_to_next_group(&self)` ### Parameters None ### Returns None ``` -------------------------------- ### rearrange_workspaces Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Rearranges workspaces. This method is intended for internal use or specific rearrangement logic and does not expose a direct user-facing command in this snippet. ```APIDOC ## rearrange_workspaces ### Description Initiates a rearrangement of workspaces. The specific logic for rearrangement is handled internally by sending commands to focus workspaces. ### Method `pub fn rearrange_workspaces(&self)` ### Parameters None ### Returns None ``` -------------------------------- ### Send IPC Message to Sway/i3 Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Constructs and sends an IPC message to the Sway/i3 socket. It formats the message with a prefix, payload length, message type, and the payload itself. Handles socket operations and potential write errors. ```rust fn send_msg(&self, msg_type: u32, payload: &str) { let payload_length = payload.len() as u32; let mut msg_prefix: [u8; 6 * mem::size_of::<u8>() + 2 * mem::size_of::<u32>()] = *b"i3-ipc00000000"; msg_prefix[6..] .as_mut() .write_u32::<NativeEndian>(payload_length) .expect("Unable to write"); msg_prefix[10..] .as_mut() .write_u32::<NativeEndian>(msg_type) .expect("Unable to write"); let mut msg: Vec<u8> = msg_prefix[..].to_vec(); msg.extend(payload.as_bytes()); let mut socket = self .socket .take() .expect("Unexisting socket, there probably is a logic error"); if socket.write_all(&msg[..]).is_err() { panic!("couldn't send message"); } self.socket.set(Some(socket)); } ``` -------------------------------- ### Move Container to Absolute Workspace Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves the currently focused container to a specified workspace. Handles logic for moving between different outputs if necessary. ```rust None => { let target_group = workspace_index / MAX_GROUP_WS; let target_screen_index = match self .workspaces .iter() .find(|w| w.num / MAX_GROUP_WS == target_group) { // If other workspaces on the same group exists Some(other_workspace) => Some( self.outputs .iter() .enumerate() .find(|i| i.1.name == other_workspace.output) .unwrap() .0, ), None => { // Or if the targeted output is currently connected if group_index < self.outputs.len() { Some(group_index) } else { None } } }; match target_screen_index { Some(target_screen_index) => { let target_output = &self.outputs[target_screen_index]; let current_output_name = self.get_current_output_name(); if target_output.name == current_output_name { let mut focus_cmd: String = "move container to workspace ".to_string(); focus_cmd.push_str(&full_ws_name); self.send_command(&focus_cmd); } else { // If we have to send it to another screen let mut focus_cmd: String = "focus output ".to_string(); focus_cmd.push_str(&target_output.name); self.send_command(&focus_cmd); let focused_workspace_index = self.get_current_workspace_index(); let mut focus_cmd: String = "workspace ".to_string(); focus_cmd.push_str(&full_ws_name); self.send_command(&focus_cmd); let mut focus_cmd: String = "focus output ".to_string(); focus_cmd.push_str(¤t_output_name); self.send_command(&focus_cmd); let mut focus_cmd: String = "move container to workspace ".to_string(); focus_cmd.push_str(&full_ws_name); self.send_command(&focus_cmd); let mut focus_cmd: String = "focus output ".to_string(); focus_cmd.push_str(&target_output.name); self.send_command(&focus_cmd); let mut focus_cmd: String = "workspace ".to_string(); focus_cmd.push_str(&focused_workspace_index.to_string()); self.send_command(&focus_cmd); let mut focus_cmd: String = "focus output ".to_string(); focus_cmd.push_str(¤t_output_name); self.send_command(&focus_cmd); } } None => { // Else, we send the container on the current output let mut focus_cmd: String = "move container to workspace ".to_string(); focus_cmd.push_str(&full_ws_name); self.send_command(&focus_cmd); } }; } ``` -------------------------------- ### SwaySome::move_container_to_workspace_group Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the currently focused container to a specified workspace group. ```APIDOC ## SwaySome::move_container_to_workspace_group ### Description Moves the currently focused container to the specified workspace group index. ### Method `move_container_to_workspace_group(&self, target_group: usize)` ### Parameters #### Path Parameters - **target_group** (`usize`): The index of the target workspace group. ``` -------------------------------- ### Move Workspace Group to Next/Previous Output Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves all workspaces belonging to the current group to the next or previous output. It iterates through workspaces, identifies those in the current group, and moves them to the target output. ```rust fn move_workspace_group_to_next_or_prev_output(&self, go_to_prev: bool) { 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()] } else { &self.outputs[(focused_output_index + 1) % self.outputs.len()] }; let current_workspace = self.get_current_workspace_index(); let current_group_index = (current_workspace / MAX_GROUP_WS) as usize; for workspace in self.get_workspaces() { let ws_index = workspace.num / MAX_GROUP_WS; if ws_index == current_group_index { let cmd: String = format!("workspace number {}", workspace.num); self.send_command(&cmd); let cmd: String = format!("move workspace to {}", target_output.name); self.send_command(&cmd); } } let cmd: String = format!("workspace number {}", current_workspace); self.send_command(&cmd); } ``` -------------------------------- ### Move Container to Relative Workspace Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves the currently focused container to a workspace relative to the current output's workspace group. ```rust fn move_container_to_workspace_relative(&self, workspace_index: usize) { let current_workspace_index: usize = 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(); let full_ws_name = format!("{}", focused_output_index * MAX_GROUP_WS + workspace_index); cmd.push_str(&full_ws_name); self.send_command(&cmd); } ``` -------------------------------- ### Move Container to Workspace Group Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Moves the currently focused container to a specified workspace group index. ```rust pub fn move_container_to_workspace_group(&self, target_group: usize) ``` -------------------------------- ### Check Command Success Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Reads a message from the socket and checks if the command executed successfully based on the 'success' field in the JSON response. Panics if the command fails or if the response cannot be read. ```rust match self.read_msg() { Ok(msg) => { let r: Vec<serde_json::Value> = serde_json::from_str(&msg).unwrap(); match r[0]["success"] { serde_json::Value::Bool(true) => eprintln!("Command successful"), _ => panic!("Command failed: {:#?}", r), } }, Err(_) => panic!("Unable to read response"), }; ``` -------------------------------- ### Move Container to Absolute Workspace Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves the current container to a specific absolute workspace number. It constructs the full workspace name and checks if the workspace already exists before sending the command. ```rust let group_index = (workspace_index / MAX_GROUP_WS) as usize; let full_ws_name = format!( "{}", group_index * MAX_GROUP_WS + workspace_index % MAX_GROUP_WS ); // If the workspace already exists match self.workspaces.iter().find(|w| w.num == workspace_index) { Some(_) => { let mut focus_cmd: String = "move container to workspace number ".to_string(); focus_cmd.push_str(&full_ws_name); ``` -------------------------------- ### Read IPC Message Response Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Reads an IPC message response from the Sway/i3 socket. It expects a specific header format and parses the payload length to extract the response payload as a UTF-8 string. Returns an error if the header is not recognized or if reading fails. ```rust fn read_msg(&self) -> Result<String, &str> { let mut response_header: [u8; 14] = *b"uninitialized."; let mut socket = self .socket .take() .expect("Unexisting socket, there probably is a logic error"); socket.read_exact(&mut response_header).unwrap(); if &response_header[0..6] == b"i3-ipc" { let mut v = Cursor::new(vec![ response_header[6], response_header[7], response_header[8], response_header[9], ]); let payload_length = v.read_u32::<NativeEndian>().unwrap(); let mut payload = vec![0; payload_length as usize]; socket.read_exact(&mut payload[..]).unwrap(); let payload_str = String::from_utf8(payload).unwrap(); self.socket.set(Some(socket)); Ok(payload_str) } else { // This part of the code is incomplete in the provided snippet. // It should likely return an error or handle the unrecognized header. unimplemented!("Unrecognized IPC message header"); } } ``` -------------------------------- ### Move Container to Workspace Group Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Moves the current container to a workspace within a specified group. It calculates the target absolute workspace index based on the current workspace's group and the target group. ```rust let current_workspace_index = self.get_current_workspace_index(); let current_workspace_index_relative = (current_workspace_index % MAX_GROUP_WS) as usize; self.move_container_to_workspace_absolute( current_workspace_index_relative + target_group * MAX_GROUP_WS, ); ``` -------------------------------- ### SwaySome Struct Definition Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Defines the SwaySome struct. It is typically used to interact with the Sway IPC interface. ```rust pub struct SwaySome { /* private fields */ } ``` -------------------------------- ### Read Message from Socket Source: https://docs.rs/swaysome/2.3.2/src/swaysome/lib.rs.html Reads data from a socket into a buffer. Handles cases where the data is not a valid i3-ipc packet by emptying the buffer and returning an error. ```rust eprintln!("Not an i3-icp packet, emptying the buffer: "); let mut v = vec![]; socket.read_to_end(&mut v).unwrap(); eprintln!("{:?}", v); self.socket.set(Some(socket)); Err("Unable to read i3-ipc packet") ``` -------------------------------- ### Borrow Value Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Immutably borrows from an owned value. This is part of the `Borrow` trait implementation. ```rust fn borrow(&self) -> &T ``` -------------------------------- ### Borrow Mutably Source: https://docs.rs/swaysome/2.3.2/swaysome/struct.SwaySome.html Mutably borrows from an owned value. This is part of the `BorrowMut` trait implementation. ```rust fn borrow_mut(&mut self) -> &mut T ```