better fractioning of the software
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
// src/client/components/form.rs
|
// src/client/components/form.rs
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
widgets::{Paragraph, Block, Borders},
|
widgets::{Paragraph, Block, Borders},
|
||||||
layout::{Layout, Constraint, Direction, Rect, Margin},
|
layout::{Layout, Constraint, Direction, Rect, Margin, Position},
|
||||||
style::Style,
|
style::Style,
|
||||||
text::{Line, Span},
|
text::{Line, Span},
|
||||||
Frame,
|
Frame,
|
||||||
@@ -81,7 +81,7 @@ pub fn render_form(
|
|||||||
if i == *current_field {
|
if i == *current_field {
|
||||||
let cursor_x = input_area.x + input.len() as u16;
|
let cursor_x = input_area.x + input.len() as u16;
|
||||||
let cursor_y = input_area.y + i 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,3 +2,7 @@
|
|||||||
pub mod form;
|
pub mod form;
|
||||||
pub mod preview_card;
|
pub mod preview_card;
|
||||||
pub mod command_line;
|
pub mod command_line;
|
||||||
|
|
||||||
|
pub use command_line::render_command_line;
|
||||||
|
pub use form::render_form;
|
||||||
|
pub use preview_card::render_preview_card;
|
||||||
|
|||||||
@@ -2,5 +2,6 @@
|
|||||||
mod ui;
|
mod ui;
|
||||||
mod colors;
|
mod colors;
|
||||||
mod components;
|
mod components;
|
||||||
|
mod terminal;
|
||||||
|
|
||||||
pub use ui::run_client;
|
pub use ui::run_ui;
|
||||||
|
|||||||
41
src/client/terminal.rs
Normal file
41
src/client/terminal.rs
Normal file
@@ -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<CrosstermBackend<io::Stdout>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AppTerminal {
|
||||||
|
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
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<F>(&mut self, f: F) -> Result<(), Box<dyn std::error::Error>>
|
||||||
|
where
|
||||||
|
F: FnOnce(&mut ratatui::Frame),
|
||||||
|
{
|
||||||
|
self.terminal.draw(f)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_event(&self) -> Result<Event, Box<dyn std::error::Error>> {
|
||||||
|
Ok(event::read()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cleanup(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
disable_raw_mode()?;
|
||||||
|
execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
101
src/client/ui.rs
101
src/client/ui.rs
@@ -1,30 +1,17 @@
|
|||||||
// src/client/ui.rs
|
// src/client/ui.rs
|
||||||
use crossterm::{
|
use crossterm::event::{Event, KeyCode, KeyModifiers};
|
||||||
event::{self, Event, KeyCode, KeyModifiers},
|
use crate::client::terminal::AppTerminal;
|
||||||
execute,
|
use crate::client::components::{render_command_line, render_form, render_preview_card};
|
||||||
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 crate::client::colors::Theme;
|
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<dyn std::error::Error>> {
|
pub fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
// Setup terminal
|
let mut app_terminal = AppTerminal::new()?;
|
||||||
enable_raw_mode()?;
|
let mut command_mode = false;
|
||||||
let mut stdout = io::stdout();
|
let mut command_input = String::new();
|
||||||
execute!(stdout, EnterAlternateScreen)?;
|
let theme = Theme::dark();
|
||||||
let backend = CrosstermBackend::new(stdout);
|
|
||||||
let mut terminal = Terminal::new(backend)?;
|
|
||||||
|
|
||||||
let mut client = AdresarClient::connect("http://[::1]:50051").await?;
|
// Initialize form fields
|
||||||
|
|
||||||
// Initialize fields
|
|
||||||
let mut firma = String::new();
|
let mut firma = String::new();
|
||||||
let mut kz = String::new();
|
let mut kz = String::new();
|
||||||
let mut drc = String::new();
|
let mut drc = String::new();
|
||||||
@@ -46,12 +33,9 @@ pub async fn run_client() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
"Firma", "KZ", "DRC", "Ulica", "PSC", "Mesto", "Stat", "Banka",
|
"Firma", "KZ", "DRC", "Ulica", "PSC", "Mesto", "Stat", "Banka",
|
||||||
"Ucet", "Skladm", "ICO", "Kontakt", "Telefon", "Skladu", "Fax",
|
"Ucet", "Skladm", "ICO", "Kontakt", "Telefon", "Skladu", "Fax",
|
||||||
];
|
];
|
||||||
let mut command_mode = false;
|
|
||||||
let mut command_input = String::new();
|
|
||||||
let theme = Theme::dark();
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
terminal.draw(|f| {
|
app_terminal.draw(|f| {
|
||||||
let root = Layout::default()
|
let root = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Vertical)
|
||||||
.constraints([Constraint::Min(10), Constraint::Length(1)])
|
.constraints([Constraint::Min(10), Constraint::Length(1)])
|
||||||
@@ -90,44 +74,24 @@ pub async fn run_client() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
render_command_line(f, root[1], &command_input, command_mode, &theme);
|
render_command_line(f, root[1], &command_input, command_mode, &theme);
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if let Event::Key(key) = event::read()? {
|
if let Event::Key(key) = app_terminal.read_event()? {
|
||||||
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 command_mode {
|
if command_mode {
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
match command_input.as_str() {
|
match command_input.as_str() {
|
||||||
"w" => break, // Save and exit
|
"w" => break, // Save and exit
|
||||||
"q" => { // Quit without saving
|
"q" => { // Quit without saving
|
||||||
disable_raw_mode()?;
|
app_terminal.cleanup()?;
|
||||||
execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
// Handle other commands here
|
||||||
|
println!("Command not recognized: {}", command_input);
|
||||||
|
}
|
||||||
|
}
|
||||||
command_mode = false;
|
command_mode = false;
|
||||||
command_input.clear();
|
command_input.clear();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
KeyCode::Char(c) => command_input.push(c),
|
KeyCode::Char(c) => command_input.push(c),
|
||||||
KeyCode::Backspace => {
|
KeyCode::Backspace => {
|
||||||
command_input.pop();
|
command_input.pop();
|
||||||
@@ -138,8 +102,7 @@ pub async fn run_client() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
continue;
|
} else {
|
||||||
}
|
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Char(':') => {
|
KeyCode::Char(':') => {
|
||||||
command_mode = true;
|
command_mode = true;
|
||||||
@@ -206,32 +169,8 @@ pub async fn run_client() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Cleanup terminal
|
app_terminal.cleanup()?;
|
||||||
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());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
match env::args().nth(1).as_deref() {
|
match env::args().nth(1).as_deref() {
|
||||||
Some("server") => server::run_server(db_pool).await?,
|
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]"),
|
_ => println!("Usage: cargo run -- [server|client]"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user