New subcommand quit-workspace-or-window

timeout_old
Tassilo Horn 4 years ago
parent 1ce548ce3d
commit a645d067b0
  1. 7
      README.md
  2. 13
      src/bin/swayr.rs
  3. 50
      src/client.rs
  4. 19
      src/con.rs

@ -9,9 +9,12 @@ Right now, there are these subcommands:
focused last and focuses the selected. focused last and focuses the selected.
* `quit-window` displays all windows and quits the selected one. * `quit-window` displays all windows and quits the selected one.
* `switch-workspace` displays all workspaces in LRU order and switches to the * `switch-workspace` displays all workspaces in LRU order and switches to the
selected. selected one.
* `switch-workspace-or-window` displays all workspaces and their windows and * `switch-workspace-or-window` displays all workspaces and their windows and
allows to switch to either a workspace or window. switches to the selected workspace or window.
* `quit-workspace-or-window` displays all workspaces and their windows and
allows to quit either the selected workspace (all its windows) or the
selected window.
* `execute-swaymsg-command` displays most swaymsg which don't require * `execute-swaymsg-command` displays most swaymsg which don't require
additional input and executes the selected one. That's handy especially for additional input and executes the selected one. That's handy especially for
less often used commands not bound to a key. less often used commands not bound to a key.

@ -18,14 +18,16 @@ struct Opts {
#[derive(Clap)] #[derive(Clap)]
enum SwayrCommand { enum SwayrCommand {
/// Switch window with display order urgent first, then LRU order, focused last /// Focus the selected window
SwitchWindow, SwitchWindow,
/// Quit a window with display order focused first, then reverse-LRU order, urgent last /// Quit the selected window
QuitWindow, QuitWindow,
/// Switch workspace with LRU display order /// Switch to the selected workspace
SwitchWorkspace, SwitchWorkspace,
/// Switch workspace or window with LRU display order /// Switch to the selected workspace or focus the selected window
SwitchWorkspaceOrWindow, SwitchWorkspaceOrWindow,
/// Quit all windows of selected workspace or the selected window
QuitWorkspaceOrWindow,
/// Select and execute a swaymsg command /// Select and execute a swaymsg command
ExecuteSwaymsgCommand, ExecuteSwaymsgCommand,
} }
@ -39,6 +41,9 @@ fn main() {
SwayrCommand::SwitchWorkspaceOrWindow => { SwayrCommand::SwitchWorkspaceOrWindow => {
client::switch_workspace_or_window() client::switch_workspace_or_window()
} }
SwayrCommand::QuitWorkspaceOrWindow => {
client::quit_workspace_or_window()
}
SwayrCommand::ExecuteSwaymsgCommand => client::exec_swaymsg_command(), SwayrCommand::ExecuteSwaymsgCommand => client::exec_swaymsg_command(),
} }
} }

@ -1,17 +1,22 @@
use crate::con; use crate::con;
use crate::ipc;
use crate::util; use crate::util;
use std::fmt; use std::fmt;
fn focus_window_by_id(id: &ipc::Id) {
util::swaymsg(&[format!("[con_id={}]", id).as_str(), "focus"]);
}
fn quit_window_by_id(id: &ipc::Id) {
util::swaymsg(&[format!("[con_id={}]", id).as_str(), "kill"]);
}
pub fn switch_window() { pub fn switch_window() {
let root = con::get_tree(); let root = con::get_tree();
let mut windows = con::get_windows(&root); let windows = con::get_windows(&root);
windows.sort();
if let Some(window) = con::select_window("Switch to window", &windows) { if let Some(window) = con::select_window("Switch to window", &windows) {
util::swaymsg(&[ focus_window_by_id(&window.get_id())
format!("[con_id={}]", window.get_id()).as_str(),
"focus",
]);
} }
} }
@ -38,26 +43,35 @@ pub fn switch_workspace_or_window() {
con::WsOrWin::Ws { ws } => { con::WsOrWin::Ws { ws } => {
util::swaymsg(&["workspace", "number", ws.get_name()]); util::swaymsg(&["workspace", "number", ws.get_name()]);
} }
con::WsOrWin::Win { win } => { con::WsOrWin::Win { win } => focus_window_by_id(&win.get_id()),
util::swaymsg(&[
format!("[con_id={}]", win.get_id()).as_str(),
"focus",
]);
}
} }
} }
} }
pub fn quit_window() { pub fn quit_window() {
let root = con::get_tree(); let root = con::get_tree();
let mut windows = con::get_windows(&root); let windows = con::get_windows(&root);
windows.sort_by(|a, b| a.cmp(b).reverse());
if let Some(window) = con::select_window("Quit window", &windows) { if let Some(window) = con::select_window("Quit window", &windows) {
util::swaymsg(&[ quit_window_by_id(window.get_id())
format!("[con_id={}]", window.get_id()).as_str(), }
"kill", }
]);
pub fn quit_workspace_or_window() {
let root = con::get_tree();
let workspaces = con::get_workspaces(&root, false);
let ws_or_wins = con::WsOrWin::from_workspaces(&workspaces);
if let Some(ws_or_win) =
con::select_workspace_or_window("Quit workspace or window", &ws_or_wins)
{
match ws_or_win {
con::WsOrWin::Ws { ws } => {
for win in &ws.windows {
quit_window_by_id(win.get_id())
}
}
con::WsOrWin::Win { win } => quit_window_by_id(win.get_id()),
}
} }
} }

@ -134,6 +134,7 @@ fn build_windows(
}) })
} }
} }
v.sort();
v v
} }
@ -159,6 +160,7 @@ fn build_workspaces(
windows: wins, windows: wins,
}) })
} }
v.sort();
v v
} }
@ -206,22 +208,7 @@ pub fn get_workspaces(
.filter(|ws| !ws.is_scratchpad()) .filter(|ws| !ws.is_scratchpad())
.collect() .collect()
}; };
workspaces.sort();
println!(
"Sorted WS: {:?}",
workspaces
.iter()
.map(Workspace::get_name)
.collect::<Vec<&str>>()
);
workspaces.rotate_left(1); workspaces.rotate_left(1);
println!(
"Rotated WS: {:?}",
workspaces
.iter()
.map(Workspace::get_name)
.collect::<Vec<&str>>()
);
workspaces workspaces
} }
@ -293,7 +280,7 @@ pub fn select_workspace_or_window<'a>(
pub struct Workspace<'a> { pub struct Workspace<'a> {
node: &'a ipc::Node, node: &'a ipc::Node,
con_props: Option<ipc::ConProps>, con_props: Option<ipc::ConProps>,
windows: Vec<Window<'a>>, pub windows: Vec<Window<'a>>,
} }
impl Workspace<'_> { impl Workspace<'_> {

Loading…
Cancel
Save