terminal.rs huge changes

This commit is contained in:
filipriec
2025-03-21 12:05:18 +01:00
parent d3f6296d67
commit 5edc286854
12 changed files with 247 additions and 207 deletions

View File

@@ -1,6 +1,8 @@
// src/client/ui/handlers/ui.rs
use crate::tui::terminal::AppTerminal;
use crate::tui::terminal::TerminalCore;
use crate::tui::terminal::GrpcClient;
use crate::tui::terminal::CommandHandler;
use crate::config::colors::Theme;
use crate::config::config::Config;
use crate::ui::handlers::{form::FormState, render::render_ui};
@@ -10,12 +12,14 @@ use crate::state::state::AppState;
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::load()?;
let mut app_terminal = AppTerminal::new().await?;
let mut terminal = TerminalCore::new()?; // Remove .await
let mut grpc_client = GrpcClient::new().await?;
let mut command_handler = CommandHandler::new(grpc_client);
let theme = Theme::from_str(&config.colors.theme);
// Fetch table structure at startup (one-time)
// TODO: Later, consider implementing a live update for table structure changes.
let table_structure = app_terminal.get_table_structure().await?;
let table_structure = grpc_client.get_table_structure().await?; // Changed
// Extract the column names from the response
let column_names: Vec<String> = table_structure
@@ -32,16 +36,16 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let mut app_state = AppState::new()?;
// Fetch the total count of Adresar entries
let total_count = app_terminal.get_adresar_count().await?;
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
form_state.reset_to_empty();
loop {
let total_count = app_terminal.get_adresar_count().await?;
let total_count = grpc_client.get_adresar_count().await?;
app_state.update_total_count(total_count);
app_terminal.draw(|f| {
terminal.draw(|f| {
render_ui(
f,
&mut form_state,
@@ -56,11 +60,13 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
);
})?;
let event = app_terminal.read_event()?;
let event = event_handler.read_event()?;
let (should_exit, message) = event_handler.handle_event(
event,
&config,
&mut app_terminal,
&mut terminal,
&mut grpc_client,
&mut command_handler,
&mut form_state,
&mut app_state.is_saved,
app_state.total_count,
@@ -88,7 +94,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
form_state.current_field = 0;
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
// Existing entry - load data
match app_terminal.get_adresar_by_position(app_state.current_position).await {
match grpc_client.get_adresar_by_position(app_state.current_position).await {
Ok(response) => {
// Set the ID properly
form_state.id = response.id;