use core::fmt::{Arguments, Write, Result};
use crate::syscall::{sys_read, sys_write};

struct Stdout;

const STDIN: usize = 0;  // 读取 标准输入的标志
const STDOUT: usize = 1;

impl Write for Stdout {
    fn write_str(&mut self, s: &str) -> Result {
        sys_write(STDOUT, s.as_bytes());
        Ok(())
    }
}

// 每次从标准输入读出一个字符, 这个会阻塞当前进程
pub fn getchar() -> u8 {
    let mut c = [0u8; 1];
    sys_read(STDIN, &mut c);
    c[0]
}


pub fn print(args: Arguments) {
    Stdout.write_fmt(args).unwrap();
}


#[macro_export]
macro_rules! print {
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::user_console::print(format_args!($fmt $(, $($arg)+)?));
    }
}

#[macro_export]
macro_rules! println {
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::user_console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
    }
}