From 018412f5151850a42d7008199eef17585fcf828b Mon Sep 17 00:00:00 2001 From: Dan Nixon Date: Wed, 28 Aug 2024 17:54:35 +0100 Subject: [PATCH 1/3] sway-helper: add move to workspace command --- .../pkgs/sway-helper/src/commands/combi.rs | 1 + overlays/pkgs/sway-helper/src/commands/mod.rs | 4 +++ .../src/commands/move_window_to_workspace.rs | 29 +++++++++++++++++++ overlays/pkgs/sway-helper/src/helpers/sway.rs | 11 +++++++ 4 files changed, 45 insertions(+) create mode 100644 overlays/pkgs/sway-helper/src/commands/move_window_to_workspace.rs diff --git a/overlays/pkgs/sway-helper/src/commands/combi.rs b/overlays/pkgs/sway-helper/src/commands/combi.rs index c3123e3..e37f83b 100644 --- a/overlays/pkgs/sway-helper/src/commands/combi.rs +++ b/overlays/pkgs/sway-helper/src/commands/combi.rs @@ -33,6 +33,7 @@ impl CliRun for Combi { "new-named-workspace", "rename-workspace", "switch-to-workspace", + "move-window-to-workspace", ] .iter() .map(|i| format!("[{}] {i}", CMD.yellow())), diff --git a/overlays/pkgs/sway-helper/src/commands/mod.rs b/overlays/pkgs/sway-helper/src/commands/mod.rs index 0df8ca9..123258e 100644 --- a/overlays/pkgs/sway-helper/src/commands/mod.rs +++ b/overlays/pkgs/sway-helper/src/commands/mod.rs @@ -1,5 +1,6 @@ mod combi; mod focus_window; +mod move_window_to_workspace; mod new_workspace; mod rename_workspace; mod run; @@ -8,6 +9,7 @@ mod switch_to_workspace; use self::{ combi::Combi, focus_window::FocusWindow, + move_window_to_workspace::MoveWindowToWorkspace, new_workspace::{NewNamedWorkspace, NewWorkspace}, rename_workspace::RenameWorkspace, run::Run, @@ -28,6 +30,7 @@ pub(crate) enum Command { RenameWorkspace, SwitchToWorkspace, FocusWindow, + MoveWindowToWorkspace, } impl CliRun for Command { @@ -40,6 +43,7 @@ impl CliRun for Command { Self::RenameWorkspace => RenameWorkspace {}.run(sway), Self::SwitchToWorkspace => SwitchToWorkspace {}.run(sway), Self::FocusWindow => FocusWindow {}.run(sway), + Self::MoveWindowToWorkspace => MoveWindowToWorkspace {}.run(sway), } } } diff --git a/overlays/pkgs/sway-helper/src/commands/move_window_to_workspace.rs b/overlays/pkgs/sway-helper/src/commands/move_window_to_workspace.rs new file mode 100644 index 0000000..b02d553 --- /dev/null +++ b/overlays/pkgs/sway-helper/src/commands/move_window_to_workspace.rs @@ -0,0 +1,29 @@ +use super::CliRun; +use crate::helpers::sway::{ + get_container_id_from_fzf_string, get_containers_fzf_strings, get_workspace_names, + move_container_to_workspace, FocusedWorkspacePosition, +}; + +pub(crate) struct MoveWindowToWorkspace {} + +impl CliRun for MoveWindowToWorkspace { + fn run(&self, sway: &mut swayipc::Connection) -> crate::Result<()> { + let tree = sway.get_tree()?; + + let mut items = Vec::new(); + get_containers_fzf_strings(&tree, None, &mut items); + + let selection = crate::helpers::run_fzf("window to move", items)?; + + let container_id = get_container_id_from_fzf_string(&selection)?; + + // Get a list of all the workspace names, with the focused workspace at the front (allowing + // fastest access to editing). + let workspace_names = get_workspace_names(sway, FocusedWorkspacePosition::Front)?; + + // Fuzzy select a workspace. + let workspace_name = crate::helpers::run_fzf("workspace to move to", workspace_names)?; + + move_container_to_workspace(sway, &container_id, &workspace_name) + } +} diff --git a/overlays/pkgs/sway-helper/src/helpers/sway.rs b/overlays/pkgs/sway-helper/src/helpers/sway.rs index 323923d..6c3d0f5 100644 --- a/overlays/pkgs/sway-helper/src/helpers/sway.rs +++ b/overlays/pkgs/sway-helper/src/helpers/sway.rs @@ -106,3 +106,14 @@ pub(crate) fn focus_container( sway.run_command(format!("[con_id={container_id}] focus"))?; Ok(()) } + +pub(crate) fn move_container_to_workspace( + sway: &mut swayipc::Connection, + container_id: &str, + workspace_name: &str, +) -> crate::Result<()> { + sway.run_command(format!( + "[con_id={container_id}] move to workspace {workspace_name}" + ))?; + Ok(()) +} From 915538d98757cdf5e3ac63901090d01103401374 Mon Sep 17 00:00:00 2001 From: Dan Nixon Date: Wed, 28 Aug 2024 18:03:12 +0100 Subject: [PATCH 2/3] sway-helper: add move window to current workspace command --- .../pkgs/sway-helper/src/commands/combi.rs | 1 + overlays/pkgs/sway-helper/src/commands/mod.rs | 6 +- .../sway-helper/src/commands/move_window.rs | 56 +++++++++++++++++++ .../src/commands/move_window_to_workspace.rs | 29 ---------- 4 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 overlays/pkgs/sway-helper/src/commands/move_window.rs delete mode 100644 overlays/pkgs/sway-helper/src/commands/move_window_to_workspace.rs diff --git a/overlays/pkgs/sway-helper/src/commands/combi.rs b/overlays/pkgs/sway-helper/src/commands/combi.rs index e37f83b..566019a 100644 --- a/overlays/pkgs/sway-helper/src/commands/combi.rs +++ b/overlays/pkgs/sway-helper/src/commands/combi.rs @@ -33,6 +33,7 @@ impl CliRun for Combi { "new-named-workspace", "rename-workspace", "switch-to-workspace", + "move-window-here", "move-window-to-workspace", ] .iter() diff --git a/overlays/pkgs/sway-helper/src/commands/mod.rs b/overlays/pkgs/sway-helper/src/commands/mod.rs index 123258e..13891e0 100644 --- a/overlays/pkgs/sway-helper/src/commands/mod.rs +++ b/overlays/pkgs/sway-helper/src/commands/mod.rs @@ -1,6 +1,6 @@ mod combi; mod focus_window; -mod move_window_to_workspace; +mod move_window; mod new_workspace; mod rename_workspace; mod run; @@ -9,7 +9,7 @@ mod switch_to_workspace; use self::{ combi::Combi, focus_window::FocusWindow, - move_window_to_workspace::MoveWindowToWorkspace, + move_window::{MoveWindowHere, MoveWindowToWorkspace}, new_workspace::{NewNamedWorkspace, NewWorkspace}, rename_workspace::RenameWorkspace, run::Run, @@ -30,6 +30,7 @@ pub(crate) enum Command { RenameWorkspace, SwitchToWorkspace, FocusWindow, + MoveWindowHere, MoveWindowToWorkspace, } @@ -43,6 +44,7 @@ impl CliRun for Command { Self::RenameWorkspace => RenameWorkspace {}.run(sway), Self::SwitchToWorkspace => SwitchToWorkspace {}.run(sway), Self::FocusWindow => FocusWindow {}.run(sway), + Self::MoveWindowHere => MoveWindowHere {}.run(sway), Self::MoveWindowToWorkspace => MoveWindowToWorkspace {}.run(sway), } } diff --git a/overlays/pkgs/sway-helper/src/commands/move_window.rs b/overlays/pkgs/sway-helper/src/commands/move_window.rs new file mode 100644 index 0000000..4c7a9c7 --- /dev/null +++ b/overlays/pkgs/sway-helper/src/commands/move_window.rs @@ -0,0 +1,56 @@ +use super::CliRun; +use crate::helpers::sway::{ + focus_container, get_container_id_from_fzf_string, get_containers_fzf_strings, + get_workspace_names, move_container_to_workspace, FocusedWorkspacePosition, +}; + +pub(crate) struct MoveWindowHere {} + +impl CliRun for MoveWindowHere { + fn run(&self, sway: &mut swayipc::Connection) -> crate::Result<()> { + let tree = sway.get_tree()?; + + let mut items = Vec::new(); + get_containers_fzf_strings(&tree, None, &mut items); + + let selection = crate::helpers::run_fzf("window to move", items)?; + + let container_id = get_container_id_from_fzf_string(&selection)?; + + let workspace_names = get_workspace_names(sway, FocusedWorkspacePosition::Front)?; + + move_container_to_workspace( + sway, + &container_id, + workspace_names + .first() + .expect("there should be at least one workspace"), + )?; + + focus_container(sway, &container_id) + } +} + +pub(crate) struct MoveWindowToWorkspace {} + +impl CliRun for MoveWindowToWorkspace { + fn run(&self, sway: &mut swayipc::Connection) -> crate::Result<()> { + let tree = sway.get_tree()?; + + let mut items = Vec::new(); + get_containers_fzf_strings(&tree, None, &mut items); + + let selection = crate::helpers::run_fzf("window to move", items)?; + + let container_id = get_container_id_from_fzf_string(&selection)?; + + // Get a list of all the workspace names, with the focused workspace at the front (allowing + // fastest access to editing). + let workspace_names = get_workspace_names(sway, FocusedWorkspacePosition::Front)?; + + // Fuzzy select a workspace. + let workspace_name = crate::helpers::run_fzf("workspace to move to", workspace_names)?; + + move_container_to_workspace(sway, &container_id, &workspace_name) + } +} diff --git a/overlays/pkgs/sway-helper/src/commands/move_window_to_workspace.rs b/overlays/pkgs/sway-helper/src/commands/move_window_to_workspace.rs deleted file mode 100644 index b02d553..0000000 --- a/overlays/pkgs/sway-helper/src/commands/move_window_to_workspace.rs +++ /dev/null @@ -1,29 +0,0 @@ -use super::CliRun; -use crate::helpers::sway::{ - get_container_id_from_fzf_string, get_containers_fzf_strings, get_workspace_names, - move_container_to_workspace, FocusedWorkspacePosition, -}; - -pub(crate) struct MoveWindowToWorkspace {} - -impl CliRun for MoveWindowToWorkspace { - fn run(&self, sway: &mut swayipc::Connection) -> crate::Result<()> { - let tree = sway.get_tree()?; - - let mut items = Vec::new(); - get_containers_fzf_strings(&tree, None, &mut items); - - let selection = crate::helpers::run_fzf("window to move", items)?; - - let container_id = get_container_id_from_fzf_string(&selection)?; - - // Get a list of all the workspace names, with the focused workspace at the front (allowing - // fastest access to editing). - let workspace_names = get_workspace_names(sway, FocusedWorkspacePosition::Front)?; - - // Fuzzy select a workspace. - let workspace_name = crate::helpers::run_fzf("workspace to move to", workspace_names)?; - - move_container_to_workspace(sway, &container_id, &workspace_name) - } -} From 875a9e2906b09c74b332cccc4fe929bd4dd1f8a4 Mon Sep 17 00:00:00 2001 From: Dan Nixon Date: Wed, 28 Aug 2024 18:08:18 +0100 Subject: [PATCH 3/3] sway-helper: ignore sway-helper Alacritty windows --- overlays/pkgs/sway-helper/src/helpers/sway.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/overlays/pkgs/sway-helper/src/helpers/sway.rs b/overlays/pkgs/sway-helper/src/helpers/sway.rs index 6c3d0f5..cf3d09d 100644 --- a/overlays/pkgs/sway-helper/src/helpers/sway.rs +++ b/overlays/pkgs/sway-helper/src/helpers/sway.rs @@ -55,6 +55,7 @@ pub(crate) fn get_containers_fzf_strings<'a>( if (node.node_type == NodeType::Con || node.node_type == NodeType::FloatingCon) && node.nodes.is_empty() + && node.app_id != Some("sway-helper".to_string()) { let mut s = String::new();