sway-profiles-rs/src/lib/launch.rs
2025-09-21 01:27:23 -07:00

32 lines
No EOL
1.3 KiB
Rust

use std::collections::HashMap;
use crate::{config::{Profile, Programs}, profile::active_profile, ErrorMessage};
pub fn append_arguments(mut command: String, args: Vec<String>) -> String {
for a in args {
command = format!("{} {}",command,a);
}
command
}
pub fn profile_launch(profiles: Vec<Profile>, programs_config: HashMap<String, Programs>, program_arg: &String) -> Result<String,ErrorMessage> {
match programs_config.iter().find(|x|x.0.eq(program_arg)) {
Some(p) => {
let mut swaymsg_command = format!("exec {}", p.1.command);
swaymsg_command = append_arguments(swaymsg_command, p.1.arguments.clone());
match active_profile(profiles) {
Ok(profile) => {
println!("{:#?}",profile);
if let Some(profile_args) = profile.program_args.unwrap_or_default().iter().find(|x|x.0.eq(p.0)) {
swaymsg_command = append_arguments(swaymsg_command, profile_args.1.to_owned());
}
Ok(swaymsg_command)
},
Err(e) => Err(e),
}
},
None => {
Err(ErrorMessage { message: Some(String::from("No matching program found")), code: Some(3) })
},
}
}