Enhanced client colors and display

This commit is contained in:
Phuntsok Drak-pa 2018-03-21 23:23:54 +01:00
parent ff88e2133d
commit 92be982856
1 changed files with 62 additions and 17 deletions

View File

@ -6,12 +6,18 @@ use std::net::TcpStream;
use std::thread;
use self::colored::*;
use self::chrono::Local;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
// static leave_msg: &str = "BYE";
fn hash_name(name: &str) -> usize {
let mut s = DefaultHasher::new();
let name = String::from(name);
name.hash(&mut s);
s.finish() as usize
}
fn get_entry() -> String {
let mut buf = String::new();
stdin().read_line(&mut buf).unwrap();
buf.replace("\n", "").replace("\r", "")
}
@ -78,6 +84,18 @@ fn exchange_with_server(stream: TcpStream) {
let server = stream.peer_addr().unwrap();
println!("Connected to {}", server);
#[allow(non_snake_case)]
let COLORS: Vec<&str> = vec![
// "black",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
// "white",
];
let stream_cpy = stream.try_clone().unwrap();
let mut reader = BufReader::new(&stream_cpy);
@ -111,22 +129,39 @@ fn exchange_with_server(stream: TcpStream) {
let spliced_input: Vec<&str> = input.split(" ").collect();
match spliced_input[0] {
"WELCOME" => {
println!("{}", "Successfully connected to the server.".green());
println!("{}", ">>> Login Successful <<<".green());
println!("Type /clients to get the list of users connected");
println!("Type /quit to disconnect and quit");
}
"FROM" => {
let date = Local::now();
let name = String::from(spliced_input[1]);
let mut msg = String::new();
for i in 3..spliced_input.len() {
msg.push_str(" ");
msg.push_str(spliced_input[i]);
}
// Hashing name for color
let mut s = DefaultHasher::new();
name.hash(&mut s);
let name_hash: usize = (s.finish() as usize) % COLORS.len();
// Formatting name
let mut name = String::new();
for _i in 0..(20 - spliced_input[1].to_string().len()) {
name.push(' ');
}
name.push('<');
name.push_str(spliced_input[1]);
name.push('>');
println!(
"{} <{}>:{}",
date.format("[%H:%M:%S]").to_string().blue(),
spliced_input[1].red(),
msg
"{} {}:{}",
date.format("[%H:%M:%S]").to_string().dimmed(),
name.color(COLORS[name_hash]),
msg.yellow().dimmed()
);
}
"BYE" => {
@ -139,20 +174,30 @@ fn exchange_with_server(stream: TcpStream) {
}
}
"JOIN" => {
let date = Local::now();
let name_hash: usize =
hash_name(spliced_input[1].clone()) % COLORS.len();
println!(
"{}{}{}",
"-------> ".green(),
spliced_input[1],
"{}{}{}{}",
date.format("[%H:%M:%S]").to_string().dimmed(),
" ------> ".green(),
spliced_input[1].color(COLORS[name_hash]),
" has joined".green()
);
)
}
"LOGOUT" => {
let date = Local::now();
let name_hash: usize =
hash_name(spliced_input[1].clone()) % COLORS.len();
println!(
"{}{}{}",
"<------- ".red(),
spliced_input[1],
"{}{}{}{}",
date.format("[%H:%M:%S]").to_string().dimmed(),
" <------ ".red(),
spliced_input[1].color(COLORS[name_hash]),
" has left".red()
);
)
}
_ => {
println!("{}", input);
@ -164,8 +209,8 @@ fn exchange_with_server(stream: TcpStream) {
Ok(_) => {
println!("{}", ">>> Logout successful <<<".green());
}
Err(e) => {
println!("{}: {}", "Error: Function terminated too early".red(), e);
Err(_) => {
println!("{}", "Error: Connection with server lost".red());
}
}
}