async_tty/
commands.rs

1pub mod cursor;
2pub mod erase;
3
4/// Control Sequence Introducer (CSI) macro
5///
6/// Returns a string that starts with the CSI escape sequence (`ESC` + `[`) followed by a sequence
7/// of parameters and a command.
8///
9/// # Examples
10/// ```
11/// let count = 1;
12/// let cursor_up = csi!("{count}A");
13/// assert_eq!(cursor_up, "\x1b[1A");
14///
15/// let x = 10;
16/// let y = 20;
17/// let cursor_position = csi!("{x};{y}H");
18/// assert_eq!(cursor_position, "\x1b[10;20H");
19/// ```
20#[macro_export]
21macro_rules! csi {
22    ($fmt:expr $(, $args:expr)*) => {{
23        let string = format!("\x1b[{}", format_args!($fmt $(, $args)*));
24        string
25    }};
26}
27
28#[macro_export]
29macro_rules! write_csi {
30    ($writer:expr, $fmt:expr $(, $args:expr)*) => {{
31        write!($writer, "\x1b[{}", format_args!($fmt $(, $args)*))
32    }};
33}
34
35#[cfg(test)]
36mod tests {
37    #[test]
38    fn csi_with_single_parameter() {
39        let count = 5;
40        assert_eq!(csi!("{count}A"), "\x1b[5A");
41    }
42
43    #[test]
44    fn csi_with_multiple_parameters() {
45        let x = 10;
46        let y = 20;
47        assert_eq!(csi!("{x};{y}H"), "\x1b[10;20H");
48    }
49
50    #[test]
51    fn csi_without_parameters() {
52        assert_eq!(csi!("K"), "\x1b[K");
53    }
54}