Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e0fae26ee | ||
|
|
bee95c3755 | ||
|
|
306f4de14f | ||
|
|
b2fc681e73 | ||
|
|
060cff3f0f | ||
|
|
07c48985e4 | ||
|
|
3d266c2ad0 | ||
|
|
e2c326bf1e | ||
|
|
7f5b671084 | ||
|
|
3ccf71ed0f | ||
|
|
efb59c5571 | ||
|
|
26cf06df14 | ||
|
|
c82b62f72b | ||
|
|
87cdb8048d |
@@ -9,7 +9,6 @@ use crate::config::colors::themes::Theme;
|
|||||||
use crate::state::canvas_state::CanvasState;
|
use crate::state::canvas_state::CanvasState;
|
||||||
use crate::components::handlers::canvas::render_canvas;
|
use crate::components::handlers::canvas::render_canvas;
|
||||||
|
|
||||||
// Original form renderer (keep for backward compatibility)
|
|
||||||
pub fn render_form(
|
pub fn render_form(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
|
|||||||
@@ -1,18 +1,21 @@
|
|||||||
// src/modes/canvas/common.rs
|
// src/modes/canvas/common.rs
|
||||||
|
|
||||||
use crate::config::binds::config::Config;
|
|
||||||
use crate::tui::terminal::core::TerminalCore;
|
use crate::tui::terminal::core::TerminalCore;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::{form::FormState, auth::AuthState};
|
||||||
use crate::state::state::AppState;
|
use crate::state::state::AppState;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::services::auth::AuthClient;
|
||||||
|
use crate::tui::functions::common::{
|
||||||
|
form::{save as form_save, revert},
|
||||||
|
login::{save as login_save, cancel}
|
||||||
|
};
|
||||||
|
|
||||||
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
|
|
||||||
|
|
||||||
/// Main handler for common core actions
|
|
||||||
pub async fn handle_core_action(
|
pub async fn handle_core_action(
|
||||||
action: &str,
|
action: &str,
|
||||||
form_state: &mut FormState,
|
form_state: &mut FormState,
|
||||||
|
auth_state: &mut AuthState,
|
||||||
grpc_client: &mut GrpcClient,
|
grpc_client: &mut GrpcClient,
|
||||||
|
auth_client: &mut AuthClient,
|
||||||
terminal: &mut TerminalCore,
|
terminal: &mut TerminalCore,
|
||||||
app_state: &mut AppState,
|
app_state: &mut AppState,
|
||||||
current_position: &mut u64,
|
current_position: &mut u64,
|
||||||
@@ -20,160 +23,53 @@ pub async fn handle_core_action(
|
|||||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||||
match action {
|
match action {
|
||||||
"save" => {
|
"save" => {
|
||||||
let message = save(
|
if app_state.ui.show_login {
|
||||||
form_state,
|
let message = login_save(auth_state, auth_client, app_state).await?;
|
||||||
grpc_client,
|
Ok((false, message))
|
||||||
&mut app_state.ui.is_saved,
|
} else {
|
||||||
current_position,
|
let message = form_save(
|
||||||
total_count,
|
form_state,
|
||||||
).await?;
|
grpc_client,
|
||||||
Ok((false, message))
|
&mut app_state.ui.is_saved,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
).await?;
|
||||||
|
Ok((false, message))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"force_quit" => {
|
"force_quit" => {
|
||||||
terminal.cleanup()?;
|
terminal.cleanup()?;
|
||||||
Ok((true, "Force exiting without saving.".to_string()))
|
Ok((true, "Force exiting without saving.".to_string()))
|
||||||
},
|
},
|
||||||
"save_and_quit" => {
|
"save_and_quit" => {
|
||||||
let message = save(
|
let message = if app_state.ui.show_login {
|
||||||
form_state,
|
login_save(auth_state, auth_client, app_state).await?
|
||||||
grpc_client,
|
} else {
|
||||||
&mut app_state.ui.is_saved,
|
form_save(
|
||||||
current_position,
|
form_state,
|
||||||
total_count,
|
grpc_client,
|
||||||
).await?;
|
&mut app_state.ui.is_saved,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
).await?
|
||||||
|
};
|
||||||
terminal.cleanup()?;
|
terminal.cleanup()?;
|
||||||
Ok((true, format!("{}. Exiting application.", message)))
|
Ok((true, format!("{}. Exiting application.", message)))
|
||||||
},
|
},
|
||||||
"revert" => {
|
"revert" => {
|
||||||
let message = revert(
|
if app_state.ui.show_login {
|
||||||
form_state,
|
let message = cancel(auth_state, app_state).await;
|
||||||
grpc_client,
|
Ok((false, message))
|
||||||
current_position,
|
} else {
|
||||||
total_count,
|
let message = revert(
|
||||||
).await?;
|
form_state,
|
||||||
Ok((false, message))
|
grpc_client,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
).await?;
|
||||||
|
Ok((false, message))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// We should never hit this case with proper filtering
|
|
||||||
_ => Ok((false, format!("Core action not handled: {}", action))),
|
_ => Ok((false, format!("Core action not handled: {}", action))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function to check if a key event should trigger a core action
|
|
||||||
pub fn is_core_action(config: &Config, key_code: crossterm::event::KeyCode, modifiers: crossterm::event::KeyModifiers) -> Option<String> {
|
|
||||||
// Check for core application actions (save, quit, etc.)
|
|
||||||
if let Some(action) = config.get_action_for_key_in_mode(
|
|
||||||
&config.keybindings.common,
|
|
||||||
key_code,
|
|
||||||
modifiers
|
|
||||||
) {
|
|
||||||
match action {
|
|
||||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
|
||||||
return Some(action.to_string())
|
|
||||||
},
|
|
||||||
_ => {} // Other actions are handled by their respective mode handlers
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Shared logic for saving the current form state
|
|
||||||
pub async fn save(
|
|
||||||
form_state: &mut FormState,
|
|
||||||
grpc_client: &mut GrpcClient,
|
|
||||||
is_saved: &mut bool,
|
|
||||||
current_position: &mut u64,
|
|
||||||
total_count: u64,
|
|
||||||
) -> Result<String, Box<dyn std::error::Error>> {
|
|
||||||
let is_new = *current_position == total_count + 1;
|
|
||||||
|
|
||||||
let message = if is_new {
|
|
||||||
let post_request = PostAdresarRequest {
|
|
||||||
firma: form_state.values[0].clone(),
|
|
||||||
kz: form_state.values[1].clone(),
|
|
||||||
drc: form_state.values[2].clone(),
|
|
||||||
ulica: form_state.values[3].clone(),
|
|
||||||
psc: form_state.values[4].clone(),
|
|
||||||
mesto: form_state.values[5].clone(),
|
|
||||||
stat: form_state.values[6].clone(),
|
|
||||||
banka: form_state.values[7].clone(),
|
|
||||||
ucet: form_state.values[8].clone(),
|
|
||||||
skladm: form_state.values[9].clone(),
|
|
||||||
ico: form_state.values[10].clone(),
|
|
||||||
kontakt: form_state.values[11].clone(),
|
|
||||||
telefon: form_state.values[12].clone(),
|
|
||||||
skladu: form_state.values[13].clone(),
|
|
||||||
fax: form_state.values[14].clone(),
|
|
||||||
};
|
|
||||||
let response = grpc_client.post_adresar(post_request).await?;
|
|
||||||
let new_total = grpc_client.get_adresar_count().await?;
|
|
||||||
*current_position = new_total;
|
|
||||||
form_state.id = response.into_inner().id;
|
|
||||||
"New entry created".to_string()
|
|
||||||
} else {
|
|
||||||
let put_request = PutAdresarRequest {
|
|
||||||
id: form_state.id,
|
|
||||||
firma: form_state.values[0].clone(),
|
|
||||||
kz: form_state.values[1].clone(),
|
|
||||||
drc: form_state.values[2].clone(),
|
|
||||||
ulica: form_state.values[3].clone(),
|
|
||||||
psc: form_state.values[4].clone(),
|
|
||||||
mesto: form_state.values[5].clone(),
|
|
||||||
stat: form_state.values[6].clone(),
|
|
||||||
banka: form_state.values[7].clone(),
|
|
||||||
ucet: form_state.values[8].clone(),
|
|
||||||
skladm: form_state.values[9].clone(),
|
|
||||||
ico: form_state.values[10].clone(),
|
|
||||||
kontakt: form_state.values[11].clone(),
|
|
||||||
telefon: form_state.values[12].clone(),
|
|
||||||
skladu: form_state.values[13].clone(),
|
|
||||||
fax: form_state.values[14].clone(),
|
|
||||||
};
|
|
||||||
let _ = grpc_client.put_adresar(put_request).await?;
|
|
||||||
"Entry updated".to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
*is_saved = true;
|
|
||||||
form_state.has_unsaved_changes = false;
|
|
||||||
Ok(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Discard changes since last save
|
|
||||||
pub async fn revert(
|
|
||||||
form_state: &mut FormState,
|
|
||||||
grpc_client: &mut GrpcClient,
|
|
||||||
current_position: &mut u64,
|
|
||||||
total_count: u64,
|
|
||||||
) -> Result<String, Box<dyn std::error::Error>> {
|
|
||||||
let is_new = *current_position == total_count + 1;
|
|
||||||
|
|
||||||
if is_new {
|
|
||||||
// Clear all fields for new entries
|
|
||||||
form_state.values.iter_mut().for_each(|v| *v = String::new());
|
|
||||||
form_state.has_unsaved_changes = false;
|
|
||||||
return Ok("New entry cleared".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
let data = grpc_client.get_adresar_by_position(*current_position).await?;
|
|
||||||
|
|
||||||
// Update form fields with saved values
|
|
||||||
form_state.values = vec![
|
|
||||||
data.firma,
|
|
||||||
data.kz,
|
|
||||||
data.drc,
|
|
||||||
data.ulica,
|
|
||||||
data.psc,
|
|
||||||
data.mesto,
|
|
||||||
data.stat,
|
|
||||||
data.banka,
|
|
||||||
data.ucet,
|
|
||||||
data.skladm,
|
|
||||||
data.ico,
|
|
||||||
data.kontakt,
|
|
||||||
data.telefon,
|
|
||||||
data.skladu,
|
|
||||||
data.fax,
|
|
||||||
];
|
|
||||||
|
|
||||||
form_state.has_unsaved_changes = false;
|
|
||||||
Ok("Changes discarded, reloaded last saved version".to_string())
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
// src/modes/canvas/edit.rs
|
// src/modes/canvas/edit.rs
|
||||||
|
|
||||||
// TODO THIS is freaking bloated with functions it never uses REFACTOR 200 LOC can be gone
|
// TODO THIS is freaking bloated with functions it never uses REFACTOR 200 LOC can be gone
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
|
use crate::state::canvas_state::CanvasState;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::modes::canvas::common;
|
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::tui::functions::common::form::{save, revert};
|
||||||
|
|
||||||
|
pub async fn handle_edit_event_internal<S: CanvasState + Any>(
|
||||||
pub async fn handle_edit_event_internal(
|
|
||||||
key: KeyEvent,
|
key: KeyEvent,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
form_state: &mut FormState,
|
state: &mut S,
|
||||||
ideal_cursor_column: &mut usize,
|
ideal_cursor_column: &mut usize,
|
||||||
command_message: &mut String,
|
command_message: &mut String,
|
||||||
is_saved: &mut bool,
|
is_saved: &mut bool,
|
||||||
@@ -20,16 +22,14 @@ pub async fn handle_edit_event_internal(
|
|||||||
grpc_client: &mut GrpcClient,
|
grpc_client: &mut GrpcClient,
|
||||||
) -> Result<String, Box<dyn std::error::Error>> {
|
) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(&config.keybindings.global, key.code, key.modifiers) {
|
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(&config.keybindings.global, key.code, key.modifiers) {
|
||||||
// Ignore in edit mode and process as normal input
|
handle_edit_specific_input(key, state, ideal_cursor_column);
|
||||||
handle_edit_specific_input(key, form_state, ideal_cursor_column);
|
|
||||||
return Ok(command_message.clone());
|
return Ok(command_message.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check common actions first
|
|
||||||
if let Some(action) = config.get_action_for_key_in_mode(&config.keybindings.common, key.code, key.modifiers) {
|
if let Some(action) = config.get_action_for_key_in_mode(&config.keybindings.common, key.code, key.modifiers) {
|
||||||
return execute_common_action(
|
return execute_common_action(
|
||||||
action,
|
action,
|
||||||
form_state,
|
state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
is_saved,
|
is_saved,
|
||||||
current_position,
|
current_position,
|
||||||
@@ -40,302 +40,340 @@ pub async fn handle_edit_event_internal(
|
|||||||
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
|
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
|
||||||
return execute_edit_action(
|
return execute_edit_action(
|
||||||
action,
|
action,
|
||||||
form_state,
|
state,
|
||||||
ideal_cursor_column,
|
ideal_cursor_column,
|
||||||
grpc_client, // Changed from AppTerminal
|
grpc_client,
|
||||||
is_saved,
|
is_saved,
|
||||||
current_position,
|
current_position,
|
||||||
total_count,
|
total_count,
|
||||||
).await;
|
).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_edit_specific_input(
|
handle_edit_specific_input(key, state, ideal_cursor_column);
|
||||||
key,
|
|
||||||
form_state,
|
|
||||||
ideal_cursor_column,
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(command_message.clone())
|
Ok(command_message.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn execute_common_action(
|
async fn execute_common_action<S: CanvasState + Any>(
|
||||||
action: &str,
|
action: &str,
|
||||||
form_state: &mut FormState,
|
state: &mut S,
|
||||||
grpc_client: &mut GrpcClient,
|
grpc_client: &mut GrpcClient,
|
||||||
is_saved: &mut bool,
|
is_saved: &mut bool,
|
||||||
current_position: &mut u64,
|
current_position: &mut u64,
|
||||||
total_count: u64,
|
total_count: u64,
|
||||||
) -> Result<String, Box<dyn std::error::Error>> {
|
) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
match action {
|
match action {
|
||||||
"save" => {
|
"save" | "revert" if state.has_unsaved_changes() => {
|
||||||
common::save(
|
if let Some(form_state) = (state as &mut dyn Any).downcast_mut::<FormState>() {
|
||||||
form_state,
|
return match action {
|
||||||
grpc_client,
|
"save" => save(form_state, grpc_client, is_saved, current_position, total_count).await,
|
||||||
is_saved,
|
"revert" => revert(form_state, grpc_client, current_position, total_count).await,
|
||||||
current_position,
|
_ => unreachable!(),
|
||||||
total_count,
|
};
|
||||||
).await
|
}
|
||||||
},
|
Ok("Action not available in this context".to_string())
|
||||||
"revert" => {
|
}
|
||||||
common::revert(
|
|
||||||
form_state,
|
|
||||||
grpc_client,
|
|
||||||
current_position,
|
|
||||||
total_count,
|
|
||||||
).await
|
|
||||||
},
|
|
||||||
"move_up" | "move_down" => {
|
"move_up" | "move_down" => {
|
||||||
// Reuse edit mode's existing logic
|
|
||||||
execute_edit_action(
|
execute_edit_action(
|
||||||
action,
|
action,
|
||||||
form_state,
|
state,
|
||||||
&mut 0, // Dummy ideal_cursor_column (not used here)
|
&mut 0,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
is_saved,
|
is_saved,
|
||||||
current_position,
|
current_position,
|
||||||
total_count,
|
total_count,
|
||||||
).await
|
).await
|
||||||
},
|
}
|
||||||
_ => Ok(format!("Common action not handled: {}", action)),
|
_ => Ok(format!("Common action not handled: {}", action)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_edit_specific_input(
|
fn handle_edit_specific_input<S: CanvasState>( // No Any needed here
|
||||||
key: KeyEvent,
|
key: KeyEvent,
|
||||||
form_state: &mut FormState,
|
state: &mut S,
|
||||||
ideal_cursor_column: &mut usize,
|
ideal_cursor_column: &mut usize,
|
||||||
) {
|
) {
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Char(c) => {
|
KeyCode::Char(c) => {
|
||||||
let cursor_pos = form_state.current_cursor_pos;
|
let cursor_pos = state.current_cursor_pos();
|
||||||
let field_value = form_state.get_current_input_mut();
|
let field_value = state.get_current_input_mut();
|
||||||
let mut chars: Vec<char> = field_value.chars().collect();
|
let mut chars: Vec<char> = field_value.chars().collect();
|
||||||
if cursor_pos <= chars.len() {
|
if cursor_pos <= chars.len() {
|
||||||
chars.insert(cursor_pos, c);
|
chars.insert(cursor_pos, c);
|
||||||
*field_value = chars.into_iter().collect();
|
*field_value = chars.into_iter().collect();
|
||||||
form_state.current_cursor_pos = cursor_pos + 1;
|
// Use trait setters
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
state.set_current_cursor_pos(cursor_pos + 1);
|
||||||
form_state.has_unsaved_changes = true;
|
state.set_has_unsaved_changes(true);
|
||||||
|
*ideal_cursor_column = state.current_cursor_pos(); // Update ideal column
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::Backspace => {
|
KeyCode::Backspace => {
|
||||||
if form_state.current_cursor_pos > 0 {
|
if state.current_cursor_pos() > 0 {
|
||||||
let cursor_pos = form_state.current_cursor_pos;
|
let cursor_pos = state.current_cursor_pos();
|
||||||
let field_value = form_state.get_current_input_mut();
|
let field_value = state.get_current_input_mut();
|
||||||
let mut chars: Vec<char> = field_value.chars().collect();
|
let mut chars: Vec<char> = field_value.chars().collect();
|
||||||
if cursor_pos <= chars.len() && cursor_pos > 0 {
|
if cursor_pos <= chars.len() && cursor_pos > 0 {
|
||||||
chars.remove(cursor_pos - 1);
|
chars.remove(cursor_pos - 1);
|
||||||
*field_value = chars.into_iter().collect();
|
*field_value = chars.into_iter().collect();
|
||||||
form_state.current_cursor_pos = cursor_pos - 1;
|
// Use trait setters
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
state.set_current_cursor_pos(cursor_pos - 1);
|
||||||
form_state.has_unsaved_changes = true;
|
state.set_has_unsaved_changes(true);
|
||||||
|
*ideal_cursor_column = state.current_cursor_pos(); // Update ideal column
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::Delete => {
|
KeyCode::Delete => {
|
||||||
let cursor_pos = form_state.current_cursor_pos;
|
let cursor_pos = state.current_cursor_pos();
|
||||||
let field_value = form_state.get_current_input_mut();
|
let field_value = state.get_current_input_mut();
|
||||||
let chars: Vec<char> = field_value.chars().collect();
|
let chars: Vec<char> = field_value.chars().collect();
|
||||||
if cursor_pos < chars.len() {
|
if cursor_pos < chars.len() {
|
||||||
let mut new_chars = chars.clone();
|
let mut new_chars = chars.clone();
|
||||||
new_chars.remove(cursor_pos);
|
new_chars.remove(cursor_pos);
|
||||||
*field_value = new_chars.into_iter().collect();
|
*field_value = new_chars.into_iter().collect();
|
||||||
form_state.has_unsaved_changes = true;
|
// Use trait setter
|
||||||
|
state.set_has_unsaved_changes(true);
|
||||||
|
// Cursor position doesn't change, but ideal might if text changed
|
||||||
|
*ideal_cursor_column = state.current_cursor_pos();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::Tab => {
|
KeyCode::Tab => {
|
||||||
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
let num_fields = state.fields().len();
|
||||||
if form_state.current_field == 0 {
|
if num_fields > 0 { // Avoid panic on empty fields
|
||||||
form_state.current_field = form_state.fields.len() - 1;
|
let current_field = state.current_field();
|
||||||
|
let new_field = if key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||||
|
if current_field == 0 { num_fields - 1 } else { current_field - 1 }
|
||||||
} else {
|
} else {
|
||||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
(current_field + 1) % num_fields
|
||||||
}
|
};
|
||||||
} else {
|
// Use trait setter
|
||||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_cursor_pos = current_input.len();
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos));
|
||||||
}
|
}
|
||||||
let current_input = form_state.get_current_input();
|
|
||||||
let max_cursor_pos = current_input.len();
|
|
||||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
|
||||||
}
|
}
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
let num_fields = state.fields().len();
|
||||||
let current_input = form_state.get_current_input();
|
if num_fields > 0 { // Avoid panic on empty fields
|
||||||
let max_cursor_pos = current_input.len();
|
let new_field = (state.current_field() + 1) % num_fields;
|
||||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
// Use trait setter
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_cursor_pos = current_input.len();
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn execute_edit_action(
|
async fn execute_edit_action<S: CanvasState>( // No Any needed here
|
||||||
action: &str,
|
action: &str,
|
||||||
form_state: &mut FormState,
|
state: &mut S,
|
||||||
ideal_cursor_column: &mut usize,
|
ideal_cursor_column: &mut usize,
|
||||||
grpc_client: &mut GrpcClient, // Changed from AppTerminal
|
// These parameters are unused if save/revert aren't handled here,
|
||||||
is_saved: &mut bool,
|
// but keep them for now if execute_common_action calls this
|
||||||
current_position: &mut u64,
|
_grpc_client: &mut GrpcClient,
|
||||||
total_count: u64,
|
_is_saved: &mut bool,
|
||||||
|
_current_position: &mut u64,
|
||||||
|
_total_count: u64,
|
||||||
) -> Result<String, Box<dyn std::error::Error>> {
|
) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
match action {
|
match action {
|
||||||
"save" => {
|
|
||||||
common::save(
|
|
||||||
form_state,
|
|
||||||
grpc_client, // Changed from AppTerminal
|
|
||||||
is_saved,
|
|
||||||
current_position,
|
|
||||||
total_count,
|
|
||||||
).await
|
|
||||||
},
|
|
||||||
"move_left" => {
|
"move_left" => {
|
||||||
form_state.current_cursor_pos = form_state.current_cursor_pos.saturating_sub(1);
|
let new_pos = state.current_cursor_pos().saturating_sub(1);
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"move_right" => {
|
"move_right" => {
|
||||||
let current_input = form_state.get_current_input();
|
let current_input = state.get_current_input();
|
||||||
if form_state.current_cursor_pos < current_input.len() {
|
let current_pos = state.current_cursor_pos();
|
||||||
form_state.current_cursor_pos += 1;
|
// Allow moving cursor to position *after* last character
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
if current_pos < current_input.len() {
|
||||||
|
let new_pos = current_pos + 1;
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
}
|
}
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"move_up" => {
|
"move_up" => {
|
||||||
if form_state.current_field == 0 {
|
let num_fields = state.fields().len();
|
||||||
form_state.current_field = form_state.fields.len() - 1;
|
if num_fields > 0 {
|
||||||
} else {
|
let current_field = state.current_field();
|
||||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
let new_field = if current_field == 0 { num_fields - 1 } else { current_field - 1 };
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
}
|
}
|
||||||
let current_input = form_state.get_current_input();
|
|
||||||
let max_cursor_pos = current_input.len();
|
|
||||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"move_down" => {
|
"move_down" => {
|
||||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
let num_fields = state.fields().len();
|
||||||
let current_input = form_state.get_current_input();
|
if num_fields > 0 {
|
||||||
let max_cursor_pos = current_input.len();
|
let new_field = (state.current_field() + 1) % num_fields;
|
||||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
// Use trait setter
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"move_line_start" => {
|
"move_line_start" => {
|
||||||
form_state.current_cursor_pos = 0;
|
// Use trait setter
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
state.set_current_cursor_pos(0);
|
||||||
|
*ideal_cursor_column = 0;
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"move_line_end" => {
|
"move_line_end" => {
|
||||||
let current_input = form_state.get_current_input();
|
let current_input = state.get_current_input();
|
||||||
form_state.current_cursor_pos = current_input.len();
|
let new_pos = current_input.len();
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"move_first_line" => {
|
"move_first_line" => {
|
||||||
form_state.current_field = 0;
|
let num_fields = state.fields().len();
|
||||||
let current_input = form_state.get_current_input();
|
if num_fields > 0 {
|
||||||
let max_cursor_pos = current_input.len();
|
// Use trait setter
|
||||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
state.set_current_field(0);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
Ok("Moved to first line".to_string())
|
Ok("Moved to first line".to_string())
|
||||||
}
|
}
|
||||||
"move_last_line" => {
|
"move_last_line" => {
|
||||||
form_state.current_field = form_state.fields.len() - 1;
|
let num_fields = state.fields().len();
|
||||||
let current_input = form_state.get_current_input();
|
if num_fields > 0 {
|
||||||
let max_cursor_pos = current_input.len();
|
let new_field = num_fields - 1;
|
||||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
// Use trait setter
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
Ok("Moved to last line".to_string())
|
Ok("Moved to last line".to_string())
|
||||||
}
|
}
|
||||||
// Word movement actions
|
|
||||||
"move_word_next" => {
|
"move_word_next" => {
|
||||||
let current_input = form_state.get_current_input();
|
let current_input = state.get_current_input();
|
||||||
if !current_input.is_empty() {
|
if !current_input.is_empty() {
|
||||||
let new_pos = find_next_word_start(current_input, form_state.current_cursor_pos);
|
let new_pos = find_next_word_start(current_input, state.current_cursor_pos());
|
||||||
form_state.current_cursor_pos = new_pos.min(current_input.len());
|
let final_pos = new_pos.min(current_input.len());
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos(final_pos);
|
||||||
|
*ideal_cursor_column = final_pos;
|
||||||
}
|
}
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"move_word_end" => {
|
"move_word_end" => {
|
||||||
let current_input = form_state.get_current_input();
|
let current_input = state.get_current_input();
|
||||||
if !current_input.is_empty() {
|
if !current_input.is_empty() {
|
||||||
let new_pos = find_word_end(current_input, form_state.current_cursor_pos);
|
let new_pos = find_word_end(current_input, state.current_cursor_pos());
|
||||||
form_state.current_cursor_pos = new_pos.min(current_input.len());
|
let final_pos = new_pos.min(current_input.len());
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos(final_pos);
|
||||||
|
*ideal_cursor_column = final_pos;
|
||||||
}
|
}
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"move_word_prev" => {
|
"move_word_prev" => {
|
||||||
let current_input = form_state.get_current_input();
|
let current_input = state.get_current_input();
|
||||||
if !current_input.is_empty() {
|
if !current_input.is_empty() {
|
||||||
let new_pos = find_prev_word_start(current_input, form_state.current_cursor_pos);
|
let new_pos = find_prev_word_start(current_input, state.current_cursor_pos());
|
||||||
form_state.current_cursor_pos = new_pos;
|
// Use trait setter
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
}
|
}
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"move_word_end_prev" => {
|
"move_word_end_prev" => {
|
||||||
let current_input = form_state.get_current_input();
|
let current_input = state.get_current_input();
|
||||||
if !current_input.is_empty() {
|
if !current_input.is_empty() {
|
||||||
let new_pos = find_prev_word_end(current_input, form_state.current_cursor_pos);
|
let new_pos = find_prev_word_end(current_input, state.current_cursor_pos());
|
||||||
form_state.current_cursor_pos = new_pos;
|
// Use trait setter
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
}
|
}
|
||||||
Ok("".to_string())
|
Ok("Moved to previous word end".to_string())
|
||||||
}
|
}
|
||||||
// Edit-specific actions that can be bound to keys
|
|
||||||
"delete_char_forward" => {
|
"delete_char_forward" => {
|
||||||
let cursor_pos = form_state.current_cursor_pos;
|
let cursor_pos = state.current_cursor_pos();
|
||||||
let field_value = form_state.get_current_input_mut();
|
let field_value = state.get_current_input_mut();
|
||||||
let chars: Vec<char> = field_value.chars().collect();
|
let mut chars: Vec<char> = field_value.chars().collect(); // Use mut for modification
|
||||||
if cursor_pos < chars.len() {
|
if cursor_pos < chars.len() {
|
||||||
let mut new_chars = chars.clone();
|
chars.remove(cursor_pos);
|
||||||
new_chars.remove(cursor_pos);
|
*field_value = chars.into_iter().collect();
|
||||||
*field_value = new_chars.into_iter().collect();
|
// Use trait setter
|
||||||
form_state.has_unsaved_changes = true;
|
state.set_has_unsaved_changes(true);
|
||||||
|
// Cursor position doesn't change
|
||||||
|
*ideal_cursor_column = cursor_pos;
|
||||||
}
|
}
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"delete_char_backward" => {
|
"delete_char_backward" => {
|
||||||
if form_state.current_cursor_pos > 0 {
|
if state.current_cursor_pos() > 0 {
|
||||||
let cursor_pos = form_state.current_cursor_pos;
|
let cursor_pos = state.current_cursor_pos();
|
||||||
let field_value = form_state.get_current_input_mut();
|
let field_value = state.get_current_input_mut();
|
||||||
let mut chars: Vec<char> = field_value.chars().collect();
|
let mut chars: Vec<char> = field_value.chars().collect(); // Use mut
|
||||||
if cursor_pos <= chars.len() && cursor_pos > 0 {
|
if cursor_pos <= chars.len() && cursor_pos > 0 {
|
||||||
chars.remove(cursor_pos - 1);
|
chars.remove(cursor_pos - 1);
|
||||||
*field_value = chars.into_iter().collect();
|
*field_value = chars.into_iter().collect();
|
||||||
form_state.current_cursor_pos = cursor_pos - 1;
|
let new_pos = cursor_pos - 1;
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
// Use trait setters
|
||||||
form_state.has_unsaved_changes = true;
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
state.set_has_unsaved_changes(true);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"insert_char" => {
|
"insert_char" => {
|
||||||
// This could be expanded to allow configurable character insertion
|
// This action doesn't make sense here as char insertion is handled
|
||||||
// For now, it's a placeholder that would need additional parameters
|
// directly in handle_edit_specific_input
|
||||||
Ok("Character insertion requires configuration".to_string())
|
Ok("Character insertion handled directly".to_string())
|
||||||
}
|
}
|
||||||
"next_field" => {
|
"next_field" => {
|
||||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
let num_fields = state.fields().len();
|
||||||
let current_input = form_state.get_current_input();
|
if num_fields > 0 {
|
||||||
let max_cursor_pos = current_input.len();
|
let new_field = (state.current_field() + 1) % num_fields;
|
||||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
// Use trait setter
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"prev_field" => {
|
"prev_field" => {
|
||||||
if form_state.current_field == 0 {
|
let num_fields = state.fields().len();
|
||||||
form_state.current_field = form_state.fields.len() - 1;
|
if num_fields > 0 {
|
||||||
} else {
|
let current_field = state.current_field();
|
||||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
let new_field = if current_field == 0 { num_fields - 1 } else { current_field - 1 };
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
// Use trait setter
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
}
|
}
|
||||||
let current_input = form_state.get_current_input();
|
|
||||||
let max_cursor_pos = current_input.len();
|
|
||||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
// Fallback for unrecognized actions
|
_ => Ok(format!("Unknown edit action: {}", action)),
|
||||||
_ => Ok(format!("Unknown action: {}", action)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reuse these character and word navigation helper functions
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum CharType {
|
enum CharType {
|
||||||
Whitespace,
|
Whitespace,
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
// src/modes/canvas/read_only.rs
|
// src/modes/canvas/read_only.rs
|
||||||
|
|
||||||
use crossterm::event::{KeyEvent};
|
use crossterm::event::{KeyEvent};
|
||||||
@@ -187,11 +186,6 @@ async fn execute_action(
|
|||||||
command_message.clear();
|
command_message.clear();
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"exit_edit_mode" => {
|
|
||||||
key_sequence_tracker.reset();
|
|
||||||
command_message.clear();
|
|
||||||
Ok("".to_string())
|
|
||||||
}
|
|
||||||
"move_left" => {
|
"move_left" => {
|
||||||
let current_pos = form_state.current_cursor_pos;
|
let current_pos = form_state.current_cursor_pos;
|
||||||
form_state.current_cursor_pos = current_pos.saturating_sub(1);
|
form_state.current_cursor_pos = current_pos.saturating_sub(1);
|
||||||
|
|||||||
@@ -4,11 +4,9 @@ use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
|||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::tui::controls::commands::CommandHandler;
|
use crate::tui::functions::common::commands::CommandHandler;
|
||||||
use crate::tui::terminal::core::TerminalCore;
|
use crate::tui::terminal::core::TerminalCore;
|
||||||
use crate::modes::{
|
use crate::tui::functions::common::form::{save, revert};
|
||||||
canvas::{common},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub async fn handle_command_event(
|
pub async fn handle_command_event(
|
||||||
key: KeyEvent,
|
key: KeyEvent,
|
||||||
@@ -97,7 +95,7 @@ async fn process_command(
|
|||||||
Ok((should_exit, message, true))
|
Ok((should_exit, message, true))
|
||||||
},
|
},
|
||||||
"save" => {
|
"save" => {
|
||||||
let message = common::save(
|
let message = save(
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
&mut command_handler.is_saved,
|
&mut command_handler.is_saved,
|
||||||
@@ -108,7 +106,7 @@ async fn process_command(
|
|||||||
return Ok((false, message, true));
|
return Ok((false, message, true));
|
||||||
},
|
},
|
||||||
"revert" => {
|
"revert" => {
|
||||||
let message = common::revert(
|
let message = revert(
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ use crate::tui::terminal::{
|
|||||||
core::TerminalCore,
|
core::TerminalCore,
|
||||||
};
|
};
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::tui::controls::commands::CommandHandler;
|
use crate::services::auth::AuthClient;
|
||||||
|
use crate::tui::functions::common::commands::CommandHandler;
|
||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::state::pages::auth::AuthState;
|
use crate::state::pages::auth::AuthState;
|
||||||
@@ -27,11 +28,12 @@ pub struct EventHandler {
|
|||||||
pub ideal_cursor_column: usize,
|
pub ideal_cursor_column: usize,
|
||||||
pub key_sequence_tracker: KeySequenceTracker,
|
pub key_sequence_tracker: KeySequenceTracker,
|
||||||
pub auth_state: AuthState,
|
pub auth_state: AuthState,
|
||||||
|
pub auth_client: AuthClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventHandler {
|
impl EventHandler {
|
||||||
pub fn new() -> Self {
|
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
EventHandler {
|
Ok(EventHandler {
|
||||||
command_mode: false,
|
command_mode: false,
|
||||||
command_input: String::new(),
|
command_input: String::new(),
|
||||||
command_message: String::new(),
|
command_message: String::new(),
|
||||||
@@ -40,7 +42,8 @@ impl EventHandler {
|
|||||||
ideal_cursor_column: 0,
|
ideal_cursor_column: 0,
|
||||||
key_sequence_tracker: KeySequenceTracker::new(800),
|
key_sequence_tracker: KeySequenceTracker::new(800),
|
||||||
auth_state: AuthState::new(),
|
auth_state: AuthState::new(),
|
||||||
}
|
auth_client: AuthClient::new().await?,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_event(
|
pub async fn handle_event(
|
||||||
@@ -131,7 +134,9 @@ impl EventHandler {
|
|||||||
return common::handle_core_action(
|
return common::handle_core_action(
|
||||||
action,
|
action,
|
||||||
form_state,
|
form_state,
|
||||||
|
&mut self.auth_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
|
&mut self.auth_client,
|
||||||
terminal,
|
terminal,
|
||||||
app_state,
|
app_state,
|
||||||
current_position,
|
current_position,
|
||||||
@@ -191,7 +196,9 @@ impl EventHandler {
|
|||||||
return common::handle_core_action(
|
return common::handle_core_action(
|
||||||
action,
|
action,
|
||||||
form_state,
|
form_state,
|
||||||
|
&mut self.auth_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
|
&mut self.auth_client,
|
||||||
terminal,
|
terminal,
|
||||||
app_state,
|
app_state,
|
||||||
current_position,
|
current_position,
|
||||||
@@ -203,17 +210,31 @@ impl EventHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Let edit mode handle its own actions (including navigation from common bindings)
|
// Let edit mode handle its own actions (including navigation from common bindings)
|
||||||
let result = edit::handle_edit_event_internal(
|
let result = if app_state.ui.show_login {
|
||||||
key,
|
edit::handle_edit_event_internal(
|
||||||
config,
|
key,
|
||||||
form_state,
|
config,
|
||||||
&mut self.ideal_cursor_column,
|
&mut self.auth_state, // Use auth_state instead of form_state
|
||||||
&mut self.command_message,
|
&mut self.ideal_cursor_column,
|
||||||
&mut app_state.ui.is_saved,
|
&mut self.command_message,
|
||||||
current_position,
|
&mut app_state.ui.is_saved,
|
||||||
total_count,
|
current_position,
|
||||||
grpc_client,
|
total_count,
|
||||||
).await?;
|
grpc_client,
|
||||||
|
).await?
|
||||||
|
} else {
|
||||||
|
edit::handle_edit_event_internal(
|
||||||
|
key,
|
||||||
|
config,
|
||||||
|
form_state,
|
||||||
|
&mut self.ideal_cursor_column,
|
||||||
|
&mut self.command_message,
|
||||||
|
&mut app_state.ui.is_saved,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
grpc_client,
|
||||||
|
).await?
|
||||||
|
};
|
||||||
|
|
||||||
self.key_sequence_tracker.reset();
|
self.key_sequence_tracker.reset();
|
||||||
return Ok((false, result));
|
return Ok((false, result));
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// src/services/login.rs
|
// src/services/auth.rs
|
||||||
|
|
||||||
use tonic::transport::Channel;
|
use tonic::transport::Channel;
|
||||||
use common::proto::multieko2::auth::{
|
use common::proto::multieko2::auth::{
|
||||||
auth_service_client::AuthServiceClient,
|
auth_service_client::AuthServiceClient,
|
||||||
|
|||||||
@@ -10,40 +10,8 @@ pub trait CanvasState {
|
|||||||
fn get_current_input(&self) -> &str;
|
fn get_current_input(&self) -> &str;
|
||||||
fn get_current_input_mut(&mut self) -> &mut String;
|
fn get_current_input_mut(&mut self) -> &mut String;
|
||||||
fn fields(&self) -> Vec<&str>;
|
fn fields(&self) -> Vec<&str>;
|
||||||
}
|
|
||||||
|
fn set_current_field(&mut self, index: usize);
|
||||||
// Implement for FormState (keep existing form.rs code and add this)
|
fn set_current_cursor_pos(&mut self, pos: usize);
|
||||||
impl CanvasState for FormState {
|
fn set_has_unsaved_changes(&mut self, changed: bool);
|
||||||
fn current_field(&self) -> usize {
|
|
||||||
self.current_field
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_cursor_pos(&self) -> usize {
|
|
||||||
self.current_cursor_pos
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_unsaved_changes(&self) -> bool {
|
|
||||||
self.has_unsaved_changes
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inputs(&self) -> Vec<&String> {
|
|
||||||
self.values.iter().collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_current_input(&self) -> &str {
|
|
||||||
self.values
|
|
||||||
.get(self.current_field)
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_current_input_mut(&mut self) -> &mut String {
|
|
||||||
self.values
|
|
||||||
.get_mut(self.current_field)
|
|
||||||
.expect("Invalid current_field index")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fields(&self) -> Vec<&str> {
|
|
||||||
self.fields.iter().map(|s| s.as_str()).collect()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,4 +67,21 @@ impl CanvasState for AuthState {
|
|||||||
fn fields(&self) -> Vec<&str> {
|
fn fields(&self) -> Vec<&str> {
|
||||||
vec!["Username/Email", "Password"]
|
vec!["Username/Email", "Password"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Implement the setter methods ---
|
||||||
|
fn set_current_field(&mut self, index: usize) {
|
||||||
|
// AuthState only has 2 fields (0 and 1)
|
||||||
|
if index < 2 {
|
||||||
|
self.current_field = index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||||
|
// Optional: Add validation based on current input length if needed
|
||||||
|
self.current_cursor_pos = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_has_unsaved_changes(&mut self, _changed: bool) {
|
||||||
|
// Auth form doesn't track unsaved changes, so do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use ratatui::layout::Rect;
|
use ratatui::layout::Rect;
|
||||||
use ratatui::Frame;
|
use ratatui::Frame;
|
||||||
|
use crate::state::canvas_state::CanvasState;
|
||||||
|
|
||||||
pub struct FormState {
|
pub struct FormState {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
@@ -82,3 +83,54 @@ impl FormState {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CanvasState for FormState {
|
||||||
|
fn current_field(&self) -> usize {
|
||||||
|
self.current_field
|
||||||
|
}
|
||||||
|
|
||||||
|
fn current_cursor_pos(&self) -> usize {
|
||||||
|
self.current_cursor_pos
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_unsaved_changes(&self) -> bool {
|
||||||
|
self.has_unsaved_changes
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inputs(&self) -> Vec<&String> {
|
||||||
|
self.values.iter().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_current_input(&self) -> &str {
|
||||||
|
self.values
|
||||||
|
.get(self.current_field)
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
.unwrap_or("")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_current_input_mut(&mut self) -> &mut String {
|
||||||
|
self.values
|
||||||
|
.get_mut(self.current_field)
|
||||||
|
.expect("Invalid current_field index")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fields(&self) -> Vec<&str> {
|
||||||
|
self.fields.iter().map(|s| s.as_str()).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Implement the setter methods ---
|
||||||
|
fn set_current_field(&mut self, index: usize) {
|
||||||
|
if index < self.fields.len() { // Basic bounds check
|
||||||
|
self.current_field = index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||||
|
// Optional: Add validation based on current input length if needed
|
||||||
|
self.current_cursor_pos = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||||
|
self.has_unsaved_changes = changed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
// src/tui/controls.rs
|
|
||||||
|
|
||||||
pub mod commands;
|
|
||||||
|
|
||||||
pub use commands::*;
|
|
||||||
@@ -4,7 +4,9 @@ pub mod admin;
|
|||||||
pub mod intro;
|
pub mod intro;
|
||||||
pub mod login;
|
pub mod login;
|
||||||
pub mod form;
|
pub mod form;
|
||||||
|
pub mod common;
|
||||||
|
|
||||||
pub use admin::*;
|
pub use admin::*;
|
||||||
pub use intro::*;
|
pub use intro::*;
|
||||||
pub use form::*;
|
pub use form::*;
|
||||||
|
pub use common::*;
|
||||||
|
|||||||
8
client/src/tui/functions/common.rs
Normal file
8
client/src/tui/functions/common.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// src/tui/functions/common.rs
|
||||||
|
pub mod commands;
|
||||||
|
pub mod form;
|
||||||
|
pub mod login;
|
||||||
|
|
||||||
|
pub use commands::*;
|
||||||
|
pub use form::{revert, save as form_save};
|
||||||
|
pub use login::{cancel, save as login_save};
|
||||||
107
client/src/tui/functions/common/form.rs
Normal file
107
client/src/tui/functions/common/form.rs
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
// src/tui/functions/common/form.rs
|
||||||
|
|
||||||
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::state::pages::form::FormState;
|
||||||
|
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
|
||||||
|
|
||||||
|
/// Shared logic for saving the current form state
|
||||||
|
pub async fn save(
|
||||||
|
form_state: &mut FormState,
|
||||||
|
grpc_client: &mut GrpcClient,
|
||||||
|
is_saved: &mut bool,
|
||||||
|
current_position: &mut u64,
|
||||||
|
total_count: u64,
|
||||||
|
) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
|
let is_new = *current_position == total_count + 1;
|
||||||
|
|
||||||
|
let message = if is_new {
|
||||||
|
let post_request = PostAdresarRequest {
|
||||||
|
firma: form_state.values[0].clone(),
|
||||||
|
kz: form_state.values[1].clone(),
|
||||||
|
drc: form_state.values[2].clone(),
|
||||||
|
ulica: form_state.values[3].clone(),
|
||||||
|
psc: form_state.values[4].clone(),
|
||||||
|
mesto: form_state.values[5].clone(),
|
||||||
|
stat: form_state.values[6].clone(),
|
||||||
|
banka: form_state.values[7].clone(),
|
||||||
|
ucet: form_state.values[8].clone(),
|
||||||
|
skladm: form_state.values[9].clone(),
|
||||||
|
ico: form_state.values[10].clone(),
|
||||||
|
kontakt: form_state.values[11].clone(),
|
||||||
|
telefon: form_state.values[12].clone(),
|
||||||
|
skladu: form_state.values[13].clone(),
|
||||||
|
fax: form_state.values[14].clone(),
|
||||||
|
};
|
||||||
|
let response = grpc_client.post_adresar(post_request).await?;
|
||||||
|
let new_total = grpc_client.get_adresar_count().await?;
|
||||||
|
*current_position = new_total;
|
||||||
|
form_state.id = response.into_inner().id;
|
||||||
|
"New entry created".to_string()
|
||||||
|
} else {
|
||||||
|
let put_request = PutAdresarRequest {
|
||||||
|
id: form_state.id,
|
||||||
|
firma: form_state.values[0].clone(),
|
||||||
|
kz: form_state.values[1].clone(),
|
||||||
|
drc: form_state.values[2].clone(),
|
||||||
|
ulica: form_state.values[3].clone(),
|
||||||
|
psc: form_state.values[4].clone(),
|
||||||
|
mesto: form_state.values[5].clone(),
|
||||||
|
stat: form_state.values[6].clone(),
|
||||||
|
banka: form_state.values[7].clone(),
|
||||||
|
ucet: form_state.values[8].clone(),
|
||||||
|
skladm: form_state.values[9].clone(),
|
||||||
|
ico: form_state.values[10].clone(),
|
||||||
|
kontakt: form_state.values[11].clone(),
|
||||||
|
telefon: form_state.values[12].clone(),
|
||||||
|
skladu: form_state.values[13].clone(),
|
||||||
|
fax: form_state.values[14].clone(),
|
||||||
|
};
|
||||||
|
let _ = grpc_client.put_adresar(put_request).await?;
|
||||||
|
"Entry updated".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
*is_saved = true;
|
||||||
|
form_state.has_unsaved_changes = false;
|
||||||
|
Ok(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Discard changes since last save
|
||||||
|
pub async fn revert(
|
||||||
|
form_state: &mut FormState,
|
||||||
|
grpc_client: &mut GrpcClient,
|
||||||
|
current_position: &mut u64,
|
||||||
|
total_count: u64,
|
||||||
|
) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
|
let is_new = *current_position == total_count + 1;
|
||||||
|
|
||||||
|
if is_new {
|
||||||
|
// Clear all fields for new entries
|
||||||
|
form_state.values.iter_mut().for_each(|v| *v = String::new());
|
||||||
|
form_state.has_unsaved_changes = false;
|
||||||
|
return Ok("New entry cleared".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = grpc_client.get_adresar_by_position(*current_position).await?;
|
||||||
|
|
||||||
|
// Update form fields with saved values
|
||||||
|
form_state.values = vec![
|
||||||
|
data.firma,
|
||||||
|
data.kz,
|
||||||
|
data.drc,
|
||||||
|
data.ulica,
|
||||||
|
data.psc,
|
||||||
|
data.mesto,
|
||||||
|
data.stat,
|
||||||
|
data.banka,
|
||||||
|
data.ucet,
|
||||||
|
data.skladm,
|
||||||
|
data.ico,
|
||||||
|
data.kontakt,
|
||||||
|
data.telefon,
|
||||||
|
data.skladu,
|
||||||
|
data.fax,
|
||||||
|
];
|
||||||
|
|
||||||
|
form_state.has_unsaved_changes = false;
|
||||||
|
Ok("Changes discarded, reloaded last saved version".to_string())
|
||||||
|
}
|
||||||
45
client/src/tui/functions/common/login.rs
Normal file
45
client/src/tui/functions/common/login.rs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
// src/tui/functions/common/login.rs
|
||||||
|
use crate::services::auth::AuthClient;
|
||||||
|
use crate::state::pages::auth::AuthState;
|
||||||
|
use crate::state::state::AppState;
|
||||||
|
|
||||||
|
pub async fn save(
|
||||||
|
auth_state: &mut AuthState,
|
||||||
|
auth_client: &mut AuthClient,
|
||||||
|
app_state: &mut AppState,
|
||||||
|
) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
|
let identifier = auth_state.username.clone();
|
||||||
|
let password = auth_state.password.clone();
|
||||||
|
|
||||||
|
match auth_client.login(identifier, password).await {
|
||||||
|
Ok(response) => {
|
||||||
|
auth_state.auth_token = Some(response.access_token);
|
||||||
|
auth_state.user_id = Some(response.user_id);
|
||||||
|
auth_state.role = Some(response.role);
|
||||||
|
auth_state.error_message = None;
|
||||||
|
|
||||||
|
// Update app state to show main interface
|
||||||
|
app_state.ui.show_login = false;
|
||||||
|
app_state.ui.show_form = true;
|
||||||
|
|
||||||
|
Ok("Login successful!".to_string())
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let error_message = format!("Login failed: {}", e);
|
||||||
|
auth_state.error_message = Some(error_message.clone());
|
||||||
|
Ok(error_message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn cancel(
|
||||||
|
auth_state: &mut AuthState,
|
||||||
|
app_state: &mut AppState,
|
||||||
|
) -> String {
|
||||||
|
auth_state.username.clear();
|
||||||
|
auth_state.password.clear();
|
||||||
|
auth_state.error_message = None;
|
||||||
|
app_state.ui.show_login = false;
|
||||||
|
app_state.ui.show_intro = true;
|
||||||
|
"Login canceled".to_string()
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
// src/tui/mod.rs
|
// src/tui/mod.rs
|
||||||
pub mod terminal;
|
pub mod terminal;
|
||||||
pub mod controls;
|
|
||||||
pub mod functions;
|
pub mod functions;
|
||||||
|
|
||||||
|
pub use functions::*;
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
|
|
||||||
use crate::tui::terminal::TerminalCore;
|
use crate::tui::terminal::TerminalCore;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::services::auth::AuthClient;
|
||||||
use crate::services::ui_service::UiService; // Add this import
|
use crate::services::ui_service::UiService; // Add this import
|
||||||
use crate::tui::controls::CommandHandler;
|
|
||||||
use crate::tui::terminal::EventReader;
|
use crate::tui::terminal::EventReader;
|
||||||
|
use crate::tui::functions::common::CommandHandler;
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::ui::handlers::render::render_ui;
|
use crate::ui::handlers::render::render_ui;
|
||||||
@@ -17,6 +18,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let config = Config::load()?;
|
let config = Config::load()?;
|
||||||
let mut terminal = TerminalCore::new()?;
|
let mut terminal = TerminalCore::new()?;
|
||||||
let mut grpc_client = GrpcClient::new().await?;
|
let mut grpc_client = GrpcClient::new().await?;
|
||||||
|
let auth_client = AuthClient::new().await?;
|
||||||
let mut command_handler = CommandHandler::new();
|
let mut command_handler = CommandHandler::new();
|
||||||
let theme = Theme::from_str(&config.colors.theme);
|
let theme = Theme::from_str(&config.colors.theme);
|
||||||
let mut auth_state = AuthState::default();
|
let mut auth_state = AuthState::default();
|
||||||
@@ -31,7 +33,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let mut form_state = FormState::new(column_names);
|
let mut form_state = FormState::new(column_names);
|
||||||
|
|
||||||
// The rest of your UI initialization remains the same
|
// The rest of your UI initialization remains the same
|
||||||
let mut event_handler = EventHandler::new();
|
let mut event_handler = EventHandler::new().await?;
|
||||||
let event_reader = EventReader::new();
|
let event_reader = EventReader::new();
|
||||||
|
|
||||||
// Fetch the total count of Adresar entries
|
// Fetch the total count of Adresar entries
|
||||||
|
|||||||
Reference in New Issue
Block a user