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/// use async_tty::csi;
12///
13/// let count = 1;
14/// let cursor_up = csi!("{count}A");
15/// assert_eq!(cursor_up, "\x1b[1A");
16///
17/// let x = 10;
18/// let y = 20;
19/// let cursor_position = csi!("{x};{y}H");
20/// assert_eq!(cursor_position, "\x1b[10;20H");
21/// ```
22#[macro_export]
23macro_rules! csi {
24    ($fmt:expr $(, $args:expr)*) => {{
25        let string = format!("\x1b[{}", format_args!($fmt $(, $args)*));
26        string
27    }};
28}
29
30#[macro_export]
31macro_rules! write_csi {
32    ($writer:expr, $fmt:expr $(, $args:expr)*) => {{
33        write!($writer, "\x1b[{}", format_args!($fmt $(, $args)*))
34    }};
35}
36
37#[cfg(test)]
38mod tests {
39    #[test]
40    fn csi_with_single_parameter() {
41        let count = 5;
42        assert_eq!(csi!("{count}A"), "\x1b[5A");
43    }
44
45    #[test]
46    fn csi_with_multiple_parameters() {
47        let x = 10;
48        let y = 20;
49        assert_eq!(csi!("{x};{y}H"), "\x1b[10;20H");
50    }
51
52    #[test]
53    fn csi_without_parameters() {
54        assert_eq!(csi!("K"), "\x1b[K");
55    }
56}