62 lines
2.0 KiB
Rust
62 lines
2.0 KiB
Rust
// src/client/ui/handlers/ui.rs
|
|
|
|
use crossterm::event::Event;
|
|
use crate::client::terminal::AppTerminal;
|
|
use crate::client::colors::Theme;
|
|
use crate::client::config::Config;
|
|
use ratatui::layout::Rect;
|
|
use super::{FormState, EventHandler, AppState};
|
|
use super::render::render_ui;
|
|
|
|
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|
let config = Config::load()?;
|
|
let mut app_terminal = AppTerminal::new().await?;
|
|
let theme = Theme::dark();
|
|
|
|
let mut form_state = FormState::new();
|
|
let mut event_handler = EventHandler::new();
|
|
let mut app_state = AppState::new()?;
|
|
|
|
// Fetch the total count of Adresar entries
|
|
let total_count = app_terminal.get_adresar_count().await?;
|
|
app_state.update_total_count(total_count.try_into().unwrap());
|
|
app_state.update_current_position((total_count + 1).try_into().unwrap());
|
|
|
|
loop {
|
|
// Fetch fresh total count on each iteration
|
|
let total_count = app_terminal.get_adresar_count().await?;
|
|
app_state.update_total_count(total_count.try_into().unwrap());
|
|
|
|
app_terminal.draw(|f| {
|
|
render_ui(
|
|
f,
|
|
&mut form_state,
|
|
&theme,
|
|
event_handler.is_edit_mode,
|
|
app_state.total_count,
|
|
app_state.current_position,
|
|
&app_state.current_dir,
|
|
&event_handler.command_input,
|
|
event_handler.command_mode,
|
|
&event_handler.command_message,
|
|
);
|
|
})?;
|
|
|
|
let event = app_terminal.read_event()?;
|
|
let (should_exit, message) = event_handler.handle_event(
|
|
event,
|
|
&config,
|
|
&mut app_terminal,
|
|
&mut form_state,
|
|
&mut app_state.is_saved,
|
|
app_state.total_count,
|
|
&mut app_state.current_position,
|
|
).await?;
|
|
|
|
event_handler.command_message = message;
|
|
if should_exit {
|
|
return Ok(());
|
|
}
|
|
}
|
|
}
|