Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed Aug 19, 2024
1 parent dc0730d commit ee1d689
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
5 changes: 2 additions & 3 deletions overlays/pkgs/sway-helper/src/commands/rename_workspace.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use super::CliRun;
use crate::helpers::FocusedWorkspacePosition;
use crate::helpers::sway::{get_workspace_names, FocusedWorkspacePosition};

pub(crate) struct RenameWorkspace {}

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);
Expand Down
4 changes: 2 additions & 2 deletions overlays/pkgs/sway-helper/src/commands/switch_to_workspace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::CliRun;
use crate::helpers::FocusedWorkspacePosition;
use crate::helpers::sway::{get_workspace_names, FocusedWorkspacePosition};

pub(crate) struct SwitchToWorkspace {}

Expand All @@ -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,
));
Expand Down
41 changes: 3 additions & 38 deletions overlays/pkgs/sway-helper/src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -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<T: IntoIterator<Item = impl Into<String>>>(prompt: &str, items: T) -> String {
let mut fzf = FzfBuilder::default()
Expand All @@ -16,40 +18,3 @@ pub(crate) fn run_fzf<T: IntoIterator<Item = impl Into<String>>>(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<String> {
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()
}
1 change: 1 addition & 0 deletions overlays/pkgs/sway-helper/src/helpers/programs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

38 changes: 38 additions & 0 deletions overlays/pkgs/sway-helper/src/helpers/sway.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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()
}

0 comments on commit ee1d689

Please sign in to comment.