use std::io::{stdin,stdout,Write};
pub fn readline<T>(max: usize, query: T) -> String where T: std::fmt::Display{
let mut out: String = String::new();
print!("{}", query);
let _ = stdout().flush();
match stdin().read_line(&mut out){
Ok(n) => {
if n >= max{
if !out.ends_with('\n') {println!();}
out.truncate(max);
return out
}
else{
if out.ends_with('\n') {
out.pop();
}
else {println!();}
if cfg!(windows) && out.ends_with('\r') {
out.pop();
}
return out
}
}
Err(error) => {println!("We got an error while getting input \nThe error is: {}", error); return out}
};
}
pub fn readlines<T>(max: usize, query: T) -> String where T: std::fmt::Display{
let mut out: String = String::new();
print!("{}", query);
let _ = stdout().flush();
let mut charcters: usize = 0;
loop{
match stdin().read_line(&mut out){
Ok(n) => {charcters += n;
if charcters >= max{
if !out.ends_with('\n') {println!();}
out.truncate(max);
return out
}
else if charcters == max + 1 {
if out.ends_with('\n') {
out.pop();
}
else {println!();}
if cfg!(windows) && out.ends_with('\r') {
out.pop();
}
return out
}
}
Err(error) => {println!("We got an error while getting input \nThe error is: {}", error);}
};
}
}