156 lines
No EOL
5.8 KiB
Rust
156 lines
No EOL
5.8 KiB
Rust
use std::fs::{self, write};
|
|
|
|
use serde_json::json;
|
|
use swayipc::Connection;
|
|
use xdg::BaseDirectories;
|
|
|
|
use crate::{config::{Config, Profile}, sway::run_sway_command, ErrorMessage};
|
|
|
|
pub fn initialize(mut sway_connection: Connection, profile: Profile,profile_index: String,uindex: usize) -> Result<(),ErrorMessage> { //payload: String) {
|
|
//todo: preserve keyboard layout, where 0 is last in workspace index
|
|
for i in 0..10 {
|
|
let _ = match run_sway_command(&mut sway_connection, format!("bindsym $mod+{} workspace number {}{}:{}",i,profile_index,i,profile.icon)) {
|
|
Ok(_) => {
|
|
let base_directories = BaseDirectories::new();
|
|
let active_profile_cache = base_directories.get_runtime_directory().unwrap().join("sway-profiles-rs/active-profile.json");
|
|
let active_profile = json!(uindex);
|
|
let _ = write(active_profile_cache, active_profile.to_string());
|
|
Ok(())
|
|
},
|
|
Err(e) => Err(e),
|
|
};
|
|
}
|
|
match run_sway_command(&mut sway_connection, format!("workspace number {}1:{}",profile_index,profile.icon)) {
|
|
Ok(_) => Ok(()),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub fn profile_from_index(config: Config, index: usize) -> Result<Profile,ErrorMessage>{
|
|
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) }),
|
|
}
|
|
}
|
|
|
|
pub fn _profile_from_name(config: Config, name: String) -> Result<Profile,ErrorMessage> {
|
|
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) }),
|
|
}
|
|
}
|
|
|
|
pub fn switch_by_index(config: Config,index: usize,sway_connection: Connection) -> Result<(),ErrorMessage> {
|
|
match profile_from_index(config, index) {
|
|
Ok(p) => match initialize(sway_connection, p.clone(), index_string(index), index) {
|
|
Ok(_) => {
|
|
println!("successfully switched to profile at index {}",index);
|
|
Ok(())
|
|
},
|
|
Err(e) => Err(e),
|
|
},
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub fn switch_by_name(config: Config,name: String,sway_connection: Connection) -> Result<(),ErrorMessage> {
|
|
match index_from_name(config.clone(), name.clone()) {
|
|
Ok(index) => match switch_by_index(config, index, sway_connection) {
|
|
Ok(_) => {
|
|
println!("Successfully switched to profile with name {}",name);
|
|
Ok(())
|
|
},
|
|
Err(e) => Err(e),
|
|
},
|
|
Err(e) => Err(e),
|
|
}
|
|
|
|
/* match profile_from_name(config, name) {
|
|
Ok(p) => match {
|
|
|
|
},
|
|
Err(e) => Err(e),
|
|
} */
|
|
}
|
|
|
|
pub fn active_profile_index() -> Result<usize,ErrorMessage> {
|
|
let base_directories = BaseDirectories::new();
|
|
let active_profile_cache_json = base_directories.get_runtime_directory().unwrap().join("sway-profiles-rs/active-profile.json");
|
|
match active_profile_cache_json.exists() {
|
|
true => {
|
|
match fs::File::open(active_profile_cache_json) {
|
|
Ok(f) => {
|
|
match serde_json::from_reader::<fs::File,usize>(f) {
|
|
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: Some("could not open active profile cache file".to_string()), code: Some(3) }),
|
|
}
|
|
},
|
|
false => Err(ErrorMessage { message: Some("no active profile cache file".to_string()), code: Some(3) }),
|
|
}
|
|
}
|
|
|
|
pub fn active_profile(profiles: Vec<Profile>) -> Result<Profile,ErrorMessage> {
|
|
//let active_profile_index =
|
|
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) }),
|
|
},
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub fn next(config: Config, sway_connection: Connection) -> Result<(),ErrorMessage> {
|
|
match active_profile_index() {
|
|
Ok(u) => {
|
|
let profile_count = config.profiles.len();
|
|
let mut next_profile = u + 1;
|
|
if next_profile.ge(&profile_count) {
|
|
next_profile = 0
|
|
}
|
|
match switch_by_index(config, next_profile, sway_connection) {
|
|
Ok(_) => {
|
|
println!("switched to next profile ({})",next_profile);
|
|
Ok(())
|
|
},
|
|
Err(e) => Err(e),
|
|
}
|
|
},
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub fn previous(config: Config, sway_connection: Connection) -> Result<(),ErrorMessage> {
|
|
match active_profile_index() {
|
|
Ok(u) => {
|
|
let prev_profile: usize = if u.eq(&0) {
|
|
config.profiles.len() - 1
|
|
} else {
|
|
u - 1
|
|
};
|
|
match switch_by_index(config, prev_profile, sway_connection) {
|
|
Ok(_) => {
|
|
println!("switched to prev profile ({})",prev_profile);
|
|
Ok(())
|
|
},
|
|
Err(e) => Err(e),
|
|
}
|
|
},
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub fn index_from_name(config: Config, name: String) -> Result<usize, ErrorMessage>{
|
|
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) }),
|
|
}
|
|
}
|
|
pub fn index_string(index: usize) -> String {
|
|
index.to_string().trim_matches('0').to_string()
|
|
} |