1use std::io::{stdin,stdout,Write};
2
3
4pub fn readline<T>(max: usize, query: T) -> String where T: std::fmt::Display{
10 let mut out: String = String::new();
11 print!("{}", query);
12 let _ = stdout().flush();
13 match stdin().read_line(&mut out){
14 Ok(n) => {
15 if n >= max{
16 if !out.ends_with('\n') {println!();}
17 out.truncate(max);
18 return out
19 }
20 else{
21 if out.ends_with('\n') {
22 out.pop();
23 }
24 else {println!();}
25 if cfg!(windows) && out.ends_with('\r') {
26 out.pop();
27 }
28 return out
29 }
30 }
31 Err(error) => {println!("We got an error while getting input \nThe error is: {}", error); return out}
32 };
33}
34
35
36pub fn readlines<T>(max: usize, query: T) -> String where T: std::fmt::Display{
43 let mut out: String = String::new();
44 print!("{}", query);
45 let _ = stdout().flush();
46 let mut charcters: usize = 0;
47 loop{
48 match stdin().read_line(&mut out){
49 Ok(n) => {charcters += n;
50 if charcters >= max{
51 if !out.ends_with('\n') {println!();}
52 out.truncate(max);
53 return out
54 }
55 else if charcters == max + 1 {
56 if out.ends_with('\n') {
57 out.pop();
58 }
59 else {println!();}
60 if cfg!(windows) && out.ends_with('\r') {
61 out.pop();
62 }
63 return out
64 }
65 }
66 Err(error) => {println!("We got an error while getting input \nThe error is: {}", error);}
67 };
68 }
69}