diff --git a/src/lib/get.rs b/src/lib/get.rs index d0fa366..8f67f42 100644 --- a/src/lib/get.rs +++ b/src/lib/get.rs @@ -1,9 +1,12 @@ -use crate::{config::Config, profile::{active_profile,profile_from_index}, ErrorMessage, ProfileGetCommand}; +use crate::{config::Config, profile::{active_profile_index,profile_from_index}, ErrorMessage, ProfileGetCommand}; +//use crate::utils::config::c; use serde_json::json; +use watchexec::Watchexec; +use watchexec_events::Tag; +use miette::{IntoDiagnostic, Result}; - -pub fn print(config: Config,info: &ProfileGetCommand) -> Result { - match active_profile() { +pub fn profile_info(config: Config,info: ProfileGetCommand) -> Result { + match active_profile_index() { Ok(i) => { match profile_from_index(config, i) { Ok(p) => { @@ -12,28 +15,33 @@ pub fn print(config: Config,info: &ProfileGetCommand) -> Result Ok(p.name), ProfileGetCommand::Icon => Ok(p.icon), } -/* Ok(if monitor { - let xdg_directories = BaseDirectories::new(); - let config = notify::Config::default().with_compare_contents(true).with_compare_contents(true).with_poll_interval(Duration::from_secs_f32(0.5)); - let (tx, rx) = std::sync::mpsc::channel(); - let mut watcher = PollWatcher::new(tx, config).unwrap(); - watcher.watch(xdg_directories.runtime_dir.unwrap().join("sway-profiles-rs/active-profile.json").as_path(),RecursiveMode::Recursive); - for _res in rx { - match info { - ProfileGetCommand::Json => println!("{}",json!(p).to_string()), - ProfileGetCommand::Name => println!("{}",p.name), - ProfileGetCommand::Icon => println!("{}",p.icon), - }; - match res { - Ok(event) => println!("changed: {:?}", event), - Err(e) => println!("watch error: {:?}", e), - }; - } - })*/ } Err(e) => Err(e), } - }, + }, Err(e) => Err(e), } +} + +#[tokio::main] +pub async fn watch(config: Config,info: ProfileGetCommand,watch_path: String) -> Result<()> { + let wx = Watchexec::new(move |mut action| { + //yeah, this is... fine + action.events.iter().find(|_event|_event.tags.iter().any(|tag| match tag { + Tag::FileEventKind(watchexec_events::filekind::FileEventKind::Access(watchexec_events::filekind::AccessKind::Close(watchexec_events::filekind::AccessMode::Write))) => { + println!("{}",profile_info(config.clone(),info).unwrap()); + true + }, + _ => false + })); + if action.signals().next().is_some() { + println!("Quitting"); + action.quit(); + } + action + })?; + let main = wx.main(); + wx.config.pathset([watch_path]); + let _ = main.await.into_diagnostic(); + Ok(()) } \ No newline at end of file diff --git a/src/lib/profile.rs b/src/lib/profile.rs index a8d11c4..1e05e8c 100644 --- a/src/lib/profile.rs +++ b/src/lib/profile.rs @@ -73,7 +73,7 @@ pub fn switch_by_name(config: Config,name: String,sway_connection: Connection) - } */ } -pub fn active_profile() -> Result { +pub fn active_profile_index() -> Result { 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() { @@ -94,8 +94,19 @@ pub fn active_profile() -> Result { } } +pub fn active_profile(profiles: Vec) -> Result { + //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() { + match active_profile_index() { Ok(u) => { let profile_count = config.profiles.len(); let mut next_profile = u + 1; @@ -115,7 +126,7 @@ pub fn next(config: Config, sway_connection: Connection) -> Result<(),ErrorMessa } pub fn previous(config: Config, sway_connection: Connection) -> Result<(),ErrorMessage> { - match active_profile() { + match active_profile_index() { Ok(u) => { let prev_profile: usize = if u.eq(&0) { config.profiles.len() - 1 diff --git a/src/lib/sway.rs b/src/lib/sway.rs index 7e850f2..fcb6627 100644 --- a/src/lib/sway.rs +++ b/src/lib/sway.rs @@ -1,6 +1,5 @@ -use swayipc::Connection; - -use crate::ErrorMessage; +use swayipc::{Connection, EventType}; +use crate::{config::Config, windows::print_window_title, workspaces::print_workspace_array, Cli, ErrorMessage}; pub fn run_sway_command(sway_connection: &mut Connection,payload: String) -> Result<(),ErrorMessage> { match sway_connection.run_command(&payload) { @@ -10,4 +9,23 @@ pub fn run_sway_command(sway_connection: &mut Connection,payload: String) -> Res }, Err(e) => Err(ErrorMessage{ message: Some(e.to_string()), code: Some(2) }), } +} + +pub fn monitor_events(event_type: EventType, cli: Cli, config: Config) { + let sway_connection = Connection::new().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); + }, + _ => unreachable!(), + } + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ee26914..27e3953 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ -#![deny(unused_crate_dependencies)] +#![warn(unused_crate_dependencies)] -use std::{process::exit, time::Duration}; +use std::process::exit; use clap::{ArgAction, Parser, Subcommand}; -use swayipc::{Connection, Event, EventType, Fallible}; -use notify::{PollWatcher, RecursiveMode, Watcher}; +use swayipc::{Connection, EventType, Fallible}; mod config; #[path = "lib/windows.rs"] @@ -27,7 +26,6 @@ mod profile; #[path = "lib/get.rs"] mod get; -use config::Config; use xdg::BaseDirectories; use crate::profile::switch_by_index; @@ -125,6 +123,7 @@ enum ProfileSwitchCommand { } } +#[derive(Clone, Copy)] #[derive(Subcommand)] enum ProfileGetCommand { /// get profile json @@ -157,10 +156,8 @@ pub fn setup_runtime_dir(xdg_directories: BaseDirectories) { } fn main() -> Fallible<()> { - //let xdg_dirs = BaseDirectories::with_prefix("sway-profiles-rs"); let xdg_directories = BaseDirectories::new(); let cli = Cli::parse(); -// let config = config::parse_config(); let config = confy::load("sway-profiles-rs", "config").unwrap(); let mut sway_connection = Connection::new()?; @@ -168,17 +165,18 @@ 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() { - monitor_sway_events(EventType::Window, cli, config); + sway::monitor_events(EventType::Window, cli, config); } } Commands::Workspaces => { print_workspace_array(sway_connection.get_workspaces().unwrap()); if cli.monitor.unwrap() { - monitor_sway_events(EventType::Workspace, cli, config); + sway::monitor_events(EventType::Workspace, cli, config); } } Commands::Launch { program } => { - match profile_launch(config.programs, program) { + println!("{:?}",config.programs); + match profile_launch(config.profiles,config.programs, program) { Ok(p) => { match run_sway_command(&mut sway_connection,p) { Ok(_) => todo!(), @@ -271,20 +269,10 @@ fn main() -> Fallible<()> { }, ProfileCommand::Get { get_command, monitor } => { match Some(get_command) { - Some(g) => { - println!("{}",get::print(config.clone(),g).unwrap()); + Some(&g) => { + println!("{}",get::profile_info(config.clone(),g).unwrap()); if monitor.unwrap() { - let notify_config = notify::Config::default().with_compare_contents(true).with_poll_interval(Duration::from_millis(500)); - let (tx, rx) = std::sync::mpsc::channel(); - let mut watcher = PollWatcher::new(tx, notify_config).unwrap(); - match watcher.watch(xdg_directories.runtime_dir.unwrap().join("sway-profiles-rs/active-profile.json").as_path(),RecursiveMode::Recursive) { - Ok(_) => { - for _res in rx { - println!("{}",get::print(config.clone(),g).unwrap()); - } - }, - Err(_) => error_handler(ErrorMessage { message: Some("Watcher failed".to_string()), code: Some(1) }), - }; + 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) }), @@ -298,22 +286,3 @@ fn main() -> Fallible<()> { } exit(0); } - -pub fn monitor_sway_events(event_type: EventType, cli: Cli, config: Config) { - let sway_connection = Connection::new().unwrap(); - for event in sway_connection.subscribe([event_type]).unwrap() { - let e = event.unwrap(); - match e { - Event::Window(w) => { - print_window_title(w.container, &cli, &config); - }, - Event::Workspace(_) => { - print_workspace_array(self::Connection::get_workspaces(&mut self::Connection::new().unwrap()).unwrap()); - }, - Event::Tick(w) => { - println!("{}",w.payload); - }, - _ => unreachable!(), - } - } -} \ No newline at end of file