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
//! 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");
/// ```
/// Formats a string and strips all Farben markup tags from the result.
///
/// Accepts the same arguments as [`format!`]. After formatting, every sequence of the form of
/// Farben markup, `[tag]` is removed. Invalid tags are left as-is without panic.
///
/// The return type is `String`
///
/// # Example
/// ```
/// use farben::prelude::markup_strip;
///
/// let stripped = markup_strip!("[bold red]Just the text");
/// assert_eq!("Just the text", stripped);
///
/// let invalid = markup_strip!("[I'm unclosed");
/// assert_eq!("[I'm unclosed", invalid);
///
/// let text = "hey!";
/// let formatted = markup_strip!("[bold red]{text}");
/// assert_eq!("hey!", formatted);
/// ```