Skip to content

Commit

Permalink
fix: Improve command performance
Browse files Browse the repository at this point in the history
Earlier in case of help command all the lines would be sent by TCP to server and broadcasted back. This has been fixed
  • Loading branch information
Saphereye committed Feb 14, 2024
1 parent 554714d commit 6fdb8ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
41 changes: 27 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
}
Expand Down
26 changes: 0 additions & 26 deletions src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,32 +170,6 @@ pub fn run_server(server_ip: &str) -> Result<(), Box<dyn std::error::Error>> {
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();
Expand Down

0 comments on commit 6fdb8ad

Please sign in to comment.