Skip to content

Commit

Permalink
Fix style resets
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years authored and uttarayan21 committed Feb 28, 2024
1 parent 5b1ac56 commit 5c458c9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl From<AnsiStates> for tui::style::Style {
let mut style = states.style;
for item in states.items {
match item.code {
AnsiCode::Reset => style = Style::default(),
AnsiCode::Reset => style = Style::reset(),
AnsiCode::Bold => style = style.add_modifier(Modifier::BOLD),
AnsiCode::Faint => style = style.add_modifier(Modifier::DIM),
AnsiCode::Italic => style = style.add_modifier(Modifier::ITALIC),
Expand Down
59 changes: 55 additions & 4 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// use ansi_to_tui::{ansi_to_text, ansi_to_text_override_style};
use ansi_to_tui::IntoText;
use tui::style::Stylize;
use tui::{
style::{Color, Style},
text::{Line, Span, Text},
Expand Down Expand Up @@ -119,7 +120,7 @@ fn test_reset() {
let string = "\x1b[33mA\x1b[0mB";
let output = Ok(Text::from(Line::from(vec![
Span::styled("A", Style::default().fg(Color::Yellow)),
Span::raw("B"),
Span::styled("B", Style::reset()),
])));
assert_eq!(string.into_text(), output);
}
Expand Down Expand Up @@ -162,11 +163,36 @@ fn empty_span() {
// Not sure whether to keep this empty span or remove it somehow
Span::styled("", Style::default().fg(Color::Yellow)),
Span::styled("Hello", Style::default().fg(Color::Green)),
Span::styled("World", Style::default()),
Span::styled("World", Style::reset()),
])));
assert_eq!(bytes.into_text(), output);
}

#[test]
fn test_color_and_style_reset() {
let bytes: Vec<u8> = String::from(
"\u{1b}[32m* \u{1b}[0mRunning before-startup command \u{1b}[1mcommand\u{1b}[0m=make my-simple-package.cabal\n\
\u{1b}[32m* \u{1b}[0m$ make my-simple-package.cabal\n\
Build profile: -w ghc-9.0.2 -O1\n").into_bytes();
let output = Ok(Text::from(vec![
Line::from(vec![
Span::styled("* ", Style::default().fg(Color::Green)),
Span::styled("Running before-startup command ", Style::reset()),
Span::styled("command", Style::reset().bold()),
Span::styled("=make my-simple-package.cabal", Style::reset()),
]),
Line::from(vec![
Span::styled("* ", Style::reset().fg(Color::Green)),
Span::styled("$ make my-simple-package.cabal", Style::reset()),
]),
Line::from(vec![Span::styled(
"Build profile: -w ghc-9.0.2 -O1",
Style::reset(),
)]),
]));
assert_eq!(bytes.into_text(), output);
}

#[cfg(feature = "zero-copy")]
mod zero_copy {
use super::*;
Expand Down Expand Up @@ -262,7 +288,7 @@ mod zero_copy {
let string = "\x1b[33mA\x1b[0mB";
let output = Ok(Text::from(Line::from(vec![
Span::styled("A", Style::default().fg(Color::Yellow)),
Span::raw("B"),
Span::styled("B", Style::reset()),
])));
assert_eq!(string.to_text(), output);
}
Expand Down Expand Up @@ -305,8 +331,33 @@ mod zero_copy {
// Not sure whether to keep this empty span or remove it somehow
Span::styled("", Style::default().fg(Color::Yellow)),
Span::styled("Hello", Style::default().fg(Color::Green)),
Span::styled("World", Style::default()),
Span::styled("World", Style::reset()),
])));
assert_eq!(bytes.to_text(), output);
}

#[test]
fn test_color_and_style_reset() {
let bytes: Vec<u8> = String::from(
"\u{1b}[32m* \u{1b}[0mRunning before-startup command \u{1b}[1mcommand\u{1b}[0m=make my-simple-package.cabal\n\
\u{1b}[32m* \u{1b}[0m$ make my-simple-package.cabal\n\
Build profile: -w ghc-9.0.2 -O1\n").into_bytes();
let output = Ok(Text::from(vec![
Line::from(vec![
Span::styled("* ", Style::default().fg(Color::Green)),
Span::styled("Running before-startup command ", Style::reset()),
Span::styled("command", Style::reset().bold()),
Span::styled("=make my-simple-package.cabal", Style::reset()),
]),
Line::from(vec![
Span::styled("* ", Style::reset().fg(Color::Green)),
Span::styled("$ make my-simple-package.cabal", Style::reset()),
]),
Line::from(vec![Span::styled(
"Build profile: -w ghc-9.0.2 -O1",
Style::reset(),
)]),
]));
assert_eq!(bytes.into_text(), output);
}
}

0 comments on commit 5c458c9

Please sign in to comment.