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
//! ANSI escape sequence stripping macro.
//!
//! [`ansi_strip!`] accepts format-string arguments, builds the resulting string,
//! then removes all CSI ANSI escape sequences from it. Non-CSI `ESC` bytes
//! pass through unchanged.
/// Formats a string and strips all CSI ANSI escape sequences from the result.
///
/// Accepts the same arguments as [`format!`]. After formatting, every sequence
/// of the form `ESC [ ... <letter>` is removed. Non-CSI `ESC` bytes are left intact.
///
/// The return type is `String`.
///
/// # Examples
///
/// ```
/// use farben::prelude::*;
///
/// let colored = "\x1b[31mError\x1b[0m";
/// let plain = ansi_strip!("{}", colored);
/// assert_eq!(plain, "Error");
/// ```
///
/// Format arguments work exactly as they do with `format!`:
///
/// ```
/// use farben::prelude::*;
///
/// let code = 42;
/// let plain = ansi_strip!("\x1b[1mcode {code}\x1b[0m");
/// assert_eq!(plain, "code 42");
/// ```