| Crates.io | strip-ansi-escapes |
| lib.rs | strip-ansi-escapes |
| version | 0.2.1 |
| created_at | 2018-02-02 12:01:08.543896+00 |
| updated_at | 2025-01-14 15:47:27.043349+00 |
| description | Strip ANSI escape sequences from byte streams. |
| homepage | https://github.com/luser/strip-ansi-escapes |
| repository | https://github.com/luser/strip-ansi-escapes |
| max_upload_size | |
| id | 49341 |
| size | 23,641 |
A crate for stripping ANSI escape sequences from byte sequences.
This can be used to take output from a program that includes escape sequences and write it somewhere that does not easily support them, such as a log file.
The strip function accepts bytes and returns a Vec of bytes with ANSI escape sequences removed.
extern crate strip_ansi_escapes;
use std::io::{self, Write};
fn work() -> io::Result<()> {
let bytes_with_colors = b"\x1b[32mfoo\x1b[m bar";
let plain_bytes = strip_ansi_escapes::strip(&bytes_with_colors);
io::stdout().write_all(&plain_bytes)?;
Ok(())
}
fn main() {
work().unwrap();
}
For writing directly to a writer, the Writer struct may be preferable.
extern crate strip_ansi_escapes;
use std::io::{self, Write};
use strip_ansi_escapes::Writer;
fn work() -> io::Result<()> {
let bytes_with_colors = b"\x1b[32mfoo\x1b[m bar";
let mut writer = Writer::new(io::stdout());
// Only `foo bar` will be written to stdout
writer.write_all(bytes_with_colors)?;
Ok(())
}
fn main() {
work().unwrap();
}
Licensed under either of
LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)LICENSE-MIT or http://opensource.org/licenses/MIT)at your option.