From 541de8877002a00bced22140b8806fe102066352 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sun, 4 Jul 2021 21:08:21 +0200 Subject: [PATCH] Don't show empty workspaces; a bit refactoring --- src/cmds.rs | 24 ++++-------------------- src/con.rs | 42 ++++-------------------------------------- src/util.rs | 5 ++++- 3 files changed, 12 insertions(+), 59 deletions(-) diff --git a/src/cmds.rs b/src/cmds.rs index b1da812..cd5beac 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -16,17 +16,14 @@ //! Functions and data structures of the swayr client. use crate::con; -use crate::con::DisplayFormat; use crate::config as cfg; use crate::ipc; use crate::ipc::SwayrCommand; use crate::util; - +use crate::util::DisplayFormat; use std::collections::HashMap; -use std::fmt; use std::sync::Arc; use std::sync::RwLock; - use swayipc as s; pub struct ExecSwayrCmdArgs<'a> { @@ -34,16 +31,10 @@ pub struct ExecSwayrCmdArgs<'a> { pub extra_props: Arc>>, } -impl fmt::Display for SwayrCommand { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - write!(f, "{:?}", self) - } -} - impl DisplayFormat for SwayrCommand { fn format_for_display(&self, _: &cfg::Config) -> std::string::String { // TODO: Add a format to Config - format!("{}", self) + format!("{:?}", self) } } @@ -124,7 +115,7 @@ pub fn switch_to_urgent_or_lru_window( .find(|w| w.is_urgent()) .or_else(|| windows.get(0)) { - println!("Switching to {}", win); + println!("Switching to {}, id: {}", win.get_app_name(), win.get_id()); focus_window_by_id(win.get_id()) } else { println!("No window to switch to.") @@ -342,16 +333,9 @@ struct SwaymsgCmd<'a> { cmd: Vec<&'a str>, } -impl<'a> fmt::Display for SwaymsgCmd<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - write!(f, "{}", self.cmd.join(" ")) - } -} - impl DisplayFormat for SwaymsgCmd<'_> { fn format_for_display(&self, _: &cfg::Config) -> std::string::String { - // TODO: Add a format to Config - format!("{}", self) + format!("{}", self.cmd.join(" ")) } } diff --git a/src/con.rs b/src/con.rs index a25666e..6f1885a 100644 --- a/src/con.rs +++ b/src/con.rs @@ -19,16 +19,12 @@ use crate::config as cfg; use crate::ipc; use crate::ipc::NodeMethods; use crate::util; +use crate::util::DisplayFormat; use lazy_static::lazy_static; use std::cmp; use std::collections::HashMap; -use std::fmt; use swayipc as s; -pub trait DisplayFormat { - fn format_for_display(&self, config: &cfg::Config) -> String; -} - #[derive(Debug)] pub struct Window<'a> { node: &'a s::Node, @@ -107,20 +103,6 @@ impl PartialOrd for Window<'_> { } } -impl<'a> fmt::Display for Window<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - write!( - f, - "\"{}\" — {} on workspace {} (id: {}, urgent: {})", - self.get_title(), - self.get_app_name(), - self.workspace.name.as_ref().unwrap(), - self.get_id(), - self.node.urgent - ) - } -} - lazy_static! { static ref APP_NAME_AND_VERSION_RX: regex::Regex = regex::Regex::new("(.+)(-[0-9.]+)").unwrap(); @@ -227,7 +209,9 @@ fn build_workspaces<'a>( ) -> Vec> { let mut v = vec![]; for workspace in root.workspaces() { - if !include_scratchpad && workspace.is_scratchpad() { + if workspace.windows().is_empty() + || workspace.is_scratchpad() && !include_scratchpad + { continue; } @@ -297,18 +281,6 @@ pub enum WsOrWin<'a> { Win { win: &'a Window<'a> }, } -impl<'a> fmt::Display for WsOrWin<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - match self { - WsOrWin::Ws { ws } => ws.fmt(f), - WsOrWin::Win { win } => match f.write_str("\t") { - Ok(()) => win.fmt(f), - Err(e) => Err(e), - }, - } - } -} - impl DisplayFormat for WsOrWin<'_> { fn format_for_display(&self, cfg: &cfg::Config) -> String { match self { @@ -390,12 +362,6 @@ impl PartialOrd for Workspace<'_> { } } -impl<'a> fmt::Display for Workspace<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - write!(f, "“Workspace {}” (id: {})", self.get_name(), self.get_id()) - } -} - impl<'a> DisplayFormat for Workspace<'a> { fn format_for_display(&self, cfg: &cfg::Config) -> String { let default_format = cfg::Format::default(); diff --git a/src/util.rs b/src/util.rs index e69d08a..0e21cb6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -15,7 +15,6 @@ //! Utility functions including selection between choices using a menu program. -use crate::con::DisplayFormat; use crate::config as cfg; use lazy_static::lazy_static; use std::collections::HashMap; @@ -175,6 +174,10 @@ fn test_desktop_entries() { } } +pub trait DisplayFormat { + fn format_for_display(&self, config: &cfg::Config) -> String; +} + pub fn select_from_menu<'a, 'b, TS>( prompt: &'a str, choices: &'b [TS],