cmasfo_dev/
lib.rs

1
2/// This flushes the buffered string.
3/// Use this after you use `print!` macro.
4/// This uses `std::io::stdout().flush` method.
5/// This panics if `flush` method returns an error.
6pub fn flush() {
7  use std::io::Write;
8  std::io::stdout().flush().unwrap();
9}
10
11/// Same with `println!` macro with no argument.
12/// This is made just for intuitive use.
13
14/// ```
15/// pub fn flushln() {
16///   println!();
17/// }
18/// ```
19pub fn flushln() {
20  println!();
21}
22
23/// While `print!` macro needs additional flushing,
24/// this `printfl!` macro automatically flushes it.
25#[macro_export]
26macro_rules! printfl {
27  () => {{
28    $crate::flush();
29  }};
30  ($($arg:tt)*) => {{
31    print!($($arg)*);
32    $crate::flush();
33  }};
34}
35
36/// This uses `std::io::stdin().read_line` method and return the string.
37/// Unlike `read_line` method, this does not append to the given string,
38/// and it is automatically trimmed.
39
40/// ```
41/// use cmasfo_dev::*;
42///
43/// println!("What is your favorite color?");
44/// let s = get_line();
45/// ```
46pub fn get_line() -> String {
47  let mut s = String::new();
48  std::io::stdin().read_line(&mut s).unwrap();
49  s.trim().to_string()
50}
51
52/// Use `printfl!` macro then call `get_line` function.
53/// This is useful if you want to get a string
54/// after you print some message at the same line.
55
56/// ```
57/// use cmasfo_dev::*;
58///
59/// println!("What is your favorite color?");
60/// let s = msg_line!("> ");
61/// ```
62
63/// Then the terminal will be shown like folling:
64/// ```sh
65/// What is your favorite color?
66/// > orange
67/// ```
68#[macro_export]
69macro_rules! msg_line {
70  () => {{
71    $crate::get_line()
72  }};
73  ($($arg:tt)*) => {{
74    $crate::printfl!($($arg)*);
75    $crate::get_line()
76  }};
77}