Factorize to a 'send_command' function
This commit is contained in:
parent
dc5aff5bd8
commit
21ea0166f7
1 changed files with 13 additions and 19 deletions
32
src/main.rs
32
src/main.rs
|
|
@ -61,6 +61,12 @@ fn send_msg(mut stream: &UnixStream, msg_type: u32, payload: &str) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn send_command(mut stream: &UnixStream, command: &str) {
|
||||||
|
println!("Sending command: '{}'", &command);
|
||||||
|
send_msg(&stream, RUN_COMMAND, &command);
|
||||||
|
check_success(&stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn read_msg(mut stream: &UnixStream) -> Result<String, &str> {
|
fn read_msg(mut stream: &UnixStream) -> Result<String, &str> {
|
||||||
let mut response_header: [u8; 14] = *b"uninitialized.";
|
let mut response_header: [u8; 14] = *b"uninitialized.";
|
||||||
|
|
@ -146,9 +152,7 @@ fn move_container_to_workspace(stream: &UnixStream, workspace_name: &String) {
|
||||||
let output = get_current_output_index(stream);
|
let output = get_current_output_index(stream);
|
||||||
cmd.push_str(&output);
|
cmd.push_str(&output);
|
||||||
cmd.push_str(&workspace_name);
|
cmd.push_str(&workspace_name);
|
||||||
println!("Sending command: '{}'", &cmd);
|
send_command(&stream, &cmd);
|
||||||
send_msg(&stream, RUN_COMMAND, &cmd);
|
|
||||||
check_success(&stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn focus_to_workspace(stream: &UnixStream, workspace_name: &String) {
|
fn focus_to_workspace(stream: &UnixStream, workspace_name: &String) {
|
||||||
|
|
@ -156,9 +160,7 @@ fn focus_to_workspace(stream: &UnixStream, workspace_name: &String) {
|
||||||
let output = get_current_output_index(stream);
|
let output = get_current_output_index(stream);
|
||||||
cmd.push_str(&output);
|
cmd.push_str(&output);
|
||||||
cmd.push_str(&workspace_name);
|
cmd.push_str(&workspace_name);
|
||||||
println!("Sending command: '{}'", &cmd);
|
send_command(&stream, &cmd);
|
||||||
send_msg(&stream, RUN_COMMAND, &cmd);
|
|
||||||
check_success(&stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn focus_all_outputs_to_workspace(stream: &UnixStream, workspace_name: &String) {
|
fn focus_all_outputs_to_workspace(stream: &UnixStream, workspace_name: &String) {
|
||||||
|
|
@ -170,9 +172,7 @@ fn focus_all_outputs_to_workspace(stream: &UnixStream, workspace_name: &String)
|
||||||
for output in outputs.iter() {
|
for output in outputs.iter() {
|
||||||
let mut cmd: String = "focus output ".to_string();
|
let mut cmd: String = "focus output ".to_string();
|
||||||
cmd.push_str(&output["name"].as_str().unwrap());
|
cmd.push_str(&output["name"].as_str().unwrap());
|
||||||
println!("Sending command: '{}'", &cmd);
|
send_command(&stream, &cmd);
|
||||||
send_msg(&stream, RUN_COMMAND, &cmd);
|
|
||||||
check_success(&stream);
|
|
||||||
|
|
||||||
focus_to_workspace(&stream, &workspace_name);
|
focus_to_workspace(&stream, &workspace_name);
|
||||||
}
|
}
|
||||||
|
|
@ -180,9 +180,7 @@ fn focus_all_outputs_to_workspace(stream: &UnixStream, workspace_name: &String)
|
||||||
// Get back to currently focused output
|
// Get back to currently focused output
|
||||||
let mut cmd: String = "focus output ".to_string();
|
let mut cmd: String = "focus output ".to_string();
|
||||||
cmd.push_str(¤t_output);
|
cmd.push_str(¤t_output);
|
||||||
println!("Sending command: '{}'", &cmd);
|
send_command(&stream, &cmd);
|
||||||
send_msg(&stream, RUN_COMMAND, &cmd);
|
|
||||||
check_success(&stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_container_to_next_output(stream: &UnixStream) {
|
fn move_container_to_next_output(stream: &UnixStream) {
|
||||||
|
|
@ -215,14 +213,12 @@ fn move_container_to_next_or_prev_output(stream: &UnixStream, go_to_prev: bool)
|
||||||
// Move container to target workspace
|
// Move container to target workspace
|
||||||
let mut cmd: String = "move container to workspace ".to_string();
|
let mut cmd: String = "move container to workspace ".to_string();
|
||||||
cmd.push_str(&target_workspace["name"].as_str().unwrap());
|
cmd.push_str(&target_workspace["name"].as_str().unwrap());
|
||||||
send_msg(&stream, RUN_COMMAND, &cmd);
|
send_command(&stream, &cmd);
|
||||||
check_success(&stream);
|
|
||||||
|
|
||||||
// Focus that workspace to follow the container
|
// Focus that workspace to follow the container
|
||||||
let mut cmd: String = "workspace ".to_string();
|
let mut cmd: String = "workspace ".to_string();
|
||||||
cmd.push_str(&target_workspace["name"].as_str().unwrap());
|
cmd.push_str(&target_workspace["name"].as_str().unwrap());
|
||||||
send_msg(&stream, RUN_COMMAND, &cmd);
|
send_command(&stream, &cmd);
|
||||||
check_success(&stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_workspaces(stream: &UnixStream, workspace_name: &String) {
|
fn init_workspaces(stream: &UnixStream, workspace_name: &String) {
|
||||||
|
|
@ -232,9 +228,7 @@ fn init_workspaces(stream: &UnixStream, workspace_name: &String) {
|
||||||
for output in outputs.iter().rev() {
|
for output in outputs.iter().rev() {
|
||||||
let mut cmd = cmd_prefix.clone();
|
let mut cmd = cmd_prefix.clone();
|
||||||
cmd.push_str(&output["name"].as_str().unwrap());
|
cmd.push_str(&output["name"].as_str().unwrap());
|
||||||
println!("Sending command: '{}'", &cmd);
|
send_command(&stream, &cmd);
|
||||||
send_msg(&stream, RUN_COMMAND, &cmd);
|
|
||||||
check_success(&stream);
|
|
||||||
focus_to_workspace(&stream, &workspace_name);
|
focus_to_workspace(&stream, &workspace_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue