use crossterm::{cursor, terminal, QueueableCommand};
use std::io::{stdout, Write};
use std::thread::sleep;
use std::time::Duration;
pub trait Traits {
fn get_header(&self) -> &String;
fn send_msg(&self, msg: &str, typingformillis: u64);
fn join_msg(&self);
fn leave_msg(&self);
fn get_nick(&self) -> &String;
}
pub struct User {
pub name: String,
pub nick: String,
pub header: String,
}
impl Traits for User {
fn send_msg(&self, msg: &str, millis: u64) {
if millis != 0 {
millitype(&self.nick, millis)
}
println!("{} {} {}", &self.nick, &self.header, msg);
}
fn join_msg(&self) {
println!(
"----- {} ({}) has joined the chat -----",
&self.name, &self.nick
);
}
fn leave_msg(&self) {
println!(
"----- {} ({}) has left the chat -----",
&self.name, &self.nick
);
}
fn get_header(&self) -> &String {
&self.header
}
fn get_nick(&self) -> &String {
&self.nick
}
}
pub fn delay(millis: u64) {
sleep(Duration::from_millis(millis));
}
fn millitype(nick: &String, millis: u64) {
let mut stdout = stdout();
stdout.queue(cursor::SavePosition).unwrap();
stdout
.write_all(format!("{} is typing...", nick).as_bytes())
.unwrap();
stdout.queue(cursor::RestorePosition).unwrap();
stdout.flush().unwrap();
delay(millis);
stdout.queue(cursor::RestorePosition).unwrap();
stdout
.queue(terminal::Clear(terminal::ClearType::FromCursorDown))
.unwrap();
}
pub fn clear() {
clearscreen::clear().expect("Failed to clear screen");
}
pub fn hidecursor() {
let mut stdout = stdout();
stdout.queue(cursor::Hide).unwrap();
}