Skip to content

Commit 7999a9b

Browse files
authored
refactor: move send_command macro into a function (#255)
* refactor: move send_command macro into a function The macro accepts expressions with the same datatype in all invocations. Moving it into a function helps language servers make inferences easier because the expressions become concrete types. * lint: cargo fmt
1 parent f8519a5 commit 7999a9b

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

swhkd/src/daemon.rs

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::config::Value;
22
use clap::Parser;
3+
use config::Hotkey;
34
use evdev::{AttributeSet, Device, InputEventKind, Key};
45
use nix::{
56
sys::stat::{umask, Mode},
@@ -109,52 +110,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
109110

110111
let mut modes = load_config();
111112
let mut mode_stack: Vec<usize> = vec![0];
112-
113-
macro_rules! send_command {
114-
($hotkey: expr, $socket_path: expr) => {
115-
log::info!("Hotkey pressed: {:#?}", $hotkey);
116-
let command = $hotkey.command;
117-
let mut commands_to_send = String::new();
118-
if modes[mode_stack[mode_stack.len() - 1]].options.oneoff {
119-
mode_stack.pop();
120-
}
121-
if command.contains('@') {
122-
let commands = command.split("&&").map(|s| s.trim()).collect::<Vec<_>>();
123-
for cmd in commands {
124-
match cmd.split(' ').next().unwrap() {
125-
config::MODE_ENTER_STATEMENT => {
126-
let enter_mode = cmd.split(' ').nth(1).unwrap();
127-
for (i, mode) in modes.iter().enumerate() {
128-
if mode.name == enter_mode {
129-
mode_stack.push(i);
130-
break;
131-
}
132-
}
133-
log::info!(
134-
"Entering mode: {}",
135-
modes[mode_stack[mode_stack.len() - 1]].name
136-
);
137-
}
138-
config::MODE_ESCAPE_STATEMENT => {
139-
mode_stack.pop();
140-
}
141-
_ => commands_to_send.push_str(format!("{cmd} &&").as_str()),
142-
}
143-
}
144-
} else {
145-
commands_to_send = command;
146-
}
147-
if commands_to_send.ends_with(" &&") {
148-
commands_to_send = commands_to_send.strip_suffix(" &&").unwrap().to_string();
149-
}
150-
if let Err(e) = socket_write(&commands_to_send, $socket_path.to_path_buf()) {
151-
log::error!("Failed to send command to swhks through IPC.");
152-
log::error!("Please make sure that swhks is running.");
153-
log::error!("Err: {:#?}", e)
154-
}
155-
};
156-
}
157-
158113
let arg_devices: Vec<String> = args.device;
159114

160115
let keyboard_devices: Vec<_> = {
@@ -247,7 +202,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
247202
if hotkey.keybinding.on_release {
248203
continue;
249204
}
250-
send_command!(hotkey.clone(), &socket_file_path);
205+
send_command(hotkey.clone(), &socket_file_path, &modes, &mut mode_stack);
251206
hotkey_repeat_timer.as_mut().reset(Instant::now() + Duration::from_millis(repeat_cooldown_duration));
252207
}
253208

@@ -367,7 +322,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
367322
0 => {
368323
if last_hotkey.is_some() && pending_release {
369324
pending_release = false;
370-
send_command!(last_hotkey.clone().unwrap(), &socket_file_path);
325+
send_command(last_hotkey.clone().unwrap(), &socket_file_path, &modes, &mut mode_stack);
371326
last_hotkey = None;
372327
}
373328
if let Some(modifier) = modifiers_map.get(&key) {
@@ -430,7 +385,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
430385
pending_release = true;
431386
break;
432387
}
433-
send_command!(hotkey.clone(), &socket_file_path);
388+
send_command(hotkey.clone(), &socket_file_path, &modes, &mut mode_stack);
434389
hotkey_repeat_timer.as_mut().reset(Instant::now() + Duration::from_millis(repeat_cooldown_duration));
435390
continue;
436391
}
@@ -538,3 +493,49 @@ pub fn setup_swhkd(invoking_uid: u32, runtime_path: String) {
538493
exit(1);
539494
}
540495
}
496+
497+
pub fn send_command(
498+
hotkey: Hotkey,
499+
socket_path: &Path,
500+
modes: &[config::Mode],
501+
mode_stack: &mut Vec<usize>,
502+
) {
503+
log::info!("Hotkey pressed: {:#?}", hotkey);
504+
let command = hotkey.command;
505+
let mut commands_to_send = String::new();
506+
if modes[mode_stack[mode_stack.len() - 1]].options.oneoff {
507+
mode_stack.pop();
508+
}
509+
if command.contains('@') {
510+
let commands = command.split("&&").map(|s| s.trim()).collect::<Vec<_>>();
511+
for cmd in commands {
512+
let mut words = cmd.split_whitespace();
513+
match words.next().unwrap() {
514+
config::MODE_ENTER_STATEMENT => {
515+
let enter_mode = cmd.split(' ').nth(1).unwrap();
516+
for (i, mode) in modes.iter().enumerate() {
517+
if mode.name == enter_mode {
518+
mode_stack.push(i);
519+
break;
520+
}
521+
}
522+
log::info!("Entering mode: {}", modes[mode_stack[mode_stack.len() - 1]].name);
523+
}
524+
config::MODE_ESCAPE_STATEMENT => {
525+
mode_stack.pop();
526+
}
527+
_ => commands_to_send.push_str(format!("{cmd} &&").as_str()),
528+
}
529+
}
530+
} else {
531+
commands_to_send = command;
532+
}
533+
if commands_to_send.ends_with(" &&") {
534+
commands_to_send = commands_to_send.strip_suffix(" &&").unwrap().to_string();
535+
}
536+
if let Err(e) = socket_write(&commands_to_send, socket_path.to_path_buf()) {
537+
log::error!("Failed to send command to swhks through IPC.");
538+
log::error!("Please make sure that swhks is running.");
539+
log::error!("Err: {:#?}", e)
540+
};
541+
}

0 commit comments

Comments
 (0)