1 //! A crate for stripping ANSI escape sequences from byte sequences.
2 //!
3 //! This can be used to take output from a program that includes escape sequences and write
4 //! it somewhere that does not easily support them, such as a log file.
5 //!
6 //! The simplest interface provided is the [`strip`] function, which takes a byte slice and returns
7 //! a `Vec` of bytes with escape sequences removed. For writing bytes directly to a writer, you
8 //! may prefer using the [`Writer`] struct, which implements `Write` and strips escape sequences
9 //! as they are written.
10 //!
11 //! [`strip`]: fn.strip.html
12 //! [`Writer`]: struct.Writer.html
13 //!
14 //! # Example
15 //!
16 //! ```
17 //! use std::io::{self, Write};
18 //!
19 //! # fn foo() -> io::Result<()> {
20 //! let bytes_with_colors = b"\x1b[32mfoo\x1b[m bar";
21 //! let plain_bytes = strip_ansi_escapes::strip(&bytes_with_colors)?;
22 //! io::stdout().write_all(&plain_bytes)?;
23 //! # Ok(())
24 //! # }
25 //! ```
26
27 extern crate vte;
28
29 use std::io::{self, Cursor, IntoInnerError, LineWriter, Write};
30 use vte::{Parser, Perform};
31
32 /// `Writer` wraps an underlying type that implements `Write`, stripping ANSI escape sequences
33 /// from bytes written to it before passing them to the underlying writer.
34 ///
35 /// # Example
36 /// ```
37 /// use std::io::{self, Write};
38 /// use strip_ansi_escapes::Writer;
39 ///
40 /// # fn foo() -> io::Result<()> {
41 /// let bytes_with_colors = b"\x1b[32mfoo\x1b[m bar";
42 /// let mut writer = Writer::new(io::stdout());
43 /// // Only `foo bar` will be written to stdout
44 /// writer.write_all(bytes_with_colors)?;
45 /// # Ok(())
46 /// # }
47 /// ```
48
49 pub struct Writer<W>
50 where W: Write,
51 {
52 performer: Performer<W>,
53 parser: Parser,
54 }
55
56 /// Strip ANSI escapes from `data` and return the remaining bytes as a `Vec<u8>`.
57 ///
58 /// See [the module documentation][mod] for an example.
59 ///
60 /// [mod]: index.html
strip<T>(data: T) -> io::Result<Vec<u8>> where T: AsRef<[u8]>,61 pub fn strip<T>(data: T) -> io::Result<Vec<u8>>
62 where T: AsRef<[u8]>,
63 {
64 let c = Cursor::new(Vec::new());
65 let mut writer = Writer::new(c);
66 writer.write_all(data.as_ref())?;
67 Ok(writer.into_inner()?.into_inner())
68 }
69
70 struct Performer<W>
71 where W: Write,
72 {
73 writer: LineWriter<W>,
74 err: Option<io::Error>,
75 }
76
77 impl<W> Writer<W>
78 where W: Write,
79 {
80 /// Create a new `Writer` that writes to `inner`.
new(inner: W) -> Writer<W>81 pub fn new(inner: W) -> Writer<W> {
82 Writer {
83 performer: Performer {
84 writer: LineWriter::new(inner),
85 err: None,
86 },
87 parser: Parser::new(),
88 }
89 }
90
91 /// Unwraps this `Writer`, returning the underlying writer.
92 ///
93 /// The internal buffer is written out before returning the writer, which
94 /// may produce an [`IntoInnerError`].
95 ///
96 /// [IntoInnerError]: https://doc.rust-lang.org/std/io/struct.IntoInnerError.html
into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>>97 pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
98 self.performer.into_inner()
99 }
100 }
101
102 impl<W> Write for Writer<W>
103 where W: Write,
104 {
write(&mut self, buf: &[u8]) -> io::Result<usize>105 fn write(&mut self, buf: &[u8]) -> io::Result<usize>
106 {
107 for b in buf.iter() {
108 self.parser.advance(&mut self.performer, *b)
109 }
110 match self.performer.err.take() {
111 Some(e) => Err(e),
112 None => Ok(buf.len()),
113 }
114 }
115
flush(&mut self) -> io::Result<()>116 fn flush(&mut self) -> io::Result<()> { self.performer.flush() }
117 }
118
119 impl<W> Performer<W>
120 where W: Write,
121 {
flush(&mut self) -> io::Result<()>122 pub fn flush(&mut self) -> io::Result<()> { self.writer.flush() }
123
into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>>124 pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
125 self.writer.into_inner()
126 }
127 }
128
129 impl<W> Perform for Performer<W>
130 where W: Write,
131 {
print(&mut self, c: char)132 fn print(&mut self, c: char) {
133 // Just print bytes to the inner writer.
134 self.err = write!(self.writer, "{}", c).err();
135 }
execute(&mut self, byte: u8)136 fn execute(&mut self, byte: u8) {
137 // We only care about executing linefeeds.
138 if byte == b'\n' {
139 self.err = writeln!(self.writer, "").err();
140 }
141 }
142 // Since we're not actually implementing a terminal, we just ignore everything else.
hook(&mut self, _params: &[i64], _intermediates: &[u8], _ignore: bool)143 fn hook(&mut self, _params: &[i64], _intermediates: &[u8], _ignore: bool) {}
put(&mut self, _byte: u8)144 fn put(&mut self, _byte: u8) {}
unhook(&mut self)145 fn unhook(&mut self) {}
osc_dispatch(&mut self, _params: &[&[u8]])146 fn osc_dispatch(&mut self, _params: &[&[u8]]) {}
csi_dispatch( &mut self, _params: &[i64], _intermediates: &[u8], _ignore: bool, _: char )147 fn csi_dispatch(
148 &mut self,
149 _params: &[i64],
150 _intermediates: &[u8],
151 _ignore: bool,
152 _: char
153 ) {}
esc_dispatch( &mut self, _params: &[i64], _intermediates: &[u8], _ignore: bool, _byte: u8 )154 fn esc_dispatch(
155 &mut self,
156 _params: &[i64],
157 _intermediates: &[u8],
158 _ignore: bool,
159 _byte: u8
160 ) {}
161 }
162
163 #[cfg(test)]
164 mod tests {
165 use super::*;
166
167 use std::env;
168 use std::env::consts::EXE_EXTENSION;
169 use std::path::Path;
170 use std::process::Command;
171
172 #[test]
readme_test()173 fn readme_test() {
174 let rustdoc = Path::new("rustdoc").with_extension(EXE_EXTENSION);
175 let readme = Path::new(file!()).parent().unwrap().parent().unwrap().join("README.md");
176 let exe = env::current_exe().unwrap();
177 let outdir = exe.parent().unwrap();
178 let mut cmd = Command::new(rustdoc);
179 cmd.args(&["--verbose", "--test", "-L"])
180 .arg(&outdir)
181 .arg(&readme);
182 println!("{:?}", cmd);
183 let result = cmd.spawn()
184 .expect("Failed to spawn process")
185 .wait()
186 .expect("Failed to run process");
187 assert!(result.success(), "Failed to run rustdoc tests on README.md!");
188 }
189
assert_parsed(input: &[u8], expected: &[u8])190 fn assert_parsed(input: &[u8], expected: &[u8]) {
191 let bytes = strip(input).expect("Failed to strip escapes");
192 assert_eq!(bytes, expected);
193 }
194
195 #[test]
test_simple()196 fn test_simple() {
197 assert_parsed(b"\x1b[m\x1b[m\x1b[32m\x1b[1m Finished\x1b[m dev [unoptimized + debuginfo] target(s) in 0.0 secs",
198 b" Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs");
199 }
200
201 #[test]
test_newlines()202 fn test_newlines() {
203 assert_parsed(b"foo\nbar\n", b"foo\nbar\n");
204 }
205
206 #[test]
test_escapes_newlines()207 fn test_escapes_newlines() {
208 assert_parsed(b"\x1b[m\x1b[m\x1b[32m\x1b[1m Compiling\x1b[m utf8parse v0.1.0
209 \x1b[m\x1b[m\x1b[32m\x1b[1m Compiling\x1b[m vte v0.3.2
210 \x1b[m\x1b[m\x1b[32m\x1b[1m Compiling\x1b[m strip-ansi-escapes v0.1.0-pre (file:///build/strip-ansi-escapes)
211 \x1b[m\x1b[m\x1b[32m\x1b[1m Finished\x1b[m dev [unoptimized + debuginfo] target(s) in 0.66 secs
212 ",
213 b" Compiling utf8parse v0.1.0
214 Compiling vte v0.3.2
215 Compiling strip-ansi-escapes v0.1.0-pre (file:///build/strip-ansi-escapes)
216 Finished dev [unoptimized + debuginfo] target(s) in 0.66 secs
217 ");
218 }
219 }
220