Skip to content

Commit

Permalink
feat: use readily available mode instructions from parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lavafroth committed Jul 6, 2024
1 parent ebd4cd3 commit 8d169ed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
2 changes: 1 addition & 1 deletion swhkd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 10 additions & 3 deletions swhkd/src/config.rs
Original file line number Diff line number Diff line change
@@ -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

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
pub const MODE_ENTER_STATEMENT: &str = "@enter";
Expand Down Expand Up @@ -82,6 +82,7 @@ impl Value for KeyBinding {
pub struct Hotkey {
pub keybinding: KeyBinding,
pub command: String,
pub mode_instructions: Vec<ModeInstruction>,
}

#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
Expand All @@ -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<Modifier>, command: String) -> Self {
Hotkey { keybinding: KeyBinding::new(keysym, modifiers), command }
Hotkey {
keybinding: KeyBinding::new(keysym, modifiers),
command,
mode_instructions: vec![],
}
}
}

Expand Down Expand Up @@ -162,6 +167,7 @@ pub fn parse_contents(contents: SwhkdParser) -> Result<Vec<Mode>, 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 {
Expand All @@ -177,6 +183,7 @@ pub fn parse_contents(contents: SwhkdParser) -> Result<Vec<Mode>, 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);
Expand Down
37 changes: 11 additions & 26 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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)
Expand Down

0 comments on commit 8d169ed

Please sign in to comment.