keybindings are now in toml file

This commit is contained in:
filipriec
2025-02-17 21:07:48 +01:00
parent c5dee41757
commit b306a6d019
3 changed files with 34 additions and 8 deletions

View File

@@ -37,11 +37,13 @@ impl Config {
"shift" => expected_modifiers |= KeyModifiers::SHIFT, "shift" => expected_modifiers |= KeyModifiers::SHIFT,
"alt" => expected_modifiers |= KeyModifiers::ALT, "alt" => expected_modifiers |= KeyModifiers::ALT,
_ => { _ => {
// Handle single character keys and special cases
expected_key = match part.to_lowercase().as_str() { expected_key = match part.to_lowercase().as_str() {
"s" => Some(KeyCode::Char('s')),
"q" => Some(KeyCode::Char('q')),
"w" => Some(KeyCode::Char('w')),
":" => Some(KeyCode::Char(':')), ":" => Some(KeyCode::Char(':')),
part if part.len() == 1 => {
let c = part.chars().next().unwrap();
Some(KeyCode::Char(c))
}
_ => None, _ => None,
}; };
} }
@@ -50,4 +52,15 @@ impl Config {
modifiers == expected_modifiers && Some(key) == expected_key modifiers == expected_modifiers && Some(key) == expected_key
} }
pub fn get_action_for_command(&self, command: &str) -> Option<&str> {
for (action, bindings) in &self.keybindings {
for binding in bindings {
if binding.starts_with(':') && binding.trim_start_matches(':') == command {
return Some(action);
}
}
}
None
}
} }

View File

@@ -50,7 +50,7 @@ impl AppTerminal {
pub async fn handle_command( pub async fn handle_command(
&mut self, &mut self,
action: &str, action: &str, // This should be the resolved action (e.g., "save")
is_saved: &mut bool, is_saved: &mut bool,
form_data: &AdresarRequest, form_data: &AdresarRequest,
) -> Result<(bool, String), Box<dyn std::error::Error>> { ) -> Result<(bool, String), Box<dyn std::error::Error>> {

View File

@@ -120,16 +120,29 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
fax: fax.clone(), fax: fax.clone(),
}; };
// Pass form data to handle_command (remove &config) // Validate command format
let command = command_input.trim();
if command.is_empty() {
command_message = "Empty command".to_string();
continue;
}
// Look up the action for the command string (e.g., "w")
let action = config.get_action_for_command(command)
.unwrap_or("unknown");
// Pass the resolved action to handle_command
let (should_exit, message) = app_terminal let (should_exit, message) = app_terminal
.handle_command(&command_input, &mut is_saved, &form_data) .handle_command(action, &mut is_saved, &form_data)
.await?; .await?;
command_message = message; command_message = message;
command_mode = false;
command_input.clear();
if should_exit { if should_exit {
return Ok(()); return Ok(());
} }
command_mode = false;
command_input.clear();
} }
KeyCode::Char(c) => command_input.push(c), KeyCode::Char(c) => command_input.push(c),
KeyCode::Backspace => { KeyCode::Backspace => {