Skip to content

Commit

Permalink
[fix] Don't include the empty spans but keep changing the style
Browse files Browse the repository at this point in the history
  • Loading branch information
uttarayan21 committed Mar 16, 2023
1 parent 0d1e9bc commit acdb671
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ fn spans(style: Style) -> impl Fn(&[u8]) -> IResult<&[u8], (Spans<'static>, Styl
let mut spans = Vec::new();
let mut last = style;
while let Ok((s, span)) = span(last)(text) {
last = span.style;
spans.push(span);
// Don't include empty spans but keep changing the style
last = last.patch(span.style);
if spans.is_empty() || span.content != "" {
spans.push(span);
}
text = s;
if text.is_empty() {
break;
Expand Down
12 changes: 12 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,15 @@ fn some_text(s: &'static str) -> Result<Text<'static>, ansi_to_tui::Error> {
}])],
})
}

#[test]
fn empty_span() {
let bytes: Vec<u8> = b"\x1b[33m\x1b[31m\x1b[32mHello\x1b[0mWorld".to_vec();
let output = Ok(Text::from(Spans::from(vec![
Span::styled("", Style::default().fg(Color::Yellow)), // Not sure whether to keep this or
// remove it somehow
Span::styled("Hello", Style::default().fg(Color::Green)),
Span::styled("World", Style::default()),
])));
assert_eq!(bytes.into_text(), output);
}

0 comments on commit acdb671

Please sign in to comment.