diff --git a/overlays/pkgs/sway-helper/src/commands/rename_workspace.rs b/overlays/pkgs/sway-helper/src/commands/rename_workspace.rs index 263e70c..f248dce 100644 --- a/overlays/pkgs/sway-helper/src/commands/rename_workspace.rs +++ b/overlays/pkgs/sway-helper/src/commands/rename_workspace.rs @@ -1,5 +1,5 @@ use super::CliRun; -use crate::helpers::FocusedWorkspacePosition; +use crate::helpers::sway::{get_workspace_names, FocusedWorkspacePosition}; pub(crate) struct RenameWorkspace {} @@ -7,8 +7,7 @@ impl CliRun for RenameWorkspace { fn run(&self, sway: &mut swayipc::Connection) { // Get a list of all the workspace names, with the focused workspace at the front (allowing // fastest access to editing). - let workspace_names = - crate::helpers::get_workspace_names(sway, FocusedWorkspacePosition::Front); + let workspace_names = get_workspace_names(sway, FocusedWorkspacePosition::Front); // Fuzzy select a workspace. let selected = crate::helpers::run_fzf("workspace to rename", workspace_names); diff --git a/overlays/pkgs/sway-helper/src/commands/switch_to_workspace.rs b/overlays/pkgs/sway-helper/src/commands/switch_to_workspace.rs index 0727fce..1664d9d 100644 --- a/overlays/pkgs/sway-helper/src/commands/switch_to_workspace.rs +++ b/overlays/pkgs/sway-helper/src/commands/switch_to_workspace.rs @@ -1,5 +1,5 @@ use super::CliRun; -use crate::helpers::FocusedWorkspacePosition; +use crate::helpers::sway::{get_workspace_names, FocusedWorkspacePosition}; pub(crate) struct SwitchToWorkspace {} @@ -10,7 +10,7 @@ impl CliRun for SwitchToWorkspace { let mut workspace_names = vec!["back_and_forth".to_string()]; // Add the names of all the workspaces, with the name of the current workspace at the back. - workspace_names.append(&mut crate::helpers::get_workspace_names( + workspace_names.append(&mut get_workspace_names( sway, FocusedWorkspacePosition::Back, )); diff --git a/overlays/pkgs/sway-helper/src/helpers/mod.rs b/overlays/pkgs/sway-helper/src/helpers/mod.rs index 72529c7..b73dfc1 100644 --- a/overlays/pkgs/sway-helper/src/helpers/mod.rs +++ b/overlays/pkgs/sway-helper/src/helpers/mod.rs @@ -1,5 +1,7 @@ +pub(crate) mod programs; +pub(crate) mod sway; + use fzf_wrapped::FzfBuilder; -use std::cmp::Ordering; pub(crate) fn run_fzf>>(prompt: &str, items: T) -> String { let mut fzf = FzfBuilder::default() @@ -16,40 +18,3 @@ pub(crate) fn run_fzf>>(prompt: &str, i fzf.output().unwrap() } - -pub(crate) enum FocusedWorkspacePosition { - Front, - // Sorted, - Back, -} - -pub(crate) fn get_workspace_names( - sway: &mut swayipc::Connection, - focused: FocusedWorkspacePosition, -) -> Vec { - let mut workspaces = sway.get_workspaces().unwrap(); - - workspaces.sort_by(|a, b| match focused { - FocusedWorkspacePosition::Front => { - if a.focused == b.focused { - Ordering::Equal - } else if a.focused { - Ordering::Less - } else { - Ordering::Greater - } - } - // FocusedWorkspacePosition::Sorted => Ordering::Equal, - FocusedWorkspacePosition::Back => { - if a.focused == b.focused { - Ordering::Equal - } else if a.focused { - Ordering::Greater - } else { - Ordering::Less - } - } - }); - - workspaces.into_iter().map(|i| i.name).collect() -} diff --git a/overlays/pkgs/sway-helper/src/helpers/programs.rs b/overlays/pkgs/sway-helper/src/helpers/programs.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/overlays/pkgs/sway-helper/src/helpers/programs.rs @@ -0,0 +1 @@ + diff --git a/overlays/pkgs/sway-helper/src/helpers/sway.rs b/overlays/pkgs/sway-helper/src/helpers/sway.rs new file mode 100644 index 0000000..2125e6d --- /dev/null +++ b/overlays/pkgs/sway-helper/src/helpers/sway.rs @@ -0,0 +1,38 @@ +use std::cmp::Ordering; + +pub(crate) enum FocusedWorkspacePosition { + Front, + // Sorted, + Back, +} + +pub(crate) fn get_workspace_names( + sway: &mut swayipc::Connection, + focused: FocusedWorkspacePosition, +) -> Vec { + let mut workspaces = sway.get_workspaces().unwrap(); + + workspaces.sort_by(|a, b| match focused { + FocusedWorkspacePosition::Front => { + if a.focused == b.focused { + Ordering::Equal + } else if a.focused { + Ordering::Less + } else { + Ordering::Greater + } + } + // FocusedWorkspacePosition::Sorted => Ordering::Equal, + FocusedWorkspacePosition::Back => { + if a.focused == b.focused { + Ordering::Equal + } else if a.focused { + Ordering::Greater + } else { + Ordering::Less + } + } + }); + + workspaces.into_iter().map(|i| i.name).collect() +}