cmasfo-dev 0.1.16

cmasfo dev crate
Documentation

/// This flushes the buffered string.
/// Use this after you use `print!` macro.
/// This uses `std::io::stdout().flush` method.
/// This panics if `flush` method returns an error.
pub fn flush() {
  use std::io::Write;
  std::io::stdout().flush().unwrap();
}

/// Same with `println!` macro with no argument.
/// This is made just for intuitive use.

/// ```
/// pub fn flushln() {
///   println!();
/// }
/// ```
pub fn flushln() {
  println!();
}

/// While `print!` macro needs additional flushing,
/// this `printfl!` macro automatically flushes it.
#[macro_export]
macro_rules! printfl {
  () => {{
    $crate::flush();
  }};
  ($($arg:tt)*) => {{
    print!($($arg)*);
    $crate::flush();
  }};
}

/// This uses `std::io::stdin().read_line` method and return the string.
/// Unlike `read_line` method, this does not append to the given string,
/// and it is automatically trimmed.

/// ```
/// use cmasfo_dev::*;
///
/// println!("What is your favorite color?");
/// let s = get_line();
/// ```
pub fn get_line() -> String {
  let mut s = String::new();
  std::io::stdin().read_line(&mut s).unwrap();
  s.trim().to_string()
}

/// Use `printfl!` macro then call `get_line` function.
/// This is useful if you want to get a string
/// after you print some message at the same line.

/// ```
/// use cmasfo_dev::*;
///
/// println!("What is your favorite color?");
/// let s = msg_line!("> ");
/// ```

/// Then the terminal will be shown like folling:
/// ```sh
/// What is your favorite color?
/// > orange
/// ```
#[macro_export]
macro_rules! msg_line {
  () => {{
    $crate::get_line()
  }};
  ($($arg:tt)*) => {{
    $crate::printfl!($($arg)*);
    $crate::get_line()
  }};
}