From e778244b53584753397249c1f51d86a0d04e9f08 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Thu, 4 Nov 2021 21:40:41 +0100 Subject: [PATCH] New move-focused-to-workspace command --- NEWS.md | 4 ++++ src/cmds.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 6ef858b..bc7063f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -18,6 +18,10 @@ swayr v0.9.0 - `s:`: Executes the sway command `` using `swaymsg`. - Any other input is assumed to be a workspace name and thus handled as `w:` would do. +- There's a new command `move-focused-to-workspace` which moves the currently + focused window or container to another workspace selected with the menu + program. Non-matching input of the form `#w:` where the hash and + `w:` shortcut are optional can be used to move it to a new workspace. swayr v0.8.0 diff --git a/src/cmds.rs b/src/cmds.rs index 390e7ae..3dabb45 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -110,6 +110,8 @@ pub enum SwayrCommand { SwitchWorkspaceOrWindow, /// Quit all windows of selected workspace or the selected window. QuitWorkspaceOrWindow, + /// Move the currently focused window or container to the selected workspace. + MoveFocusedToWorkspace, /// Tab or shuffle-and-tile the windows on the current workspace, including /// or excluding floating windows. ToggleTabShuffleTileWorkspace { @@ -278,6 +280,9 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { &*props.read().unwrap(), ) } + SwayrCommand::MoveFocusedToWorkspace => { + move_focused_container_to_workspace(&*props.read().unwrap()) + } SwayrCommand::QuitWindow => quit_window(&*props.read().unwrap()), SwayrCommand::SwitchWorkspace => { switch_workspace(&*props.read().unwrap()) @@ -304,6 +309,7 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { SwayrCommand::ExecuteSwayrCommand => { let mut cmds = vec![ SwayrCommand::ExecuteSwaymsgCommand, + SwayrCommand::MoveFocusedToWorkspace, SwayrCommand::QuitWindow, SwayrCommand::QuitWorkspaceOrWindow, SwayrCommand::SwitchWindow, @@ -413,6 +419,13 @@ lazy_static! { regex::Regex::new(r"^#*s:(.*)").unwrap(); } +fn chop_workspace_shortcut(input: &str) -> &str { + match SPECIAL_WORKSPACE.captures(input) { + Some(c) => c.get(1).unwrap().as_str(), + None => input, + } +} + fn handle_non_matching_input(input: &str) { if input.is_empty() { return; @@ -420,10 +433,9 @@ fn handle_non_matching_input(input: &str) { if let Some(c) = SPECIAL_SWAY.captures(input) { run_sway_command(&c[1].split_ascii_whitespace().collect::>()); - } else if let Some(c) = SPECIAL_WORKSPACE.captures(input) { - create_workspace(&c[1]); } else { - create_workspace(input); + let ws = chop_workspace_shortcut(input); + create_workspace(ws); } } @@ -552,6 +564,35 @@ pub fn switch_workspace(extra_props: &HashMap) { } } +pub fn move_focused_container_to_workspace( + extra_props: &HashMap, +) { + let root = get_tree(); + let workspaces = con::get_workspaces(&root, false, extra_props); + + let val = util::select_from_menu( + "Move focused container to workspace", + &workspaces, + ); + let ws_name = &match val { + Ok(workspace) => String::from(workspace.get_name()), + Err(input) => String::from(chop_workspace_shortcut(&input)), + }; + + if DIGIT_AND_NAME.is_match(ws_name) { + run_sway_command(&[ + "move", + "container", + "to", + "workspace", + "number", + ws_name, + ]); + } else { + run_sway_command(&["move", "container", "to", "workspace", ws_name]); + } +} + pub fn switch_workspace_or_window(extra_props: &HashMap) { let root = get_tree(); let workspaces = con::get_workspaces(&root, true, extra_props);