Merge branch 'cli_parser_argument_types' into 'master'

Cli parser argument types

Just a little stronger typing on the CLI arguments 

See merge request hyask/swaysome!5
This commit is contained in:
Skia 2023-01-13 16:09:43 +00:00
commit 29a0afe619

View file

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