Skip to content

strip

Removes the ANSI escape sequences from text, leaving what a terminal would display.

Handles the colors and styles (ESC [ ... m) and every other CSI sequence (cursor moves, erase), operating system commands such as window titles and hyperlinks (ESC ] ... BEL or ESC ] ... ESC \), and the two-character escapes (ESC c, ESC 7, ESC ( B). A lone escape character is dropped, and a sequence cut short at the end of the text is dropped with it. Returns the input borrowed, without allocating, when it has no escape character.

use helpers4::ansi::strip;

Cargo feature ansi (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features ansi

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["ansi"] }
pub fn strip(text: &str) -> Cow<'_, str>
ParameterTypeDescription
text&strThe text that may contain escape sequences, such as captured command output.

Cow<'_, str> — The text without escape sequences.

use helpers4::ansi::strip;

assert_eq!(strip("\u{1b}[1;31merror\u{1b}[0m: it broke"), "error: it broke");
assert_eq!(strip("plain"), "plain");

src/ansi/strip.rs