Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7fd1c1e41d | ||
|
|
1eaa716f42 | ||
|
|
704bb7401a | ||
|
|
b0c3fdbdc6 | ||
|
|
1d0ceb7045 | ||
|
|
ce85a050d3 | ||
|
|
94ecbcd639 | ||
|
|
ef1d8bcc9c | ||
|
|
1a7bbdc541 | ||
|
|
3dc5a89459 | ||
|
|
9f6268dbc9 | ||
|
|
2e912dd261 | ||
|
|
6d6fff474f | ||
|
|
e725c70570 | ||
|
|
07aeecbbfc | ||
|
|
9bfac04c44 | ||
|
|
6acf0d6378 | ||
|
|
1a09624242 | ||
|
|
9e36385e63 | ||
|
|
43edadde0c | ||
|
|
d577ff6715 | ||
|
|
ca75c90ee9 | ||
|
|
8e0fae26ee | ||
|
|
bee95c3755 | ||
|
|
306f4de14f | ||
|
|
b2fc681e73 | ||
|
|
060cff3f0f | ||
|
|
07c48985e4 | ||
|
|
3d266c2ad0 | ||
|
|
e2c326bf1e | ||
|
|
7f5b671084 | ||
|
|
3ccf71ed0f | ||
|
|
efb59c5571 | ||
|
|
26cf06df14 | ||
|
|
c82b62f72b | ||
|
|
87cdb8048d | ||
|
|
f71498703a | ||
|
|
94eea47b76 | ||
|
|
ac833294c4 | ||
|
|
81f0527085 |
@@ -10,8 +10,7 @@ use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect, Margin},
|
||||
style::{Style, Modifier, Color}, // Removed unused Color import
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
text::Line, // Removed unused Span import
|
||||
Frame, // Removed unused Span import
|
||||
};
|
||||
|
||||
pub fn render_login(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/components/common/dialog.rs
|
||||
use ratatui::{
|
||||
layout::{Constraint, Direction, Layout, Rect, Margin},
|
||||
style::{Color, Modifier, Style},
|
||||
style::{Modifier, Style},
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
text::{Text, Line, Span}
|
||||
|
||||
@@ -9,7 +9,6 @@ use crate::config::colors::themes::Theme;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use crate::components::handlers::canvas::render_canvas;
|
||||
|
||||
// Original form renderer (keep for backward compatibility)
|
||||
pub fn render_form(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
|
||||
2
client/src/functions/common.rs
Normal file
2
client/src/functions/common.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
// src/functions/common.rs
|
||||
|
||||
7
client/src/functions/mod.rs
Normal file
7
client/src/functions/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
// src/functions/mod.rs
|
||||
|
||||
pub mod common;
|
||||
pub mod modes;
|
||||
|
||||
pub use common::*;
|
||||
pub use modes::*;
|
||||
8
client/src/functions/modes.rs
Normal file
8
client/src/functions/modes.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
// src/functions/modes.rs
|
||||
|
||||
pub mod read_only;
|
||||
pub mod edit;
|
||||
|
||||
|
||||
pub use read_only::*;
|
||||
pub use edit::*;
|
||||
4
client/src/functions/modes/edit.rs
Normal file
4
client/src/functions/modes/edit.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
// src/functions/modes/edit.rs
|
||||
|
||||
pub mod form_e;
|
||||
pub mod auth_e;
|
||||
431
client/src/functions/modes/edit/auth_e.rs
Normal file
431
client/src/functions/modes/edit/auth_e.rs
Normal file
@@ -0,0 +1,431 @@
|
||||
// src/functions/modes/edit/auth_e.rs
|
||||
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::tui::functions::common::form::{revert, save};
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use std::any::Any;
|
||||
|
||||
pub async fn execute_common_action<S: CanvasState + Any>(
|
||||
action: &str,
|
||||
state: &mut S,
|
||||
grpc_client: &mut GrpcClient,
|
||||
is_saved: &mut bool,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"save" | "revert" => {
|
||||
if !state.has_unsaved_changes() {
|
||||
return Ok("No changes to save or revert.".to_string());
|
||||
}
|
||||
if let Some(form_state) =
|
||||
(state as &mut dyn Any).downcast_mut::<FormState>()
|
||||
{
|
||||
match action {
|
||||
"save" => {
|
||||
save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
)
|
||||
.await
|
||||
}
|
||||
"revert" => {
|
||||
revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count,
|
||||
)
|
||||
.await
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
} else {
|
||||
Ok(format!(
|
||||
"Action '{}' not implemented for this state type.",
|
||||
action
|
||||
))
|
||||
}
|
||||
}
|
||||
_ => Ok(format!("Common action '{}' not handled here.", action)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_edit_action<S: CanvasState>(
|
||||
action: &str,
|
||||
key: KeyEvent,
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
_grpc_client: &mut GrpcClient,
|
||||
_is_saved: &mut bool,
|
||||
_current_position: &mut u64,
|
||||
_total_count: u64,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"insert_char" => {
|
||||
if let KeyCode::Char(c) = key.code {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let field_value = state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() {
|
||||
chars.insert(cursor_pos, c);
|
||||
*field_value = chars.into_iter().collect();
|
||||
state.set_current_cursor_pos(cursor_pos + 1);
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = state.current_cursor_pos();
|
||||
}
|
||||
} else {
|
||||
return Ok("Error: insert_char called without a char key."
|
||||
.to_string());
|
||||
}
|
||||
Ok("working?".to_string())
|
||||
}
|
||||
|
||||
"delete_char_backward" => {
|
||||
if state.current_cursor_pos() > 0 {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let field_value = state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() {
|
||||
chars.remove(cursor_pos - 1);
|
||||
*field_value = chars.into_iter().collect();
|
||||
let new_pos = cursor_pos - 1;
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"delete_char_forward" => {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let field_value = state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos < chars.len() {
|
||||
chars.remove(cursor_pos);
|
||||
*field_value = chars.into_iter().collect();
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = cursor_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"next_field" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let current_field = state.current_field();
|
||||
let new_field = (current_field + 1) % num_fields;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"prev_field" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let current_field = state.current_field();
|
||||
let new_field = if current_field == 0 {
|
||||
num_fields - 1
|
||||
} else {
|
||||
current_field - 1
|
||||
};
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_left" => {
|
||||
let new_pos = state.current_cursor_pos().saturating_sub(1);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_right" => {
|
||||
let current_input = state.get_current_input();
|
||||
let current_pos = state.current_cursor_pos();
|
||||
if current_pos < current_input.len() {
|
||||
let new_pos = current_pos + 1;
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_up" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let current_field = state.current_field();
|
||||
let new_field = if current_field == 0 {
|
||||
num_fields - 1
|
||||
} else {
|
||||
current_field - 1
|
||||
};
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_down" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let new_field = (state.current_field() + 1) % num_fields;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_line_start" => {
|
||||
state.set_current_cursor_pos(0);
|
||||
*ideal_cursor_column = 0;
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_line_end" => {
|
||||
let current_input = state.get_current_input();
|
||||
let new_pos = current_input.len();
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_first_line" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
state.set_current_field(0);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("Moved to first field".to_string())
|
||||
}
|
||||
|
||||
"move_last_line" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let new_field = num_fields - 1;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("Moved to last field".to_string())
|
||||
}
|
||||
|
||||
"move_word_next" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_next_word_start(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
let final_pos = new_pos.min(current_input.len());
|
||||
state.set_current_cursor_pos(final_pos);
|
||||
*ideal_cursor_column = final_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_word_end" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let current_pos = state.current_cursor_pos();
|
||||
let new_pos = find_word_end(current_input, current_pos);
|
||||
|
||||
let final_pos = if new_pos == current_pos {
|
||||
find_word_end(current_input, new_pos + 1)
|
||||
} else {
|
||||
new_pos
|
||||
};
|
||||
|
||||
let max_valid_index = current_input.len().saturating_sub(1);
|
||||
let clamped_pos = final_pos.min(max_valid_index);
|
||||
state.set_current_cursor_pos(clamped_pos);
|
||||
*ideal_cursor_column = clamped_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_word_prev" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_prev_word_start(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_word_end_prev" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_prev_word_end(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("Moved to previous word end".to_string())
|
||||
}
|
||||
|
||||
_ => Ok(format!("Unknown or unhandled edit action: {}", action)),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum CharType {
|
||||
Whitespace,
|
||||
Alphanumeric,
|
||||
Punctuation,
|
||||
}
|
||||
|
||||
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();
|
||||
let len = chars.len();
|
||||
if len == 0 || current_pos >= len {
|
||||
return len;
|
||||
}
|
||||
|
||||
let mut pos = current_pos;
|
||||
let initial_type = get_char_type(chars[pos]);
|
||||
|
||||
while pos < len && get_char_type(chars[pos]) == initial_type {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
while pos < 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();
|
||||
let len = chars.len();
|
||||
if len == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut pos = current_pos.min(len - 1);
|
||||
|
||||
if get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
pos = find_next_word_start(text, pos);
|
||||
}
|
||||
|
||||
if pos >= len {
|
||||
return len.saturating_sub(1);
|
||||
}
|
||||
|
||||
let word_type = get_char_type(chars[pos]);
|
||||
while pos < len && get_char_type(chars[pos]) == word_type {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
pos.saturating_sub(1).min(len.saturating_sub(1))
|
||||
}
|
||||
|
||||
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 pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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();
|
||||
let len = chars.len();
|
||||
if len == 0 || 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 pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
if pos == 0 && get_char_type(chars[pos]) != CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
431
client/src/functions/modes/edit/form_e.rs
Normal file
431
client/src/functions/modes/edit/form_e.rs
Normal file
@@ -0,0 +1,431 @@
|
||||
// src/functions/modes/edit/form_e.rs
|
||||
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::tui::functions::common::form::{revert, save};
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use std::any::Any;
|
||||
|
||||
pub async fn execute_common_action<S: CanvasState + Any>(
|
||||
action: &str,
|
||||
state: &mut S,
|
||||
grpc_client: &mut GrpcClient,
|
||||
is_saved: &mut bool,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"save" | "revert" => {
|
||||
if !state.has_unsaved_changes() {
|
||||
return Ok("No changes to save or revert.".to_string());
|
||||
}
|
||||
if let Some(form_state) =
|
||||
(state as &mut dyn Any).downcast_mut::<FormState>()
|
||||
{
|
||||
match action {
|
||||
"save" => {
|
||||
save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
)
|
||||
.await
|
||||
}
|
||||
"revert" => {
|
||||
revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count,
|
||||
)
|
||||
.await
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
} else {
|
||||
Ok(format!(
|
||||
"Action '{}' not implemented for this state type.",
|
||||
action
|
||||
))
|
||||
}
|
||||
}
|
||||
_ => Ok(format!("Common action '{}' not handled here.", action)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_edit_action<S: CanvasState>(
|
||||
action: &str,
|
||||
key: KeyEvent,
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
_grpc_client: &mut GrpcClient,
|
||||
_is_saved: &mut bool,
|
||||
_current_position: &mut u64,
|
||||
_total_count: u64,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"insert_char" => {
|
||||
if let KeyCode::Char(c) = key.code {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let field_value = state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() {
|
||||
chars.insert(cursor_pos, c);
|
||||
*field_value = chars.into_iter().collect();
|
||||
state.set_current_cursor_pos(cursor_pos + 1);
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = state.current_cursor_pos();
|
||||
}
|
||||
} else {
|
||||
return Ok("Error: insert_char called without a char key."
|
||||
.to_string());
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"delete_char_backward" => {
|
||||
if state.current_cursor_pos() > 0 {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let field_value = state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() {
|
||||
chars.remove(cursor_pos - 1);
|
||||
*field_value = chars.into_iter().collect();
|
||||
let new_pos = cursor_pos - 1;
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"delete_char_forward" => {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let field_value = state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos < chars.len() {
|
||||
chars.remove(cursor_pos);
|
||||
*field_value = chars.into_iter().collect();
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = cursor_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"next_field" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let current_field = state.current_field();
|
||||
let new_field = (current_field + 1) % num_fields;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"prev_field" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let current_field = state.current_field();
|
||||
let new_field = if current_field == 0 {
|
||||
num_fields - 1
|
||||
} else {
|
||||
current_field - 1
|
||||
};
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_left" => {
|
||||
let new_pos = state.current_cursor_pos().saturating_sub(1);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_right" => {
|
||||
let current_input = state.get_current_input();
|
||||
let current_pos = state.current_cursor_pos();
|
||||
if current_pos < current_input.len() {
|
||||
let new_pos = current_pos + 1;
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_up" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let current_field = state.current_field();
|
||||
let new_field = if current_field == 0 {
|
||||
num_fields - 1
|
||||
} else {
|
||||
current_field - 1
|
||||
};
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_down" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let new_field = (state.current_field() + 1) % num_fields;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_line_start" => {
|
||||
state.set_current_cursor_pos(0);
|
||||
*ideal_cursor_column = 0;
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_line_end" => {
|
||||
let current_input = state.get_current_input();
|
||||
let new_pos = current_input.len();
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_first_line" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
state.set_current_field(0);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("Moved to first field".to_string())
|
||||
}
|
||||
|
||||
"move_last_line" => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let new_field = num_fields - 1;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos(
|
||||
(*ideal_cursor_column).min(max_pos),
|
||||
);
|
||||
}
|
||||
Ok("Moved to last field".to_string())
|
||||
}
|
||||
|
||||
"move_word_next" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_next_word_start(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
let final_pos = new_pos.min(current_input.len());
|
||||
state.set_current_cursor_pos(final_pos);
|
||||
*ideal_cursor_column = final_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_word_end" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let current_pos = state.current_cursor_pos();
|
||||
let new_pos = find_word_end(current_input, current_pos);
|
||||
|
||||
let final_pos = if new_pos == current_pos {
|
||||
find_word_end(current_input, new_pos + 1)
|
||||
} else {
|
||||
new_pos
|
||||
};
|
||||
|
||||
let max_valid_index = current_input.len().saturating_sub(1);
|
||||
let clamped_pos = final_pos.min(max_valid_index);
|
||||
state.set_current_cursor_pos(clamped_pos);
|
||||
*ideal_cursor_column = clamped_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_word_prev" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_prev_word_start(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
||||
"move_word_end_prev" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_prev_word_end(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("Moved to previous word end".to_string())
|
||||
}
|
||||
|
||||
_ => Ok(format!("Unknown or unhandled edit action: {}", action)),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum CharType {
|
||||
Whitespace,
|
||||
Alphanumeric,
|
||||
Punctuation,
|
||||
}
|
||||
|
||||
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();
|
||||
let len = chars.len();
|
||||
if len == 0 || current_pos >= len {
|
||||
return len;
|
||||
}
|
||||
|
||||
let mut pos = current_pos;
|
||||
let initial_type = get_char_type(chars[pos]);
|
||||
|
||||
while pos < len && get_char_type(chars[pos]) == initial_type {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
while pos < 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();
|
||||
let len = chars.len();
|
||||
if len == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut pos = current_pos.min(len - 1);
|
||||
|
||||
if get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
pos = find_next_word_start(text, pos);
|
||||
}
|
||||
|
||||
if pos >= len {
|
||||
return len.saturating_sub(1);
|
||||
}
|
||||
|
||||
let word_type = get_char_type(chars[pos]);
|
||||
while pos < len && get_char_type(chars[pos]) == word_type {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
pos.saturating_sub(1).min(len.saturating_sub(1))
|
||||
}
|
||||
|
||||
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 pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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();
|
||||
let len = chars.len();
|
||||
if len == 0 || 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 pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
if pos == 0 && get_char_type(chars[pos]) != CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
4
client/src/functions/modes/read_only.rs
Normal file
4
client/src/functions/modes/read_only.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
// src/functions/modes/read_only.rs
|
||||
|
||||
pub mod auth_ro;
|
||||
pub mod form_ro;
|
||||
358
client/src/functions/modes/read_only/auth_ro.rs
Normal file
358
client/src/functions/modes/read_only/auth_ro.rs
Normal file
@@ -0,0 +1,358 @@
|
||||
// src/functions/modes/read_only/auth_ro.rs
|
||||
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum CharType {
|
||||
Whitespace,
|
||||
Alphanumeric,
|
||||
Punctuation,
|
||||
}
|
||||
|
||||
pub async fn execute_action<S: CanvasState>(
|
||||
action: &str,
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
key_sequence_tracker: &mut KeySequenceTracker,
|
||||
command_message: &mut String,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
match action {
|
||||
"previous_entry" | "next_entry" => {
|
||||
key_sequence_tracker.reset();
|
||||
Ok(format!(
|
||||
"Action '{}' should be handled by context-specific logic",
|
||||
action
|
||||
))
|
||||
}
|
||||
"move_up" => {
|
||||
key_sequence_tracker.reset();
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields == 0 {
|
||||
return Ok("No fields to navigate.".to_string());
|
||||
}
|
||||
let current_field = state.current_field();
|
||||
let new_field = if current_field == 0 {
|
||||
num_fields - 1
|
||||
} else {
|
||||
current_field - 1
|
||||
};
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos = if current_input.is_empty() {
|
||||
0
|
||||
} else {
|
||||
current_input.len().saturating_sub(1)
|
||||
};
|
||||
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
|
||||
Ok("move up from functions/modes/read_only/auth_ro.rs".to_string())
|
||||
}
|
||||
"move_down" => {
|
||||
key_sequence_tracker.reset();
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields == 0 {
|
||||
return Ok("No fields to navigate.".to_string());
|
||||
}
|
||||
let current_field = state.current_field();
|
||||
let new_field = (current_field + 1) % num_fields;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos = if current_input.is_empty() {
|
||||
0
|
||||
} else {
|
||||
current_input.len().saturating_sub(1)
|
||||
};
|
||||
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
Ok("move down from functions/modes/read_only/auth_ro.rs".to_string())
|
||||
}
|
||||
"move_first_line" => {
|
||||
key_sequence_tracker.reset();
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields == 0 {
|
||||
return Ok("No fields to navigate to.".to_string());
|
||||
}
|
||||
state.set_current_field(0);
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos = if current_input.is_empty() {
|
||||
0
|
||||
} else {
|
||||
current_input.len().saturating_sub(1)
|
||||
};
|
||||
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_last_line" => {
|
||||
key_sequence_tracker.reset();
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields == 0 {
|
||||
return Ok("No fields to navigate to.".to_string());
|
||||
}
|
||||
let last_field_index = num_fields - 1;
|
||||
state.set_current_field(last_field_index);
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos = if current_input.is_empty() {
|
||||
0
|
||||
} else {
|
||||
current_input.len().saturating_sub(1)
|
||||
};
|
||||
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
"exit_edit_mode" => {
|
||||
key_sequence_tracker.reset();
|
||||
command_message.clear();
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_left" => {
|
||||
let current_pos = state.current_cursor_pos();
|
||||
let new_pos = current_pos.saturating_sub(1);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_right" => {
|
||||
let current_input = state.get_current_input();
|
||||
let current_pos = state.current_cursor_pos();
|
||||
if !current_input.is_empty()
|
||||
&& current_pos < current_input.len().saturating_sub(1)
|
||||
{
|
||||
let new_pos = current_pos + 1;
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_word_next" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos =
|
||||
find_next_word_start(current_input, state.current_cursor_pos());
|
||||
let final_pos = new_pos.min(current_input.len().saturating_sub(1));
|
||||
state.set_current_cursor_pos(final_pos);
|
||||
*ideal_cursor_column = final_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_word_end" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let current_pos = state.current_cursor_pos();
|
||||
let new_pos = find_word_end(current_input, current_pos);
|
||||
|
||||
let final_pos = if new_pos != current_pos {
|
||||
new_pos
|
||||
} else {
|
||||
find_word_end(current_input, new_pos + 1)
|
||||
};
|
||||
|
||||
let max_valid_index = current_input.len().saturating_sub(1);
|
||||
let clamped_pos = final_pos.min(max_valid_index);
|
||||
state.set_current_cursor_pos(clamped_pos);
|
||||
*ideal_cursor_column = clamped_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_word_prev" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_prev_word_start(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_word_end_prev" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_prev_word_end(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("Moved to previous word end".to_string())
|
||||
}
|
||||
"move_line_start" => {
|
||||
state.set_current_cursor_pos(0);
|
||||
*ideal_cursor_column = 0;
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_line_end" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = current_input.len().saturating_sub(1);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
} else {
|
||||
state.set_current_cursor_pos(0);
|
||||
*ideal_cursor_column = 0;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
_ => {
|
||||
key_sequence_tracker.reset();
|
||||
Ok(format!("Unknown read-only 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() {
|
||||
return 0;
|
||||
}
|
||||
let current_pos = current_pos.min(chars.len());
|
||||
|
||||
if 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_next_word_end(text: &str, current_pos: usize) -> usize {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
if chars.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let next_start = find_next_word_start(text, current_pos);
|
||||
|
||||
if next_start >= chars.len() {
|
||||
return chars.len().saturating_sub(1);
|
||||
}
|
||||
|
||||
let mut pos = next_start;
|
||||
let word_type = get_char_type(chars[pos]);
|
||||
|
||||
while pos < chars.len() && get_char_type(chars[pos]) == word_type {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
pos.saturating_sub(1).min(chars.len().saturating_sub(1))
|
||||
}
|
||||
|
||||
fn find_word_end(text: &str, current_pos: usize) -> usize {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
let len = chars.len();
|
||||
if len == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut pos = current_pos.min(len - 1);
|
||||
let original_pos = pos;
|
||||
|
||||
let current_type = get_char_type(chars[pos]);
|
||||
if current_type != CharType::Whitespace {
|
||||
while pos < len && get_char_type(chars[pos]) == current_type {
|
||||
pos += 1;
|
||||
}
|
||||
return pos.saturating_sub(1);
|
||||
}
|
||||
|
||||
pos = find_next_word_start(text, pos);
|
||||
if pos >= len {
|
||||
return len.saturating_sub(1);
|
||||
}
|
||||
|
||||
let word_type = get_char_type(chars[pos]);
|
||||
while pos < len && get_char_type(chars[pos]) == word_type {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
pos.saturating_sub(1).min(len.saturating_sub(1))
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if pos == 0 && get_char_type(chars[0]) == CharType::Whitespace {
|
||||
0
|
||||
} else {
|
||||
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 == 0 {
|
||||
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[0]) == CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
if pos == 0 && get_char_type(chars[0]) != CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
357
client/src/functions/modes/read_only/form_ro.rs
Normal file
357
client/src/functions/modes/read_only/form_ro.rs
Normal file
@@ -0,0 +1,357 @@
|
||||
// src/functions/modes/read_only/form_ro.rs
|
||||
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum CharType {
|
||||
Whitespace,
|
||||
Alphanumeric,
|
||||
Punctuation,
|
||||
}
|
||||
|
||||
pub async fn execute_action<S: CanvasState>(
|
||||
action: &str,
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
key_sequence_tracker: &mut KeySequenceTracker,
|
||||
command_message: &mut String,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
match action {
|
||||
"previous_entry" | "next_entry" => {
|
||||
key_sequence_tracker.reset();
|
||||
Ok(format!(
|
||||
"Action '{}' should be handled by context-specific logic",
|
||||
action
|
||||
))
|
||||
}
|
||||
"move_up" => {
|
||||
key_sequence_tracker.reset();
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields == 0 {
|
||||
return Ok("No fields to navigate.".to_string());
|
||||
}
|
||||
let current_field = state.current_field();
|
||||
let new_field = if current_field == 0 {
|
||||
num_fields - 1
|
||||
} else {
|
||||
current_field - 1
|
||||
};
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos = if current_input.is_empty() {
|
||||
0
|
||||
} else {
|
||||
current_input.len().saturating_sub(1)
|
||||
};
|
||||
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_down" => {
|
||||
key_sequence_tracker.reset();
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields == 0 {
|
||||
return Ok("No fields to navigate.".to_string());
|
||||
}
|
||||
let current_field = state.current_field();
|
||||
let new_field = (current_field + 1) % num_fields;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos = if current_input.is_empty() {
|
||||
0
|
||||
} else {
|
||||
current_input.len().saturating_sub(1)
|
||||
};
|
||||
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_first_line" => {
|
||||
key_sequence_tracker.reset();
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields == 0 {
|
||||
return Ok("No fields to navigate to.".to_string());
|
||||
}
|
||||
state.set_current_field(0);
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos = if current_input.is_empty() {
|
||||
0
|
||||
} else {
|
||||
current_input.len().saturating_sub(1)
|
||||
};
|
||||
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_last_line" => {
|
||||
key_sequence_tracker.reset();
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields == 0 {
|
||||
return Ok("No fields to navigate to.".to_string());
|
||||
}
|
||||
let last_field_index = num_fields - 1;
|
||||
state.set_current_field(last_field_index);
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos = if current_input.is_empty() {
|
||||
0
|
||||
} else {
|
||||
current_input.len().saturating_sub(1)
|
||||
};
|
||||
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
"exit_edit_mode" => {
|
||||
key_sequence_tracker.reset();
|
||||
command_message.clear();
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_left" => {
|
||||
let current_pos = state.current_cursor_pos();
|
||||
let new_pos = current_pos.saturating_sub(1);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_right" => {
|
||||
let current_input = state.get_current_input();
|
||||
let current_pos = state.current_cursor_pos();
|
||||
if !current_input.is_empty()
|
||||
&& current_pos < current_input.len().saturating_sub(1)
|
||||
{
|
||||
let new_pos = current_pos + 1;
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_word_next" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos =
|
||||
find_next_word_start(current_input, state.current_cursor_pos());
|
||||
let final_pos = new_pos.min(current_input.len().saturating_sub(1));
|
||||
state.set_current_cursor_pos(final_pos);
|
||||
*ideal_cursor_column = final_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_word_end" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let current_pos = state.current_cursor_pos();
|
||||
let new_pos = find_word_end(current_input, current_pos);
|
||||
|
||||
let final_pos = if new_pos != current_pos {
|
||||
new_pos
|
||||
} else {
|
||||
find_word_end(current_input, new_pos + 1)
|
||||
};
|
||||
|
||||
let max_valid_index = current_input.len().saturating_sub(1);
|
||||
let clamped_pos = final_pos.min(max_valid_index);
|
||||
state.set_current_cursor_pos(clamped_pos);
|
||||
*ideal_cursor_column = clamped_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_word_prev" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_prev_word_start(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_word_end_prev" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_prev_word_end(
|
||||
current_input,
|
||||
state.current_cursor_pos(),
|
||||
);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok("Moved to previous word end".to_string())
|
||||
}
|
||||
"move_line_start" => {
|
||||
state.set_current_cursor_pos(0);
|
||||
*ideal_cursor_column = 0;
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_line_end" => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = current_input.len().saturating_sub(1);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
} else {
|
||||
state.set_current_cursor_pos(0);
|
||||
*ideal_cursor_column = 0;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
_ => {
|
||||
key_sequence_tracker.reset();
|
||||
Ok(format!("Unknown read-only 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() {
|
||||
return 0;
|
||||
}
|
||||
let current_pos = current_pos.min(chars.len());
|
||||
|
||||
if 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_next_word_end(text: &str, current_pos: usize) -> usize {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
if chars.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let next_start = find_next_word_start(text, current_pos);
|
||||
|
||||
if next_start >= chars.len() {
|
||||
return chars.len().saturating_sub(1);
|
||||
}
|
||||
|
||||
let mut pos = next_start;
|
||||
let word_type = get_char_type(chars[pos]);
|
||||
|
||||
while pos < chars.len() && get_char_type(chars[pos]) == word_type {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
pos.saturating_sub(1).min(chars.len().saturating_sub(1))
|
||||
}
|
||||
|
||||
fn find_word_end(text: &str, current_pos: usize) -> usize {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
let len = chars.len();
|
||||
if len == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut pos = current_pos.min(len - 1);
|
||||
let original_pos = pos;
|
||||
|
||||
let current_type = get_char_type(chars[pos]);
|
||||
if current_type != CharType::Whitespace {
|
||||
while pos < len && get_char_type(chars[pos]) == current_type {
|
||||
pos += 1;
|
||||
}
|
||||
return pos.saturating_sub(1);
|
||||
}
|
||||
|
||||
pos = find_next_word_start(text, pos);
|
||||
if pos >= len {
|
||||
return len.saturating_sub(1);
|
||||
}
|
||||
|
||||
let word_type = get_char_type(chars[pos]);
|
||||
while pos < len && get_char_type(chars[pos]) == word_type {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
pos.saturating_sub(1).min(len.saturating_sub(1))
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if pos == 0 && get_char_type(chars[0]) == CharType::Whitespace {
|
||||
0
|
||||
} else {
|
||||
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 == 0 {
|
||||
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[0]) == CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
if pos == 0 && get_char_type(chars[0]) != CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ pub mod config;
|
||||
pub mod state;
|
||||
pub mod components;
|
||||
pub mod modes;
|
||||
pub mod functions;
|
||||
pub mod services;
|
||||
|
||||
pub use ui::run_ui;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// src/client/modes/canvas.rs
|
||||
pub mod edit;
|
||||
pub mod common;
|
||||
pub mod common_mode;
|
||||
pub mod read_only;
|
||||
|
||||
75
client/src/modes/canvas/common_mode.rs
Normal file
75
client/src/modes/canvas/common_mode.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
// src/modes/canvas/common.rs
|
||||
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::state::pages::{form::FormState, auth::AuthState};
|
||||
use crate::state::state::AppState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::tui::functions::common::{
|
||||
form::{save as form_save, revert as form_revert},
|
||||
login::{save as login_save, revert as login_revert}
|
||||
};
|
||||
|
||||
pub async fn handle_core_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
grpc_client: &mut GrpcClient,
|
||||
auth_client: &mut AuthClient,
|
||||
terminal: &mut TerminalCore,
|
||||
app_state: &mut AppState,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"save" => {
|
||||
if app_state.ui.show_login {
|
||||
let message = login_save(auth_state, auth_client, app_state).await?;
|
||||
Ok((false, message))
|
||||
} else {
|
||||
let message = form_save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
&mut app_state.ui.is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await?;
|
||||
Ok((false, message))
|
||||
}
|
||||
},
|
||||
"force_quit" => {
|
||||
terminal.cleanup()?;
|
||||
Ok((true, "Force exiting without saving.".to_string()))
|
||||
},
|
||||
"save_and_quit" => {
|
||||
let message = if app_state.ui.show_login {
|
||||
login_save(auth_state, auth_client, app_state).await?
|
||||
} else {
|
||||
form_save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
&mut app_state.ui.is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await?
|
||||
};
|
||||
terminal.cleanup()?;
|
||||
Ok((true, format!("{}. Exiting application.", message)))
|
||||
},
|
||||
"revert" => {
|
||||
if app_state.ui.show_login {
|
||||
let message = login_revert(auth_state, app_state).await;
|
||||
Ok((false, message))
|
||||
} else {
|
||||
let message = form_revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count,
|
||||
).await?;
|
||||
Ok((false, message))
|
||||
}
|
||||
},
|
||||
_ => Ok((false, format!("Core action not handled: {}", action))),
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
// src/modes/canvas/edit.rs
|
||||
|
||||
// TODO THIS is freaking bloated with functions it never uses REFACTOR 200 LOC can be gone
|
||||
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::modes::canvas::common;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::pages::{auth::AuthState, form::FormState};
|
||||
use crate::state::state::AppState;
|
||||
use crate::functions::modes::edit::{auth_e, form_e};
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
|
||||
|
||||
pub async fn handle_edit_event_internal(
|
||||
pub async fn handle_edit_event(
|
||||
is_auth_context: bool,
|
||||
key: KeyEvent,
|
||||
config: &Config,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
ideal_cursor_column: &mut usize,
|
||||
command_message: &mut String,
|
||||
is_saved: &mut bool,
|
||||
@@ -19,455 +20,99 @@ pub async fn handle_edit_event_internal(
|
||||
total_count: u64,
|
||||
grpc_client: &mut GrpcClient,
|
||||
) -> 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) {
|
||||
// Ignore in edit mode and process as normal input
|
||||
handle_edit_specific_input(key, form_state, ideal_cursor_column);
|
||||
|
||||
// Global command mode check
|
||||
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.global,
|
||||
key.code,
|
||||
key.modifiers
|
||||
) {
|
||||
*command_message = "Switching to Command Mode...".to_string();
|
||||
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) {
|
||||
return execute_common_action(
|
||||
action,
|
||||
form_state,
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await;
|
||||
// Common actions (save/revert)
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.common,
|
||||
key.code,
|
||||
key.modifiers
|
||||
) {
|
||||
if matches!(action, "save" | "revert") {
|
||||
return if is_auth_context {
|
||||
auth_e::execute_common_action(
|
||||
action,
|
||||
auth_state, // Concrete AuthState
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
} else {
|
||||
form_e::execute_common_action(
|
||||
action,
|
||||
form_state, // Concrete FormState
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Edit-specific actions
|
||||
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
|
||||
return execute_edit_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
grpc_client, // Changed from AppTerminal
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await;
|
||||
return if is_auth_context {
|
||||
auth_e::execute_edit_action(
|
||||
action,
|
||||
key,
|
||||
auth_state, // Full access to AuthState fields
|
||||
ideal_cursor_column,
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
} else {
|
||||
form_e::execute_edit_action(
|
||||
action,
|
||||
key,
|
||||
form_state, // Full access to FormState fields
|
||||
ideal_cursor_column,
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
};
|
||||
}
|
||||
|
||||
handle_edit_specific_input(
|
||||
key,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
);
|
||||
// Character insertion
|
||||
if let KeyCode::Char(_) = key.code {
|
||||
return if is_auth_context {
|
||||
auth_e::execute_edit_action(
|
||||
"insert_char",
|
||||
key,
|
||||
auth_state,
|
||||
ideal_cursor_column,
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
} else {
|
||||
form_e::execute_edit_action(
|
||||
"insert_char",
|
||||
key,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
};
|
||||
}
|
||||
|
||||
Ok(command_message.clone())
|
||||
}
|
||||
|
||||
async fn execute_common_action(
|
||||
action: &str,
|
||||
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>> {
|
||||
match action {
|
||||
"save" => {
|
||||
common::save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await
|
||||
},
|
||||
"revert" => {
|
||||
common::revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count,
|
||||
).await
|
||||
},
|
||||
"move_up" | "move_down" => {
|
||||
// Reuse edit mode's existing logic
|
||||
execute_edit_action(
|
||||
action,
|
||||
form_state,
|
||||
&mut 0, // Dummy ideal_cursor_column (not used here)
|
||||
grpc_client,
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await
|
||||
},
|
||||
_ => Ok(format!("Common action not handled: {}", action)),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_edit_specific_input(
|
||||
key: KeyEvent,
|
||||
form_state: &mut FormState,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) {
|
||||
match key.code {
|
||||
KeyCode::Char(c) => {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() {
|
||||
chars.insert(cursor_pos, c);
|
||||
*field_value = chars.into_iter().collect();
|
||||
form_state.current_cursor_pos = cursor_pos + 1;
|
||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if form_state.current_cursor_pos > 0 {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() && cursor_pos > 0 {
|
||||
chars.remove(cursor_pos - 1);
|
||||
*field_value = chars.into_iter().collect();
|
||||
form_state.current_cursor_pos = cursor_pos - 1;
|
||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Delete => {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos < chars.len() {
|
||||
let mut new_chars = chars.clone();
|
||||
new_chars.remove(cursor_pos);
|
||||
*field_value = new_chars.into_iter().collect();
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
KeyCode::Tab => {
|
||||
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
}
|
||||
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 => {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
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);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
async fn execute_edit_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
ideal_cursor_column: &mut usize,
|
||||
grpc_client: &mut GrpcClient, // Changed from AppTerminal
|
||||
is_saved: &mut bool,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"save" => {
|
||||
common::save(
|
||||
form_state,
|
||||
grpc_client, // Changed from AppTerminal
|
||||
is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await
|
||||
},
|
||||
"move_left" => {
|
||||
form_state.current_cursor_pos = form_state.current_cursor_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();
|
||||
if form_state.current_cursor_pos < current_input.len() {
|
||||
form_state.current_cursor_pos += 1;
|
||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_up" => {
|
||||
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);
|
||||
}
|
||||
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())
|
||||
}
|
||||
"move_down" => {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
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())
|
||||
}
|
||||
"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();
|
||||
form_state.current_cursor_pos = current_input.len();
|
||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_first_line" => {
|
||||
form_state.current_field = 0;
|
||||
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("Moved to first line".to_string())
|
||||
}
|
||||
"move_last_line" => {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
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("Moved to last line".to_string())
|
||||
}
|
||||
// Word movement actions
|
||||
"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());
|
||||
*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());
|
||||
*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("".to_string())
|
||||
}
|
||||
// Edit-specific actions that can be bound to keys
|
||||
"delete_char_forward" => {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos < chars.len() {
|
||||
let mut new_chars = chars.clone();
|
||||
new_chars.remove(cursor_pos);
|
||||
*field_value = new_chars.into_iter().collect();
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"delete_char_backward" => {
|
||||
if form_state.current_cursor_pos > 0 {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() && cursor_pos > 0 {
|
||||
chars.remove(cursor_pos - 1);
|
||||
*field_value = chars.into_iter().collect();
|
||||
form_state.current_cursor_pos = cursor_pos - 1;
|
||||
*ideal_cursor_column = form_state.current_cursor_pos;
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"insert_char" => {
|
||||
// This could be expanded to allow configurable character insertion
|
||||
// For now, it's a placeholder that would need additional parameters
|
||||
Ok("Character insertion requires configuration".to_string())
|
||||
}
|
||||
"next_field" => {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
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())
|
||||
}
|
||||
"prev_field" => {
|
||||
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);
|
||||
}
|
||||
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())
|
||||
}
|
||||
// Fallback for unrecognized actions
|
||||
_ => Ok(format!("Unknown action: {}", action)),
|
||||
}
|
||||
}
|
||||
|
||||
// Reuse these character and word navigation helper functions
|
||||
#[derive(PartialEq)]
|
||||
enum CharType {
|
||||
Whitespace,
|
||||
Alphanumeric,
|
||||
Punctuation,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,19 +1,13 @@
|
||||
|
||||
// src/modes/canvas/read_only.rs
|
||||
|
||||
use crossterm::event::{KeyEvent};
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum CharType {
|
||||
Whitespace,
|
||||
Alphanumeric,
|
||||
Punctuation,
|
||||
}
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::functions::modes::read_only::{auth_ro, form_ro};
|
||||
use crossterm::event::KeyEvent;
|
||||
|
||||
pub async fn handle_read_only_event(
|
||||
app_state: &crate::state::state::AppState,
|
||||
@@ -29,7 +23,6 @@ pub async fn handle_read_only_event(
|
||||
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();
|
||||
@@ -37,25 +30,47 @@ pub async fn handle_read_only_event(
|
||||
}
|
||||
|
||||
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;
|
||||
let (current_input, current_pos) = if app_state.ui.show_login {
|
||||
(
|
||||
auth_state.get_current_input(),
|
||||
auth_state.current_cursor_pos(),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
form_state.get_current_input(),
|
||||
form_state.current_cursor_pos(),
|
||||
)
|
||||
};
|
||||
|
||||
if !current_input.is_empty() && current_pos < current_input.len() {
|
||||
if app_state.ui.show_login {
|
||||
auth_state.set_current_cursor_pos(current_pos + 1);
|
||||
*ideal_cursor_column = auth_state.current_cursor_pos();
|
||||
} else {
|
||||
form_state.set_current_cursor_pos(current_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
|
||||
const CONTEXT_ACTIONS_FORM: &[&str] = &[
|
||||
"previous_entry",
|
||||
"next_entry",
|
||||
];
|
||||
const CONTEXT_ACTIONS_LOGIN: &[&str] = &[
|
||||
"previous_entry",
|
||||
"next_entry",
|
||||
];
|
||||
|
||||
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" || action == "next_entry" ||
|
||||
action == "move_up" || action == "move_down") && app_state.ui.show_form {
|
||||
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
||||
crate::tui::functions::form::handle_action(
|
||||
action,
|
||||
form_state,
|
||||
@@ -63,38 +78,45 @@ pub async fn handle_read_only_event(
|
||||
current_position,
|
||||
total_count,
|
||||
ideal_cursor_column,
|
||||
).await?
|
||||
} else if (action == "move_up" || action == "move_down") && app_state.ui.show_login {
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) {
|
||||
crate::tui::functions::login::handle_action(
|
||||
action,
|
||||
auth_state,
|
||||
ideal_cursor_column,
|
||||
).await?
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_login {
|
||||
auth_ro::execute_action(
|
||||
action,
|
||||
auth_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
)
|
||||
.await?
|
||||
} else {
|
||||
execute_action(
|
||||
form_ro::execute_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
).await?
|
||||
)
|
||||
.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 {
|
||||
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
||||
crate::tui::functions::form::handle_action(
|
||||
action,
|
||||
form_state,
|
||||
@@ -102,29 +124,44 @@ pub async fn handle_read_only_event(
|
||||
current_position,
|
||||
total_count,
|
||||
ideal_cursor_column,
|
||||
).await?
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) {
|
||||
crate::tui::functions::login::handle_action(
|
||||
action,
|
||||
auth_state,
|
||||
ideal_cursor_column,
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_login {
|
||||
auth_ro::execute_action(
|
||||
action,
|
||||
auth_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
)
|
||||
.await?
|
||||
} else {
|
||||
execute_action(
|
||||
form_ro::execute_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
).await?
|
||||
)
|
||||
.await?
|
||||
};
|
||||
key_sequence_tracker.reset();
|
||||
return Ok((false, result));
|
||||
}
|
||||
}
|
||||
key_sequence_tracker.reset();
|
||||
} 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 {
|
||||
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
||||
crate::tui::functions::form::handle_action(
|
||||
action,
|
||||
form_state,
|
||||
@@ -132,287 +169,51 @@ pub async fn handle_read_only_event(
|
||||
current_position,
|
||||
total_count,
|
||||
ideal_cursor_column,
|
||||
).await?
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) {
|
||||
crate::tui::functions::login::handle_action(
|
||||
action,
|
||||
auth_state,
|
||||
ideal_cursor_column,
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_login {
|
||||
auth_ro::execute_action(
|
||||
action,
|
||||
auth_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
)
|
||||
.await?
|
||||
} else {
|
||||
execute_action(
|
||||
form_ro::execute_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
).await?
|
||||
)
|
||||
.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")
|
||||
let edit_key = config
|
||||
.keybindings
|
||||
.read_only
|
||||
.get("enter_edit_mode_before")
|
||||
.and_then(|keys| keys.first())
|
||||
.unwrap_or(&default_key);
|
||||
.map(|k| k.to_string())
|
||||
.unwrap_or(default_key);
|
||||
*command_message = format!("Read-only mode - press {} to edit", edit_key);
|
||||
}
|
||||
|
||||
*edit_mode_cooldown = false;
|
||||
|
||||
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" | "next_entry" => {
|
||||
// This will only be called when no component is active
|
||||
key_sequence_tracker.reset();
|
||||
Ok(format!("Navigation prev/next only available in form mode"))
|
||||
}
|
||||
"move_up" | "move_down" => {
|
||||
// This will only be called when no component is active
|
||||
key_sequence_tracker.reset();
|
||||
Ok(format!("Navigation up/down only available in form mode"))
|
||||
}
|
||||
"exit_edit_mode" => {
|
||||
key_sequence_tracker.reset();
|
||||
command_message.clear();
|
||||
Ok("".to_string())
|
||||
}
|
||||
"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_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
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// src/client/modes/common.rs
|
||||
pub mod command_mode;
|
||||
pub mod highlight;
|
||||
pub mod commands;
|
||||
|
||||
pub use commands::*;
|
||||
|
||||
@@ -4,11 +4,9 @@ use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::tui::controls::commands::CommandHandler;
|
||||
use crate::modes::common::commands::CommandHandler;
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::modes::{
|
||||
canvas::{common},
|
||||
};
|
||||
use crate::tui::functions::common::form::{save, revert};
|
||||
|
||||
pub async fn handle_command_event(
|
||||
key: KeyEvent,
|
||||
@@ -97,7 +95,7 @@ async fn process_command(
|
||||
Ok((should_exit, message, true))
|
||||
},
|
||||
"save" => {
|
||||
let message = common::save(
|
||||
let message = save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
&mut command_handler.is_saved,
|
||||
@@ -108,7 +106,7 @@ async fn process_command(
|
||||
return Ok((false, message, true));
|
||||
},
|
||||
"revert" => {
|
||||
let message = common::revert(
|
||||
let message = revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
|
||||
@@ -5,14 +5,16 @@ use crate::tui::terminal::{
|
||||
core::TerminalCore,
|
||||
};
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::tui::controls::commands::CommandHandler;
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::modes::common::commands::CommandHandler;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use crate::ui::handlers::rat_state::UiStateHandler;
|
||||
use crate::modes::{
|
||||
common::{command_mode},
|
||||
canvas::{edit, read_only, common},
|
||||
canvas::{edit, read_only, common_mode},
|
||||
general::navigation,
|
||||
};
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
@@ -26,12 +28,13 @@ pub struct EventHandler {
|
||||
pub edit_mode_cooldown: bool,
|
||||
pub ideal_cursor_column: usize,
|
||||
pub key_sequence_tracker: KeySequenceTracker,
|
||||
pub auth_state: AuthState,
|
||||
// pub auth_state: AuthState, // Removed
|
||||
pub auth_client: AuthClient,
|
||||
}
|
||||
|
||||
impl EventHandler {
|
||||
pub fn new() -> Self {
|
||||
EventHandler {
|
||||
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
Ok(EventHandler {
|
||||
command_mode: false,
|
||||
command_input: String::new(),
|
||||
command_message: String::new(),
|
||||
@@ -39,8 +42,9 @@ impl EventHandler {
|
||||
edit_mode_cooldown: false,
|
||||
ideal_cursor_column: 0,
|
||||
key_sequence_tracker: KeySequenceTracker::new(800),
|
||||
auth_state: AuthState::new(),
|
||||
}
|
||||
// auth_state: AuthState::new(), // Removed
|
||||
auth_client: AuthClient::new().await?,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn handle_event(
|
||||
@@ -51,6 +55,7 @@ impl EventHandler {
|
||||
grpc_client: &mut GrpcClient,
|
||||
command_handler: &mut CommandHandler,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState, // Added
|
||||
app_state: &mut crate::state::state::AppState,
|
||||
total_count: u64,
|
||||
current_position: &mut u64,
|
||||
@@ -97,10 +102,25 @@ impl EventHandler {
|
||||
|
||||
if config.is_enter_edit_mode_after(key_code, modifiers) &&
|
||||
ModeManager::can_enter_edit_mode(current_mode) {
|
||||
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;
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
let current_input = if app_state.ui.show_login {
|
||||
auth_state.get_current_input()
|
||||
} else {
|
||||
form_state.get_current_input()
|
||||
};
|
||||
let current_cursor_pos = if app_state.ui.show_login {
|
||||
auth_state.current_cursor_pos()
|
||||
} else {
|
||||
form_state.current_cursor_pos()
|
||||
};
|
||||
|
||||
if !current_input.is_empty() && current_cursor_pos < current_input.len() {
|
||||
if app_state.ui.show_login {
|
||||
auth_state.set_current_cursor_pos(current_cursor_pos + 1);
|
||||
self.ideal_cursor_column = auth_state.current_cursor_pos();
|
||||
} else {
|
||||
form_state.set_current_cursor_pos(current_cursor_pos + 1);
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos();
|
||||
}
|
||||
}
|
||||
self.is_edit_mode = true;
|
||||
self.edit_mode_cooldown = true;
|
||||
@@ -120,7 +140,6 @@ impl EventHandler {
|
||||
}
|
||||
|
||||
// Check for core application actions (save, quit, etc.)
|
||||
// ONLY handle a limited subset of core actions here
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.common,
|
||||
key_code,
|
||||
@@ -128,27 +147,29 @@ impl EventHandler {
|
||||
) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
return common::handle_core_action(
|
||||
return common_mode::handle_core_action(
|
||||
action,
|
||||
form_state,
|
||||
auth_state, // Changed
|
||||
grpc_client,
|
||||
&mut self.auth_client,
|
||||
terminal,
|
||||
app_state,
|
||||
current_position,
|
||||
total_count,
|
||||
).await;
|
||||
},
|
||||
_ => {} // For other actions, let the mode-specific handler take care of it
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Let read_only mode handle its own actions (including navigation from common bindings)
|
||||
// Let read_only mode handle its own actions
|
||||
return read_only::handle_read_only_event(
|
||||
&app_state,
|
||||
key,
|
||||
config,
|
||||
form_state,
|
||||
&mut self.auth_state,
|
||||
auth_state, // Changed
|
||||
&mut self.key_sequence_tracker,
|
||||
current_position,
|
||||
total_count,
|
||||
@@ -162,7 +183,13 @@ impl EventHandler {
|
||||
AppMode::Edit => {
|
||||
// Check for exiting edit mode
|
||||
if config.is_exit_edit_mode(key_code, modifiers) {
|
||||
if form_state.has_unsaved_changes {
|
||||
let has_changes = if app_state.ui.show_login {
|
||||
auth_state.has_unsaved_changes()
|
||||
} else {
|
||||
form_state.has_unsaved_changes()
|
||||
};
|
||||
|
||||
if has_changes {
|
||||
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
@@ -171,16 +198,31 @@ impl EventHandler {
|
||||
self.command_message = "Read-only mode".to_string();
|
||||
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
||||
|
||||
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 = current_input.len() - 1;
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
let current_input = if app_state.ui.show_login {
|
||||
auth_state.get_current_input()
|
||||
} else {
|
||||
form_state.get_current_input()
|
||||
};
|
||||
let current_cursor_pos = if app_state.ui.show_login {
|
||||
auth_state.current_cursor_pos()
|
||||
} else {
|
||||
form_state.current_cursor_pos()
|
||||
};
|
||||
|
||||
if !current_input.is_empty() && current_cursor_pos >= current_input.len() {
|
||||
let new_pos = current_input.len() - 1;
|
||||
if app_state.ui.show_login {
|
||||
auth_state.set_current_cursor_pos(new_pos);
|
||||
self.ideal_cursor_column = auth_state.current_cursor_pos();
|
||||
} else {
|
||||
form_state.set_current_cursor_pos(new_pos);
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos();
|
||||
}
|
||||
}
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
|
||||
// Check for core application actions (save, quit, etc.)
|
||||
// ONLY handle a limited subset of core actions here
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.common,
|
||||
key_code,
|
||||
@@ -188,25 +230,29 @@ impl EventHandler {
|
||||
) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
return common::handle_core_action(
|
||||
return common_mode::handle_core_action(
|
||||
action,
|
||||
form_state,
|
||||
auth_state, // Changed
|
||||
grpc_client,
|
||||
&mut self.auth_client,
|
||||
terminal,
|
||||
app_state,
|
||||
current_position,
|
||||
total_count,
|
||||
).await;
|
||||
},
|
||||
_ => {} // For other actions, let the mode-specific handler take care of it
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Let edit mode handle its own actions (including navigation from common bindings)
|
||||
let result = edit::handle_edit_event_internal(
|
||||
// Let edit mode handle its own actions
|
||||
let result = edit::handle_edit_event(
|
||||
app_state.ui.show_login,
|
||||
key,
|
||||
config,
|
||||
form_state,
|
||||
auth_state, // Changed
|
||||
&mut self.ideal_cursor_column,
|
||||
&mut self.command_message,
|
||||
&mut app_state.ui.is_saved,
|
||||
@@ -246,6 +292,5 @@ impl EventHandler {
|
||||
self.edit_mode_cooldown = false;
|
||||
Ok((false, self.command_message.clone()))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ impl ModeManager {
|
||||
return AppMode::Command;
|
||||
}
|
||||
|
||||
if app_state.ui.show_login { // NEW: Check auth visibility
|
||||
if app_state.ui.show_login {
|
||||
if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
} else {
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
// src/services/adresar.rs
|
||||
@@ -0,0 +1,23 @@
|
||||
// src/services/auth.rs
|
||||
use tonic::transport::Channel;
|
||||
use common::proto::multieko2::auth::{
|
||||
auth_service_client::AuthServiceClient,
|
||||
LoginRequest, LoginResponse
|
||||
};
|
||||
|
||||
pub struct AuthClient {
|
||||
client: AuthServiceClient<Channel>,
|
||||
}
|
||||
|
||||
impl AuthClient {
|
||||
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let client = AuthServiceClient::connect("http://[::1]:50051").await?;
|
||||
Ok(Self { client })
|
||||
}
|
||||
|
||||
pub async fn login(&mut self, identifier: String, password: String) -> Result<LoginResponse, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(LoginRequest { identifier, password });
|
||||
let response = self.client.login(request).await?.into_inner();
|
||||
Ok(response)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,17 +10,12 @@ use common::proto::multieko2::table_definition::{
|
||||
table_definition_client::TableDefinitionClient,
|
||||
ProfileTreeResponse
|
||||
};
|
||||
use common::proto::multieko2::auth::{
|
||||
auth_service_client::AuthServiceClient,
|
||||
LoginRequest, LoginResponse
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GrpcClient {
|
||||
adresar_client: AdresarClient<Channel>,
|
||||
table_structure_client: TableStructureServiceClient<Channel>,
|
||||
table_definition_client: TableDefinitionClient<Channel>,
|
||||
auth_client: AuthServiceClient<Channel>,
|
||||
}
|
||||
|
||||
impl GrpcClient {
|
||||
@@ -28,13 +23,11 @@ impl GrpcClient {
|
||||
let adresar_client = AdresarClient::connect("http://[::1]:50051").await?;
|
||||
let table_structure_client = TableStructureServiceClient::connect("http://[::1]:50051").await?;
|
||||
let table_definition_client = TableDefinitionClient::connect("http://[::1]:50051").await?;
|
||||
let auth_client = AuthServiceClient::connect("http://[::1]:50051").await?;
|
||||
|
||||
Ok(Self {
|
||||
adresar_client,
|
||||
table_structure_client,
|
||||
table_definition_client,
|
||||
auth_client,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -73,10 +66,4 @@ impl GrpcClient {
|
||||
let response = self.table_definition_client.get_profile_tree(request).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
pub async fn login(&mut self, identifier: String, password: String) -> Result<LoginResponse, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(LoginRequest { identifier, password });
|
||||
let response = self.auth_client.login(request).await?.into_inner();
|
||||
Ok(response)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
// services/mod.rs
|
||||
|
||||
pub mod grpc_client;
|
||||
pub mod adresar;
|
||||
pub mod table;
|
||||
pub mod profile;
|
||||
pub mod auth;
|
||||
pub mod ui_service;
|
||||
|
||||
pub use grpc_client::*;
|
||||
pub use adresar::*;
|
||||
pub use table::*;
|
||||
pub use profile::*;
|
||||
pub use ui_service::*;
|
||||
pub use auth::*;
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
// src/services/profile.rs
|
||||
@@ -1 +0,0 @@
|
||||
// src/services/table.rs
|
||||
89
client/src/services/ui_service.rs
Normal file
89
client/src/services/ui_service.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
// src/services/ui_service.rs
|
||||
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::state::AppState;
|
||||
|
||||
pub struct UiService;
|
||||
|
||||
impl UiService {
|
||||
pub async fn initialize_app_state(
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
||||
// Fetch profile tree
|
||||
let profile_tree = grpc_client.get_profile_tree().await?;
|
||||
app_state.profile_tree = profile_tree;
|
||||
|
||||
// Fetch table structure
|
||||
let table_structure = grpc_client.get_table_structure().await?;
|
||||
|
||||
// Extract the column names from the response
|
||||
let column_names: Vec<String> = table_structure
|
||||
.columns
|
||||
.iter()
|
||||
.map(|col| col.name.clone())
|
||||
.collect();
|
||||
|
||||
Ok(column_names)
|
||||
}
|
||||
|
||||
pub async fn initialize_adresar_count(
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let total_count = grpc_client.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_adresar_count(
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let total_count = grpc_client.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn load_adresar_by_position(
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
form_state: &mut FormState,
|
||||
position: u64,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match grpc_client.get_adresar_by_position(position).await {
|
||||
Ok(response) => {
|
||||
// Set the ID properly
|
||||
form_state.id = response.id;
|
||||
|
||||
// Update form values dynamically
|
||||
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,
|
||||
];
|
||||
|
||||
form_state.has_unsaved_changes = false;
|
||||
Ok(format!("Loaded entry {}", position))
|
||||
}
|
||||
Err(e) => {
|
||||
Ok(format!("Error loading entry: {}", e))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,40 +10,8 @@ pub trait CanvasState {
|
||||
fn get_current_input(&self) -> &str;
|
||||
fn get_current_input_mut(&mut self) -> &mut String;
|
||||
fn fields(&self) -> Vec<&str>;
|
||||
}
|
||||
|
||||
// Implement for FormState (keep existing form.rs code and add this)
|
||||
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()
|
||||
}
|
||||
|
||||
fn set_current_field(&mut self, index: usize);
|
||||
fn set_current_cursor_pos(&mut self, pos: usize);
|
||||
fn set_has_unsaved_changes(&mut self, changed: bool);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ pub struct AuthState {
|
||||
pub auth_token: Option<String>,
|
||||
pub user_id: Option<String>,
|
||||
pub role: Option<String>,
|
||||
pub has_unsaved_changes: bool,
|
||||
}
|
||||
|
||||
impl AuthState {
|
||||
@@ -26,6 +27,7 @@ impl AuthState {
|
||||
auth_token: None,
|
||||
user_id: None,
|
||||
role: None,
|
||||
has_unsaved_changes: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,12 +38,16 @@ impl CanvasState for AuthState {
|
||||
}
|
||||
|
||||
fn current_cursor_pos(&self) -> usize {
|
||||
self.current_cursor_pos
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.password.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos.min(len)
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
// Auth form doesn't need unsaved changes tracking
|
||||
false
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
@@ -52,7 +58,7 @@ impl CanvasState for AuthState {
|
||||
match self.current_field {
|
||||
0 => &self.username,
|
||||
1 => &self.password,
|
||||
_ => "", // Return empty string for invalid index instead of panicking
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,4 +73,32 @@ impl CanvasState for AuthState {
|
||||
fn fields(&self) -> Vec<&str> {
|
||||
vec!["Username/Email", "Password"]
|
||||
}
|
||||
|
||||
fn set_current_field(&mut self, index: usize) {
|
||||
if index < 2 { // AuthState only has 2 fields
|
||||
self.current_field = index;
|
||||
// IMPORTANT: Clamp cursor position to the length of the NEW field
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.password.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos = self.current_cursor_pos.min(len);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.password.len(),
|
||||
_ => 0,
|
||||
};
|
||||
// Ensure stored position is always valid
|
||||
self.current_cursor_pos = pos.min(len);
|
||||
}
|
||||
|
||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
// Allow the generic handler to signal changes
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
use crate::config::colors::themes::Theme;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::Frame;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
|
||||
pub struct FormState {
|
||||
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 login;
|
||||
pub mod form;
|
||||
pub mod common;
|
||||
|
||||
pub use admin::*;
|
||||
pub use intro::*;
|
||||
pub use form::*;
|
||||
pub use common::*;
|
||||
|
||||
7
client/src/tui/functions/common.rs
Normal file
7
client/src/tui/functions/common.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
// src/tui/functions/common.rs
|
||||
|
||||
pub mod form;
|
||||
pub mod login;
|
||||
|
||||
pub use form::*;
|
||||
pub use login::*;
|
||||
@@ -1,81 +1,9 @@
|
||||
// src/modes/canvas/common.rs
|
||||
// src/tui/functions/common/form.rs
|
||||
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::state::AppState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
|
||||
use crate::state::pages::form::FormState;
|
||||
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
|
||||
|
||||
/// Main handler for common core actions
|
||||
pub async fn handle_core_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
grpc_client: &mut GrpcClient,
|
||||
terminal: &mut TerminalCore,
|
||||
app_state: &mut AppState,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"save" => {
|
||||
let message = save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
&mut app_state.ui.is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await?;
|
||||
Ok((false, message))
|
||||
},
|
||||
"force_quit" => {
|
||||
terminal.cleanup()?;
|
||||
Ok((true, "Force exiting without saving.".to_string()))
|
||||
},
|
||||
"save_and_quit" => {
|
||||
let message = save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
&mut app_state.ui.is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await?;
|
||||
terminal.cleanup()?;
|
||||
Ok((true, format!("{}. Exiting application.", message)))
|
||||
},
|
||||
"revert" => {
|
||||
let message = revert(
|
||||
form_state,
|
||||
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))),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
62
client/src/tui/functions/common/login.rs
Normal file
62
client/src/tui/functions/common/login.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
// src/tui/functions/common/login.rs
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::state::AppState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
|
||||
/// Attempts to log the user in using the provided credentials via gRPC.
|
||||
/// Updates AuthState and AppState on success or failure.
|
||||
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();
|
||||
|
||||
// Call the gRPC login method
|
||||
match auth_client.login(identifier, password).await {
|
||||
Ok(response) => {
|
||||
// Store authentication details on success
|
||||
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;
|
||||
auth_state.set_has_unsaved_changes(false); // Mark as "saved"
|
||||
|
||||
// Update app state to transition from login to the main form view
|
||||
app_state.ui.show_login = false;
|
||||
app_state.ui.show_form = true; // Assuming form is the next view
|
||||
|
||||
Ok("Login successful!".to_string())
|
||||
}
|
||||
Err(e) => {
|
||||
// Store error message on failure
|
||||
let error_message = format!("Login failed: {}", e);
|
||||
auth_state.error_message = Some(error_message.clone());
|
||||
// Keep unsaved changes true if login fails, allowing retry/revert
|
||||
auth_state.set_has_unsaved_changes(true);
|
||||
Ok(error_message) // Return error message to display
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reverts the login form fields to empty and returns to the previous screen (Intro).
|
||||
/// This function is now named 'revert' to match the 'form' counterpart.
|
||||
pub async fn revert(
|
||||
auth_state: &mut AuthState,
|
||||
app_state: &mut AppState,
|
||||
) -> String {
|
||||
// Clear the input fields
|
||||
auth_state.username.clear();
|
||||
auth_state.password.clear();
|
||||
auth_state.error_message = None; // Clear any previous error
|
||||
auth_state.set_has_unsaved_changes(false); // Fields are cleared, no unsaved changes
|
||||
|
||||
// Update app state to hide login and show the previous screen (Intro)
|
||||
app_state.ui.show_login = false;
|
||||
app_state.ui.show_intro = true; // Assuming Intro is the screen before login
|
||||
|
||||
"Login reverted".to_string()
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/tui/functions/form.rs
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use common::proto::multieko2::adresar::AdresarResponse;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
|
||||
pub async fn handle_action(
|
||||
action: &str,
|
||||
@@ -75,38 +75,6 @@ pub async fn handle_action(
|
||||
Ok("Already at last entry".into())
|
||||
}
|
||||
}
|
||||
"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())
|
||||
}
|
||||
_ => Err("Unknown form action".into())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,56 +8,11 @@ pub async fn handle_action(
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"move_up" => {
|
||||
if auth_state.return_selected {
|
||||
// From Return button to last field (password)
|
||||
auth_state.return_selected = false;
|
||||
auth_state.current_field = 1;
|
||||
} else if auth_state.current_field == 1 {
|
||||
// Password -> Username
|
||||
auth_state.current_field = 0;
|
||||
} else if auth_state.current_field == 0 {
|
||||
// Username -> Password (wrap around fields only)
|
||||
auth_state.current_field = 1;
|
||||
} else if auth_state.current_field == 2 {
|
||||
// From Login button to Password field
|
||||
auth_state.current_field = 1;
|
||||
}
|
||||
|
||||
// Update cursor position only when in a field
|
||||
if auth_state.current_field < 2 {
|
||||
let current_input = auth_state.get_current_input();
|
||||
let max_cursor_pos = current_input.len();
|
||||
auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
}
|
||||
|
||||
Ok(format!("Navigation 'up' from functions/login"))
|
||||
"previous_entry" => {
|
||||
Ok("Previous entry at tui/functions/login.rs not implemented".into())
|
||||
}
|
||||
"move_down" => {
|
||||
if auth_state.return_selected {
|
||||
// From Return button to first field (username)
|
||||
auth_state.return_selected = false;
|
||||
auth_state.current_field = 0;
|
||||
} else if auth_state.current_field == 0 {
|
||||
// Username -> Password
|
||||
auth_state.current_field = 1;
|
||||
} else if auth_state.current_field == 1 {
|
||||
// Password -> Login button
|
||||
auth_state.current_field = 2;
|
||||
auth_state.return_selected = false;
|
||||
} else if auth_state.current_field == 2 {
|
||||
// Login button -> Return button
|
||||
auth_state.return_selected = true;
|
||||
}
|
||||
|
||||
// Update cursor position only when in a field
|
||||
if auth_state.current_field < 2 {
|
||||
let current_input = auth_state.get_current_input();
|
||||
let max_cursor_pos = current_input.len();
|
||||
auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
}
|
||||
|
||||
Ok(format!("Navigation 'down' from functions/login"))
|
||||
"next_entry" => {
|
||||
Ok("Next entry at tui/functions/login.rs not implemented".into())
|
||||
}
|
||||
_ => Err("Unknown login action".into())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// src/tui/mod.rs
|
||||
pub mod terminal;
|
||||
pub mod controls;
|
||||
pub mod functions;
|
||||
|
||||
pub use functions::*;
|
||||
|
||||
@@ -2,13 +2,16 @@
|
||||
|
||||
use crate::tui::terminal::TerminalCore;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::tui::controls::CommandHandler;
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::services::ui_service::UiService;
|
||||
use crate::tui::terminal::EventReader;
|
||||
use crate::modes::common::commands::CommandHandler;
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::ui::handlers::render::render_ui;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
use crate::state::state::AppState;
|
||||
|
||||
@@ -16,51 +19,41 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::load()?;
|
||||
let mut terminal = TerminalCore::new()?;
|
||||
let mut grpc_client = GrpcClient::new().await?;
|
||||
// let auth_client = AuthClient::new().await?; // AuthClient is now inside EventHandler
|
||||
let mut command_handler = CommandHandler::new();
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
let mut auth_state = AuthState::default();
|
||||
let mut auth_state = AuthState::default(); // The single source of truth for AuthState
|
||||
|
||||
// Initialize app_state first
|
||||
let mut app_state = AppState::new()?;
|
||||
|
||||
// Fetch profile tree and table structure
|
||||
let profile_tree = grpc_client.get_profile_tree().await?;
|
||||
app_state.profile_tree = profile_tree;
|
||||
|
||||
// Fetch table structure at startup (one-time)
|
||||
let table_structure = grpc_client.get_table_structure().await?;
|
||||
|
||||
// Extract the column names from the response
|
||||
let column_names: Vec<String> = table_structure
|
||||
.columns
|
||||
.iter()
|
||||
.map(|col| col.name.clone())
|
||||
.collect();
|
||||
// Initialize app state with profile tree and table structure
|
||||
let column_names = UiService::initialize_app_state(&mut grpc_client, &mut app_state).await?;
|
||||
|
||||
// Initialize FormState with dynamic fields
|
||||
let mut form_state = FormState::new(column_names);
|
||||
|
||||
// The rest of your UI initialization remains the same
|
||||
let mut event_handler = EventHandler::new();
|
||||
// Initialize EventHandler (which now contains AuthClient)
|
||||
let mut event_handler = EventHandler::new().await?;
|
||||
let event_reader = EventReader::new();
|
||||
|
||||
// Fetch the total count of Adresar entries
|
||||
let total_count = grpc_client.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
|
||||
UiService::initialize_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||
form_state.reset_to_empty();
|
||||
|
||||
loop {
|
||||
let total_count = grpc_client.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
UiService::update_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||
|
||||
// Determine edit mode based on EventHandler state
|
||||
let is_edit_mode = event_handler.is_edit_mode;
|
||||
|
||||
terminal.draw(|f| {
|
||||
render_ui(
|
||||
f,
|
||||
&mut form_state,
|
||||
&mut auth_state,
|
||||
&mut auth_state, // Pass the single AuthState instance
|
||||
&theme,
|
||||
event_handler.is_edit_mode,
|
||||
is_edit_mode, // Use determined edit mode
|
||||
app_state.total_count,
|
||||
app_state.current_position,
|
||||
&app_state.current_dir,
|
||||
@@ -82,6 +75,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
&mut grpc_client,
|
||||
&mut command_handler,
|
||||
&mut form_state,
|
||||
&mut auth_state, // Pass the single AuthState instance here too
|
||||
&mut app_state,
|
||||
total_count,
|
||||
&mut current_position,
|
||||
@@ -89,73 +83,77 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
app_state.current_position = current_position;
|
||||
|
||||
// Handle position changes and update form state
|
||||
if !event_handler.is_edit_mode {
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1 // Limit to last character in readonly mode
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
// Handle position changes and update form state (Only when form is shown)
|
||||
if app_state.ui.show_form { // Added check
|
||||
if !event_handler.is_edit_mode {
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1 // Limit to last character in readonly mode
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
|
||||
// Ensure position never exceeds total_count + 1
|
||||
if app_state.current_position > total_count + 1 {
|
||||
app_state.current_position = total_count + 1;
|
||||
}
|
||||
if app_state.current_position > total_count {
|
||||
// New entry - reset form
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
||||
// Existing entry - load data
|
||||
match grpc_client.get_adresar_by_position(app_state.current_position).await {
|
||||
Ok(response) => {
|
||||
// Set the ID properly
|
||||
form_state.id = response.id;
|
||||
|
||||
// Update form values dynamically
|
||||
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 !event_handler.is_edit_mode && !current_input.is_empty() {
|
||||
current_input.len() - 1 // In readonly mode, limit to last character
|
||||
} else {
|
||||
current_input.len()
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
form_state.has_unsaved_changes = false;
|
||||
event_handler.command_message = format!("Loaded entry {}", app_state.current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
event_handler.command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
// Ensure position never exceeds total_count + 1
|
||||
if app_state.current_position > total_count + 1 {
|
||||
app_state.current_position = total_count + 1;
|
||||
}
|
||||
if app_state.current_position > total_count {
|
||||
// New entry - reset form
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
||||
// Existing entry - load data
|
||||
let current_position_to_load = app_state.current_position; // Use a copy
|
||||
let load_message = UiService::load_adresar_by_position(
|
||||
&mut grpc_client,
|
||||
&mut app_state, // Pass app_state mutably if needed by the service
|
||||
&mut form_state,
|
||||
current_position_to_load
|
||||
).await?;
|
||||
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() {
|
||||
current_input.len() - 1 // In readonly mode, limit to last character
|
||||
} else {
|
||||
current_input.len()
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
// Don't overwrite message from handle_event if load_message is simple success
|
||||
if !load_message.starts_with("Loaded entry") || message.is_empty() {
|
||||
event_handler.command_message = load_message;
|
||||
}
|
||||
} else {
|
||||
// Invalid position (e.g., 0) - reset to first entry or new entry mode
|
||||
app_state.current_position = 1.min(total_count + 1); // Go to 1 or new entry if empty
|
||||
if app_state.current_position > total_count {
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Invalid position - reset to first entry
|
||||
app_state.current_position = 1;
|
||||
}
|
||||
} else if app_state.ui.show_login {
|
||||
// Handle cursor updates for AuthState if needed, similar to FormState
|
||||
if !event_handler.is_edit_mode {
|
||||
let current_input = auth_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
auth_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Only update command message if handle_event provided one
|
||||
if !message.is_empty() {
|
||||
event_handler.command_message = message;
|
||||
}
|
||||
|
||||
event_handler.command_message = message;
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user