moving to config.toml
This commit is contained in:
53
src/client/config.rs
Normal file
53
src/client/config.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
// src/client/config.rs
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use crossterm::event::{KeyCode, KeyModifiers};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
pub keybindings: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let config_str = std::fs::read_to_string("config.toml")?;
|
||||
let config: Config = toml::from_str(&config_str)?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn get_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
for (action, bindings) in &self.keybindings {
|
||||
for binding in bindings {
|
||||
if Self::matches_keybinding(binding, key, modifiers) {
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn matches_keybinding(binding: &str, key: KeyCode, modifiers: KeyModifiers) -> bool {
|
||||
let parts: Vec<&str> = binding.split('+').collect();
|
||||
let mut expected_modifiers = KeyModifiers::empty();
|
||||
let mut expected_key = None;
|
||||
|
||||
for part in parts {
|
||||
match part.to_lowercase().as_str() {
|
||||
"ctrl" => expected_modifiers |= KeyModifiers::CONTROL,
|
||||
"shift" => expected_modifiers |= KeyModifiers::SHIFT,
|
||||
"alt" => expected_modifiers |= KeyModifiers::ALT,
|
||||
_ => {
|
||||
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(':')),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
modifiers == expected_modifiers && Some(key) == expected_key
|
||||
}
|
||||
}
|
||||
@@ -3,5 +3,7 @@ mod ui;
|
||||
mod colors;
|
||||
mod components;
|
||||
mod terminal;
|
||||
mod config;
|
||||
|
||||
pub use ui::run_ui;
|
||||
pub use config::Config;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// src/client/terminal.rs
|
||||
use crossterm::event::{self, Event};
|
||||
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
|
||||
use crossterm::{
|
||||
execute,
|
||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||
@@ -8,6 +8,7 @@ use ratatui::{backend::CrosstermBackend, Terminal};
|
||||
use std::io::{self, stdout};
|
||||
use tonic::transport::Channel;
|
||||
use crate::proto::multieko2::adresar_client::AdresarClient;
|
||||
use crate::client::config::Config;
|
||||
use crate::proto::multieko2::AdresarRequest;
|
||||
|
||||
pub struct AppTerminal {
|
||||
@@ -49,12 +50,12 @@ impl AppTerminal {
|
||||
|
||||
pub async fn handle_command(
|
||||
&mut self,
|
||||
command_input: &str,
|
||||
action: &str,
|
||||
is_saved: &mut bool,
|
||||
form_data: &AdresarRequest, // Pass form data here
|
||||
form_data: &AdresarRequest,
|
||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
match command_input {
|
||||
"w" => {
|
||||
match action {
|
||||
"save" => {
|
||||
// Send data to the server
|
||||
let request = tonic::Request::new(form_data.clone());
|
||||
let response = self.grpc_client.create_adresar(request).await?;
|
||||
@@ -62,7 +63,7 @@ impl AppTerminal {
|
||||
*is_saved = true;
|
||||
Ok((false, format!("State saved. Response: {:?}", response)))
|
||||
}
|
||||
"q" => {
|
||||
"quit" => {
|
||||
if *is_saved {
|
||||
self.cleanup()?;
|
||||
Ok((true, "Exiting.".to_string()))
|
||||
@@ -70,16 +71,16 @@ impl AppTerminal {
|
||||
Ok((false, "No changes saved. Use :q! to force quit.".to_string()))
|
||||
}
|
||||
}
|
||||
"q!" => {
|
||||
"force_quit" => {
|
||||
self.cleanup()?;
|
||||
Ok((true, "Force exiting without saving.".to_string()))
|
||||
}
|
||||
"wq" => {
|
||||
"save_and_quit" => {
|
||||
*is_saved = true;
|
||||
self.cleanup()?;
|
||||
Ok((true, "State saved. Exiting.".to_string()))
|
||||
}
|
||||
_ => Ok((false, format!("Command not recognized: {}", command_input))),
|
||||
_ => Ok((false, format!("Action not recognized: {}", action))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
146
src/client/ui.rs
146
src/client/ui.rs
@@ -3,11 +3,13 @@ use crossterm::event::{Event, KeyCode, KeyModifiers};
|
||||
use crate::client::terminal::AppTerminal;
|
||||
use crate::client::components::{render_command_line, render_form, render_preview_card, render_status_line};
|
||||
use crate::client::colors::Theme;
|
||||
use crate::client::config::Config;
|
||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||
use std::env;
|
||||
use crate::proto::multieko2::AdresarRequest;
|
||||
|
||||
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::load()?;
|
||||
let mut app_terminal = AppTerminal::new().await?;
|
||||
let mut command_mode = false;
|
||||
let mut command_input = String::new();
|
||||
@@ -118,8 +120,10 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
fax: fax.clone(),
|
||||
};
|
||||
|
||||
// Pass form data to handle_command
|
||||
let (should_exit, message) = app_terminal.handle_command(&command_input, &mut is_saved, &form_data).await?;
|
||||
// Pass form data to handle_command (remove &config)
|
||||
let (should_exit, message) = app_terminal
|
||||
.handle_command(&command_input, &mut is_saved, &form_data)
|
||||
.await?;
|
||||
command_message = message;
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
@@ -139,64 +143,94 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
match key.code {
|
||||
KeyCode::Char(':') => {
|
||||
command_mode = true;
|
||||
command_input.clear();
|
||||
command_message.clear();
|
||||
// Check for keybindings
|
||||
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
|
||||
let form_data = AdresarRequest {
|
||||
firma: firma.clone(),
|
||||
kz: kz.clone(),
|
||||
drc: drc.clone(),
|
||||
ulica: ulica.clone(),
|
||||
psc: psc.clone(),
|
||||
mesto: mesto.clone(),
|
||||
stat: stat.clone(),
|
||||
banka: banka.clone(),
|
||||
ucet: ucet.clone(),
|
||||
skladm: skladm.clone(),
|
||||
ico: ico.clone(),
|
||||
kontakt: kontakt.clone(),
|
||||
telefon: telefon.clone(),
|
||||
skladu: skladu.clone(),
|
||||
fax: fax.clone(),
|
||||
};
|
||||
|
||||
// Pass form data to handle_command (remove &config)
|
||||
let (should_exit, message) = app_terminal
|
||||
.handle_command(action, &mut is_saved, &form_data)
|
||||
.await?;
|
||||
command_message = message;
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
}
|
||||
KeyCode::Tab => {
|
||||
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||
current_field = current_field.saturating_sub(1);
|
||||
} else {
|
||||
current_field = (current_field + 1) % fields.len();
|
||||
} else {
|
||||
match key.code {
|
||||
KeyCode::Char(':') => {
|
||||
command_mode = true;
|
||||
command_input.clear();
|
||||
command_message.clear();
|
||||
}
|
||||
}
|
||||
KeyCode::BackTab => current_field = current_field.saturating_sub(1),
|
||||
KeyCode::Down => current_field = (current_field + 1) % fields.len(),
|
||||
KeyCode::Up => current_field = current_field.saturating_sub(1),
|
||||
KeyCode::Enter => current_field = (current_field + 1) % fields.len(),
|
||||
KeyCode::Char(c) => {
|
||||
match current_field {
|
||||
0 => firma.push(c),
|
||||
1 => kz.push(c),
|
||||
2 => drc.push(c),
|
||||
3 => ulica.push(c),
|
||||
4 => psc.push(c),
|
||||
5 => mesto.push(c),
|
||||
6 => stat.push(c),
|
||||
7 => banka.push(c),
|
||||
8 => ucet.push(c),
|
||||
9 => skladm.push(c),
|
||||
10 => ico.push(c),
|
||||
11 => kontakt.push(c),
|
||||
12 => telefon.push(c),
|
||||
13 => skladu.push(c),
|
||||
14 => fax.push(c),
|
||||
_ => (),
|
||||
KeyCode::Tab => {
|
||||
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||
current_field = current_field.saturating_sub(1);
|
||||
} else {
|
||||
current_field = (current_field + 1) % fields.len();
|
||||
}
|
||||
}
|
||||
KeyCode::BackTab => current_field = current_field.saturating_sub(1),
|
||||
KeyCode::Down => current_field = (current_field + 1) % fields.len(),
|
||||
KeyCode::Up => current_field = current_field.saturating_sub(1),
|
||||
KeyCode::Enter => current_field = (current_field + 1) % fields.len(),
|
||||
KeyCode::Char(c) => {
|
||||
match current_field {
|
||||
0 => firma.push(c),
|
||||
1 => kz.push(c),
|
||||
2 => drc.push(c),
|
||||
3 => ulica.push(c),
|
||||
4 => psc.push(c),
|
||||
5 => mesto.push(c),
|
||||
6 => stat.push(c),
|
||||
7 => banka.push(c),
|
||||
8 => ucet.push(c),
|
||||
9 => skladm.push(c),
|
||||
10 => ico.push(c),
|
||||
11 => kontakt.push(c),
|
||||
12 => telefon.push(c),
|
||||
13 => skladu.push(c),
|
||||
14 => fax.push(c),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
match current_field {
|
||||
0 => firma.pop(),
|
||||
1 => kz.pop(),
|
||||
2 => drc.pop(),
|
||||
3 => ulica.pop(),
|
||||
4 => psc.pop(),
|
||||
5 => mesto.pop(),
|
||||
6 => stat.pop(),
|
||||
7 => banka.pop(),
|
||||
8 => ucet.pop(),
|
||||
9 => skladm.pop(),
|
||||
10 => ico.pop(),
|
||||
11 => kontakt.pop(),
|
||||
12 => telefon.pop(),
|
||||
13 => skladu.pop(),
|
||||
14 => fax.pop(),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
match current_field {
|
||||
0 => firma.pop(),
|
||||
1 => kz.pop(),
|
||||
2 => drc.pop(),
|
||||
3 => ulica.pop(),
|
||||
4 => psc.pop(),
|
||||
5 => mesto.pop(),
|
||||
6 => stat.pop(),
|
||||
7 => banka.pop(),
|
||||
8 => ucet.pop(),
|
||||
9 => skladm.pop(),
|
||||
10 => ico.pop(),
|
||||
11 => kontakt.pop(),
|
||||
12 => telefon.pop(),
|
||||
13 => skladu.pop(),
|
||||
14 => fax.pop(),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user