gRPC implemented back
This commit is contained in:
@@ -6,19 +6,27 @@ use crossterm::{
|
|||||||
};
|
};
|
||||||
use ratatui::{backend::CrosstermBackend, Terminal};
|
use ratatui::{backend::CrosstermBackend, Terminal};
|
||||||
use std::io::{self, stdout};
|
use std::io::{self, stdout};
|
||||||
|
use tonic::transport::Channel;
|
||||||
|
use crate::proto::multieko2::adresar_client::AdresarClient;
|
||||||
|
use crate::proto::multieko2::AdresarRequest;
|
||||||
|
|
||||||
pub struct AppTerminal {
|
pub struct AppTerminal {
|
||||||
terminal: Terminal<CrosstermBackend<io::Stdout>>,
|
terminal: Terminal<CrosstermBackend<io::Stdout>>,
|
||||||
|
grpc_client: AdresarClient<Channel>, // gRPC client
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppTerminal {
|
impl AppTerminal {
|
||||||
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
enable_raw_mode()?; // Moved before execute!
|
enable_raw_mode()?;
|
||||||
let mut stdout = stdout();
|
let mut stdout = stdout();
|
||||||
execute!(stdout, EnterAlternateScreen)?;
|
execute!(stdout, EnterAlternateScreen)?;
|
||||||
let backend = CrosstermBackend::new(stdout);
|
let backend = CrosstermBackend::new(stdout);
|
||||||
let terminal = Terminal::new(backend)?;
|
let terminal = Terminal::new(backend)?;
|
||||||
Ok(Self { terminal })
|
|
||||||
|
// Initialize gRPC client
|
||||||
|
let grpc_client = AdresarClient::connect("http://[::1]:50051").await?;
|
||||||
|
|
||||||
|
Ok(Self { terminal, grpc_client })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw<F>(&mut self, f: F) -> Result<(), Box<dyn std::error::Error>>
|
pub fn draw<F>(&mut self, f: F) -> Result<(), Box<dyn std::error::Error>>
|
||||||
@@ -39,37 +47,39 @@ impl AppTerminal {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_command(&mut self, command_input: &str, is_saved: &mut bool) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
pub async fn handle_command(
|
||||||
|
&mut self,
|
||||||
|
command_input: &str,
|
||||||
|
is_saved: &mut bool,
|
||||||
|
form_data: &AdresarRequest, // Pass form data here
|
||||||
|
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||||
match command_input {
|
match command_input {
|
||||||
"w" => {
|
"w" => {
|
||||||
// Save the state
|
// Send data to the server
|
||||||
|
let request = tonic::Request::new(form_data.clone());
|
||||||
|
let response = self.grpc_client.create_adresar(request).await?;
|
||||||
|
|
||||||
*is_saved = true;
|
*is_saved = true;
|
||||||
Ok((false, "State saved.".to_string())) // Do not exit
|
Ok((false, format!("State saved. Response: {:?}", response)))
|
||||||
}
|
}
|
||||||
"q" => {
|
"q" => {
|
||||||
// Quit if saved
|
|
||||||
if *is_saved {
|
if *is_saved {
|
||||||
self.cleanup()?;
|
self.cleanup()?;
|
||||||
Ok((true, "Exiting.".to_string())) // Exit
|
Ok((true, "Exiting.".to_string()))
|
||||||
} else {
|
} else {
|
||||||
Ok((false, "No changes saved. Use :q! to force quit.".to_string())) // Do not exit
|
Ok((false, "No changes saved. Use :q! to force quit.".to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"q!" => {
|
"q!" => {
|
||||||
// Force quit without saving
|
|
||||||
self.cleanup()?;
|
self.cleanup()?;
|
||||||
Ok((true, "Force exiting without saving.".to_string())) // Exit
|
Ok((true, "Force exiting without saving.".to_string()))
|
||||||
}
|
}
|
||||||
"wq" => {
|
"wq" => {
|
||||||
// Save and quit
|
|
||||||
*is_saved = true;
|
*is_saved = true;
|
||||||
self.cleanup()?;
|
self.cleanup()?;
|
||||||
Ok((true, "State saved. Exiting.".to_string())) // Exit
|
Ok((true, "State saved. Exiting.".to_string()))
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
// Handle other commands here
|
|
||||||
Ok((false, format!("Command not recognized: {}", command_input))) // Do not exit
|
|
||||||
}
|
}
|
||||||
|
_ => Ok((false, format!("Command not recognized: {}", command_input))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,10 @@ use crate::client::components::{render_command_line, render_form, render_preview
|
|||||||
use crate::client::colors::Theme;
|
use crate::client::colors::Theme;
|
||||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use crate::proto::multieko2::AdresarRequest;
|
||||||
|
|
||||||
pub fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut app_terminal = AppTerminal::new()?;
|
let mut app_terminal = AppTerminal::new().await?;
|
||||||
let mut command_mode = false;
|
let mut command_mode = false;
|
||||||
let mut command_input = String::new();
|
let mut command_input = String::new();
|
||||||
let theme = Theme::dark();
|
let theme = Theme::dark();
|
||||||
@@ -98,9 +99,28 @@ pub fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
if command_mode {
|
if command_mode {
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
// Handle the command
|
// Create AdresarRequest from form data
|
||||||
let (should_exit, message) = app_terminal.handle_command(&command_input, &mut is_saved)?;
|
let form_data = AdresarRequest {
|
||||||
command_message = message; // Set the message to display
|
firma: firma.clone(),
|
||||||
|
kz: kz.clone(),
|
||||||
|
drc: drc.clone(),
|
||||||
|
ulica: ulica.clone(),
|
||||||
|
psc: psc.clone(),
|
||||||
|
mesto: mesto.clone(),
|
||||||
|
stat: stat.clone(),
|
||||||
|
banka: banka.clone(),
|
||||||
|
ucet: ucet.clone(),
|
||||||
|
skladm: skladm.clone(),
|
||||||
|
ico: ico.clone(),
|
||||||
|
kontakt: kontakt.clone(),
|
||||||
|
telefon: telefon.clone(),
|
||||||
|
skladu: skladu.clone(),
|
||||||
|
fax: fax.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Pass form data to handle_command
|
||||||
|
let (should_exit, message) = app_terminal.handle_command(&command_input, &mut is_saved, &form_data).await?;
|
||||||
|
command_message = message;
|
||||||
if should_exit {
|
if should_exit {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -109,12 +129,12 @@ pub fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
KeyCode::Char(c) => command_input.push(c),
|
KeyCode::Char(c) => command_input.push(c),
|
||||||
KeyCode::Backspace => {
|
KeyCode::Backspace => {
|
||||||
command_input.pop();
|
command_input.pop(); // Ignore the return value
|
||||||
}
|
}
|
||||||
KeyCode::Esc => {
|
KeyCode::Esc => {
|
||||||
command_mode = false;
|
command_mode = false;
|
||||||
command_input.clear();
|
command_input.clear();
|
||||||
command_message.clear(); // Clear the message when exiting command mode
|
command_message.clear();
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -123,7 +143,7 @@ pub fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
KeyCode::Char(':') => {
|
KeyCode::Char(':') => {
|
||||||
command_mode = true;
|
command_mode = true;
|
||||||
command_input.clear();
|
command_input.clear();
|
||||||
command_message.clear(); // Clear the message when entering command mode
|
command_message.clear();
|
||||||
}
|
}
|
||||||
KeyCode::Tab => {
|
KeyCode::Tab => {
|
||||||
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||||
@@ -135,10 +155,7 @@ pub fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
KeyCode::BackTab => current_field = current_field.saturating_sub(1),
|
KeyCode::BackTab => current_field = current_field.saturating_sub(1),
|
||||||
KeyCode::Down => current_field = (current_field + 1) % fields.len(),
|
KeyCode::Down => current_field = (current_field + 1) % fields.len(),
|
||||||
KeyCode::Up => current_field = current_field.saturating_sub(1),
|
KeyCode::Up => current_field = current_field.saturating_sub(1),
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => current_field = (current_field + 1) % fields.len(),
|
||||||
// Move to the next field, wrapping around to the first field if necessary
|
|
||||||
current_field = (current_field + 1) % fields.len();
|
|
||||||
}
|
|
||||||
KeyCode::Char(c) => {
|
KeyCode::Char(c) => {
|
||||||
match current_field {
|
match current_field {
|
||||||
0 => firma.push(c),
|
0 => firma.push(c),
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ 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()?,
|
Some("client") => client::run_ui().await?,
|
||||||
_ => println!("Usage: cargo run -- [server|client]"),
|
_ => println!("Usage: cargo run -- [server|client]"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user