495 lines
18 KiB
Rust
495 lines
18 KiB
Rust
|
|
// src/modes/canvas/read_only.rs
|
|
|
|
use crossterm::event::{KeyEvent};
|
|
use crate::config::binds::config::Config;
|
|
use crate::state::pages::form::FormState;
|
|
use crate::config::binds::key_sequences::KeySequenceTracker;
|
|
use crate::tui::terminal::grpc_client::GrpcClient;
|
|
|
|
#[derive(PartialEq)]
|
|
enum CharType {
|
|
Whitespace,
|
|
Alphanumeric,
|
|
Punctuation,
|
|
}
|
|
|
|
pub async fn handle_read_only_event(
|
|
app_state: &crate::state::state::AppState,
|
|
key: KeyEvent,
|
|
config: &Config,
|
|
form_state: &mut FormState,
|
|
key_sequence_tracker: &mut KeySequenceTracker,
|
|
current_position: &mut u64,
|
|
total_count: u64,
|
|
grpc_client: &mut GrpcClient,
|
|
command_message: &mut String,
|
|
edit_mode_cooldown: &mut bool,
|
|
ideal_cursor_column: &mut usize,
|
|
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
|
// Check for entering Edit mode from Read-Only mode
|
|
if config.is_enter_edit_mode_before(key.code, key.modifiers) {
|
|
*edit_mode_cooldown = true;
|
|
*command_message = "Entering Edit mode".to_string();
|
|
return Ok((false, command_message.clone()));
|
|
}
|
|
|
|
if config.is_enter_edit_mode_after(key.code, key.modifiers) {
|
|
let current_input = form_state.get_current_input();
|
|
if !current_input.is_empty() && form_state.current_cursor_pos < current_input.len() {
|
|
form_state.current_cursor_pos += 1;
|
|
*ideal_cursor_column = form_state.current_cursor_pos;
|
|
}
|
|
*edit_mode_cooldown = true;
|
|
*command_message = "Entering Edit mode (after cursor)".to_string();
|
|
return Ok((false, command_message.clone()));
|
|
}
|
|
|
|
// Handle Read-Only mode keybindings
|
|
if key.modifiers.is_empty() {
|
|
key_sequence_tracker.add_key(key.code);
|
|
let sequence = key_sequence_tracker.get_sequence();
|
|
|
|
// Try to match the current sequence against Read-Only mode bindings
|
|
if let Some(action) = config.matches_key_sequence_generalized(&sequence) {
|
|
let result = if action == "previous_entry" && app_state.ui.show_form {
|
|
crate::tui::functions::form::handle_action(
|
|
action,
|
|
form_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
ideal_cursor_column,
|
|
).await?
|
|
} else {
|
|
execute_action(
|
|
action,
|
|
form_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
current_position,
|
|
total_count,
|
|
grpc_client,
|
|
).await?
|
|
};
|
|
key_sequence_tracker.reset();
|
|
return Ok((false, result));
|
|
}
|
|
|
|
// Check if this might be a prefix of a longer sequence
|
|
if config.is_key_sequence_prefix(&sequence) {
|
|
return Ok((false, command_message.clone()));
|
|
}
|
|
|
|
// Since it's not part of a multi-key sequence, check for a direct action
|
|
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
|
|
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
|
|
let result = if action == "previous_entry" && app_state.ui.show_form {
|
|
crate::tui::functions::form::handle_action(
|
|
action,
|
|
form_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
ideal_cursor_column,
|
|
).await?
|
|
} else {
|
|
execute_action(
|
|
action,
|
|
form_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
current_position,
|
|
total_count,
|
|
grpc_client,
|
|
).await?
|
|
};
|
|
key_sequence_tracker.reset();
|
|
return Ok((false, result));
|
|
}
|
|
}
|
|
} else {
|
|
// If modifiers are pressed, check for direct key bindings
|
|
key_sequence_tracker.reset();
|
|
|
|
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
|
|
let result = if action == "previous_entry" && app_state.ui.show_form {
|
|
crate::tui::functions::form::handle_action(
|
|
action,
|
|
form_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
ideal_cursor_column,
|
|
).await?
|
|
} else {
|
|
execute_action(
|
|
action,
|
|
form_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
current_position,
|
|
total_count,
|
|
grpc_client,
|
|
).await?
|
|
};
|
|
return Ok((false, result));
|
|
}
|
|
}
|
|
|
|
// Show a helpful message when no binding was found
|
|
if !*edit_mode_cooldown {
|
|
let default_key = "i".to_string();
|
|
let edit_key = config.keybindings.read_only.get("enter_edit_mode_before")
|
|
.and_then(|keys| keys.first())
|
|
.unwrap_or(&default_key);
|
|
*command_message = format!("Read-only mode - press {} to edit", edit_key);
|
|
}
|
|
|
|
Ok((false, command_message.clone()))
|
|
}
|
|
|
|
async fn execute_action(
|
|
action: &str,
|
|
form_state: &mut FormState,
|
|
ideal_cursor_column: &mut usize,
|
|
key_sequence_tracker: &mut KeySequenceTracker,
|
|
command_message: &mut String,
|
|
current_position: &mut u64,
|
|
total_count: u64,
|
|
grpc_client: &mut GrpcClient,
|
|
) -> Result<String, Box<dyn std::error::Error>> {
|
|
match action {
|
|
"previous_entry" => {
|
|
let new_position = current_position.saturating_sub(1);
|
|
if new_position >= 1 {
|
|
*current_position = new_position;
|
|
match grpc_client.get_adresar_by_position(*current_position).await {
|
|
Ok(response) => {
|
|
// Replace update_from_response with direct field assignments
|
|
form_state.id = response.id;
|
|
form_state.values = vec![
|
|
response.firma, response.kz, response.drc,
|
|
response.ulica, response.psc, response.mesto,
|
|
response.stat, response.banka, response.ucet,
|
|
response.skladm, response.ico, response.kontakt,
|
|
response.telefon, response.skladu, response.fax,
|
|
];
|
|
let current_input = form_state.get_current_input();
|
|
let max_cursor_pos = if !current_input.is_empty() {
|
|
current_input.len() - 1
|
|
} else { 0 };
|
|
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
|
|
form_state.has_unsaved_changes = false;
|
|
*command_message = format!("Loaded entry {}", *current_position);
|
|
}
|
|
Err(e) => {
|
|
*command_message = format!("Error loading entry: {}", e);
|
|
}
|
|
}
|
|
key_sequence_tracker.reset();
|
|
}
|
|
Ok(command_message.clone())
|
|
}
|
|
"next_entry" => {
|
|
if *current_position <= total_count {
|
|
*current_position += 1;
|
|
if *current_position <= total_count {
|
|
match grpc_client.get_adresar_by_position(*current_position).await {
|
|
Ok(response) => {
|
|
form_state.id = response.id;
|
|
form_state.values = vec![
|
|
response.firma, response.kz, response.drc,
|
|
response.ulica, response.psc, response.mesto,
|
|
response.stat, response.banka, response.ucet,
|
|
response.skladm, response.ico, response.kontakt,
|
|
response.telefon, response.skladu, response.fax,
|
|
];
|
|
let current_input = form_state.get_current_input();
|
|
let max_cursor_pos = if !current_input.is_empty() {
|
|
current_input.len() - 1
|
|
} else { 0 };
|
|
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
|
|
form_state.has_unsaved_changes = false;
|
|
*command_message = format!("Loaded entry {}", *current_position);
|
|
}
|
|
Err(e) => {
|
|
*command_message = format!("Error loading entry: {}", e);
|
|
}
|
|
}
|
|
} else {
|
|
form_state.reset_to_empty();
|
|
form_state.current_field = 0;
|
|
form_state.current_cursor_pos = 0;
|
|
*ideal_cursor_column = 0;
|
|
*command_message = "New entry mode".to_string();
|
|
}
|
|
key_sequence_tracker.reset();
|
|
}
|
|
Ok(command_message.clone())
|
|
}
|
|
"exit_edit_mode" => {
|
|
key_sequence_tracker.reset();
|
|
command_message.clear();
|
|
Ok("".to_string())
|
|
}
|
|
"move_left" => {
|
|
let current_pos = form_state.current_cursor_pos;
|
|
form_state.current_cursor_pos = current_pos.saturating_sub(1);
|
|
*ideal_cursor_column = form_state.current_cursor_pos;
|
|
Ok("".to_string())
|
|
}
|
|
"move_right" => {
|
|
let current_input = form_state.get_current_input();
|
|
let current_pos = form_state.current_cursor_pos;
|
|
if !current_input.is_empty() && current_pos < current_input.len() - 1 {
|
|
form_state.current_cursor_pos += 1;
|
|
*ideal_cursor_column = form_state.current_cursor_pos;
|
|
}
|
|
Ok("".to_string())
|
|
}
|
|
"move_up" => {
|
|
// Change field first
|
|
if form_state.current_field == 0 {
|
|
form_state.current_field = form_state.fields.len() - 1;
|
|
} else {
|
|
form_state.current_field = form_state.current_field.saturating_sub(1);
|
|
}
|
|
|
|
// Get current input AFTER changing field
|
|
let current_input = form_state.get_current_input();
|
|
let max_cursor_pos = if !current_input.is_empty() {
|
|
current_input.len() - 1
|
|
} else {
|
|
0
|
|
};
|
|
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
|
Ok("".to_string())
|
|
}
|
|
"move_down" => {
|
|
// Change field first
|
|
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
|
|
|
// Get current input AFTER changing field
|
|
let current_input = form_state.get_current_input();
|
|
let max_cursor_pos = if !current_input.is_empty() {
|
|
current_input.len() - 1
|
|
} else {
|
|
0
|
|
};
|
|
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
|
Ok("".to_string())
|
|
}
|
|
"move_word_next" => {
|
|
let current_input = form_state.get_current_input();
|
|
if !current_input.is_empty() {
|
|
let new_pos = find_next_word_start(current_input, form_state.current_cursor_pos);
|
|
form_state.current_cursor_pos = new_pos.min(current_input.len().saturating_sub(1));
|
|
*ideal_cursor_column = form_state.current_cursor_pos;
|
|
}
|
|
Ok("".to_string())
|
|
}
|
|
"move_word_end" => {
|
|
let current_input = form_state.get_current_input();
|
|
if !current_input.is_empty() {
|
|
let new_pos = find_word_end(current_input, form_state.current_cursor_pos);
|
|
form_state.current_cursor_pos = new_pos.min(current_input.len().saturating_sub(1));
|
|
*ideal_cursor_column = form_state.current_cursor_pos;
|
|
}
|
|
Ok("".to_string())
|
|
}
|
|
"move_word_prev" => {
|
|
let current_input = form_state.get_current_input();
|
|
if !current_input.is_empty() {
|
|
let new_pos = find_prev_word_start(current_input, form_state.current_cursor_pos);
|
|
form_state.current_cursor_pos = new_pos;
|
|
*ideal_cursor_column = form_state.current_cursor_pos;
|
|
}
|
|
Ok("".to_string())
|
|
}
|
|
"move_word_end_prev" => {
|
|
let current_input = form_state.get_current_input();
|
|
if !current_input.is_empty() {
|
|
let new_pos = find_prev_word_end(current_input, form_state.current_cursor_pos);
|
|
form_state.current_cursor_pos = new_pos;
|
|
*ideal_cursor_column = form_state.current_cursor_pos;
|
|
}
|
|
Ok("Moved to previous word end".to_string())
|
|
}
|
|
"move_line_start" => {
|
|
form_state.current_cursor_pos = 0;
|
|
*ideal_cursor_column = form_state.current_cursor_pos;
|
|
Ok("".to_string())
|
|
}
|
|
"move_line_end" => {
|
|
let current_input = form_state.get_current_input();
|
|
if !current_input.is_empty() {
|
|
form_state.current_cursor_pos = current_input.len() - 1;
|
|
*ideal_cursor_column = form_state.current_cursor_pos;
|
|
}
|
|
Ok("".to_string())
|
|
}
|
|
"move_first_line" => {
|
|
// Change field first
|
|
form_state.current_field = 0;
|
|
|
|
// Get current input AFTER changing field
|
|
let current_input = form_state.get_current_input();
|
|
let max_cursor_pos = if !current_input.is_empty() {
|
|
current_input.len() - 1
|
|
} else {
|
|
current_input.len()
|
|
};
|
|
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
|
Ok("Moved to first line".to_string())
|
|
}
|
|
"move_last_line" => {
|
|
// Change field first
|
|
form_state.current_field = form_state.fields.len() - 1;
|
|
|
|
// Get current input AFTER changing field
|
|
let current_input = form_state.get_current_input();
|
|
let max_cursor_pos = if !current_input.is_empty() {
|
|
current_input.len() - 1
|
|
} else {
|
|
current_input.len()
|
|
};
|
|
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
|
Ok("Moved to last line".to_string())
|
|
}
|
|
_ => Ok(format!("Unknown action: {}", action)),
|
|
}
|
|
}
|
|
|
|
fn get_char_type(c: char) -> CharType {
|
|
if c.is_whitespace() {
|
|
CharType::Whitespace
|
|
} else if c.is_alphanumeric() {
|
|
CharType::Alphanumeric
|
|
} else {
|
|
CharType::Punctuation
|
|
}
|
|
}
|
|
|
|
fn find_next_word_start(text: &str, current_pos: usize) -> usize {
|
|
let chars: Vec<char> = text.chars().collect();
|
|
if chars.is_empty() || current_pos >= chars.len() {
|
|
return current_pos;
|
|
}
|
|
|
|
let mut pos = current_pos;
|
|
let initial_type = get_char_type(chars[pos]);
|
|
while pos < chars.len() && get_char_type(chars[pos]) == initial_type {
|
|
pos += 1;
|
|
}
|
|
|
|
while pos < chars.len() && get_char_type(chars[pos]) == CharType::Whitespace {
|
|
pos += 1;
|
|
}
|
|
|
|
pos
|
|
}
|
|
|
|
fn find_word_end(text: &str, current_pos: usize) -> usize {
|
|
let chars: Vec<char> = text.chars().collect();
|
|
if chars.is_empty() {
|
|
return 0;
|
|
}
|
|
|
|
if current_pos >= chars.len() - 1 {
|
|
return chars.len() - 1;
|
|
}
|
|
|
|
let mut pos = current_pos;
|
|
|
|
if get_char_type(chars[pos]) == CharType::Whitespace {
|
|
while pos < chars.len() && get_char_type(chars[pos]) == CharType::Whitespace {
|
|
pos += 1;
|
|
}
|
|
} else {
|
|
let current_type = get_char_type(chars[pos]);
|
|
if pos + 1 < chars.len() && get_char_type(chars[pos + 1]) != current_type {
|
|
pos += 1;
|
|
while pos < chars.len() && get_char_type(chars[pos]) == CharType::Whitespace {
|
|
pos += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
if pos >= chars.len() {
|
|
return chars.len() - 1;
|
|
}
|
|
|
|
let word_type = get_char_type(chars[pos]);
|
|
while pos + 1 < chars.len() && get_char_type(chars[pos + 1]) == word_type {
|
|
pos += 1;
|
|
}
|
|
|
|
pos
|
|
}
|
|
|
|
fn find_prev_word_start(text: &str, current_pos: usize) -> usize {
|
|
let chars: Vec<char> = text.chars().collect();
|
|
if chars.is_empty() || current_pos == 0 {
|
|
return 0;
|
|
}
|
|
|
|
let mut pos = current_pos.saturating_sub(1);
|
|
|
|
while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
|
pos -= 1;
|
|
}
|
|
|
|
if get_char_type(chars[pos]) != CharType::Whitespace {
|
|
let word_type = get_char_type(chars[pos]);
|
|
while pos > 0 && get_char_type(chars[pos - 1]) == word_type {
|
|
pos -= 1;
|
|
}
|
|
}
|
|
|
|
pos
|
|
}
|
|
|
|
fn find_prev_word_end(text: &str, current_pos: usize) -> usize {
|
|
let chars: Vec<char> = text.chars().collect();
|
|
if chars.is_empty() || current_pos <= 1 {
|
|
return 0;
|
|
}
|
|
|
|
let mut pos = current_pos.saturating_sub(1);
|
|
|
|
while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
|
pos -= 1;
|
|
}
|
|
|
|
if pos > 0 && get_char_type(chars[pos]) != CharType::Whitespace {
|
|
let word_type = get_char_type(chars[pos]);
|
|
|
|
while pos > 0 && get_char_type(chars[pos - 1]) == word_type {
|
|
pos -= 1;
|
|
}
|
|
|
|
while pos > 0 && get_char_type(chars[pos - 1]) == CharType::Whitespace {
|
|
pos -= 1;
|
|
}
|
|
|
|
if pos > 0 {
|
|
pos -= 1;
|
|
let prev_word_type = get_char_type(chars[pos]);
|
|
while pos > 0 && get_char_type(chars[pos - 1]) == prev_word_type {
|
|
pos -= 1;
|
|
}
|
|
|
|
while pos < chars.len() - 1 &&
|
|
get_char_type(chars[pos + 1]) == prev_word_type {
|
|
pos += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
pos
|
|
}
|