From 6fdb8adc475e9aee1593480d220bc1a78d9db867 Mon Sep 17 00:00:00 2001 From: Saphereye Date: Wed, 14 Feb 2024 18:41:13 +0530 Subject: [PATCH] fix: Improve command performance Earlier in case of help command all the lines would be sent by TCP to server and broadcasted back. This has been fixed --- src/main.rs | 41 +++++++++++++++++++++++++++-------------- src/networking.rs | 26 -------------------------- 2 files changed, 27 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index a3263ba..b0157a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,13 +103,33 @@ fn handle_events( let message = text_area.lines()[0].clone(); if let Some(prefix) = message.strip_prefix('/') { - if prefix == "quit" { - send_message( - stream, - &MessageType::Leave(stream.local_addr().unwrap().to_string()), - )?; - return Ok(true); + match prefix { + "help" => { + message_vector.push(MessageType::Info("".to_string())); + message_vector.push(MessageType::Info(format!( + "Running program version {}, Created by {}", + env!("CARGO_PKG_VERSION"), + env!("CARGO_PKG_AUTHORS") + ))); + + for command in ["help", "quit", "smile", "laugh", "thumbs_up"] { + message_vector.push(MessageType::Info(format!("> {}\n", command))); + } + + message_vector.push(MessageType::Info("".to_string())); + } + "quit" => { + send_message( + stream, + &MessageType::Leave( + stream.local_addr().unwrap().to_string(), + ), + )?; + return Ok(true); + } + _ => {} } + send_message(stream, &MessageType::Command(prefix.to_string()))?; message_vector.push(MessageType::Command(prefix.to_string())); } else if !message.is_empty() { @@ -173,19 +193,12 @@ fn ui( } MessageType::Message(source, message) => { let formatted_message = format!("({}): {}", source, message); - // let padded_message = if *source == local_address { - // format!("{:>1$}", formatted_message, frame.size().width as usize) - // } else { - // formatted_message - // }; Span::styled(formatted_message, Style::default().fg(Color::White)) } MessageType::Error(error) => { Span::styled(error.clone(), Style::default().fg(Color::Red)) } - MessageType::Command(command) => { - Span::styled(command, Style::default()) - } + _ => continue, }; message_lines.push(Line::from(span)); } diff --git a/src/networking.rs b/src/networking.rs index eb271db..e0d2586 100644 --- a/src/networking.rs +++ b/src/networking.rs @@ -170,32 +170,6 @@ pub fn run_server(server_ip: &str) -> Result<(), Box> { info!("Client {} has run the command '{}'", client_addr_clone, command); match command.as_str() { - "help" => { - send_message(&mut stream, &MessageType::Info("".to_string())) - .unwrap(); - - send_message( - &mut stream, - &MessageType::Info(format!( - "Running program version {}, Created by {}", - env!("CARGO_PKG_VERSION"), - env!("CARGO_PKG_AUTHORS") - )), - ) - .unwrap(); - - for command in ["help", "quit", "smile", "laugh", "thumbs_up"] { - send_message( - &mut stream, - &MessageType::Info(format!("> {}", command)), - ) - .unwrap(); - std::thread::sleep(std::time::Duration::from_millis(100)); - } - - send_message(&mut stream, &MessageType::Info("".to_string())) - .unwrap(); - } "smile" => { send_message(&mut stream, &MessageType::Message(client_addr_clone.clone(), "😊".to_string())) .unwrap();