Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 38 additions & 28 deletions src/message/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -97,12 +98,14 @@ impl ListStyle {
pub type StyleTreeChildren = Vec<StyleTreeNode>;

/// 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)>,
}
Expand All @@ -120,6 +123,7 @@ impl TableRow {
}

/// A collection of rows in a table.
#[derive(Debug)]
pub struct TableSection {
rows: Vec<TableRow>,
}
Expand All @@ -137,6 +141,7 @@ impl TableSection {
}

/// A table.
#[derive(Debug)]
pub struct Table {
caption: Option<Box<StyleTreeNode>>,
sections: Vec<TableSection>,
Expand Down Expand Up @@ -266,6 +271,7 @@ impl Table {
}

/// A processed HTML element that we can render to the terminal.
#[derive(Debug)]
pub enum StyleTreeNode {
Anchor(Box<StyleTreeNode>, char, Url),
Blockquote(Box<StyleTreeNode>),
Expand Down Expand Up @@ -365,7 +371,10 @@ impl StyleTreeNode {
}
},
StyleTreeNode::Code(child, _) => {
let style = style.bg(CODE_BACKGROUND);
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);
Expand Down Expand Up @@ -1442,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],
Expand All @@ -1455,55 +1465,55 @@ 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)
])
);
assert_eq!(
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)
])
);
assert_eq!(
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)
])
);
assert_eq!(
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)
])
);
Expand Down
5 changes: 5 additions & 0 deletions src/message/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading