1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#![allow(dead_code)]
use std::io::Write;
pub const COLOR_SERVER: &str = "\x1b[34m";
pub const COLOR_CLIENT: &str = "\x1b[31m";
pub const RESET: &str = "\x1b[0m";
#[derive(Clone, Copy, Debug)]
pub enum Role {
Client,
Server,
}
pub fn read_more(role: Role, message_begin: bool) -> Vec<u8> {
let prompt = if message_begin {
match role {
Role::Client => "C: ",
Role::Server => "S: ",
}
} else {
".. "
};
let line = read_line(prompt, role);
// If `read_line` produces an empty string, standard input has been closed.
if line.is_empty() || line.trim() == "exit" {
println!("Exiting.");
std::process::exit(0);
}
line.into_bytes()
}
fn read_line(prompt: &str, role: Role) -> String {
match role {
Role::Client => print!("{}{COLOR_CLIENT}", prompt),
Role::Server => print!("{}{COLOR_SERVER}", prompt),
}
std::io::stdout().flush().unwrap();
let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
print!("{RESET}");
// If `Stdin::read_line` produces an empty string, standard input has been closed.
if line.is_empty() {
return line;
}
// Ensure `CRLF` line ending of resulting string.
// Line ending of `line` can be one of:
// - `CRLF` on Windows
// - `LF` on Unix-like
// - none when EOF of standard input is reached
if line.ends_with("\r\n") {
return line;
}
if line.ends_with('\n') {
line.pop();
}
line + "\r\n"
}
|