anyhow used

This commit is contained in:
filipriec
2025-04-18 19:04:05 +02:00
parent 09ccad2bd4
commit 5a029283a1
12 changed files with 69 additions and 74 deletions

View File

@@ -18,30 +18,28 @@ use crate::state::pages::intro::IntroState;
use crate::state::app::buffer::BufferState;
use crate::state::app::buffer::AppView;
use crate::state::app::state::AppState;
use crate::ui::handlers::context::DialogPurpose; // <-- Add DialogPurpose import
// Import SaveOutcome
use crate::ui::handlers::context::DialogPurpose;
use crate::tui::terminal::{EventReader, TerminalCore};
use crate::ui::handlers::render::render_ui;
use crate::tui::functions::common::login;
use crate::tui::functions::common::login::LoginResult;
use std::time::Instant;
use std::error::Error;
use anyhow::{Context, Result};
use crossterm::cursor::SetCursorStyle;
use crossterm::event as crossterm_event;
use tracing::{info, error};
use tokio::sync::mpsc;
pub async fn run_ui() -> Result<(), Box<dyn Error + Send + Sync>> {
let config = Config::load()?;
pub async fn run_ui() -> Result<()> {
let config = Config::load().context("Failed to load configuration")?;
let theme = Theme::from_str(&config.colors.theme);
let mut terminal = TerminalCore::new()?;
let mut terminal = TerminalCore::new().context("Failed to initialize terminal")?;
let mut grpc_client = GrpcClient::new().await?;
let mut command_handler = CommandHandler::new();
// --- Channel for Login Results ---
let (login_result_sender, mut login_result_receiver) =
mpsc::channel::<LoginResult>(1);
let mut event_handler = EventHandler::new(login_result_sender.clone()).await?;
let mut event_handler = EventHandler::new(login_result_sender.clone()).await.context("Failed to create event handler")?;
let event_reader = EventReader::new();
let mut auth_state = AuthState::default();
@@ -50,16 +48,16 @@ pub async fn run_ui() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut intro_state = IntroState::default();
let mut admin_state = AdminState::default();
let mut buffer_state = BufferState::default();
let mut app_state = AppState::new()?;
let mut app_state = AppState::new().context("Failed to create initial app state")?;
// Initialize app state with profile tree and table structure
let column_names =
UiService::initialize_app_state(&mut grpc_client, &mut app_state)
.await?;
.await.context("Failed to initialize app state from UI service")?;
let mut form_state = FormState::new(column_names);
// Fetch the total count of Adresar entries
UiService::initialize_adresar_count(&mut grpc_client, &mut app_state)
UiService::initialize_adresar_count(&mut grpc_client, &mut app_state).await
.await?;
form_state.reset_to_empty();
@@ -121,7 +119,7 @@ pub async fn run_ui() -> Result<(), Box<dyn Error + Send + Sync>> {
current_fps,
&app_state,
);
})?;
}).context("Terminal draw call failed")?;
// --- Cursor Visibility Logic ---
// (Keep existing cursor logic here - depends on state drawn above)
@@ -132,13 +130,13 @@ pub async fn run_ui() -> Result<(), Box<dyn Error + Send + Sync>> {
AppMode::ReadOnly => {
if !app_state.ui.focus_outside_canvas { terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?; }
else { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; }
terminal.show_cursor()?;
terminal.show_cursor().context("Failed to show cursor in ReadOnly mode")?;
}
AppMode::General => {
if app_state.ui.focus_outside_canvas { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; terminal.show_cursor()?; }
else { terminal.hide_cursor()?; }
}
AppMode::Command => { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; terminal.show_cursor()?; }
AppMode::Command => { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; terminal.show_cursor().context("Failed to show cursor in Command mode")?; }
}
// --- End Cursor Visibility Logic ---
@@ -150,7 +148,7 @@ pub async fn run_ui() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut event_outcome_result = Ok(EventOutcome::Ok(String::new()));
// Poll for events *after* drawing and checking pending actions
if crossterm_event::poll(std::time::Duration::from_millis(20))? {
let event = event_reader.read_event()?;
let event = event_reader.read_event().context("Failed to read terminal event")?;
event_outcome_result = event_handler
.handle_event(
event,
@@ -308,7 +306,7 @@ pub async fn run_ui() -> Result<(), Box<dyn Error + Send + Sync>> {
&mut form_state,
current_position_to_load,
)
.await?;
.await.with_context(|| format!("Failed to load adresar by position: {}", current_position_to_load))?;
let current_input = form_state.get_current_input();
let max_cursor_pos = if !event_handler.is_edit_mode