clean up sway and better title length handling

This commit is contained in:
Penelope Gwen 2025-09-24 20:08:10 -07:00
parent b7951cbcc2
commit 38e5d317f8
6 changed files with 55 additions and 39 deletions

View file

@ -26,7 +26,7 @@ pub struct LockConf {
} }
#[derive(Serialize,Deserialize,Clone,Default)] #[derive(Serialize,Deserialize,Clone,Default)]
pub struct Config { pub struct Config {
pub title_length: usize, pub title_length: Option<usize>,
pub window_icons: Vec<WindowIcon>, pub window_icons: Vec<WindowIcon>,
pub programs: HashMap<String, Programs>, pub programs: HashMap<String, Programs>,
pub lock: LockConf, pub lock: LockConf,

View file

@ -26,7 +26,7 @@ pub fn profile_launch(profiles: Vec<Profile>, programs_config: HashMap<String, P
} }
}, },
None => { None => {
Err(ErrorMessage { message: Some(String::from("No matching program found")), code: Some(3) }) Err(ErrorMessage { message: String::from("No matching program found"), code: 3 })
}, },
} }
} }

View file

@ -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<Profile,ErrorMessage>{ pub fn profile_from_index(config: Config, index: usize) -> Result<Profile,ErrorMessage>{
match config.profiles.get(index) { match config.profiles.get(index) {
Some(p) => Ok(p.clone()), 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<Profile,ErrorMessage> { pub fn _profile_from_name(config: Config, name: String) -> Result<Profile,ErrorMessage> {
match config.profiles.iter().find(|x|x.name == name) { match config.profiles.iter().find(|x|x.name == name) {
Some(p) => Ok(p.clone()), 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<usize,ErrorMessage> {
Ok(u) => { Ok(u) => {
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<Profile>) -> Result<Profile,ErrorMessage> {
match active_profile_index() { match active_profile_index() {
Ok(i) => match profiles.get(i) { Ok(i) => match profiles.get(i) {
Some(p) => Ok(p.clone()), 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), 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<usize, ErrorMessage>{ pub fn index_from_name(config: Config, name: String) -> Result<usize, ErrorMessage>{
match config.profiles.iter().position(|x|x.name == name) { match config.profiles.iter().position(|x|x.name == name) {
Some(i) => Ok(i), 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 { pub fn index_string(index: usize) -> String {

View file

@ -7,24 +7,18 @@ pub fn run_sway_command(sway_connection: &mut Connection,payload: String) -> Res
println!("Command [{}] ran successfully",payload); println!("Command [{}] ran successfully",payload);
Ok(()) 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<EventType>, cli: Cli, config: Config) {
let sway_connection = Connection::new().unwrap(); 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(); let e = event.unwrap();
match e { match e {
swayipc::Event::Window(w) => { swayipc::Event::Window(w) => print_window_title(w.container, &cli, &config),
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!(),
swayipc::Event::Workspace(_) => {
print_workspace_array(self::Connection::get_workspaces(&mut self::Connection::new().unwrap()).unwrap());
},
swayipc::Event::Tick(w) => {
println!("{}",w.payload);
},
_ => unreachable!(), _ => unreachable!(),
} }
} }

View file

@ -1,25 +1,47 @@
use std::{fmt::Write};
use serde_json::json; use serde_json::json;
use swayipc::Node; use swayipc::Node;
use crate::{config::Config, Cli}; use crate::{config::Config, Cli, ErrorMessage};
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
pub struct WindowInfo { pub struct WindowInfo {
pub title: String pub title: String
} }
pub fn get_window_name(window_node: Node, cli: &Cli, config: &Config) -> Result<String,ErrorMessage> {
// 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) { pub fn print_window_title(window_node: Node,cli: &Cli,config: &Config) {
let mut window_title_display: String = window_node.name.unwrap(); // let mut window_title_display: String = window_node.name.unwrap();
for pair in &config.window_icons { /* for pair in &config.window_icons {
if window_title_display.contains(&pair.substring) { if window_title_display.contains(&pair.substring) {
window_title_display = pair.icon.clone() + " " + &window_title_display.replace(&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) { if !cli.no_truncate_title.unwrap() && window_title_display.chars().count().gt(&config.title_length) {
window_title_display.truncate(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_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); let window_output = json!(window_info);
println!("{}",window_output) println!("{}",window_output)
} }

View file

@ -139,13 +139,13 @@ enum ProfileGetCommand {
#[derive(Debug)] #[derive(Debug)]
pub struct ErrorMessage { pub struct ErrorMessage {
message: Option<String>, message: String,
code: Option<i32> code: i32
} }
pub fn error_handler(error: ErrorMessage) { pub fn error_handler(error: ErrorMessage) {
println!("ERROR: {}",error.message.unwrap_or_default()); println!("ERROR: {}",error.message);
exit(error.code.unwrap_or(1)) exit(error.code)
} }
pub fn setup_runtime_dir(xdg_directories: BaseDirectories) { pub fn setup_runtime_dir(xdg_directories: BaseDirectories) {
@ -165,13 +165,13 @@ fn main() -> Fallible<()> {
Commands::Windows => { Commands::Windows => {
print_window_title(sway_connection.get_tree().unwrap().iter().find(|&x | x.focused).unwrap().clone(), &cli, &config); print_window_title(sway_connection.get_tree().unwrap().iter().find(|&x | x.focused).unwrap().clone(), &cli, &config);
if cli.monitor.unwrap() { if cli.monitor.unwrap() {
sway::monitor_events(EventType::Window, cli, config); sway::monitor_events(vec![EventType::Window,EventType::Workspace], cli, config);
} }
} }
Commands::Workspaces => { Commands::Workspaces => {
print_workspace_array(sway_connection.get_workspaces().unwrap()); print_workspace_array(sway_connection.get_workspaces().unwrap());
if cli.monitor.unwrap() { if cli.monitor.unwrap() {
sway::monitor_events(EventType::Workspace, cli, config); sway::monitor_events(vec![EventType::Workspace], cli, config);
} }
} }
Commands::Launch { program } => { Commands::Launch { program } => {
@ -231,15 +231,15 @@ fn main() -> Fallible<()> {
Ok(_) => (), Ok(_) => (),
Err(_) => match profile::switch_by_name(config, q.to_string(), Connection::new().unwrap()) { Err(_) => match profile::switch_by_name(config, q.to_string(), Connection::new().unwrap()) {
Ok(_) => (), 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) { Err(_) => match profile::switch_by_name(config, q.to_string(), sway_connection) {
Ok(_) => (), 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()); 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 }),
} }
} }
} }