From b376b8dfaee3cb743ff790e4363e0b7baa8b5571 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 16 Feb 2025 23:03:40 +0100 Subject: [PATCH] better fractioning of the software --- src/client/components/form.rs | 4 +- src/client/components/mod.rs | 4 + src/client/mod.rs | 3 +- src/client/terminal.rs | 41 +++++++ src/client/ui.rs | 219 ++++++++++++---------------------- src/main.rs | 3 +- 6 files changed, 130 insertions(+), 144 deletions(-) create mode 100644 src/client/terminal.rs diff --git a/src/client/components/form.rs b/src/client/components/form.rs index 13f35e3..0c568ca 100644 --- a/src/client/components/form.rs +++ b/src/client/components/form.rs @@ -1,7 +1,7 @@ // src/client/components/form.rs use ratatui::{ widgets::{Paragraph, Block, Borders}, - layout::{Layout, Constraint, Direction, Rect, Margin}, + layout::{Layout, Constraint, Direction, Rect, Margin, Position}, style::Style, text::{Line, Span}, Frame, @@ -81,7 +81,7 @@ pub fn render_form( if i == *current_field { let cursor_x = input_area.x + input.len() as u16; let cursor_y = input_area.y + i as u16; - f.set_cursor(cursor_x, cursor_y); + f.set_cursor_position(Position::new(cursor_x, cursor_y)); } } } diff --git a/src/client/components/mod.rs b/src/client/components/mod.rs index d6b6735..919140b 100644 --- a/src/client/components/mod.rs +++ b/src/client/components/mod.rs @@ -2,3 +2,7 @@ pub mod form; pub mod preview_card; pub mod command_line; + +pub use command_line::render_command_line; +pub use form::render_form; +pub use preview_card::render_preview_card; diff --git a/src/client/mod.rs b/src/client/mod.rs index a186a9b..eb97178 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -2,5 +2,6 @@ mod ui; mod colors; mod components; +mod terminal; -pub use ui::run_client; +pub use ui::run_ui; diff --git a/src/client/terminal.rs b/src/client/terminal.rs new file mode 100644 index 0000000..85eee8d --- /dev/null +++ b/src/client/terminal.rs @@ -0,0 +1,41 @@ +// src/client/terminal.rs +use crossterm::event::{self, Event}; +use crossterm::{ + execute, + terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, +}; +use ratatui::{backend::CrosstermBackend, Terminal}; +use std::io::{self, stdout}; + +pub struct AppTerminal { + terminal: Terminal>, +} + +impl AppTerminal { + pub fn new() -> Result> { + enable_raw_mode()?; // Moved before execute! + let mut stdout = stdout(); + execute!(stdout, EnterAlternateScreen)?; + let backend = CrosstermBackend::new(stdout); + let terminal = Terminal::new(backend)?; + Ok(Self { terminal }) + } + + pub fn draw(&mut self, f: F) -> Result<(), Box> + where + F: FnOnce(&mut ratatui::Frame), + { + self.terminal.draw(f)?; + Ok(()) + } + + pub fn read_event(&self) -> Result> { + Ok(event::read()?) + } + + pub fn cleanup(&mut self) -> Result<(), Box> { + disable_raw_mode()?; + execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; + Ok(()) + } +} diff --git a/src/client/ui.rs b/src/client/ui.rs index 1ddc291..5c1472f 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -1,30 +1,17 @@ // src/client/ui.rs -use crossterm::{ - event::{self, Event, KeyCode, KeyModifiers}, - execute, - terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, -}; -use ratatui::{ - backend::CrosstermBackend, - layout::{Constraint, Direction, Layout}, - Terminal, -}; -use std::io; -use crate::proto::multieko2::{AdresarRequest, adresar_client::AdresarClient}; +use crossterm::event::{Event, KeyCode, KeyModifiers}; +use crate::client::terminal::AppTerminal; +use crate::client::components::{render_command_line, render_form, render_preview_card}; use crate::client::colors::Theme; -use crate::client::components::{form::render_form, preview_card::render_preview_card, command_line::render_command_line}; +use ratatui::layout::{Constraint, Direction, Layout, Rect}; -pub async fn run_client() -> Result<(), Box> { - // Setup terminal - enable_raw_mode()?; - let mut stdout = io::stdout(); - execute!(stdout, EnterAlternateScreen)?; - let backend = CrosstermBackend::new(stdout); - let mut terminal = Terminal::new(backend)?; +pub fn run_ui() -> Result<(), Box> { + let mut app_terminal = AppTerminal::new()?; + let mut command_mode = false; + let mut command_input = String::new(); + let theme = Theme::dark(); - let mut client = AdresarClient::connect("http://[::1]:50051").await?; - - // Initialize fields + // Initialize form fields let mut firma = String::new(); let mut kz = String::new(); let mut drc = String::new(); @@ -46,12 +33,9 @@ pub async fn run_client() -> Result<(), Box> { "Firma", "KZ", "DRC", "Ulica", "PSC", "Mesto", "Stat", "Banka", "Ucet", "Skladm", "ICO", "Kontakt", "Telefon", "Skladu", "Fax", ]; - let mut command_mode = false; - let mut command_input = String::new(); - let theme = Theme::dark(); loop { - terminal.draw(|f| { + app_terminal.draw(|f| { let root = Layout::default() .direction(Direction::Vertical) .constraints([Constraint::Min(10), Constraint::Length(1)]) @@ -90,43 +74,23 @@ pub async fn run_client() -> Result<(), Box> { render_command_line(f, root[1], &command_input, command_mode, &theme); })?; - if let Event::Key(key) = event::read()? { - if command_mode { - match key.code { - KeyCode::Enter => { - if command_input == "w" { - break; - } - command_mode = false; - command_input.clear(); - } - KeyCode::Char(c) => command_input.push(c), - KeyCode::Backspace => { - command_input.pop(); - } - KeyCode::Esc => { - command_mode = false; - command_input.clear(); - } - _ => {} - } - continue; - } + if let Event::Key(key) = app_terminal.read_event()? { if command_mode { match key.code { KeyCode::Enter => { match command_input.as_str() { "w" => break, // Save and exit "q" => { // Quit without saving - disable_raw_mode()?; - execute!(terminal.backend_mut(), LeaveAlternateScreen)?; + app_terminal.cleanup()?; return Ok(()); } _ => { - command_mode = false; - command_input.clear(); + // Handle other commands here + println!("Command not recognized: {}", command_input); } } + command_mode = false; + command_input.clear(); } KeyCode::Char(c) => command_input.push(c), KeyCode::Backspace => { @@ -138,100 +102,75 @@ pub async fn run_client() -> Result<(), Box> { } _ => {} } - continue; - } - match key.code { - KeyCode::Char(':') => { - command_mode = true; - command_input.clear(); - } - KeyCode::Tab => { - if key.modifiers.contains(KeyModifiers::SHIFT) { - current_field = current_field.saturating_sub(1); - } else { - current_field = (current_field + 1) % fields.len(); + } else { + match key.code { + KeyCode::Char(':') => { + command_mode = true; + command_input.clear(); } - } - KeyCode::BackTab => current_field = current_field.saturating_sub(1), - KeyCode::Down => current_field = (current_field + 1) % fields.len(), - KeyCode::Up => current_field = current_field.saturating_sub(1), - KeyCode::Char(c) => { - match current_field { - 0 => firma.push(c), - 1 => kz.push(c), - 2 => drc.push(c), - 3 => ulica.push(c), - 4 => psc.push(c), - 5 => mesto.push(c), - 6 => stat.push(c), - 7 => banka.push(c), - 8 => ucet.push(c), - 9 => skladm.push(c), - 10 => ico.push(c), - 11 => kontakt.push(c), - 12 => telefon.push(c), - 13 => skladu.push(c), - 14 => fax.push(c), - _ => (), + KeyCode::Tab => { + if key.modifiers.contains(KeyModifiers::SHIFT) { + current_field = current_field.saturating_sub(1); + } else { + current_field = (current_field + 1) % fields.len(); + } } - } - KeyCode::Backspace => { - match current_field { - 0 => firma.pop(), - 1 => kz.pop(), - 2 => drc.pop(), - 3 => ulica.pop(), - 4 => psc.pop(), - 5 => mesto.pop(), - 6 => stat.pop(), - 7 => banka.pop(), - 8 => ucet.pop(), - 9 => skladm.pop(), - 10 => ico.pop(), - 11 => kontakt.pop(), - 12 => telefon.pop(), - 13 => skladu.pop(), - 14 => fax.pop(), - _ => None, - }; - } - KeyCode::Enter => { - if current_field == fields.len() - 1 { - break; - } else { - current_field += 1; + KeyCode::BackTab => current_field = current_field.saturating_sub(1), + KeyCode::Down => current_field = (current_field + 1) % fields.len(), + KeyCode::Up => current_field = current_field.saturating_sub(1), + KeyCode::Char(c) => { + match current_field { + 0 => firma.push(c), + 1 => kz.push(c), + 2 => drc.push(c), + 3 => ulica.push(c), + 4 => psc.push(c), + 5 => mesto.push(c), + 6 => stat.push(c), + 7 => banka.push(c), + 8 => ucet.push(c), + 9 => skladm.push(c), + 10 => ico.push(c), + 11 => kontakt.push(c), + 12 => telefon.push(c), + 13 => skladu.push(c), + 14 => fax.push(c), + _ => (), + } } + KeyCode::Backspace => { + match current_field { + 0 => firma.pop(), + 1 => kz.pop(), + 2 => drc.pop(), + 3 => ulica.pop(), + 4 => psc.pop(), + 5 => mesto.pop(), + 6 => stat.pop(), + 7 => banka.pop(), + 8 => ucet.pop(), + 9 => skladm.pop(), + 10 => ico.pop(), + 11 => kontakt.pop(), + 12 => telefon.pop(), + 13 => skladu.pop(), + 14 => fax.pop(), + _ => None, + }; + } + KeyCode::Enter => { + if current_field == fields.len() - 1 { + break; + } else { + current_field += 1; + } + } + _ => {} } - _ => {} } } } - // Cleanup terminal - disable_raw_mode()?; - execute!(terminal.backend_mut(), LeaveAlternateScreen)?; - - // Create and send request - let request = tonic::Request::new(AdresarRequest { - firma, - kz, - drc, - ulica, - psc, - mesto, - stat, - banka, - ucet, - skladm, - ico, - kontakt, - telefon, - skladu, - fax, - }); - - let response = client.create_adresar(request).await?; - println!("Adresar created: {:?}", response.into_inner()); - + app_terminal.cleanup()?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 91d5bfd..6e7474a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,8 @@ async fn main() -> Result<(), Box> { match env::args().nth(1).as_deref() { Some("server") => server::run_server(db_pool).await?, - Some("client") => client::run_client().await?, + // Some("client") => client::run_client().await?, + Some("client") => client::run_ui()?, _ => println!("Usage: cargo run -- [server|client]"), }