Skip to content

Commit

Permalink
fix html output for title-rows
Browse files Browse the repository at this point in the history
- adds `CellType` for each `Cell` with corresponding getter/setter
- in `Cell::print_html()` choose `<th>` or `<td>` 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`
  • Loading branch information
michaelsippel committed Feb 1, 2024
1 parent 121ca97 commit d65ea7f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
34 changes: 32 additions & 2 deletions src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,6 +25,7 @@ pub struct Cell {
align: Alignment,
style: Vec<Attr>,
hspan: usize,
cell_type: CellType
}

impl Cell {
Expand All @@ -38,6 +46,7 @@ impl Cell {
align,
style: Vec::new(),
hspan: 1,
cell_type: CellType::Data
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -306,10 +331,14 @@ impl Cell {
let content = self.content.join("<br />");
out.write_all(
format!(
"<td{1} style=\"{2}\">{0}</td>",
"<{3}{1} style=\"{2}\">{0}</{3}>",
HtmlEscape(&content),
colspan,
styles
styles,
match self.cell_type {
CellType::Head => "th",
CellType::Data => "td",
}
)
.as_bytes(),
)?;
Expand Down Expand Up @@ -345,6 +374,7 @@ impl Default for Cell {
align: Alignment::LEFT,
style: Vec::new(),
hspan: 1,
cell_type: CellType::Data
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -218,9 +218,9 @@ impl<'a> TableSlice<'a> {
out.write_all(b"<table>")?;
// Print titles / table header
if let Some(ref t) = *self.titles {
out.write_all(b"<th>")?;
out.write_all(b"<tr>")?;
t.print_html(out, column_num)?;
out.write_all(b"</th>")?;
out.write_all(b"</tr>")?;
}
// Print rows
for r in self.rows {
Expand Down Expand Up @@ -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);
}

Expand Down
10 changes: 9 additions & 1 deletion src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<T: Write + ?Sized, F>(
&self,
Expand Down Expand Up @@ -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<T: Write + ?Sized>(&self, out: &mut T, col_num: usize) -> Result<(), Error> {
let mut printed_columns = 0;
for cell in self.iter() {
Expand Down

0 comments on commit d65ea7f

Please sign in to comment.