Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix display of empty tables #127

Merged
merged 2 commits into from Dec 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/lib.rs
Expand Up @@ -70,7 +70,10 @@ impl<'a> TableSlice<'a> {
/// Compute and return the number of column
// #[deprecated(since="0.8.0", note="Will become private in future release. See [issue #87](https://github.com/phsym/prettytable-rs/issues/87)")]
fn get_column_num(&self) -> usize {
let mut cnum = 0;
let mut cnum = match *self.titles {
Some(ref t) => t.column_count(),
None => 0,
};
for r in self.rows {
let l = r.column_count();
if l > cnum {
Expand Down Expand Up @@ -975,6 +978,26 @@ mod tests {
assert_eq!(6, table.print(&mut StringWriter::new()).unwrap());
}

#[test]
fn test_empty_table_with_title() {
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);

table.set_titles(Row::new(vec![Cell::new("Title 1"), Cell::new("Title 2")]));

let out = "\
+---------+---------+
| Title 1 | Title 2 |
+---------+---------+
+---------+---------+
";
println!("{}", out);
println!("____");
println!("{}", table.to_string().replace("\r\n","\n"));
assert_eq!(out, table.to_string().replace("\r\n","\n"));
assert_eq!(4, table.print(&mut StringWriter::new()).unwrap());
}

#[test]
fn test_horizontal_span() {
let mut table = Table::new();
Expand Down