From 121ca9790346a7eb014e22e36b4939d328d97c73 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sun, 21 Jan 2024 19:15:06 +0100 Subject: [PATCH 1/2] tests: fix expected html output title rows used improper instead of --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e4fdb78d4..83d59cabd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1251,7 +1251,7 @@ mod tests { ])); let out = "\ \ -\ +\ \ \
t1t2t3
t1t2t3
abcdef
defbca
"; @@ -1297,7 +1297,7 @@ mod tests { )); let out = "\ \ -\ +\ \ \ \ From d65ea7f6a36ebb9d6f28baacf2aedaa90173c6f2 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sun, 21 Jan 2024 19:28:12 +0100 Subject: [PATCH 2/2] fix html output for title-rows - adds `CellType` for each `Cell` with corresponding getter/setter - in `Cell::print_html()` choose `", + "<{3}{1} style=\"{2}\">{0}", HtmlEscape(&content), colspan, - styles + styles, + match self.cell_type { + CellType::Head => "th", + CellType::Data => "td", + } ) .as_bytes(), )?; @@ -345,6 +374,7 @@ impl Default for Cell { align: Alignment::LEFT, style: Vec::new(), hspan: 1, + cell_type: CellType::Data } } } diff --git a/src/lib.rs b/src/lib.rs index 83d59cabd..dbf7e815b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ pub mod csv; #[cfg(feature = "evcxr")] pub mod evcxr; -pub use cell::Cell; +pub use cell::{Cell, CellType}; use format::{consts, LinePosition, TableFormat}; pub use row::Row; use utils::StringWriter; @@ -218,9 +218,9 @@ impl<'a> TableSlice<'a> { out.write_all(b"
span horizontal
span horizontal
bolditalicunderline
leftcenterright
redblackyellow
` or `` based on `cell_type` - add `Row::set_cell_type()` to set type for all cells in a row - always set title-row to `CellType::Head` --- src/cell.rs | 34 ++++++++++++++++++++++++++++++++-- src/lib.rs | 9 +++++---- src/row.rs | 10 +++++++++- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/cell.rs b/src/cell.rs index 5db73fa93..abfad4ee5 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -7,6 +7,13 @@ use std::io::{Error, Write}; use std::str::FromStr; use std::string::ToString; + +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] +pub enum CellType { + Head, + Data +} + /// Represent a table cell containing a string. /// /// Once created, a cell's content cannot be modified. @@ -18,6 +25,7 @@ pub struct Cell { align: Alignment, style: Vec, hspan: usize, + cell_type: CellType } impl Cell { @@ -38,6 +46,7 @@ impl Cell { align, style: Vec::new(), hspan: 1, + cell_type: CellType::Data } } @@ -69,6 +78,12 @@ impl Cell { self } + /// Set cell type (head or data) + pub fn with_cell_type(mut self, cell_type: CellType) -> Cell { + self.set_cell_type(cell_type); + self + } + /// Remove all style attributes and reset alignment to default (LEFT) pub fn reset_style(&mut self) { self.style.clear(); @@ -197,6 +212,16 @@ impl Cell { self.hspan } + /// Set type for this cell (head or data) + pub fn set_cell_type(&mut self, cell_type: CellType) { + self.cell_type = cell_type; + } + + /// Get type of this cell (head or data) + pub fn get_cell_type(&self) -> CellType { + self.cell_type.clone() + } + /// Return a copy of the full string contained in the cell pub fn get_content(&self) -> String { self.content.join("\n") @@ -306,10 +331,14 @@ impl Cell { let content = self.content.join("
"); out.write_all( format!( - "{0}
")?; // Print titles / table header if let Some(ref t) = *self.titles { - out.write_all(b"")?; t.print_html(out, column_num)?; - out.write_all(b"")?; + out.write_all(b"")?; } // Print rows for r in self.rows { @@ -285,7 +285,8 @@ impl Table { } /// Set the optional title lines - pub fn set_titles(&mut self, titles: Row) { + pub fn set_titles(&mut self, mut titles: Row) { + titles.set_cell_type(CellType::Head); *self.titles = Some(titles); } diff --git a/src/row.rs b/src/row.rs index 419032203..ead3dbaa6 100644 --- a/src/row.rs +++ b/src/row.rs @@ -9,7 +9,7 @@ use super::Terminal; use super::format::{ColumnPosition, TableFormat}; use super::utils::NEWLINE; -use super::Cell; +use super::{Cell, CellType}; /// Represent a table row made of cells #[derive(Clone, Debug, Hash, PartialEq, Eq)] @@ -140,6 +140,13 @@ impl Row { self.cells.iter_mut() } + /// Set cell type for all cells in this row + pub fn set_cell_type(&mut self, cell_type: CellType) { + for c in self.iter_mut() { + c.set_cell_type(cell_type.clone()); + } + } + /// Internal only fn __print( &self, @@ -221,6 +228,7 @@ impl Row { /// Print the row in HTML format to `out`. /// /// If the row is has fewer columns than `col_num`, the row is padded with empty cells. + /// Parameter `title` tells, if the row is a title-row pub fn print_html(&self, out: &mut T, col_num: usize) -> Result<(), Error> { let mut printed_columns = 0; for cell in self.iter() {
")?; + out.write_all(b"