Improved message display in client

This commit is contained in:
Phuntsok Drak-pa 2018-03-22 11:36:06 +01:00
parent de2523b083
commit 66a6bc2bc5
4 changed files with 67 additions and 18 deletions

View File

@ -6,3 +6,4 @@ authors = ["Lucien Cartier-Tilet <drakpa@drakpa.fr>"]
[dependencies] [dependencies]
colored = "1.6" colored = "1.6"
chrono = "0.4" chrono = "0.4"
term_size = "0.3"

View File

@ -1,5 +1,6 @@
extern crate chrono; extern crate chrono;
extern crate colored; extern crate colored;
extern crate term_size;
use std; use std;
use std::io::*; use std::io::*;
use std::net::TcpStream; use std::net::TcpStream;
@ -10,7 +11,8 @@ use self::colored::*;
use self::chrono::Local; use self::chrono::Local;
// TODO: Limit usernames to ascii // TODO: Limit usernames to ascii
// TODO: implement requests 1.x from protocol // TODO: Implement requests 1.x from protocol
// TODO: forbid usernames already in use
fn hash_name(name: &str) -> usize { fn hash_name(name: &str) -> usize {
let mut s = DefaultHasher::new(); let mut s = DefaultHasher::new();
@ -82,8 +84,7 @@ fn write_to_server(stream: TcpStream) {
if line.len() > 2000 { if line.len() > 2000 {
println!( println!(
"{}", "{}",
"Cannot send a message longer than 2000 characters" "Cannot send a message longer than 2000 characters".bright_red()
.bright_red()
); );
continue; continue;
} }
@ -150,12 +151,7 @@ fn exchange_with_server(stream: TcpStream) {
"FROM" => { "FROM" => {
let date = Local::now(); let date = Local::now();
let name = String::from(spliced_input[1]); let name = String::from(spliced_input[1]);
let mut first_line = true;
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 // Hashing name for color
let mut s = DefaultHasher::new(); let mut s = DefaultHasher::new();
@ -171,12 +167,63 @@ fn exchange_with_server(stream: TcpStream) {
name.push_str(spliced_input[1]); name.push_str(spliced_input[1]);
name.push('>'); name.push('>');
println!( if let Some((w, _)) = term_size::dimensions() {
"{} {}:{}", let mut msg = String::new();
date.format("[%H:%M:%S]").to_string().dimmed(), let w = w - 34;
name.color(COLORS[name_hash]),
msg.yellow().dimmed() for mut i in 3..spliced_input.len() {
); if w > msg.len() + spliced_input[i].len() + 1 {
msg.push(' ');
msg.push_str(spliced_input[i]);
} else {
if first_line == true {
println!(
"{} {}:{}",
date.format("[%H:%M:%S]").to_string().dimmed(),
name.color(COLORS[name_hash]),
msg.yellow().dimmed()
);
first_line = false;
} else {
println!(
"{}{}",
" |".green(),
msg.yellow().dimmed()
);
}
msg = String::new();
#[allow(unused_assignments)]
i = i - 1;
}
}
if first_line == true {
println!(
"{} {}:{}",
date.format("[%H:%M:%S]").to_string().dimmed(),
name.color(COLORS[name_hash]),
msg.yellow().dimmed()
);
} else {
println!(
"{}{}",
" |".green(),
msg.yellow().dimmed()
);
}
} else {
let mut msg = String::new();
for i in 3..spliced_input.len() {
msg.push_str(" ");
msg.push_str(spliced_input[i]);
}
println!(
"{} {}{}",
date.format("[%H:%M:%S]").to_string().dimmed(),
name.color(COLORS[name_hash]),
msg.yellow().dimmed()
);
}
} }
"BYE" => { "BYE" => {
return Ok("Ok"); return Ok("Ok");
@ -194,7 +241,7 @@ fn exchange_with_server(stream: TcpStream) {
println!( println!(
"{}{}{}{}", "{}{}{}{}",
date.format("[%H:%M:%S]").to_string().dimmed(), date.format("[%H:%M:%S]").to_string().dimmed(),
" ------> ".green(), " ------> ".green(),
spliced_input[1].color(COLORS[name_hash]), spliced_input[1].color(COLORS[name_hash]),
" has joined".green() " has joined".green()
) )
@ -206,7 +253,7 @@ fn exchange_with_server(stream: TcpStream) {
println!( println!(
"{}{}{}{}", "{}{}{}{}",
date.format("[%H:%M:%S]").to_string().dimmed(), date.format("[%H:%M:%S]").to_string().dimmed(),
" <------ ".red(), " <------ ".red(),
spliced_input[1].color(COLORS[name_hash]), spliced_input[1].color(COLORS[name_hash]),
" has left".red() " has left".red()
) )

View File

@ -1,5 +1,5 @@
#![feature(type_ascription)] #![feature(type_ascription)]
#![feature(stmt_expr_attributes)]
use std::env; use std::env;
pub mod client; pub mod client;

View File

@ -5,6 +5,7 @@ use std::sync::{Arc, Mutex, MutexGuard};
use std::collections::HashMap; use std::collections::HashMap;
// TODO: implement requests 1.x from protocol // TODO: implement requests 1.x from protocol
// TODO: forbid usernames already in use
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Evolution implementation protocole // // Evolution implementation protocole //