phsym / prettytable-rs

A rust library to print aligned and formatted tables
https://crates.io/crates/prettytable-rs
BSD 3-Clause "New" or "Revised" License
938 stars 73 forks source link

.printhtml() method doesnt insert table headers (titles) correctly #159

Open belakm opened 11 months ago

belakm commented 11 months ago

Description

When using .printhtml() method on a table, it doesnt work correctly for titles. It inserts <td> elements into <th> elements, treating <th> as <tr> and <td> as <th>.

Current solution around this is to treat table headers as just a row as those seem to get printed just fine.

Example snippet:

let mut out = File::create("table.html").unwrap();
let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
table.set_titles(row!["Title 1", "Title 2"]);
table.print_html(&mut out);

it renders the below table as such in HTML:

<table>
  <th>
    <td style="text-align: left;">Title 1</td>
    <td style="text-align: left;">Title 2</td>
  </th>
  <!-- ... data rows -->
</table>

The correct way for table headers would be:

<table>
  <tr>
    <th style="text-align: left;">Title 1</th>
    <th style="text-align: left;">Title 2</th>
  </tr>
  <tr>
  <!-- ... data rows -->
</table>

Chrome based browsers try their best to render this and they somehow get it, but it is skewed because of the empty <th> element:

image

michaelsippel commented 9 months ago

Hi there, thanks for this nice and simple crate!

Since I ran into the exact same issue as described by @belakm and this thread remained unanswered, I decided to quickly put together a fix. PR #161

pinkforest commented 8 months ago

Hey thanks for the fix and very nicely detailed issue - I left a comment in your PR just honing the API a bit :)