diff --git a/README.md b/README.md index 7d1edf0..7e091d6 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,9 @@ Right now, there are these subcommands: * `execute-swaymsg-command` displays most swaymsg which don't require additional input and executes the selected one. That's handy especially for less often used commands not bound to a key. +* `execute-swayr-command` displays all commands above and executes the selected + one. (This is useful for accessing swayr commands which are not bound to a + key.) Swayr is licensed under the diff --git a/src/bin/swayr.rs b/src/bin/swayr.rs index 7383fe6..329cb52 100644 --- a/src/bin/swayr.rs +++ b/src/bin/swayr.rs @@ -13,37 +13,10 @@ use swayr::client; )] struct Opts { #[clap(subcommand)] - command: SwayrCommand, -} - -#[derive(Clap)] -enum SwayrCommand { - /// Focus the selected window - SwitchWindow, - /// Quit the selected window - QuitWindow, - /// Switch to the selected workspace - SwitchWorkspace, - /// Switch to the selected workspace or focus the selected window - SwitchWorkspaceOrWindow, - /// Quit all windows of selected workspace or the selected window - QuitWorkspaceOrWindow, - /// Select and execute a swaymsg command - ExecuteSwaymsgCommand, + command: client::SwayrCommand, } fn main() { let opts: Opts = Opts::parse(); - match opts.command { - SwayrCommand::SwitchWindow => client::switch_window(), - SwayrCommand::QuitWindow => client::quit_window(), - SwayrCommand::SwitchWorkspace => client::switch_workspace(), - SwayrCommand::SwitchWorkspaceOrWindow => { - client::switch_workspace_or_window() - } - SwayrCommand::QuitWorkspaceOrWindow => { - client::quit_workspace_or_window() - } - SwayrCommand::ExecuteSwaymsgCommand => client::exec_swaymsg_command(), - } + client::exec_swayr_cmd(&opts.command); } diff --git a/src/bin/swayrd.rs b/src/bin/swayrd.rs index 2bbe262..dff52ec 100644 --- a/src/bin/swayrd.rs +++ b/src/bin/swayrd.rs @@ -14,7 +14,7 @@ fn main() { let con_props_for_ev_handler = con_props.clone(); let subscriber_handle = thread::spawn(move || { - demon::monitor_window_events(con_props_for_ev_handler) + demon::monitor_con_events(con_props_for_ev_handler) }); match demon::serve_client_requests(con_props) { diff --git a/src/client.rs b/src/client.rs index 8a670fe..03d5997 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,8 +1,59 @@ use crate::con; use crate::ipc; use crate::util; +use clap::Clap; use std::fmt; +#[derive(Clap, Debug)] +pub enum SwayrCommand { + /// Focus the selected window + SwitchWindow, + /// Quit the selected window + QuitWindow, + /// Switch to the selected workspace + SwitchWorkspace, + /// Switch to the selected workspace or focus the selected window + SwitchWorkspaceOrWindow, + /// Quit all windows of selected workspace or the selected window + QuitWorkspaceOrWindow, + /// Select and execute a swaymsg command + ExecuteSwaymsgCommand, + /// Select and execute a swayr command + ExecuteSwayrCommand, +} + +impl fmt::Display for SwayrCommand { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + write!(f, "{:?}", self) + } +} + +pub fn exec_swayr_cmd(cmd: &SwayrCommand) { + match cmd { + SwayrCommand::SwitchWindow => switch_window(), + SwayrCommand::QuitWindow => quit_window(), + SwayrCommand::SwitchWorkspace => switch_workspace(), + SwayrCommand::SwitchWorkspaceOrWindow => switch_workspace_or_window(), + SwayrCommand::QuitWorkspaceOrWindow => quit_workspace_or_window(), + SwayrCommand::ExecuteSwaymsgCommand => exec_swaymsg_command(), + SwayrCommand::ExecuteSwayrCommand => { + if let Some(c) = util::wofi_select( + "Select swayr command", + &[ + SwayrCommand::ExecuteSwaymsgCommand, + SwayrCommand::QuitWindow, + SwayrCommand::QuitWorkspaceOrWindow, + SwayrCommand::SwitchWindow, + SwayrCommand::SwitchWorkspace, + SwayrCommand::SwitchWorkspaceOrWindow, + ], + ) { + exec_swayr_cmd(c); + } + } + } +} + fn focus_window_by_id(id: &ipc::Id) { util::swaymsg(&[format!("[con_id={}]", id).as_str(), "focus"]); } diff --git a/src/demon.rs b/src/demon.rs index 19b27bf..b460006 100644 --- a/src/demon.rs +++ b/src/demon.rs @@ -10,7 +10,7 @@ use std::sync::RwLock; use std::thread; use std::time::{SystemTime, UNIX_EPOCH}; -pub fn monitor_window_events( +pub fn monitor_con_events( con_props: Arc>>, ) { let child = proc::Command::new("swaymsg") @@ -31,6 +31,8 @@ pub fn monitor_window_events( Err(err) => eprintln!("Error handling window event:\n{:?}", err), } } + eprintln!("Stopped monitoring con events. Restarting..."); + monitor_con_events(con_props); } fn update_last_focus_time( @@ -50,7 +52,7 @@ fn update_last_focus_time( } } -fn remove_winprops( +fn remove_con_props( id: &ipc::Id, con_props: Arc>>, ) { @@ -70,7 +72,7 @@ fn handle_con_event( update_last_focus_time(container.id, con_props) } ipc::WindowEventType::Close => { - remove_winprops(&container.id, con_props) + remove_con_props(&container.id, con_props) } _ => handled = false, }, @@ -84,7 +86,7 @@ fn handle_con_event( update_last_focus_time(current.id, con_props) } ipc::WorkspaceEventType::Empty => { - remove_winprops(¤t.id, con_props) + remove_con_props(¤t.id, con_props) } _ => handled = false, },