Don't show empty workspaces; a bit refactoring

timeout_old
Tassilo Horn 3 years ago
parent 673619a52c
commit 541de88770
  1. 24
      src/cmds.rs
  2. 42
      src/con.rs
  3. 5
      src/util.rs

@ -16,17 +16,14 @@
//! Functions and data structures of the swayr client. //! Functions and data structures of the swayr client.
use crate::con; use crate::con;
use crate::con::DisplayFormat;
use crate::config as cfg; use crate::config as cfg;
use crate::ipc; use crate::ipc;
use crate::ipc::SwayrCommand; use crate::ipc::SwayrCommand;
use crate::util; use crate::util;
use crate::util::DisplayFormat;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt;
use std::sync::Arc; use std::sync::Arc;
use std::sync::RwLock; use std::sync::RwLock;
use swayipc as s; use swayipc as s;
pub struct ExecSwayrCmdArgs<'a> { pub struct ExecSwayrCmdArgs<'a> {
@ -34,16 +31,10 @@ pub struct ExecSwayrCmdArgs<'a> {
pub extra_props: Arc<RwLock<HashMap<i64, ipc::ExtraProps>>>, pub extra_props: Arc<RwLock<HashMap<i64, ipc::ExtraProps>>>,
} }
impl fmt::Display for SwayrCommand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{:?}", self)
}
}
impl DisplayFormat for SwayrCommand { impl DisplayFormat for SwayrCommand {
fn format_for_display(&self, _: &cfg::Config) -> std::string::String { fn format_for_display(&self, _: &cfg::Config) -> std::string::String {
// TODO: Add a format to Config // 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()) .find(|w| w.is_urgent())
.or_else(|| windows.get(0)) .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()) focus_window_by_id(win.get_id())
} else { } else {
println!("No window to switch to.") println!("No window to switch to.")
@ -342,16 +333,9 @@ struct SwaymsgCmd<'a> {
cmd: Vec<&'a str>, 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<'_> { impl DisplayFormat for SwaymsgCmd<'_> {
fn format_for_display(&self, _: &cfg::Config) -> std::string::String { fn format_for_display(&self, _: &cfg::Config) -> std::string::String {
// TODO: Add a format to Config format!("{}", self.cmd.join(" "))
format!("{}", self)
} }
} }

@ -19,16 +19,12 @@ use crate::config as cfg;
use crate::ipc; use crate::ipc;
use crate::ipc::NodeMethods; use crate::ipc::NodeMethods;
use crate::util; use crate::util;
use crate::util::DisplayFormat;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::cmp; use std::cmp;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt;
use swayipc as s; use swayipc as s;
pub trait DisplayFormat {
fn format_for_display(&self, config: &cfg::Config) -> String;
}
#[derive(Debug)] #[derive(Debug)]
pub struct Window<'a> { pub struct Window<'a> {
node: &'a s::Node, 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! { lazy_static! {
static ref APP_NAME_AND_VERSION_RX: regex::Regex = static ref APP_NAME_AND_VERSION_RX: regex::Regex =
regex::Regex::new("(.+)(-[0-9.]+)").unwrap(); regex::Regex::new("(.+)(-[0-9.]+)").unwrap();
@ -227,7 +209,9 @@ fn build_workspaces<'a>(
) -> Vec<Workspace<'a>> { ) -> Vec<Workspace<'a>> {
let mut v = vec![]; let mut v = vec![];
for workspace in root.workspaces() { for workspace in root.workspaces() {
if !include_scratchpad && workspace.is_scratchpad() { if workspace.windows().is_empty()
|| workspace.is_scratchpad() && !include_scratchpad
{
continue; continue;
} }
@ -297,18 +281,6 @@ pub enum WsOrWin<'a> {
Win { win: &'a Window<'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<'_> { impl DisplayFormat for WsOrWin<'_> {
fn format_for_display(&self, cfg: &cfg::Config) -> String { fn format_for_display(&self, cfg: &cfg::Config) -> String {
match self { 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> { impl<'a> DisplayFormat for Workspace<'a> {
fn format_for_display(&self, cfg: &cfg::Config) -> String { fn format_for_display(&self, cfg: &cfg::Config) -> String {
let default_format = cfg::Format::default(); let default_format = cfg::Format::default();

@ -15,7 +15,6 @@
//! Utility functions including selection between choices using a menu program. //! Utility functions including selection between choices using a menu program.
use crate::con::DisplayFormat;
use crate::config as cfg; use crate::config as cfg;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::collections::HashMap; 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>( pub fn select_from_menu<'a, 'b, TS>(
prompt: &'a str, prompt: &'a str,
choices: &'b [TS], choices: &'b [TS],

Loading…
Cancel
Save