rusty_mos/library/
print.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Apply print-related functions.

use crate::arch::machine::print_charc;

use core::fmt::{self, Write};

/// An empty struct to hold the `fmt::Write` trait.
struct Stdout;

impl fmt::Write for Stdout {
    /// Put strings onto the screen. See Also: [_write_str].
    fn write_str(&mut self, s: &str) -> fmt::Result {
        _write_str(s);
        Ok(())
    }
}

/// Formatted print wrapper. See Also: [Stdout::write_fmt].
pub fn _print(args: fmt::Arguments) {
    let _ = Stdout.write_fmt(args);
}

/// Put the strings directly onto the screen. See Also: [print_charc].
fn _write_str(s: &str) {
    for c in s.chars() {
        print_charc(c as u8);
    }
}

/// Formatted Print: with *no* *new-line* at the end.
#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {{
        $crate::library::print::_print(format_args!($($arg)*));
    }};
}

/// Formatted Print: with a *new-line* at the end.
#[macro_export]
macro_rules! println {
    () => {$crate::print!("\n")};
    ($($arg:tt)*) => {
        $crate::print!($($arg)*);
        $crate::print!("\n")
    };
}

/// Debug Formatted Print: with a *new-line* at the end. Will perform nothing
/// in *release* profile.
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debugln {
    () => {$crate::print!("\n")};
    ($($arg:tt)*) => {
        $crate::print!("\x1b[35m");
        $crate::print!($($arg)*);
        $crate::print!("\x1b[0m");
        $crate::print!("\n")
    };
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debugln {
    () => {};
    ($($arg:tt)*) => {};
}

/// Debug Formatted Print: with *no* *new-line* at the end. Will perform nothing
/// in *release* profile.
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug {
    ($($arg:tt)*) => {
        $crate::print!("\x1b[35m");
        $crate::print!($($arg)*);
        $crate::print!("\x1b[0m");
    };
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debug {
    ($($arg:tt)*) => {};
}