Made it show messages from before joining

This commit is contained in:
Arthur Beck 2025-03-20 13:10:34 +00:00
parent 58fd728dbf
commit db11b57965

View file

@ -58,6 +58,33 @@ struct UnwrappedChannel {
online_users: Vec<User>,
}
fn display_msg(stream: &mut TcpStream, msg: &Message) -> bool {
let time_format = time::format_description::well_known::Rfc2822;
if !msg.me_message {
if stream
.write_fmt(format_args!(
"\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1m{}\x1b[0m \x1b[31m⇛\x1b[0m \x1b[35m{}\x1b[0m\n",
msg.timestamp.format(&time_format).expect("the time is evil"),
msg.sender.name,
msg.contents
))
.is_err() {
return false;
}
} else if stream
.write_fmt(format_args!(
"\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1;3m{}\x1b[0m \x1b[3m{}\x1b[0m\n",
msg.timestamp.format(&time_format).expect("the time is evil"),
msg.sender.name,
msg.contents
))
.is_err() {
return false;
}
true
}
fn handle_client(mut stream: TcpStream, channel: Channel) {
stream
.write_all(b"\x1b[33mEnter your chosen nickname(up to 30 characters):\x1b[0m\x1b[4m\n")
@ -144,6 +171,15 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
}
}
{
let lock = channel.lock().unwrap();
for msg in &lock.messages {
if !display_msg(&mut stream, msg) {
break;
}
}
}
stream
.write_fmt(format_args!(
"\x1b[0m\x1b[32;1mWelcome, {}! Joining server \"{}\".\x1b[0m\n",
@ -192,8 +228,6 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
let channel = channel.clone();
let mut stream = stream.try_clone().unwrap();
let time_format = time::format_description::well_known::Rfc2822;
recieve_thread = thread::spawn(move || {
let mut current_message_count = channel.lock().unwrap().messages.len();
loop {
@ -210,27 +244,9 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
if current_message_count != new_message_count {
let new_messages = &lock.messages[current_message_count..];
for msg in new_messages {
if !msg.me_message {
if stream
.write_fmt(format_args!(
"\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1m{}\x1b[0m \x1b[31m⇛\x1b[0m \x1b[35m{}\x1b[0m\n",
msg.timestamp.format(&time_format).expect("the time is evil"),
msg.sender.name,
msg.contents
))
.is_err() {
break;
}
} else if stream
.write_fmt(format_args!(
"\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1;3m{}\x1b[0m \x1b[3m{}\x1b[0m\n",
msg.timestamp.format(&time_format).expect("the time is evil"),
msg.sender.name,
msg.contents
))
.is_err() {
break;
}
if !display_msg(&mut stream, msg) {
break;
}
}
current_message_count = new_message_count;