[go: up one dir, main page]

 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
/// Creates a control sequence.
///
/// This macro prepends provided sequence with the control sequence introducer (`ESC [`).
///
/// # Examples
///
/// ```
/// use anes::csi;
///
/// let switch_to_alternate_screen = csi!("?1049h");
/// assert_eq!("\x1B[?1049h", switch_to_alternate_screen);
/// ```
#[macro_export]
macro_rules! csi {
    ($($arg:expr),*) => { concat!("\x1B[", $($arg),*) };
}

/// Creates an escape sequence.
///
/// This macro prepends provided sequence with the `ESC` character.
///
/// # Examples
///
/// ```
/// use anes::esc;
///
/// let save_cursor_position = esc!("7");
/// assert_eq!("\x1B7", save_cursor_position);
/// ```
#[macro_export]
macro_rules! esc {
    ($($arg:expr),*) => { concat!("\x1B", $($arg),*) };
}

macro_rules! impl_sequence {
    ($doc:expr, $name:ident, $value:expr) => {
        #[doc = $doc]
        #[derive(Clone, Copy, Debug)]
        pub struct $name;

        impl ::std::fmt::Display for $name {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, $value)
            }
        }
    }
}

macro_rules! impl_csi_sequence {
    ($doc:expr, $name:ident, $value:expr) => {
        impl_sequence!($doc, $name, csi!($value));
    };
}

macro_rules! impl_esc_sequence {
    ($doc:expr, $name:ident, $value:expr) => {
        impl_sequence!($doc, $name, esc!($value));
    };
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_csi() {
        assert_eq!(csi!("foo"), "\x1B[foo");
    }

    #[test]
    fn test_esc() {
        assert_eq!(esc!("bar"), "\x1Bbar");
    }

    #[test]
    fn test_impl_csi_sequence() {
        impl_csi_sequence!("", TestSeq, "foo");
        assert_eq!(&format!("{}", TestSeq), "\x1B[foo");
    }

    #[test]
    fn test_impl_esc_sequence() {
        impl_esc_sequence!("", TestSeq, "bar");
        assert_eq!(&format!("{}", TestSeq), "\x1Bbar");
    }
}