Strongly typed workspace indexes and output indexes

This commit is contained in:
Nabos 2022-08-03 17:58:18 +02:00
parent 6fe7ca7bc3
commit dd848dfe04

View file

@ -54,19 +54,19 @@ enum Command {
#[derive(Args, Debug)]
struct InitAction {
#[clap(value_name = "index", help = "The index to initialize with")]
name: String,
index: usize,
}
#[derive(Args, Debug)]
struct FocusAction {
#[clap(value_name = "index", help = "The index to focus on")]
name: String,
index: usize,
}
#[derive(Args, Debug)]
struct MoveAction {
#[clap(value_name = "index", help = "The index to move the container to")]
name: String,
index: usize,
}
fn get_stream() -> UnixStream {
@ -175,7 +175,7 @@ fn get_outputs(stream: &UnixStream) -> Vec<Output> {
#[derive(Serialize, Deserialize, Debug)]
struct Workspace {
num: u32,
num: usize,
output: String,
visible: bool,
}
@ -191,15 +191,13 @@ fn get_workspaces(stream: &UnixStream) -> Vec<Workspace> {
workspaces
}
fn get_current_output_index(stream: &UnixStream) -> String {
fn get_current_output_index(stream: &UnixStream) -> usize {
let outputs = get_outputs(stream);
let focused_output_index = match outputs.iter().position(|x| x.focused) {
match outputs.iter().position(|x| x.focused) {
Some(i) => i,
None => panic!("WTF! No focused output???"),
};
format!("{}", focused_output_index)
}
}
fn get_current_output_name(stream: &UnixStream) -> String {
@ -213,25 +211,25 @@ fn get_current_output_name(stream: &UnixStream) -> String {
focused_output_index.to_string()
}
fn move_container_to_workspace(stream: &UnixStream, workspace_name: &String) {
fn move_container_to_workspace(stream: &UnixStream, workspace_index: usize) {
let mut cmd: String = "move container to workspace number ".to_string();
let full_ws_name = format!("{}{}", get_current_output_index(stream), &workspace_name)
let full_ws_name = format!("{}{}", get_current_output_index(stream), workspace_index)
.parse::<i32>()
.unwrap();
cmd.push_str(&full_ws_name.to_string());
send_command(stream, &cmd);
}
fn focus_to_workspace(stream: &UnixStream, workspace_name: &String) {
fn focus_to_workspace(stream: &UnixStream, workspace_index: usize) {
let mut cmd: String = "workspace number ".to_string();
let full_ws_name = format!("{}{}", get_current_output_index(stream), &workspace_name)
let full_ws_name = format!("{}{}", get_current_output_index(stream), &workspace_index)
.parse::<i32>()
.unwrap();
cmd.push_str(&full_ws_name.to_string());
send_command(stream, &cmd);
}
fn focus_all_outputs_to_workspace(stream: &UnixStream, workspace_name: &String) {
fn focus_all_outputs_to_workspace(stream: &UnixStream, workspace_index: usize) {
let current_output = get_current_output_name(stream);
// Iterate on all outputs to focus on the given workspace
@ -241,7 +239,7 @@ fn focus_all_outputs_to_workspace(stream: &UnixStream, workspace_name: &String)
cmd.push_str(output.name.as_str());
send_command(stream, &cmd);
focus_to_workspace(stream, workspace_name);
focus_to_workspace(stream, workspace_index);
}
// Get back to currently focused output
@ -288,7 +286,7 @@ fn move_container_to_next_or_prev_output(stream: &UnixStream, go_to_prev: bool)
send_command(stream, &cmd);
}
fn init_workspaces(stream: &UnixStream, workspace_name: &String) {
fn init_workspaces(stream: &UnixStream, workspace_index: usize) {
let outputs = get_outputs(stream);
let cmd_prefix: String = "focus output ".to_string();
@ -297,7 +295,7 @@ fn init_workspaces(stream: &UnixStream, workspace_name: &String) {
cmd.push_str(output.name.as_str());
println!("{:?}", cmd.clone());
send_command(stream, &cmd);
focus_to_workspace(stream, workspace_name);
focus_to_workspace(stream, workspace_index);
}
}
@ -312,7 +310,7 @@ fn rearrange_workspaces(stream: &UnixStream) {
focus_cmd.push_str(&workspace.num.to_string());
send_command(stream, &focus_cmd);
let output_index = (workspace.num / 10) as usize;
let output_index = workspace.num / 10;
if output_index < outputs.len() {
let mut move_cmd = move_cmd_prefix.clone();
move_cmd.push_str(&outputs[output_index].name);
@ -327,16 +325,16 @@ fn main() {
match &cli.command {
Command::Init(action) => {
init_workspaces(&stream, &action.name);
init_workspaces(&stream, action.index);
}
Command::Move(action) => {
move_container_to_workspace(&stream, &action.name);
move_container_to_workspace(&stream, action.index);
}
Command::Focus(action) => {
focus_to_workspace(&stream, &action.name);
focus_to_workspace(&stream, action.index);
}
Command::FocusAllOutputs(action) => {
focus_all_outputs_to_workspace(&stream, &action.name);
focus_all_outputs_to_workspace(&stream, action.index);
}
Command::NextOutput => {
move_container_to_next_output(&stream);