From 32210a5f7c2c630689ee01e888cbd68efd294688 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 25 May 2025 22:24:26 +0200 Subject: [PATCH] killing of the buffer now works amazingly well --- client/src/components/handlers/buffer_list.rs | 7 +++++- client/src/functions/common/buffer.rs | 2 +- client/src/modes/handlers/event.rs | 13 +++++++---- client/src/state/app/buffer.rs | 23 +++++++++++++++---- client/src/tui/functions/intro.rs | 5 +--- client/src/ui/handlers/render.rs | 2 +- client/src/ui/handlers/ui.rs | 4 ++-- 7 files changed, 38 insertions(+), 18 deletions(-) diff --git a/client/src/components/handlers/buffer_list.rs b/client/src/components/handlers/buffer_list.rs index 079d2f1..a8283d6 100644 --- a/client/src/components/handlers/buffer_list.rs +++ b/client/src/components/handlers/buffer_list.rs @@ -2,6 +2,7 @@ use crate::config::colors::themes::Theme; use crate::state::app::buffer::BufferState; +use crate::state::app::state::AppState; // Add this import use ratatui::{ layout::{Alignment, Rect}, style::Style, @@ -17,6 +18,7 @@ pub fn render_buffer_list( area: Rect, theme: &Theme, buffer_state: &BufferState, + app_state: &AppState, // Add this parameter ) { // --- Style Definitions --- let active_style = Style::default() @@ -37,6 +39,9 @@ pub fn render_buffer_list( let mut spans = Vec::new(); let mut current_width = 0; + // TODO: Replace with actual table name from server response + let current_table_name = Some("2025_customer"); + for (original_index, view) in buffer_state.history.iter().enumerate() { // Filter: Only process views matching the active layer if get_view_layer(view) != active_layer { @@ -44,7 +49,7 @@ pub fn render_buffer_list( } let is_active = original_index == buffer_state.active_index; - let buffer_name = view.display_name(); + let buffer_name = view.display_name_with_context(current_table_name); let buffer_text = format!(" {} ", buffer_name); let text_width = UnicodeWidthStr::width(buffer_text.as_str()); diff --git a/client/src/functions/common/buffer.rs b/client/src/functions/common/buffer.rs index a1d8ba4..ee4ef33 100644 --- a/client/src/functions/common/buffer.rs +++ b/client/src/functions/common/buffer.rs @@ -7,7 +7,7 @@ pub fn get_view_layer(view: &AppView) -> u8 { match view { AppView::Intro => 1, AppView::Login | AppView::Register | AppView::Admin | AppView::AddTable | AppView::AddLogic => 2, - AppView::Form(_) | AppView::Scratch => 3, + AppView::Form | AppView::Scratch => 3, } } diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index 45f7dc8..d0cd8f7 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -121,10 +121,7 @@ impl EventHandler { else if ui.show_admin { AppView::Admin } else if ui.show_add_logic { AppView::AddLogic } else if ui.show_add_table { AppView::AddTable } - else if ui.show_form { - let form_name = app_state.selected_profile.clone().unwrap_or_else(|| "Data Form".to_string()); - AppView::Form(form_name) - } + else if ui.show_form { AppView::Form } // Remove the dynamic name part else { AppView::Scratch } }; buffer_state.update_history(current_view); @@ -177,8 +174,14 @@ impl EventHandler { return Ok(EventOutcome::Ok("Switched to previous buffer".to_string())); } } + //"close_buffer" => { + // let message = buffer_state.close_buffer_with_intro_fallback(); + // return Ok(EventOutcome::Ok(message)); + //} "close_buffer" => { - let message = buffer_state.close_buffer_with_intro_fallback(); + // TODO: Replace with actual table name from server response + let current_table_name = Some("2025_customer"); // Your hardcoded table name + let message = buffer_state.close_buffer_with_intro_fallback(current_table_name); return Ok(EventOutcome::Ok(message)); } _ => {} diff --git a/client/src/state/app/buffer.rs b/client/src/state/app/buffer.rs index 83ec5b6..753b15e 100644 --- a/client/src/state/app/buffer.rs +++ b/client/src/state/app/buffer.rs @@ -8,11 +8,13 @@ pub enum AppView { Admin, AddTable, AddLogic, - Form(String), + Form, // Remove the String parameter Scratch, } impl AppView { + /// Returns the display name for the view. + /// For Form, pass the current table name to get dynamic naming. pub fn display_name(&self) -> &str { match self { AppView::Intro => "Intro", @@ -21,12 +23,25 @@ impl AppView { AppView::Admin => "Admin_Panel", AppView::AddTable => "Add_Table", AppView::AddLogic => "Add_Logic", - AppView::Form(name) => name.as_str(), + AppView::Form => "Form", // Default fallback AppView::Scratch => "*scratch*", } } + + /// Returns the display name with dynamic context (for Form buffers) + pub fn display_name_with_context(&self, current_table_name: Option<&str>) -> String { + match self { + AppView::Form => { + current_table_name + .unwrap_or("Data Form") + .to_string() + } + _ => self.display_name().to_string(), + } + } } +// Rest of BufferState implementation remains the same #[derive(Debug, Clone)] pub struct BufferState { pub history: Vec, @@ -80,7 +95,7 @@ impl BufferState { true } - pub fn close_buffer_with_intro_fallback(&mut self) -> String { + pub fn close_buffer_with_intro_fallback(&mut self, current_table_name: Option<&str>) -> String { let current_view_cloned = self.get_active_view().cloned(); if let Some(AppView::Intro) = current_view_cloned { return "Cannot close intro buffer".to_string(); @@ -88,7 +103,7 @@ impl BufferState { let closed_name = current_view_cloned .as_ref() - .map(|v| v.display_name().to_string()) + .map(|v| v.display_name_with_context(current_table_name)) .unwrap_or_else(|| "Unknown".to_string()); if self.close_active_buffer() { diff --git a/client/src/tui/functions/intro.rs b/client/src/tui/functions/intro.rs index ca0d055..89be99c 100644 --- a/client/src/tui/functions/intro.rs +++ b/client/src/tui/functions/intro.rs @@ -13,10 +13,7 @@ pub fn handle_intro_selection( index: usize, ) { let target_view = match index { - 0 => { - let form_name = app_state.selected_profile.clone().unwrap_or_else(|| "Data Form".to_string()); - AppView::Form(form_name) - } + 0 => AppView::Form, 1 => AppView::Admin, 2 => AppView::Login, 3 => AppView::Register, diff --git a/client/src/ui/handlers/render.rs b/client/src/ui/handlers/render.rs index c688908..4094380 100644 --- a/client/src/ui/handlers/render.rs +++ b/client/src/ui/handlers/render.rs @@ -201,7 +201,7 @@ pub fn render_ui( // Render buffer list if enabled and area is available if let Some(area) = buffer_list_area { if app_state.ui.show_buffer_list { - render_buffer_list(f, area, theme, buffer_state); + render_buffer_list(f, area, theme, buffer_state, app_state); } } render_status_line(f, status_line_area, current_dir, theme, is_edit_mode, current_fps); diff --git a/client/src/ui/handlers/ui.rs b/client/src/ui/handlers/ui.rs index 37e28e9..ccfeaee 100644 --- a/client/src/ui/handlers/ui.rs +++ b/client/src/ui/handlers/ui.rs @@ -104,7 +104,7 @@ pub async fn run_ui() -> Result<()> { // --- DATA2: Adjust initial view based on auth status --- if auto_logged_in { // User is auto-logged in, go to main app view - buffer_state.history = vec![AppView::Form("Adresar".to_string())]; + buffer_state.history = vec![AppView::Form]; buffer_state.active_index = 0; info!("Initial view set to Form due to auto-login."); } @@ -163,7 +163,7 @@ pub async fn run_ui() -> Result<()> { } AppView::AddTable => app_state.ui.show_add_table = true, AppView::AddLogic => app_state.ui.show_add_logic = true, - AppView::Form(_) => app_state.ui.show_form = true, + AppView::Form => app_state.ui.show_form = true, AppView::Scratch => {} // Or show a scratchpad component } }