Enhanced client colors and display
This commit is contained in:
parent
ff88e2133d
commit
92be982856
@ -6,12 +6,18 @@ use std::net::TcpStream;
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use self::colored::*;
|
use self::colored::*;
|
||||||
use self::chrono::Local;
|
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 {
|
fn get_entry() -> String {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
||||||
stdin().read_line(&mut buf).unwrap();
|
stdin().read_line(&mut buf).unwrap();
|
||||||
buf.replace("\n", "").replace("\r", "")
|
buf.replace("\n", "").replace("\r", "")
|
||||||
}
|
}
|
||||||
@ -78,6 +84,18 @@ fn exchange_with_server(stream: TcpStream) {
|
|||||||
let server = stream.peer_addr().unwrap();
|
let server = stream.peer_addr().unwrap();
|
||||||
println!("Connected to {}", server);
|
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 stream_cpy = stream.try_clone().unwrap();
|
||||||
let mut reader = BufReader::new(&stream_cpy);
|
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();
|
let spliced_input: Vec<&str> = input.split(" ").collect();
|
||||||
match spliced_input[0] {
|
match spliced_input[0] {
|
||||||
"WELCOME" => {
|
"WELCOME" => {
|
||||||
println!("{}", "Successfully connected to the server.".green());
|
println!("{}", ">>> Login Successful <<<".green());
|
||||||
println!("Type /clients to get the list of users connected");
|
println!("Type /clients to get the list of users connected");
|
||||||
println!("Type /quit to disconnect and quit");
|
println!("Type /quit to disconnect and quit");
|
||||||
}
|
}
|
||||||
"FROM" => {
|
"FROM" => {
|
||||||
let date = Local::now();
|
let date = Local::now();
|
||||||
|
let name = String::from(spliced_input[1]);
|
||||||
|
|
||||||
let mut msg = String::new();
|
let mut msg = String::new();
|
||||||
for i in 3..spliced_input.len() {
|
for i in 3..spliced_input.len() {
|
||||||
msg.push_str(" ");
|
msg.push_str(" ");
|
||||||
msg.push_str(spliced_input[i]);
|
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!(
|
println!(
|
||||||
"{} <{}>:{}",
|
"{} {}:{}",
|
||||||
date.format("[%H:%M:%S]").to_string().blue(),
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
||||||
spliced_input[1].red(),
|
name.color(COLORS[name_hash]),
|
||||||
msg
|
msg.yellow().dimmed()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
"BYE" => {
|
"BYE" => {
|
||||||
@ -139,20 +174,30 @@ fn exchange_with_server(stream: TcpStream) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"JOIN" => {
|
"JOIN" => {
|
||||||
|
let date = Local::now();
|
||||||
|
let name_hash: usize =
|
||||||
|
hash_name(spliced_input[1].clone()) % COLORS.len();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{}{}{}",
|
"{}{}{}{}",
|
||||||
"-------> ".green(),
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
||||||
spliced_input[1],
|
" ------> ".green(),
|
||||||
|
spliced_input[1].color(COLORS[name_hash]),
|
||||||
" has joined".green()
|
" has joined".green()
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
"LOGOUT" => {
|
"LOGOUT" => {
|
||||||
|
let date = Local::now();
|
||||||
|
let name_hash: usize =
|
||||||
|
hash_name(spliced_input[1].clone()) % COLORS.len();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{}{}{}",
|
"{}{}{}{}",
|
||||||
"<------- ".red(),
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
||||||
spliced_input[1],
|
" <------ ".red(),
|
||||||
|
spliced_input[1].color(COLORS[name_hash]),
|
||||||
" has left".red()
|
" has left".red()
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("{}", input);
|
println!("{}", input);
|
||||||
@ -164,8 +209,8 @@ fn exchange_with_server(stream: TcpStream) {
|
|||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
println!("{}", ">>> Logout successful <<<".green());
|
println!("{}", ">>> Logout successful <<<".green());
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
println!("{}: {}", "Error: Function terminated too early".red(), e);
|
println!("{}", "Error: Connection with server lost".red());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user