#[macro_use]
extern crate lazy_static;
extern crate regex;
#[cfg(test)]
extern crate itertools;
use regex::bytes::{Matches, Regex};
pub const ANSI_RE: &str =
r"[\x1b\x9b]\[[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]";
lazy_static! {
pub static ref ANSI_REGEX: Regex = Regex::new(ANSI_RE).unwrap();
}
pub fn parse_bytes(text: &[u8]) -> Matches {
ANSI_REGEX.find_iter(text)
}
#[cfg(test)]
mod tests {
use super::parse_bytes;
use itertools::zip_eq;
fn test_parse(text: &[u8], expected: &[(usize, usize, &[u8])]) {
for (match_, expected_match) in zip_eq(parse_bytes(text), expected.iter()) {
assert_eq!(
&(match_.start(), match_.end(), match_.as_bytes()),
expected_match,
);
}
}
#[test]
fn red_underline() {
test_parse(
b"before \x1b[31;4mred underline\x1b[0m after",
&[(7, 14, b"\x1b[31;4m"), (27, 31, b"\x1b[0m")],
);
}
}