From 8d169edcfcf6f89647a8fde1808d6330ac1b0848 Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Sat, 6 Jul 2024 14:04:18 +0530 Subject: [PATCH] feat: use readily available mode instructions from parser --- swhkd/Cargo.toml | 2 +- swhkd/src/config.rs | 13 ++++++++++--- swhkd/src/daemon.rs | 37 +++++++++++-------------------------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/swhkd/Cargo.toml b/swhkd/Cargo.toml index cb6370a..78a1555 100644 --- a/swhkd/Cargo.toml +++ b/swhkd/Cargo.toml @@ -21,7 +21,7 @@ log = "0.4.14" nix = "0.23.1" signal-hook = "0.3.13" signal-hook-tokio = { version = "0.3.1", features = ["futures-v0_3"] } -sweet = { git = "https://github.com/lavafroth/sweet.git", version = "0.1.0" } +sweet = { git = "https://github.com/lavafroth/sweet.git", version = "0.2.0" } sysinfo = "0.23.5" tokio = { version = "1.24.2", features = ["full"] } tokio-stream = "0.1.8" diff --git a/swhkd/src/config.rs b/swhkd/src/config.rs index 189d863..271bbb3 100644 --- a/swhkd/src/config.rs +++ b/swhkd/src/config.rs @@ -1,7 +1,7 @@ use std::path::Path; use sweet::token::KeyAttribute; -use sweet::ParseError; use sweet::{Definition, SwhkdParser}; +use sweet::{ModeInstruction, ParseError}; /// TODO: implement these in the code side of the parser crate pub const MODE_ENTER_STATEMENT: &str = "@enter"; @@ -82,6 +82,7 @@ impl Value for KeyBinding { pub struct Hotkey { pub keybinding: KeyBinding, pub command: String, + pub mode_instructions: Vec, } #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)] @@ -96,11 +97,15 @@ pub enum Modifier { impl Hotkey { pub fn from_keybinding(keybinding: KeyBinding, command: String) -> Self { - Hotkey { keybinding, command } + Hotkey { keybinding, command, mode_instructions: vec![] } } #[cfg(test)] pub fn new(keysym: evdev::Key, modifiers: Vec, command: String) -> Self { - Hotkey { keybinding: KeyBinding::new(keysym, modifiers), command } + Hotkey { + keybinding: KeyBinding::new(keysym, modifiers), + command, + mode_instructions: vec![], + } } } @@ -162,6 +167,7 @@ pub fn parse_contents(contents: SwhkdParser) -> Result, ParseError> { default_mode.hotkeys.push(Hotkey { keybinding: sweet_def_to_kb(&binding.definition), command: binding.command.clone(), + mode_instructions: binding.mode_instructions.clone(), }); } for unbind in contents.unbinds { @@ -177,6 +183,7 @@ pub fn parse_contents(contents: SwhkdParser) -> Result, ParseError> { let hotkey = Hotkey { keybinding: sweet_def_to_kb(&binding.definition), command: binding.command, + mode_instructions: binding.mode_instructions.clone(), }; pushmode.hotkeys.retain(|h| h.keybinding != hotkey.keybinding); pushmode.hotkeys.push(hotkey); diff --git a/swhkd/src/daemon.rs b/swhkd/src/daemon.rs index 4c67f36..628223f 100644 --- a/swhkd/src/daemon.rs +++ b/swhkd/src/daemon.rs @@ -502,38 +502,23 @@ pub fn send_command( ) { log::info!("Hotkey pressed: {:#?}", hotkey); let command = hotkey.command; - let mut commands_to_send = String::new(); - if modes[mode_stack[mode_stack.len() - 1]].options.oneoff { + if modes[*mode_stack.last().unwrap()].options.oneoff { mode_stack.pop(); } - if command.contains('@') { - let commands = command.split("&&").map(|s| s.trim()).collect::>(); - for cmd in commands { - let mut words = cmd.split_whitespace(); - match words.next().unwrap() { - config::MODE_ENTER_STATEMENT => { - let enter_mode = cmd.split(' ').nth(1).unwrap(); - for (i, mode) in modes.iter().enumerate() { - if mode.name == enter_mode { - mode_stack.push(i); - break; - } - } - log::info!("Entering mode: {}", modes[mode_stack[mode_stack.len() - 1]].name); - } - config::MODE_ESCAPE_STATEMENT => { - mode_stack.pop(); + for mode in hotkey.mode_instructions.iter() { + match mode { + sweet::ModeInstruction::Enter(name) => { + if let Some(mode_index) = modes.iter().position(|modename| modename.name.eq(name)) { + mode_stack.push(mode_index); + log::info!("Entering mode: {}", name); } - _ => commands_to_send.push_str(format!("{cmd} &&").as_str()), + } + sweet::ModeInstruction::Escape => { + mode_stack.pop(); } } - } else { - commands_to_send = command; - } - if commands_to_send.ends_with(" &&") { - commands_to_send = commands_to_send.strip_suffix(" &&").unwrap().to_string(); } - if let Err(e) = socket_write(&commands_to_send, socket_path.to_path_buf()) { + if let Err(e) = socket_write(&command, socket_path.to_path_buf()) { log::error!("Failed to send command to swhks through IPC."); log::error!("Please make sure that swhks is running."); log::error!("Err: {:#?}", e)