From 8530b78d971ac76918545e31dd580c4ce95aaf85 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Fri, 22 Jan 2021 22:28:25 +0100 Subject: [PATCH] Reorganize a bit --- src/client.rs | 12 ++++++------ src/{window.rs => con.rs} | 25 ++++++++++++++++--------- src/lib.rs | 2 +- src/util.rs | 10 +--------- 4 files changed, 24 insertions(+), 25 deletions(-) rename src/{window.rs => con.rs} (84%) diff --git a/src/client.rs b/src/client.rs index 0279053..5101087 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,13 +1,13 @@ +use crate::con; use crate::ipc; use crate::util; -use crate::window; pub fn switch_window() { let root_node = get_tree(); - let mut windows = window::get_windows(&root_node); + let mut windows = con::get_windows(&root_node); windows.sort(); - if let Some(window) = util::select_window("Switch to window", &windows) { + if let Some(window) = con::select_window("Switch to window", &windows) { util::swaymsg(vec![ format!("[con_id={}]", window.get_id()).as_str(), "focus", @@ -17,10 +17,10 @@ pub fn switch_window() { pub fn quit_window() { let root_node = get_tree(); - let mut windows = window::get_windows(&root_node); + let mut windows = con::get_windows(&root_node); windows.sort_by(|a, b| a.cmp(b).reverse()); - if let Some(window) = util::select_window("Quit window", &windows) { + if let Some(window) = con::select_window("Quit window", &windows) { util::swaymsg(vec![ format!("[con_id={}]", window.get_id()).as_str(), "kill", @@ -54,7 +54,7 @@ fn test_get_tree() { #[test] fn test_get_windows() { let tree = get_tree(); - let cons = window::get_windows(&tree); + let cons = con::get_windows(&tree); println!("There are {} cons.", cons.len()); diff --git a/src/window.rs b/src/con.rs similarity index 84% rename from src/window.rs rename to src/con.rs index 8a4a28a..fb340dd 100644 --- a/src/window.rs +++ b/src/con.rs @@ -49,14 +49,14 @@ 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 { + } else if self.node.urgent && !other.node.urgent + || !self.node.focused && other.node.focused + { cmp::Ordering::Less - } else if !self.node.urgent && other.node.urgent { - std::cmp::Ordering::Greater - } else if self.node.focused && !other.node.focused { + } else if !self.node.urgent && other.node.urgent + || self.node.focused && !other.node.focused + { std::cmp::Ordering::Greater - } else if !self.node.focused && other.node.focused { - std::cmp::Ordering::Less } else { let lru_a = self.win_props.as_ref().map_or(0, |wp| wp.last_focus_time); @@ -73,8 +73,8 @@ impl PartialOrd for Window<'_> { } } -impl<'a> std::fmt::Display for Window<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { +impl<'a> fmt::Display for Window<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!( f, "“{}” \ @@ -130,5 +130,12 @@ pub fn get_windows(root_node: &ipc::Node) -> Vec { } }; - build_windows(&root_node, win_props.unwrap_or(HashMap::new())) + build_windows(&root_node, win_props.unwrap_or_default()) +} + +pub fn select_window<'a>( + prompt: &'a str, + windows: &'a [Window], +) -> Option<&'a Window<'a>> { + util::wofi_select(prompt, windows) } diff --git a/src/lib.rs b/src/lib.rs index 26548ed..00a6caf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ pub mod client; +pub mod con; pub mod demon; pub mod ipc; pub mod util; -pub mod window; diff --git a/src/util.rs b/src/util.rs index 1c574d1..c01f6a5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,3 @@ -use crate::window; use std::collections::HashMap; use std::io::Write; use std::process as proc; @@ -28,16 +27,9 @@ pub fn swaymsg(args: Vec<&str>) -> String { String::from_utf8(output.stdout).unwrap() } -pub fn select_window<'a>( - prompt: &'a str, - windows: &'a Vec, -) -> Option<&'a window::Window<'a>> { - wofi_select(prompt, windows) -} - pub fn wofi_select<'a, 'b, TS>( prompt: &'a str, - choices: &'b Vec, + choices: &'b [TS], ) -> Option<&'b TS> where TS: std::fmt::Display + Sized,