From 5c458c9428a514ab1e9e74319002a65c2a221238 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Tue, 27 Feb 2024 11:29:48 -0800 Subject: [PATCH] Fix style resets --- src/parser.rs | 2 +- tests/tests.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index d995c15..8b17af1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -42,7 +42,7 @@ impl From 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), diff --git a/tests/tests.rs b/tests/tests.rs index fa7e84a..0f093f5 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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}, @@ -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); } @@ -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 = 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::*; @@ -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); } @@ -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 = 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); + } }