From 02855c4c460a2c4775c21d2ea0ab8dff3f1cb8d4 Mon Sep 17 00:00:00 2001 From: filipriec Date: Tue, 18 Feb 2025 16:32:18 +0100 Subject: [PATCH] trying to do the count --- src/client/components/form.rs | 23 ++++++++++++++++++++++- src/client/terminal.rs | 15 +++++++++++++++ src/client/ui.rs | 13 ++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/client/components/form.rs b/src/client/components/form.rs index 2c8a790..d750f26 100644 --- a/src/client/components/form.rs +++ b/src/client/components/form.rs @@ -16,6 +16,8 @@ pub fn render_form( inputs: &[&String], theme: &Theme, is_edit_mode: bool, + total_count: u64, // Add total count + current_id: u64, // Add current ID ) { // Create Adresar card let adresar_card = Block::default() @@ -60,10 +62,29 @@ pub fn render_form( // Input area inside borders let input_area = input_container.inner(input_container_area); + + // Add a new layout for the count and ID display + let count_id_layout = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Length(1), // For count and ID + Constraint::Min(1), // For the rest of the form + ]) + .split(input_area); + + // Render the count and ID + let count_id_text = format!("Total: {} | Current ID: {}", total_count, current_id); + let count_id_paragraph = Paragraph::new(count_id_text) + .style(Style::default().fg(theme.fg)) + .alignment(Alignment::Left); + + f.render_widget(count_id_paragraph, count_id_layout[0]); + + // Split the remaining area for the form inputs let input_rows = Layout::default() .direction(Direction::Vertical) .constraints(vec![Constraint::Length(1); fields.len()]) - .split(input_area); + .split(count_id_layout[1]); // Render labels close to the border for (i, field) in fields.iter().enumerate() { diff --git a/src/client/terminal.rs b/src/client/terminal.rs index c12f9cb..2a9afaa 100644 --- a/src/client/terminal.rs +++ b/src/client/terminal.rs @@ -9,6 +9,7 @@ use std::io::{self, stdout}; use tonic::transport::Channel; use crate::proto::multieko2::adresar_client::AdresarClient; use crate::client::config::Config; +use crate::proto::multieko2::{Empty, CountResponse, PositionRequest, AdresarResponse}; use crate::proto::multieko2::PostAdresarRequest; pub struct AppTerminal { @@ -83,4 +84,18 @@ impl AppTerminal { _ => Ok((false, format!("Action not recognized: {}", action))), } } + + // Add a method to get the total count of Adresar entries + pub async fn get_adresar_count(&self) -> Result> { + let request = tonic::Request::new(Empty {}); + let response: CountResponse = self.grpc_client.get_adresar_count(request).await?.into_inner(); + Ok(response.count as u64) + } + + // Add a method to get an Adresar entry by its position + pub async fn get_adresar_by_position(&self, position: u64) -> Result> { + let request = tonic::Request::new(PositionRequest { position: position as i64 }); + let response: AdresarResponse = self.grpc_client.get_adresar_by_position(request).await?.into_inner(); + Ok(response) + } } diff --git a/src/client/ui.rs b/src/client/ui.rs index f32631b..8e8d464 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -8,6 +8,7 @@ use ratatui::layout::{Constraint, Direction, Layout}; use std::env; use crate::proto::multieko2::PostAdresarRequest; + pub async fn run_ui() -> Result<(), Box> { let config = Config::load()?; let mut app_terminal = AppTerminal::new().await?; @@ -51,6 +52,14 @@ pub async fn run_ui() -> Result<(), Box> { let mut is_edit_mode = false; let mut edit_mode_cooldown = false; + // Fetch the total count of Adresar entries + let total_count = app_terminal.get_adresar_count().await?; + + // Fetch the current Adresar entry by position (e.g., position 1) + let current_position = 1; // You can change this dynamically based on user input + let current_adresar = app_terminal.get_adresar_by_position(current_position).await?; + let current_id = current_adresar.id as u64; + loop { app_terminal.draw(|f| { let root = Layout::default() @@ -80,6 +89,8 @@ pub async fn run_ui() -> Result<(), Box> { ], &theme, is_edit_mode, + total_count, // Pass total count + current_id, // Pass current ID ); // Right panel - Preview Card @@ -99,7 +110,7 @@ pub async fn run_ui() -> Result<(), Box> { render_command_line(f, root[2], &command_input, command_mode, &theme, &command_message); })?; - // Event handling + // Event handling (unchanged) if let Event::Key(key) = app_terminal.read_event()? { // Handle enter/edit mode keys if !is_edit_mode && config.is_enter_edit_mode(key.code, key.modifiers) {