From 38e5d317f8624eb3f2ea945b6fe605d2fc0d9715 Mon Sep 17 00:00:00 2001 From: Penelope Gwen Date: Wed, 24 Sep 2025 20:08:10 -0700 Subject: [PATCH] clean up sway and better title length handling --- src/config.rs | 2 +- src/lib/launch.rs | 2 +- src/lib/profile.rs | 14 +++++++------- src/lib/sway.rs | 18 ++++++------------ src/lib/windows.rs | 38 ++++++++++++++++++++++++++++++-------- src/main.rs | 20 ++++++++++---------- 6 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/config.rs b/src/config.rs index f4c3e2b..cd6cd01 100644 --- a/src/config.rs +++ b/src/config.rs @@ -26,7 +26,7 @@ pub struct LockConf { } #[derive(Serialize,Deserialize,Clone,Default)] pub struct Config { - pub title_length: usize, + pub title_length: Option, pub window_icons: Vec, pub programs: HashMap, pub lock: LockConf, diff --git a/src/lib/launch.rs b/src/lib/launch.rs index 521ea19..8614fad 100644 --- a/src/lib/launch.rs +++ b/src/lib/launch.rs @@ -26,7 +26,7 @@ pub fn profile_launch(profiles: Vec, programs_config: HashMap { - Err(ErrorMessage { message: Some(String::from("No matching program found")), code: Some(3) }) + Err(ErrorMessage { message: String::from("No matching program found"), code: 3 }) }, } } \ No newline at end of file diff --git a/src/lib/profile.rs b/src/lib/profile.rs index 1e05e8c..1f7b012 100644 --- a/src/lib/profile.rs +++ b/src/lib/profile.rs @@ -29,14 +29,14 @@ pub fn initialize(mut sway_connection: Connection, profile: Profile,profile_inde pub fn profile_from_index(config: Config, index: usize) -> Result{ match config.profiles.get(index) { Some(p) => Ok(p.clone()), - None => Err(ErrorMessage { message: Some("Profile not found for index".to_string()), code: Some(3) }), + None => Err(ErrorMessage { message: "Profile not found for index".to_string(), code: 3 }), } } pub fn _profile_from_name(config: Config, name: String) -> Result { match config.profiles.iter().find(|x|x.name == name) { Some(p) => Ok(p.clone()), - None => Err(ErrorMessage { message: Some(format!("Profile not found with name {}",name)), code: Some(3) }), + None => Err(ErrorMessage { message: format!("Profile not found with name {}",name), code: 3 }), } } @@ -84,13 +84,13 @@ pub fn active_profile_index() -> Result { Ok(u) => { Ok(u) }, - Err(_) => Err(ErrorMessage { message: Some("could not parse json from active profile cache file".to_string()), code: Some(3) }), + Err(_) => Err(ErrorMessage { message: "could not parse json from active profile cache file".to_string(), code: 3 }), } }, - Err(_) => Err(ErrorMessage { message: Some("could not open active profile cache file".to_string()), code: Some(3) }), + Err(_) => Err(ErrorMessage { message: "could not open active profile cache file".to_string(), code: 3 }), } }, - false => Err(ErrorMessage { message: Some("no active profile cache file".to_string()), code: Some(3) }), + false => Err(ErrorMessage { message: "no active profile cache file".to_string(), code: 3 }), } } @@ -99,7 +99,7 @@ pub fn active_profile(profiles: Vec) -> Result { match active_profile_index() { Ok(i) => match profiles.get(i) { Some(p) => Ok(p.clone()), - None => Err(ErrorMessage { message: Some("Could not get profile by index".to_string()), code: Some(1) }), + None => Err(ErrorMessage { message: "Could not get profile by index".to_string(), code: 1 }), }, Err(e) => Err(e), } @@ -148,7 +148,7 @@ pub fn previous(config: Config, sway_connection: Connection) -> Result<(),ErrorM pub fn index_from_name(config: Config, name: String) -> Result{ match config.profiles.iter().position(|x|x.name == name) { Some(i) => Ok(i), - None => Err(ErrorMessage { message: Some(String::from("Index not found for profile?")), code: Some(3) }), + None => Err(ErrorMessage { message: "Index not found for profile?".to_string(), code: 3 }), } } pub fn index_string(index: usize) -> String { diff --git a/src/lib/sway.rs b/src/lib/sway.rs index fcb6627..ae8f3fe 100644 --- a/src/lib/sway.rs +++ b/src/lib/sway.rs @@ -7,24 +7,18 @@ pub fn run_sway_command(sway_connection: &mut Connection,payload: String) -> Res println!("Command [{}] ran successfully",payload); Ok(()) }, - Err(e) => Err(ErrorMessage{ message: Some(e.to_string()), code: Some(2) }), + Err(e) => Err(ErrorMessage{ message: e.to_string(), code: 2 }), } } -pub fn monitor_events(event_type: EventType, cli: Cli, config: Config) { +pub fn monitor_events(event_type: Vec, cli: Cli, config: Config) { let sway_connection = Connection::new().unwrap(); - for event in sway_connection.subscribe([event_type]).unwrap() { + for event in sway_connection.subscribe(event_type).unwrap() { let e = event.unwrap(); match e { - swayipc::Event::Window(w) => { - print_window_title(w.container, &cli, &config); - }, - swayipc::Event::Workspace(_) => { - print_workspace_array(self::Connection::get_workspaces(&mut self::Connection::new().unwrap()).unwrap()); - }, - swayipc::Event::Tick(w) => { - println!("{}",w.payload); - }, + swayipc::Event::Window(w) => print_window_title(w.container, &cli, &config), + swayipc::Event::Workspace(_) => print_workspace_array(self::Connection::get_workspaces(&mut self::Connection::new().unwrap()).unwrap()), + swayipc::Event::Tick(_) => todo!(), _ => unreachable!(), } } diff --git a/src/lib/windows.rs b/src/lib/windows.rs index 3694024..78f4d93 100644 --- a/src/lib/windows.rs +++ b/src/lib/windows.rs @@ -1,25 +1,47 @@ -use std::{fmt::Write}; use serde_json::json; use swayipc::Node; -use crate::{config::Config, Cli}; +use crate::{config::Config, Cli, ErrorMessage}; #[derive(serde::Serialize)] pub struct WindowInfo { pub title: String } +pub fn get_window_name(window_node: Node, cli: &Cli, config: &Config) -> Result { +// println!("{:#?}",window_node); + match window_node.name { + Some(mut window_title) => { + for pair in config.window_icons.clone() { + if window_title.contains(&pair.substring) { + window_title = format!("{} {}", pair.icon, window_title.replace(&pair.substring,"")) + } + } + if cli.no_truncate_title.is_some_and(|x|!x) && config.title_length.is_some_and(|x|x.lt(&window_title.chars().count())) { + let trunc_point = window_title.char_indices().map(|(i, _)| i).nth(config.title_length.unwrap()).unwrap_or(window_title.chars().count()); + window_title.truncate(trunc_point); + window_title.push('…'); + } + Ok(window_title) + }, + None => { + Ok("".to_string()) + } + } +} + pub fn print_window_title(window_node: Node,cli: &Cli,config: &Config) { - let mut window_title_display: String = window_node.name.unwrap(); - for pair in &config.window_icons { +// let mut window_title_display: String = window_node.name.unwrap(); +/* for pair in &config.window_icons { if window_title_display.contains(&pair.substring) { window_title_display = pair.icon.clone() + " " + &window_title_display.replace(&pair.substring, ""); } } - if !cli.no_truncate_title.unwrap() && window_title_display.len().gt(&config.title_length) { - window_title_display.truncate(config.title_length); + if !cli.no_truncate_title.unwrap() && window_title_display.chars().count().gt(&config.title_length) { + let trunc_point = window_title_display.char_indices().map(|(i, _)| i).nth(config.title_length).unwrap_or(window_title_display.chars().count()); + window_title_display.truncate(trunc_point); let _ = window_title_display.write_char('…'); - } - let window_info = WindowInfo { title: window_title_display }; + } */ + let window_info = WindowInfo { title: get_window_name(window_node, cli, config).unwrap() }; let window_output = json!(window_info); println!("{}",window_output) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 27e3953..b0513c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -139,13 +139,13 @@ enum ProfileGetCommand { #[derive(Debug)] pub struct ErrorMessage { - message: Option, - code: Option + message: String, + code: i32 } pub fn error_handler(error: ErrorMessage) { - println!("ERROR: {}",error.message.unwrap_or_default()); - exit(error.code.unwrap_or(1)) + println!("ERROR: {}",error.message); + exit(error.code) } pub fn setup_runtime_dir(xdg_directories: BaseDirectories) { @@ -165,13 +165,13 @@ fn main() -> Fallible<()> { Commands::Windows => { print_window_title(sway_connection.get_tree().unwrap().iter().find(|&x | x.focused).unwrap().clone(), &cli, &config); if cli.monitor.unwrap() { - sway::monitor_events(EventType::Window, cli, config); + sway::monitor_events(vec![EventType::Window,EventType::Workspace], cli, config); } } Commands::Workspaces => { print_workspace_array(sway_connection.get_workspaces().unwrap()); if cli.monitor.unwrap() { - sway::monitor_events(EventType::Workspace, cli, config); + sway::monitor_events(vec![EventType::Workspace], cli, config); } } Commands::Launch { program } => { @@ -231,15 +231,15 @@ fn main() -> Fallible<()> { Ok(_) => (), Err(_) => match profile::switch_by_name(config, q.to_string(), Connection::new().unwrap()) { Ok(_) => (), - Err(_) => error_handler(ErrorMessage { message: Some(format!("Could not find profile with index or name: {}",q)), code: Some(4) }), + Err(_) => error_handler(ErrorMessage { message: format!("Could not find profile with index or name: {}",q), code: 4 }), }, }, Err(_) => match profile::switch_by_name(config, q.to_string(), sway_connection) { Ok(_) => (), - Err(_) => error_handler(ErrorMessage { message: Some(format!("Could not find profile with index or name: {}",q)), code: Some(4) }), + Err(_) => error_handler(ErrorMessage { message: format!("Could not find profile with index or name: {}",q), code: 4 }), }, }, - None => error_handler(ErrorMessage { message: Some("No profile index or name provided.".to_string()), code: Some(4) }), + None => error_handler(ErrorMessage { message: "No profile index or name provided.".to_string(), code: 4 }), }, } }, @@ -275,7 +275,7 @@ fn main() -> Fallible<()> { let _ = get::watch(config, g,xdg_directories.runtime_dir.unwrap().join("sway-profiles-rs/active-profile.json").to_str().unwrap().to_string()); } }, - None => error_handler(ErrorMessage { message: Some("No matching Profile Detail".to_string()), code: Some(4) }), + None => error_handler(ErrorMessage { message: "No matching Profile Detail".to_string(), code: 4 }), } } }