finally working properly config parsing
This commit is contained in:
@@ -186,4 +186,102 @@ impl Config {
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
// This method handles all keybinding formats, both with and without +
|
||||
pub fn matches_key_sequence_generalized(&self, sequence: &[KeyCode]) -> Option<&str> {
|
||||
if sequence.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Get string representations of the sequence
|
||||
let sequence_str = sequence.iter()
|
||||
.map(|k| crate::config::key_sequences::key_to_string(k))
|
||||
.collect::<Vec<String>>()
|
||||
.join("");
|
||||
|
||||
// Add the missing sequence_plus definition
|
||||
let sequence_plus = sequence.iter()
|
||||
.map(|k| crate::config::key_sequences::key_to_string(k))
|
||||
.collect::<Vec<String>>()
|
||||
.join("+");
|
||||
|
||||
// Check for matches in all binding formats
|
||||
for (action, bindings) in &self.keybindings {
|
||||
for binding in bindings {
|
||||
let normalized_binding = binding.to_lowercase();
|
||||
|
||||
// Check if binding matches any of our formats
|
||||
if normalized_binding == sequence_str || normalized_binding == sequence_plus {
|
||||
return Some(action);
|
||||
}
|
||||
|
||||
// Special case for + format in bindings
|
||||
if binding.contains('+') {
|
||||
let normalized_sequence = sequence.iter()
|
||||
.map(|k| crate::config::key_sequences::key_to_string(k))
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
let binding_parts: Vec<&str> = binding.split('+').collect();
|
||||
|
||||
if binding_parts.len() == sequence.len() {
|
||||
let matches = binding_parts.iter().enumerate().all(|(i, part)| {
|
||||
part.to_lowercase() == normalized_sequence[i].to_lowercase()
|
||||
});
|
||||
|
||||
if matches {
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
// Check if the current key sequence is a prefix of a longer binding
|
||||
pub fn is_key_sequence_prefix(&self, sequence: &[KeyCode]) -> bool {
|
||||
if sequence.is_empty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get string representation of the sequence
|
||||
let sequence_str = sequence.iter()
|
||||
.map(|k| crate::config::key_sequences::key_to_string(k))
|
||||
.collect::<Vec<String>>()
|
||||
.join("");
|
||||
|
||||
// Check all bindings to see if our sequence is a prefix
|
||||
for (_, bindings) in &self.keybindings {
|
||||
for binding in bindings {
|
||||
let normalized_binding = binding.to_lowercase();
|
||||
|
||||
// Check standard format
|
||||
if normalized_binding.starts_with(&sequence_str) &&
|
||||
normalized_binding.len() > sequence_str.len() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check + format
|
||||
if binding.contains('+') {
|
||||
let binding_parts: Vec<&str> = binding.split('+').collect();
|
||||
let sequence_parts = sequence.iter()
|
||||
.map(|k| crate::config::key_sequences::key_to_string(k))
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
if binding_parts.len() > sequence_parts.len() {
|
||||
let prefix_matches = sequence_parts.iter().enumerate().all(|(i, part)| {
|
||||
binding_parts.get(i).map_or(false, |b| b.to_lowercase() == part.to_lowercase())
|
||||
});
|
||||
|
||||
if prefix_matches {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// client/src/key_sequences.rs
|
||||
// client/src/config/key_sequences.rs
|
||||
use crossterm::event::{KeyCode, KeyModifiers};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -35,7 +35,7 @@ impl KeySequenceTracker {
|
||||
if now.duration_since(self.last_key_time) > self.timeout {
|
||||
self.reset();
|
||||
}
|
||||
|
||||
|
||||
self.current_sequence.push(key);
|
||||
self.last_key_time = now;
|
||||
true
|
||||
@@ -44,38 +44,91 @@ impl KeySequenceTracker {
|
||||
pub fn get_sequence(&self) -> Vec<KeyCode> {
|
||||
self.current_sequence.clone()
|
||||
}
|
||||
|
||||
|
||||
// Convert a sequence of keys to a string representation
|
||||
pub fn sequence_to_string(&self) -> String {
|
||||
self.current_sequence.iter().map(|k| match k {
|
||||
KeyCode::Char(c) => c.to_string(),
|
||||
KeyCode::Left => "Left".into(),
|
||||
KeyCode::Right => "Right".into(),
|
||||
KeyCode::Up => "Up".into(),
|
||||
KeyCode::Down => "Down".into(),
|
||||
KeyCode::Esc => "Esc".into(),
|
||||
KeyCode::Enter => "Enter".into(),
|
||||
_ => String::new(),
|
||||
}).collect()
|
||||
self.current_sequence.iter().map(|k| key_to_string(k)).collect()
|
||||
}
|
||||
|
||||
// Convert a sequence to a format with + between keys
|
||||
pub fn sequence_to_plus_format(&self) -> String {
|
||||
if self.current_sequence.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
|
||||
let parts: Vec<String> = self.current_sequence.iter()
|
||||
.map(|k| key_to_string(k))
|
||||
.collect();
|
||||
|
||||
parts.join("+")
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to convert any KeyCode to a string representation
|
||||
pub fn key_to_string(key: &KeyCode) -> String {
|
||||
match key {
|
||||
KeyCode::Char(c) => c.to_string(),
|
||||
KeyCode::Left => "left".to_string(),
|
||||
KeyCode::Right => "right".to_string(),
|
||||
KeyCode::Up => "up".to_string(),
|
||||
KeyCode::Down => "down".to_string(),
|
||||
KeyCode::Esc => "esc".to_string(),
|
||||
KeyCode::Enter => "enter".to_string(),
|
||||
KeyCode::Backspace => "backspace".to_string(),
|
||||
KeyCode::Delete => "delete".to_string(),
|
||||
KeyCode::Tab => "tab".to_string(),
|
||||
KeyCode::BackTab => "backtab".to_string(),
|
||||
KeyCode::Home => "home".to_string(),
|
||||
KeyCode::End => "end".to_string(),
|
||||
KeyCode::PageUp => "pageup".to_string(),
|
||||
KeyCode::PageDown => "pagedown".to_string(),
|
||||
KeyCode::Insert => "insert".to_string(),
|
||||
_ => format!("{:?}", key).to_lowercase(),
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to convert a string to a KeyCode
|
||||
pub fn string_to_keycode(s: &str) -> Option<KeyCode> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"left" => Some(KeyCode::Left),
|
||||
"right" => Some(KeyCode::Right),
|
||||
"up" => Some(KeyCode::Up),
|
||||
"down" => Some(KeyCode::Down),
|
||||
"esc" => Some(KeyCode::Esc),
|
||||
"enter" => Some(KeyCode::Enter),
|
||||
"backspace" => Some(KeyCode::Backspace),
|
||||
"delete" => Some(KeyCode::Delete),
|
||||
"tab" => Some(KeyCode::Tab),
|
||||
"backtab" => Some(KeyCode::BackTab),
|
||||
"home" => Some(KeyCode::Home),
|
||||
"end" => Some(KeyCode::End),
|
||||
"pageup" => Some(KeyCode::PageUp),
|
||||
"pagedown" => Some(KeyCode::PageDown),
|
||||
"insert" => Some(KeyCode::Insert),
|
||||
s if s.len() == 1 => s.chars().next().map(KeyCode::Char),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
// Handle different binding formats
|
||||
let parts: Vec<String> = if binding.contains('+') {
|
||||
// Format with explicit '+' separators like "g+left"
|
||||
binding.split('+').map(|s| s.to_string()).collect()
|
||||
} else if binding.contains(' ') {
|
||||
// Format with spaces like "g left"
|
||||
binding.split(' ').map(|s| s.to_string()).collect()
|
||||
} else if is_compound_key(binding) {
|
||||
// A single compound key like "left" or "enter"
|
||||
vec![binding.to_string()]
|
||||
} else {
|
||||
// Fixed implementation using char indices
|
||||
binding.char_indices().map(|(i, _)| {
|
||||
let start = i;
|
||||
let end = i + 1;
|
||||
&binding[start..end]
|
||||
}).collect()
|
||||
// Simple character sequence like "gg"
|
||||
binding.chars().map(|c| c.to_string()).collect()
|
||||
};
|
||||
|
||||
for part in parts {
|
||||
for part in &parts {
|
||||
if let Some(key) = parse_key_part(part) {
|
||||
sequence.push(key);
|
||||
}
|
||||
@@ -83,39 +136,36 @@ pub fn parse_binding(binding: &str) -> Vec<ParsedKey> {
|
||||
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 is_compound_key(part: &str) -> bool {
|
||||
matches!(part.to_lowercase().as_str(),
|
||||
"esc" | "up" | "down" | "left" | "right" | "enter" |
|
||||
"backspace" | "delete" | "tab" | "backtab" | "home" |
|
||||
"end" | "pageup" | "pagedown" | "insert"
|
||||
)
|
||||
}
|
||||
|
||||
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),
|
||||
"left" => code = Some(KeyCode::Left),
|
||||
"right" => code = Some(KeyCode::Right),
|
||||
"up" => code = Some(KeyCode::Up),
|
||||
"down" => code = Some(KeyCode::Down),
|
||||
"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()));
|
||||
if part.contains('+') {
|
||||
// This handles modifiers like "ctrl+s"
|
||||
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,
|
||||
_ => {
|
||||
// Last component is the key
|
||||
code = string_to_keycode(component);
|
||||
}
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
} else {
|
||||
// Simple key without modifiers
|
||||
code = string_to_keycode(part);
|
||||
}
|
||||
|
||||
code.map(|code| ParsedKey { code, modifiers })
|
||||
|
||||
Reference in New Issue
Block a user