Skip to content

Commit

Permalink
Added Commands Enum instead of matching strings
Browse files Browse the repository at this point in the history
  • Loading branch information
hbere committed Oct 30, 2024
1 parent 17e64ce commit fd59460
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 67 deletions.
57 changes: 29 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 36 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@ use workspaces::Workspaces;
pub mod ipcadapter;
pub mod workspaces;

pub enum Command {
Next,
Prev,
SwapWithPrev,
SwapWithNext,
Increase,
Decrease,
RenameTo,
Number,
MoveContainerToWorkspaceNumber,
Select,
PrintFocusedName,
PrintFocusedNumber,
RofiSelectWorkspace,
RofiMoveWindow,
Usage,
}

pub fn execute_userinput(
workspaces: Rc<Workspaces>,
argument: String,
command: Command,
argument_parameter: Option<String>,
) -> IpcResult {
match argument.as_str() {
"next" => match &mut workspaces
match command {
Command::Next => match &mut workspaces
.on_same_screen()
.next_of(workspaces.focused_index())
{
Expand All @@ -32,7 +50,7 @@ pub fn execute_userinput(
None => workspaces.select(&workspaces.on_same_screen().first().unwrap().basename),
},
},
"prev" => {
Command::Prev => {
match &workspaces
.on_same_screen()
.prev_of(workspaces.focused_index())
Expand All @@ -54,21 +72,21 @@ pub fn execute_userinput(
},
}
}
"swap_with_next" => swap_workspace(
Command::SwapWithNext => swap_workspace(
&workspaces,
workspaces.on_same_screen().get(workspaces.focused_index()),
workspaces
.on_same_screen()
.next_of(workspaces.focused_index()),
),
"swap_with_prev" => swap_workspace(
Command::SwapWithPrev => swap_workspace(
&workspaces,
workspaces
.on_same_screen()
.prev_of(workspaces.focused_index()),
workspaces.on_same_screen().get(workspaces.focused_index()),
),
"increase" => match (
Command::Increase => match (
workspaces.get_focused(),
workspaces
.on_same_screen()
Expand All @@ -81,7 +99,7 @@ pub fn execute_userinput(
(ws1, Some(ws2)) => workspaces.swap(ws1, ws2),
(ws, None) => workspaces.increase_number(ws),
},
"decrease" => {
Command::Decrease => {
match (
workspaces
.on_same_screen()
Expand All @@ -96,7 +114,7 @@ pub fn execute_userinput(
(None, ws) => workspaces.decrease_number(ws),
}
}
"rename_to" => {
Command::RenameTo => {
if let Some(new_name) = argument_parameter {
workspaces.get_focused().rename(&format!(
"{} {}",
Expand All @@ -109,7 +127,7 @@ pub fn execute_userinput(
.rename(&format!("{}", workspaces.get_focused().get_number()))
}
}
"number" => match argument_parameter {
Command::Number => match argument_parameter {
Some(number) => {
if let Ok(number) = number.parse::<usize>() {
workspaces.select_or_create_number(number)
Expand All @@ -120,7 +138,7 @@ pub fn execute_userinput(
_ => panic!("desired workspace Number missing"),
},

"move_container_to_workspace_number" => match argument_parameter {
Command::MoveContainerToWorkspaceNumber => match argument_parameter {
Some(number) => {
if let Ok(number) = number.parse::<usize>() {
workspaces.move_container_to_number(number)
Expand All @@ -130,27 +148,27 @@ pub fn execute_userinput(
}
_ => panic!("desired workspace Number missing"),
},
"select" => {
Command::Select => {
if let Some(workspace) = argument_parameter {
workspaces.select(&workspace)
} else {
panic!("Workspace missing")
}
}
"print_focused_name" => {
Command::PrintFocusedName => {
println!("{}", workspaces.get_focused().get_name());
// Ok(vec![workspaces.get_focused().get_name().to_string()])
Ok(vec!["nonipc: print_focused_name".to_string()])
}
"print_focused_number" => {
Command::PrintFocusedNumber => {
println!("{}", workspaces.get_focused().get_number());
// Ok(vec![workspaces.get_focused().get_number().to_string()])
Ok(vec!["nonipc: print_focused_number".to_string()])
}
"rofi_select_workspace" => match argument_parameter {
Command::RofiSelectWorkspace => match argument_parameter {
Some(workspacename) => execute_userinput(
workspaces.clone(),
"select".to_string(),
Command::Select,
Some(workspacename),
),
None => {
Expand All @@ -161,7 +179,7 @@ pub fn execute_userinput(
Ok(vec!["nonipc: rofi_select_workspace".to_string()])
}
},
"rofi_move_window" => match argument_parameter {
Command::RofiMoveWindow => match argument_parameter {
Some(workspacename) => match extract_starting_number(&workspacename) {
Some(_number) => workspaces.move_container_to(&workspacename),
None => workspaces.move_container_to(&format!(
Expand All @@ -178,7 +196,7 @@ pub fn execute_userinput(
Ok(vec!["nonipc: rofi_move_window".to_string()])
}
},
_ => {
Command::Usage => {
eprintln!("valid arguments: [ next prev swap_with_prev swap_with_next increase decrease rename_to select print_focused_name print_focused_number rofi_select_workspace rofi_move_window ]. ");
Ok(vec!["nonipc: Instructions printed".to_string()])
}
Expand Down
24 changes: 21 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use swaymsg_workspace::execute_userinput;
use swaymsg_workspace::{execute_userinput, Command};
pub mod ipcadapter;
pub mod workspaces;

Expand All @@ -10,17 +10,35 @@ fn main() {
let ipcadapter = SwayIPCAdapter::new();
let workspaces = Workspaces::new(ipcadapter);

// split args by ' ' to handle the combined argument which rofi supplies / until i figured out how to read the piped in signal
// split args by ' ' to handle the combined argument which rofi supplies
let mut args = env::args()
.collect::<Vec<String>>()
.into_iter()
.flat_map(|arg| arg.split(' ').map(str::to_owned).collect::<Vec<String>>())
.into_iter();

if let Some(main_argument) = args.nth(1) {
let command_from_argument = match main_argument.as_str() {
"next" => Command::Next,
"prev" => Command::Prev,
"swap_with_prev" => Command::SwapWithPrev,
"swap_with_next" => Command::SwapWithNext,
"increase" => Command::Increase,
"decrease" => Command::Decrease,
"rename_to" => Command::RenameTo,
"number" => Command::Number,
"move_container_to_workspace_number" => Command::MoveContainerToWorkspaceNumber,
"select" => Command::Select,
"print_focused_name" => Command::PrintFocusedName,
"print_focused_number" => Command::PrintFocusedNumber,
"rofi_select_workspace" => Command::RofiSelectWorkspace,
"rofi_move_window" => Command::RofiMoveWindow,
_ => Command::Usage,
};

if let Err(error) = execute_userinput(
workspaces,
main_argument,
command_from_argument,
args.reduce(|a, b| format!("{} {}", a, b)), // parameters to argument
) {
eprintln!("Something broke: {error}");
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_test_container_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn test_moving_container() {
let (workspaces, commandhistory) = common::setup_4_workspaces_across_3_outputs();
let result = swaymsg_workspace::execute_userinput(
workspaces,
String::from("move_container_to_workspace_number"),
swaymsg_workspace::Command::MoveContainerToWorkspaceNumber,
Some("1".to_string()),
);
let expected = "move window to workspace '1 Foo'";
Expand Down
Loading

0 comments on commit fd59460

Please sign in to comment.