From 0f1989acd726c622022f02af6ad157acc15a8a65 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Thu, 11 Feb 2021 21:20:56 +0100 Subject: [PATCH] New switch-to-urgent-or-lru-window command; add module docs --- Cargo.toml | 2 +- README.md | 15 ++++++++++----- src/bin/swayr.rs | 2 ++ src/bin/swayrd.rs | 2 ++ src/client.rs | 23 +++++++++++++++++++++++ src/con.rs | 18 ++++++++++++++---- src/demon.rs | 2 ++ src/ipc.rs | 2 ++ src/lib.rs | 10 ++++++++++ src/util.rs | 2 ++ 10 files changed, 68 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 029109e..24b171b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "swayr" version = "0.0.9" -description = "A wofi-based LRU window-switcher for sway written in Rust" +description = "A wofi-based LRU window-switcher (and more) for the sway window manager" homepage = "https://sr.ht/~tsdh/swayr/" repository = "https://git.sr.ht/~tsdh/swayr" authors = ["Tassilo Horn "] diff --git a/README.md b/README.md index 1151d15..d82c0e8 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,15 @@ -# Swayr is a window switcher for sway +# Swayr is a window switcher (and more) for sway -Swayr consists of a demon, and a client. The demon `swayrd` records window -creations, deletions, and focus changes using sway's JSON IPC interface. The -client `swayr` offers subcommands, see `swayr --help`. +Swayr consists of a demon, and a client. The demon `swayrd` records +window/workspace creations, deletions, and focus changes using sway's JSON IPC +interface. The client `swayr` offers subcommands, see `swayr --help`. Right now, there are these subcommands: * `switch-window` displays all windows in the order urgent first, then LRU, focused last and focuses the selected. * `quit-window` displays all windows and quits the selected one. +* `switch-to-urgent-or-lru-window` switches to the next window with urgency + hint (if any) or to the last recently used window. * `switch-workspace` displays all workspaces in LRU order and switches to the selected one. * `switch-workspace-or-window` displays all workspaces and their windows and @@ -43,7 +45,10 @@ so: ``` bindsym $mod+Delete exec env RUST_BACKTRACE=1 swayr quit-window > /tmp/swayr.log 2>&1 bindsym $mod+Space exec env RUST_BACKTRACE=1 swayr switch-window >> /tmp/swayr.log 2>&1 -bindsym $mod+Shift+Space exec env RUST_BACKTRACE=1 swayr switch-workspace-or-window >> /tmp/swayr.log 2>&1 +bindsym $mod+Tab exec env RUST_BACKTRACE=1 \ + swayr switch-to-urgent-or-lru-window >> /tmp/swayr.log 2>&1 +bindsym $mod+Shift+Space exec env RUST_BACKTRACE=1 \ + swayr switch-workspace-or-window >> /tmp/swayr.log 2>&1 bindsym $mod+c exec env RUST_BACKTRACE=1 swayr execute-swaymsg-command >> /tmp/swayr.log 2>&1 bindsym $mod+Shift+c exec env RUST_BACKTRACE=1 swayr execute-swayr-command >> /tmp/swa ``` diff --git a/src/bin/swayr.rs b/src/bin/swayr.rs index 329cb52..c98fa85 100644 --- a/src/bin/swayr.rs +++ b/src/bin/swayr.rs @@ -1,3 +1,5 @@ +//! The `swayr` binary. + #[macro_use] extern crate clap; use clap::Clap; diff --git a/src/bin/swayrd.rs b/src/bin/swayrd.rs index 259c1bf..5c2c616 100644 --- a/src/bin/swayrd.rs +++ b/src/bin/swayrd.rs @@ -1,3 +1,5 @@ +//! The `swayrd` binary. + extern crate serde; extern crate serde_json; diff --git a/src/client.rs b/src/client.rs index 8d67792..662617a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,3 +1,5 @@ +//! Functions and data structures of the swayr client. + use crate::con; use crate::ipc; use crate::util; @@ -6,6 +8,8 @@ use std::fmt; #[derive(Clap, Debug)] pub enum SwayrCommand { + /// Switch to next urgent window (if any) or to last recently used window. + SwitchToUrgentOrLRUWindow, /// Focus the selected window SwitchWindow, /// Quit the selected window @@ -30,6 +34,9 @@ impl fmt::Display for SwayrCommand { pub fn exec_swayr_cmd(cmd: &SwayrCommand) { match cmd { + SwayrCommand::SwitchToUrgentOrLRUWindow => { + switch_to_urgent_or_lru_window() + } SwayrCommand::SwitchWindow => switch_window(), SwayrCommand::QuitWindow => quit_window(), SwayrCommand::SwitchWorkspace => switch_workspace(), @@ -62,6 +69,22 @@ fn quit_window_by_id(id: ipc::Id) { util::swaymsg(&[format!("[con_id={}]", id).as_str(), "kill"]); } +pub fn switch_to_urgent_or_lru_window() { + let root = con::get_tree(); + let windows = con::get_windows(&root); + if let Some(win) = windows + .iter() + .filter(|w| w.is_urgent()) + .next() + .or_else(|| windows.iter().next()) + { + println!("Switching to {}", win); + focus_window_by_id(win.get_id()) + } else { + println!("No window to switch to.") + } +} + pub fn switch_window() { let root = con::get_tree(); let windows = con::get_windows(&root); diff --git a/src/con.rs b/src/con.rs index aac3efc..3bd44c4 100644 --- a/src/con.rs +++ b/src/con.rs @@ -1,3 +1,5 @@ +//! Convenience data structures built from the IPC structs. + use crate::ipc; use crate::util; use std::cmp; @@ -61,6 +63,14 @@ impl Window<'_> { pub fn get_title(&self) -> &str { self.node.name.as_ref().unwrap() } + + pub fn is_urgent(&self) -> bool { + self.node.urgent + } + + pub fn is_focused(&self) -> bool { + self.node.focused + } } impl PartialEq for Window<'_> { @@ -75,12 +85,12 @@ impl Ord for Window<'_> { fn cmp(&self, other: &Self) -> cmp::Ordering { if self == other { cmp::Ordering::Equal - } else if self.node.urgent && !other.node.urgent - || !self.node.focused && other.node.focused + } else if self.is_urgent() && !other.is_urgent() + || !self.is_focused() && other.is_focused() { cmp::Ordering::Less - } else if !self.node.urgent && other.node.urgent - || self.node.focused && !other.node.focused + } else if !self.is_urgent() && other.is_urgent() + || self.is_focused() && !other.is_focused() { std::cmp::Ordering::Greater } else { diff --git a/src/demon.rs b/src/demon.rs index 0e1eb68..caaa3c1 100644 --- a/src/demon.rs +++ b/src/demon.rs @@ -1,3 +1,5 @@ +//! Functions and data structures of the swayrd demon. + use crate::ipc; use crate::util; use serde_json::Deserializer; diff --git a/src/ipc.rs b/src/ipc.rs index ae8c547..27f0b0c 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -1,3 +1,5 @@ +//! Functions and data structures concerned with sway JSON IPC. + extern crate serde; extern crate serde_json; extern crate users; diff --git a/src/lib.rs b/src/lib.rs index 00a6caf..3dc667a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,13 @@ +// TODO: Possibly just include README.md when this feature is in the release +// channel. +// +// #![doc(include = "../README.md")] + +//! **Swayr** is a wofi-based LRU window-switcher and more for the sway window +//! manager. It consists of a demon, and a client. The demon `swayrd` records +//! window/workspace creations, deletions, and focus changes using sway's JSON +//! IPC interface. The client `swayr` offers subcommands, see `swayr --help`. + pub mod client; pub mod con; pub mod demon; diff --git a/src/util.rs b/src/util.rs index 547364e..ba90366 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,3 +1,5 @@ +//! Utility functions including wofi-selection. + use std::collections::HashMap; use std::io::Write; use std::process as proc;