terminal.rs huge changes
This commit is contained in:
48
client/src/tui/terminal/commands.rs
Normal file
48
client/src/tui/terminal/commands.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
// src/tui/terminal/commands.rs
|
||||
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::tui::terminal::grpc_client::GrpcClient;
|
||||
|
||||
pub struct CommandHandler {
|
||||
grpc_client: GrpcClient,
|
||||
is_saved: bool,
|
||||
}
|
||||
|
||||
impl CommandHandler {
|
||||
pub fn new(grpc_client: GrpcClient) -> Self {
|
||||
Self { grpc_client, is_saved: false }
|
||||
}
|
||||
|
||||
pub async fn handle_command(
|
||||
&mut self,
|
||||
action: &str,
|
||||
terminal: &mut TerminalCore
|
||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"quit" => self.handle_quit(terminal).await,
|
||||
"force_quit" => self.handle_force_quit(terminal).await,
|
||||
"save_and_quit" => self.handle_save_quit(terminal).await,
|
||||
_ => Ok((false, format!("Unknown command: {}", action))),
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_quit(&self, terminal: &mut TerminalCore) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
if self.is_saved {
|
||||
terminal.cleanup()?;
|
||||
Ok((true, "Exiting.".into()))
|
||||
} else {
|
||||
Ok((false, "No changes saved. Use :q! to force quit.".into()))
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_force_quit(&self, terminal: &mut TerminalCore) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
terminal.cleanup()?;
|
||||
Ok((true, "Force exiting without saving.".into()))
|
||||
}
|
||||
|
||||
async fn handle_save_quit(&mut self, terminal: &mut TerminalCore) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
self.is_saved = true;
|
||||
terminal.cleanup()?;
|
||||
Ok((true, "State saved. Exiting.".into()))
|
||||
}
|
||||
}
|
||||
65
client/src/tui/terminal/core.rs
Normal file
65
client/src/tui/terminal/core.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
// src/tui/terminal/core.rs
|
||||
|
||||
use crossterm::{
|
||||
execute,
|
||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||
cursor::{SetCursorStyle, EnableBlinking, Show, MoveTo},
|
||||
event::Event,
|
||||
};
|
||||
use ratatui::{backend::CrosstermBackend, Terminal};
|
||||
use std::io::{self, stdout, Write};
|
||||
|
||||
pub struct TerminalCore {
|
||||
terminal: Terminal<CrosstermBackend<io::Stdout>>,
|
||||
}
|
||||
|
||||
impl TerminalCore {
|
||||
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
enable_raw_mode()?;
|
||||
let mut stdout = stdout();
|
||||
execute!(
|
||||
stdout,
|
||||
EnterAlternateScreen,
|
||||
SetCursorStyle::SteadyBlock,
|
||||
EnableBlinking
|
||||
)?;
|
||||
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 cleanup(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut stdout = stdout();
|
||||
execute!(stdout, Show)?;
|
||||
execute!(stdout, SetCursorStyle::DefaultUserShape)?;
|
||||
execute!(stdout, LeaveAlternateScreen)?;
|
||||
disable_raw_mode()?;
|
||||
stdout.flush()?;
|
||||
execute!(
|
||||
stdout,
|
||||
crossterm::terminal::Clear(crossterm::terminal::ClearType::All),
|
||||
MoveTo(0, 0)
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_cursor_style(
|
||||
&mut self,
|
||||
style: SetCursorStyle,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
execute!(
|
||||
self.terminal.backend_mut(),
|
||||
style,
|
||||
EnableBlinking,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
15
client/src/tui/terminal/events.rs
Normal file
15
client/src/tui/terminal/events.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
// src/tui/terminal/events.rs
|
||||
|
||||
use crossterm::event::{self, Event};
|
||||
|
||||
pub struct EventHandler;
|
||||
|
||||
impl EventHandler {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
pub fn read_event(&self) -> Result<Event, Box<dyn std::error::Error>> {
|
||||
Ok(event::read()?)
|
||||
}
|
||||
}
|
||||
51
client/src/tui/terminal/grpc_client.rs
Normal file
51
client/src/tui/terminal/grpc_client.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
// src/tui/terminal/grpc_client.rs
|
||||
|
||||
use tonic::transport::Channel;
|
||||
use common::proto::multieko2::adresar::adresar_client::AdresarClient;
|
||||
use common::proto::multieko2::adresar::{AdresarResponse, PostAdresarRequest, PutAdresarRequest};
|
||||
use common::proto::multieko2::common::{CountResponse, PositionRequest, Empty};
|
||||
use common::proto::multieko2::table_structure::table_structure_service_client::TableStructureServiceClient;
|
||||
use common::proto::multieko2::table_structure::TableStructureResponse;
|
||||
|
||||
pub struct GrpcClient {
|
||||
adresar_client: AdresarClient<Channel>,
|
||||
table_structure_client: TableStructureServiceClient<Channel>,
|
||||
}
|
||||
|
||||
impl GrpcClient {
|
||||
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let adresar_client = AdresarClient::connect("http://[::1]:50051").await?;
|
||||
let table_structure_client = TableStructureServiceClient::connect("http://[::1]:50051").await?;
|
||||
Ok(Self { adresar_client, table_structure_client })
|
||||
}
|
||||
|
||||
pub async fn get_adresar_count(&mut self) -> Result<u64, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(Empty::default());
|
||||
let response: CountResponse = self.adresar_client.get_adresar_count(request).await?.into_inner();
|
||||
Ok(response.count as u64)
|
||||
}
|
||||
|
||||
pub async fn get_adresar_by_position(&mut self, position: u64) -> Result<AdresarResponse, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(PositionRequest { position: position as i64 });
|
||||
let response: AdresarResponse = self.adresar_client.get_adresar_by_position(request).await?.into_inner();
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn post_adresar(&mut self, request: PostAdresarRequest) -> Result<tonic::Response<AdresarResponse>, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(request);
|
||||
let response = self.adresar_client.post_adresar(request).await?;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn put_adresar(&mut self, request: PutAdresarRequest) -> Result<tonic::Response<AdresarResponse>, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(request);
|
||||
let response = self.adresar_client.put_adresar(request).await?;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn get_table_structure(&mut self) -> Result<TableStructureResponse, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(Empty::default());
|
||||
let response = self.table_structure_client.get_adresar_table_structure(request).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user