finally working properly config parsing
This commit is contained in:
@@ -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