Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More sway-helper functions #52

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions overlays/pkgs/sway-helper/src/commands/combi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ impl CliRun for Combi {
"new-named-workspace",
"rename-workspace",
"switch-to-workspace",
"move-window-here",
"move-window-to-workspace",
]
.iter()
.map(|i| format!("[{}] {i}", CMD.yellow())),
Expand Down
6 changes: 6 additions & 0 deletions overlays/pkgs/sway-helper/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod combi;
mod focus_window;
mod move_window;
mod new_workspace;
mod rename_workspace;
mod run;
Expand All @@ -8,6 +9,7 @@ mod switch_to_workspace;
use self::{
combi::Combi,
focus_window::FocusWindow,
move_window::{MoveWindowHere, MoveWindowToWorkspace},
new_workspace::{NewNamedWorkspace, NewWorkspace},
rename_workspace::RenameWorkspace,
run::Run,
Expand All @@ -28,6 +30,8 @@ pub(crate) enum Command {
RenameWorkspace,
SwitchToWorkspace,
FocusWindow,
MoveWindowHere,
MoveWindowToWorkspace,
}

impl CliRun for Command {
Expand All @@ -40,6 +44,8 @@ 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),
}
}
}
56 changes: 56 additions & 0 deletions overlays/pkgs/sway-helper/src/commands/move_window.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
12 changes: 12 additions & 0 deletions overlays/pkgs/sway-helper/src/helpers/sway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -106,3 +107,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(())
}