Command processing

This commit is contained in:
2026-07-23 01:34:04 +02:00
parent d74d94af89
commit 8e83c5cd5b
4 changed files with 47 additions and 38 deletions
+9 -1
View File
@@ -83,7 +83,15 @@ impl<'a, T> Drop for StateGuard<'a, T> {
} }
impl Context { impl Context {
pub async fn event<E: Any + Send + Sync>(&self, event: E) {
self.tx.send(Box::new(event)).await.unwrap()
}
pub async fn close(&self) { pub async fn close(&self) {
self.tx.send(Box::new(Close)).await.unwrap() self.event(Close).await
}
pub async fn refresh(&self) {
self.event(Refresh).await
} }
} }
+35 -20
View File
@@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use pulse_wire::PulseWire; use pulse_wire::{PulseWire, terminal::TerminalClientMessage};
use tokio::{ use tokio::{
io::{AsyncReadExt, AsyncWriteExt}, io::{AsyncReadExt, AsyncWriteExt},
net::{ net::{
@@ -40,37 +40,52 @@ impl TerminalServer {
self.clients.lock().await.push(writer); self.clients.lock().await.push(writer);
let s = self.clone();
tokio::spawn(async move { tokio::spawn(async move {
if let Err(err) = Self::handle_client(reader).await { if let Err(err) = s.handle_client(reader).await {
eprintln!("Terminal connection error: {err}"); eprintln!("Terminal connection error: {err}");
} }
}); });
} }
} }
async fn handle_client(mut reader: OwnedReadHalf) -> tokio::io::Result<()> { async fn handle_client(&self, mut reader: OwnedReadHalf) -> tokio::io::Result<()> {
let input = tokio::spawn(async move { loop {
loop { let mut len_buf = [0u8; size_of::<usize>()];
let mut len_buf = [0u8; size_of::<usize>()]; let size = reader.read_exact(&mut len_buf).await?;
let size = reader.read_exact(&mut len_buf).await?;
let len = usize::from_le_bytes(len_buf); let len = usize::from_le_bytes(len_buf);
if size == 0 || len == 0 { if size == 0 || len == 0 {
break; break;
}
let mut buffer = vec![0u8; len];
reader.read_exact(&mut buffer).await?;
println!("Received {} bytes", len);
} }
Ok::<(), tokio::io::Error>(()) let mut buffer = vec![0u8; len];
});
let _ = tokio::try_join!(input)?; reader.read_exact(&mut buffer).await?;
match TerminalClientMessage::from_com(&mut buffer) {
TerminalClientMessage::ExecuteCommand(command) => {
let command = command.as_str();
let (command, _args) = if let Some((command, args)) = command.split_once(" ") {
(command, args.split(" ").collect())
} else {
(command, Vec::new())
};
self.broadcast(pulse_wire::terminal::TerminalServerMessage::AddLog(
pulse_wire::terminal::EventLog {
kind: pulse_wire::terminal::LogKind::Err,
name: "Command executor".to_string(),
message: format!("Command '{}' not found", command),
},
))
.await?;
}
}
}
Ok(()) Ok(())
} }
+2 -14
View File
@@ -6,24 +6,12 @@ impl PulseTradeApp {
return; return;
} }
let (command, _args) = if let Some((command, args)) = command.split_once(" ") { match command.trim() {
(command, args.split(" ").collect())
} else {
(command, Vec::new())
};
match command {
"exit" | "quit" | "leave" => { "exit" | "quit" | "leave" => {
ctx.close().await; ctx.close().await;
} }
_ => { _ => {}
self.logs.lock().await.push(pulse_wire::terminal::EventLog {
kind: pulse_wire::terminal::LogKind::Err,
name: "cmd".to_string(),
message: format!("Command '{}' not found", command),
});
}
} }
} }
} }
+1 -3
View File
@@ -43,9 +43,7 @@ impl App for PulseTradeApp {
async fn update(&mut self, ctx: &pulse_ui::state::Context, event: Box<dyn Any + Send + Sync>) { async fn update(&mut self, ctx: &pulse_ui::state::Context, event: Box<dyn Any + Send + Sync>) {
if let Some(Refresh) = event.downcast_ref() { if let Some(Refresh) = event.downcast_ref() {
return; return;
} } else if let Some(event) = event.downcast_ref() {
if let Some(event) = event.downcast_ref() {
if self.command.value.lock().await.handle_event(event) { if self.command.value.lock().await.handle_event(event) {
return; return;
} else if let crossterm::event::Event::Key(key) = event { } else if let crossterm::event::Event::Key(key) = event {