From f6c9dd5639eec4c914f4dcac8d9815b8449dd321 Mon Sep 17 00:00:00 2001 From: vaw Date: Sun, 15 Jun 2025 14:35:37 +0200 Subject: [PATCH 1/4] Derive Debug for StyleTreeNode --- src/message/html.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/message/html.rs b/src/message/html.rs index d2b40ae3..161645c1 100644 --- a/src/message/html.rs +++ b/src/message/html.rs @@ -97,12 +97,14 @@ impl ListStyle { pub type StyleTreeChildren = Vec; /// Type of contents in a table cell. +#[derive(Debug)] pub enum CellType { Data, Header, } /// A collection of cells for a single row in a table. +#[derive(Debug)] pub struct TableRow { cells: Vec<(CellType, StyleTreeNode)>, } @@ -120,6 +122,7 @@ impl TableRow { } /// A collection of rows in a table. +#[derive(Debug)] pub struct TableSection { rows: Vec, } @@ -137,6 +140,7 @@ impl TableSection { } /// A table. +#[derive(Debug)] pub struct Table { caption: Option>, sections: Vec, @@ -266,6 +270,7 @@ impl Table { } /// A processed HTML element that we can render to the terminal. +#[derive(Debug)] pub enum StyleTreeNode { Anchor(Box, char, Url), Blockquote(Box), From d018d0b748c613059e07b73350845ae7fcae1784 Mon Sep 17 00:00:00 2001 From: vaw Date: Sun, 15 Jun 2025 14:35:56 +0200 Subject: [PATCH 2/4] Set background for text in code blocks --- src/message/html.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/message/html.rs b/src/message/html.rs index 161645c1..fc16ab0c 100644 --- a/src/message/html.rs +++ b/src/message/html.rs @@ -370,6 +370,7 @@ impl StyleTreeNode { } }, StyleTreeNode::Code(child, _) => { + let style = style.bg(Color::Indexed(236)); child.print(printer, style); }, StyleTreeNode::Header(child, level) => { From 8fa00ea36b13516009e9305bcbf9b3df43b14878 Mon Sep 17 00:00:00 2001 From: vaw Date: Sun, 15 Jun 2025 14:50:32 +0200 Subject: [PATCH 3/4] Format whole background for code blocks --- src/message/html.rs | 2 ++ src/message/printer.rs | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/message/html.rs b/src/message/html.rs index fc16ab0c..63439bdb 100644 --- a/src/message/html.rs +++ b/src/message/html.rs @@ -371,7 +371,9 @@ impl StyleTreeNode { }, StyleTreeNode::Code(child, _) => { let style = style.bg(Color::Indexed(236)); + let old = printer.set_base_style(style); child.print(printer, style); + printer.set_base_style(old); }, StyleTreeNode::Header(child, level) => { let style = style.add_modifier(StyleModifier::BOLD); diff --git a/src/message/printer.rs b/src/message/printer.rs index 34187521..2c31bbbf 100644 --- a/src/message/printer.rs +++ b/src/message/printer.rs @@ -92,6 +92,11 @@ impl<'a> TextPrinter<'a> { self.width } + /// Sets the base style and returns the old style + pub fn set_base_style(&mut self, new: Style) -> Style { + std::mem::replace(&mut self.base_style, new) + } + /// Create a new printer with a smaller width. pub fn sub(&self, indent: usize) -> Self { TextPrinter { From af517902d23d55d4558a8547859dead03f9214f5 Mon Sep 17 00:00:00 2001 From: vaw Date: Sun, 15 Jun 2025 14:56:59 +0200 Subject: [PATCH 4/4] Fix test --- src/message/html.rs | 60 +++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/message/html.rs b/src/message/html.rs index 63439bdb..f6428057 100644 --- a/src/message/html.rs +++ b/src/message/html.rs @@ -41,6 +41,7 @@ use crate::{ util::{join_cell_text, space_text}, }; +const CODE_BACKGROUND: Color = Color::Indexed(236); const QUOTE_COLOR: Color = Color::Indexed(236); /// Generate bullet points from a [ListStyle]. @@ -370,7 +371,7 @@ impl StyleTreeNode { } }, StyleTreeNode::Code(child, _) => { - let style = style.bg(Color::Indexed(236)); + let style = style.bg(CODE_BACKGROUND); let old = printer.set_base_style(style); child.print(printer, style); printer.set_base_style(old); @@ -1450,6 +1451,7 @@ pub mod tests { ); let tree = parse_matrix_html(s); let text = tree.to_text(25, Style::default(), true, &settings); + let code_style = Style::new().bg(CODE_BACKGROUND); assert_eq!(text.lines.len(), 6); assert_eq!( text.lines[0], @@ -1463,19 +1465,19 @@ pub mod tests { text.lines[1], Line::from(vec![ Span::raw(line::VERTICAL), - Span::raw("fn"), - Span::raw(" "), - Span::raw("hello"), - Span::raw("("), - Span::raw(")"), - Span::raw(" "), - Span::raw("-"), - Span::raw(">"), - Span::raw(" "), - Span::raw("usize"), - Span::raw(" "), - Span::raw("{"), - Span::raw(" "), + Span::styled("fn", code_style), + Span::styled(" ", code_style), + Span::styled("hello", code_style), + Span::styled("(", code_style), + Span::styled(")", code_style), + Span::styled(" ", code_style), + Span::styled("-", code_style), + Span::styled(">", code_style), + Span::styled(" ", code_style), + Span::styled("usize", code_style), + Span::styled(" ", code_style), + Span::styled("{", code_style), + Span::styled(" ", code_style), Span::raw(line::VERTICAL) ]) ); @@ -1483,13 +1485,13 @@ pub mod tests { text.lines[2], Line::from(vec![ Span::raw(line::VERTICAL), - Span::raw(" "), - Span::raw(" "), - Span::raw("/"), - Span::raw("/"), - Span::raw(" "), - Span::raw("weired"), - Span::raw(" "), + Span::styled(" ", code_style), + Span::styled(" ", code_style), + Span::styled("/", code_style), + Span::styled("/", code_style), + Span::styled(" ", code_style), + Span::styled("weired", code_style), + Span::styled(" ", code_style), Span::raw(line::VERTICAL) ]) ); @@ -1497,12 +1499,12 @@ pub mod tests { text.lines[3], Line::from(vec![ Span::raw(line::VERTICAL), - Span::raw(" "), - Span::raw("return"), - Span::raw(" "), - Span::raw("5"), - Span::raw(";"), - Span::raw(" "), + Span::styled(" ", code_style), + Span::styled("return", code_style), + Span::styled(" ", code_style), + Span::styled("5", code_style), + Span::styled(";", code_style), + Span::styled(" ", code_style), Span::raw(line::VERTICAL) ]) ); @@ -1510,8 +1512,8 @@ pub mod tests { text.lines[4], Line::from(vec![ Span::raw(line::VERTICAL), - Span::raw("}"), - Span::raw(" ".repeat(22)), + Span::styled("}", code_style), + Span::styled(" ".repeat(22), code_style), Span::raw(line::VERTICAL) ]) );