working double shortcuts

This commit is contained in:
filipriec
2025-02-27 13:58:47 +01:00
parent 312d5fff36
commit a7c105d903
5 changed files with 345 additions and 154 deletions

View File

@@ -46,6 +46,11 @@ impl Config {
continue;
}
for binding in bindings {
// Skip multi-key bindings when checking for single key actions
if binding.len() > 1 && !binding.contains('+') {
continue;
}
if Self::matches_keybinding(binding, key, modifiers) {
return Some(action);
}
@@ -54,11 +59,52 @@ impl Config {
None
}
/// Checks if a sequence of keys matches any keybinding
pub fn matches_key_sequence(&self, sequence: &[KeyCode]) -> Option<&str> {
if sequence.is_empty() {
return None;
}
// Convert key sequence to a string (for simple character sequences)
let sequence_str: String = sequence.iter().filter_map(|key| {
if let KeyCode::Char(c) = key {
Some(*c)
} else {
None
}
}).collect();
if sequence_str.is_empty() {
return None;
}
// Check if this sequence matches any binding
for (action, bindings) in &self.keybindings {
for binding in bindings {
// Skip bindings with modifiers (those contain '+')
if binding.contains('+') {
continue;
}
if binding == &sequence_str {
return Some(action);
}
}
}
None
}
fn matches_keybinding(
binding: &str,
key: KeyCode,
modifiers: KeyModifiers,
) -> bool {
// For multi-character bindings without modifiers, we handle them in matches_key_sequence
if binding.len() > 1 && !binding.contains('+') {
return false;
}
let parts: Vec<&str> = binding.split('+').collect();
let mut expected_modifiers = KeyModifiers::empty();
let mut expected_key = None;

View File

@@ -0,0 +1,115 @@
use crossterm::event::{KeyCode, KeyModifiers};
use std::time::{Duration, Instant};
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedKey {
pub code: KeyCode,
pub modifiers: KeyModifiers,
}
#[derive(Debug, Clone)]
pub struct KeySequenceTracker {
pub current_sequence: Vec<KeyCode>,
pub last_key_time: Instant,
pub timeout: Duration,
}
impl KeySequenceTracker {
pub fn new(timeout_ms: u64) -> Self {
Self {
current_sequence: Vec::new(),
last_key_time: Instant::now(),
timeout: Duration::from_millis(timeout_ms),
}
}
pub fn reset(&mut self) {
self.current_sequence.clear();
self.last_key_time = Instant::now();
}
pub fn add_key(&mut self, key: KeyCode) -> bool {
// Check if timeout has expired
let now = Instant::now();
if now.duration_since(self.last_key_time) > self.timeout {
self.reset();
}
self.current_sequence.push(key);
self.last_key_time = now;
true
}
pub fn get_sequence(&self) -> Vec<KeyCode> {
self.current_sequence.clone()
}
pub fn sequence_to_string(&self) -> String {
self.current_sequence.iter().map(|k| match k {
KeyCode::Char(c) => c.to_string(),
_ => String::new(),
}).collect()
}
}
pub fn parse_binding(binding: &str) -> Vec<ParsedKey> {
let mut sequence = Vec::new();
let parts: Vec<&str> = if binding.contains(' ') {
binding.split(' ').collect()
} else if is_special_key(binding) {
vec![binding]
} else {
// Fixed implementation using char indices
binding.char_indices().map(|(i, _)| {
let start = i;
let end = i + 1;
&binding[start..end]
}).collect()
};
for part in parts {
if let Some(key) = parse_key_part(part) {
sequence.push(key);
}
}
sequence
}
fn is_special_key(part: &str) -> bool {
matches!(part.to_lowercase().as_str(),
"esc" | "up" | "down" | "left" | "right" |
"enter" | "backspace" | "delete" | "tab" | "backtab"
)
}
fn parse_key_part(part: &str) -> Option<ParsedKey> {
let mut modifiers = KeyModifiers::empty();
let mut code = None;
let components: Vec<&str> = part.split('+').collect();
for component in components {
match component.to_lowercase().as_str() {
"ctrl" => modifiers |= KeyModifiers::CONTROL,
"shift" => modifiers |= KeyModifiers::SHIFT,
"alt" => modifiers |= KeyModifiers::ALT,
"esc" => code = Some(KeyCode::Esc),
"up" => code = Some(KeyCode::Up),
"down" => code = Some(KeyCode::Down),
"left" => code = Some(KeyCode::Left),
"right" => code = Some(KeyCode::Right),
"enter" => code = Some(KeyCode::Enter),
"backspace" => code = Some(KeyCode::Backspace),
"delete" => code = Some(KeyCode::Delete),
"tab" => code = Some(KeyCode::Tab),
"backtab" => code = Some(KeyCode::BackTab),
":" => code = Some(KeyCode::Char(':')),
c if c.len() == 1 => {
code = Some(KeyCode::Char(c.chars().next().unwrap()));
}
_ => return None,
}
}
code.map(|code| ParsedKey { code, modifiers })
}

View File

@@ -1,3 +1,4 @@
// src/config/mod.rs
pub mod colors;
pub mod config;
pub mod key_sequences;