Made it show messages from before joining
This commit is contained in:
parent
58fd728dbf
commit
db11b57965
1 changed files with 39 additions and 23 deletions
62
src/main.rs
62
src/main.rs
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue