Skip to content

Commit 6c55071

Browse files
committed
add strip_str function (#10)
This is a convenience method that works on UTF-8 strings, and relies on the invariant that ANSI escape codes are always in the range 0..128.
1 parent 5e24faf commit 6c55071

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ where
7373
strip_impl(data.as_ref()).expect("writing to a Cursor<Vec<u8>> cannot fail")
7474
}
7575

76+
/// Strip ANSI escapes from `data` and return the remaining contents as a `String`.
77+
///
78+
/// # Example
79+
///
80+
/// ```
81+
/// let str_with_colors = "\x1b[32mfoo\x1b[m bar";
82+
/// let string_without_colors = strip_ansi_escapes::strip_str(string_with_colors);
83+
/// assert_eq!(string_without_colors, "foo bar");
84+
/// ```
85+
pub fn strip_str<T>(data: T) -> String
86+
where
87+
T: AsRef<str>,
88+
{
89+
let bytes = strip(data.as_ref());
90+
String::from_utf8(bytes)
91+
.expect("stripping ANSI escapes from a UTF-8 string always results in UTF-8")
92+
}
93+
7694
struct Performer<W>
7795
where
7896
W: Write,

0 commit comments

Comments
 (0)