Skip to content

Commit

Permalink
feat: added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uttarayan21 committed Jun 24, 2024
1 parent df87a4f commit 56fdc8b
Showing 1 changed file with 55 additions and 36 deletions.
91 changes: 55 additions & 36 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn line_fast(style: Style) -> impl Fn(&[u8]) -> IResult<&[u8], (Line<'_>, Style)
fn span(last: Style) -> impl Fn(&[u8]) -> IResult<&[u8], Span<'static>, nom::error::Error<&[u8]>> {
move |s: &[u8]| -> IResult<&[u8], Span<'static>> {
let mut last = last;
let (s, style) = opt(style_fast(last))(s)?;
let (s, style) = opt(style(last))(s)?;

#[cfg(feature = "simd")]
let (s, text) = map_res(take_while(|c| c != b'\x1b' | b'\n'), |t| {
Expand All @@ -184,7 +184,7 @@ fn span(last: Style) -> impl Fn(&[u8]) -> IResult<&[u8], Span<'static>, nom::err
fn span_fast(last: Style) -> impl Fn(&[u8]) -> IResult<&[u8], Span<'_>, nom::error::Error<&[u8]>> {
move |s: &[u8]| -> IResult<&[u8], Span<'_>> {
let mut last = last;
let (s, style) = opt(style_fast(last))(s)?;
let (s, style) = opt(style(last))(s)?;

#[cfg(feature = "simd")]
let (s, text) = map_res(take_while(|c| c != b'\x1b' | b'\n'), |t| {
Expand All @@ -204,29 +204,11 @@ fn span_fast(last: Style) -> impl Fn(&[u8]) -> IResult<&[u8], Span<'_>, nom::err
}
}

// fn style(
// style: Style,
// ) -> impl Fn(&[u8]) -> IResult<&[u8], Option<Style>, nom::error::Error<&[u8]>> {
// move |s: &[u8]| -> IResult<&[u8], Option<Style>> {
// let (s, r) = match opt(ansi_sgr_code_fast)(s)? {
// (s, Some(r)) => (s, Some(r)),
// (s, None) => {
// // This is not a valid style code so we need to consume it
// // Since we already use opt(style) we can safely return an error here which will
// // skip that style
// let (s, _garbage) = any_escape_sequence(s)?;
// (s, None)
// }
// };
// Ok((s, r.map(|r| Style::from(AnsiStates { style, items: r }))))
// }
// }

fn style_fast(
fn style(
style: Style,
) -> impl Fn(&[u8]) -> IResult<&[u8], Option<Style>, nom::error::Error<&[u8]>> {
move |s: &[u8]| -> IResult<&[u8], Option<Style>> {
let (s, r) = match opt(ansi_sgr_code_fast)(s)? {
let (s, r) = match opt(ansi_sgr_code)(s)? {
(s, Some(r)) => (s, Some(r)),
(s, None) => {
let (s, _) = any_escape_sequence(s)?;
Expand All @@ -238,7 +220,7 @@ fn style_fast(
}

/// A complete ANSI SGR code
fn ansi_sgr_code_fast(
fn ansi_sgr_code(
s: &[u8],
) -> IResult<&[u8], smallvec::SmallVec<[AnsiItem; 2]>, nom::error::Error<&[u8]>> {
delimited(
Expand All @@ -251,18 +233,6 @@ fn ansi_sgr_code_fast(
)(s)
}

#[cfg(disabled)]
#[deprecated]
/// Old implementation of ansi_sgr_code
/// This is kept for reference
fn ansi_sgr_code(s: &[u8]) -> IResult<&[u8], Vec<AnsiItem>, nom::error::Error<&[u8]>> {
delimited(
tag("\x1b["),
separated_list0(tag(";"), ansi_sgr_item),
char('m'),
)(s)
}

fn any_escape_sequence(s: &[u8]) -> IResult<&[u8], Option<&[u8]>> {
// Attempt to consume most escape codes, including a single escape char.
//
Expand Down Expand Up @@ -342,7 +312,7 @@ fn color_test() {
#[test]
fn ansi_items_test() {
let sc = Default::default();
let t = style_fast(sc)(b"\x1b[38;2;3;3;3m").unwrap().1.unwrap();
let t = style(sc)(b"\x1b[38;2;3;3;3m").unwrap().1.unwrap();
assert_eq!(
t,
Style::from(AnsiStates {
Expand All @@ -354,4 +324,53 @@ fn ansi_items_test() {
.into()
})
);
assert_eq!(
style(sc)(b"\x1b[38;5;3m").unwrap().1.unwrap(),
Style::from(AnsiStates {
style: sc,
items: vec![AnsiItem {
code: AnsiCode::SetForegroundColor,
color: Some(Color::Indexed(3))
}]
.into()
})
);
assert_eq!(
style(sc)(b"\x1b[38;5;3;48;5;3m").unwrap().1.unwrap(),
Style::from(AnsiStates {
style: sc,
items: vec![
AnsiItem {
code: AnsiCode::SetForegroundColor,
color: Some(Color::Indexed(3))
},
AnsiItem {
code: AnsiCode::SetBackgroundColor,
color: Some(Color::Indexed(3))
}
]
.into()
})
);
assert_eq!(
style(sc)(b"\x1b[38;5;3;48;5;3;1m").unwrap().1.unwrap(),
Style::from(AnsiStates {
style: sc,
items: vec![
AnsiItem {
code: AnsiCode::SetForegroundColor,
color: Some(Color::Indexed(3))
},
AnsiItem {
code: AnsiCode::SetBackgroundColor,
color: Some(Color::Indexed(3))
},
AnsiItem {
code: AnsiCode::Bold,
color: None
}
]
.into()
})
);
}

0 comments on commit 56fdc8b

Please sign in to comment.