quick_io 2.0.0

A simple crate to facilitate input and output within programs, with a set of macros.
Documentation
//! A simple crate to facilitate input and output within programs, with a set of macros.
//! Example:
//! ```
//! use quick_io::*;
//!
//! clear!();
//! addstr!("Input something: ");
//!
//! println!("You typed: {}.", getstr!());
//!
//! getstr!();
//!
//! down!(10);
//! right!(20);
//! addstr!("Went down 10 characters and right 20 characters");
//!
//! getstr!();
//!
//! addstr!(0, 10, "Moved to 0, 10 within addstr!");
//!
//! getstr!();
//!
//! mv!(0, 20);
//! addstr!("Moved to 0, 20 with mv!");
//!
//! getstr!();
//!
//! down!(20);
//! addln!("Press enter to clear this line.");
//!
//! getstr!();
//!
//! line_up!(2);
//! clear_line!();
//! line_down!(2);
//!
//! println!("Press enter to clear the entire screen.");
//!
//! getstr!();
//!
//! clear!();
//! ```
//! Copypaste this into a main.rs for a demonstration of the crate (rust playground doesn't support input for some reason?)
//! Note: Since this uses ANSI escape codes for clear!(), mv!(), etc. it may not work on some terminals/systems.

/// Moves the cursor to the specified location. Moves to top left corner (0, 0) if no arguments are
/// provided. If argument specified is out of bounds, it clamps it to be in bounds. I have no idea
/// what happens if you provide a number outside of the u16 limits.
#[macro_export()]
macro_rules! mv {
    ($x:tt, $y:tt) => {
        print!("\x1b[{};{}H", $y + 1, $x + 1);
    };
    () => {
        print!("\x1b[1;1H");
    };
}

/// Clears the entire screen.
#[macro_export()]
macro_rules! clear {
    () => {
        print!("\x1bc");
    };
}
/// Clears the line the cursor is on. Does not move it to the beginning of the line.
#[macro_export()]
macro_rules! clear_line {
    () => {
        print!("\x1b[2K");
    };
}

/// Moves the cursor up a specified amount of lines, and positions it at the start of that line.
/// If no arguments are given, defaults to 1.
#[macro_export()]
macro_rules! line_up {
    ($a:tt) => {
        print!("\x1b[{}F", $a);
    };
    () => {
        print!("\x1b[1F");
    };
}
/// Moves the cursor down a specified amount of lines, and positions it at the start of that line.
/// If no arguments are given, defaults to 1.
#[macro_export()]
macro_rules! line_down {
    ($a:tt) => {
        print!("\x1b[{}E", $a);
    };
    () => {
        print!("\x1b[1E");
    };
}

/// Moves the cusor up the specified amount of characters. If no arguments are given, defaults to
/// 1.
#[macro_export()]
macro_rules! up {
    ($a:tt) => {
        print!("\x1b[{}A", $a);
    };
    () => {
        print!("\x1b[1A");
    };
}
/// Moves the cusor down the specified amount of characters. If no arguments are given, defaults to
/// 1.
#[macro_export()]
macro_rules! down {
    ($a:tt) => {
        print!("\x1b[{}B", $a);
    };
    () => {
        print!("\x1b[1B");
    };
}
/// Moves the cusor right the specified amount of characters. If no arguments are given, defaults to
/// 1.
#[macro_export()]
macro_rules! right {
    ($a:tt) => {
        print!("\x1b[{}C", $a);
    };
    () => {
        print!("\x1b[1C");
    };
}
/// Moves the cusor left the specified amount of characters. If no arguments are given, defaults to
/// 1.
#[macro_export()]
macro_rules! left {
    ($a:tt) => {
        print!("\x1b[{}D", $a);
    };
    () => {
        print!("\x1b[1D");
    };
}

/// Adds a string at the cursor location, or at a specified location. Flushes buffer as well
/// (normally stdout is only flushed on newlines.)
#[macro_export()]
macro_rules! addstr {
    ($x:tt, $y:tt, $($arg:tt)+) => {
        $crate::mv!($x, $y); // function that moves the cursor
        print!($($arg)*);
        let _ = std::io::stdout().flush();
    };
    ($($arg:tt)+) => {
        print!($($arg)*);
        use std::io::Write;
        let _ = std::io::stdout().flush();
    };
}

/// Gets a string from stdin. Output trimmed.
#[macro_export()]
macro_rules! getstr {
    () => {{
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).unwrap();
        input.trim().to_string()
    };};
}