diff options
author | Pierre-Henri Symoneaux <phsym@users.noreply.github.com> | 2018-08-20 10:40:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-20 10:40:51 +0200 |
commit | ece1d8e012eef9782e175f0485b8da2d9ab8b1db (patch) | |
tree | 3a9578e2f5d4f624fde2a34b69de73e18c444fd1 | |
parent | f92e4df1f9894a63d0eadcd127fcf023aac5e72b (diff) | |
parent | 5320d55be62d55a2a2be5458e46b9352cd70edf0 (diff) |
Merge pull request #82 from pjf/readme_formats
Document set_titles() in README, plus bonus tests
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | src/lib.rs | 57 |
2 files changed, 67 insertions, 1 deletions
@@ -269,13 +269,14 @@ Configurable settings include: - Junctions
- Column separators
- Line separators
+- Titles (using `table.set_titles()`)
To do this, either:
- create a new `TableFormat` object, then call setters until you get the desired configuration;
- or use the convenient `FormatBuilder` and Builder pattern, shown below
```rust
-let mut table = /* Initialize table */;
+let mut table = Table::new();
let format = format::FormatBuilder::new()
.column_separator('|')
.borders('|')
@@ -285,6 +286,10 @@ let format = format::FormatBuilder::new() .padding(1, 1)
.build();
table.set_format(format);
+
+table.set_titles(row!["Title 1", "Title 2"]);
+table.add_row(row!["Value 1", "Value 2"]);
+table.add_row(row!["Value three", "Value four"]);
```
The code above will make the table look like
@@ -300,6 +305,8 @@ For convenience, several formats are predefined in `prettytable::format::consts` Some formats and their respective outputs:
- ```rust
+ use prettytable::format;
+
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
```
```
@@ -311,6 +318,8 @@ Some formats and their respective outputs: +-------------+------------+
```
- ```rust
+ use prettytable::format;
+
table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
```
```
@@ -967,6 +967,63 @@ mod tests { assert_eq!(out, table.to_string().replace("\r\n", "\n")); } + #[test] + fn test_readme_format() { + + // The below is lifted from the README + + let mut table = Table::new(); + let format = format::FormatBuilder::new() + .column_separator('|') + .borders('|') + .separators(&[format::LinePosition::Top, + format::LinePosition::Bottom], + format::LineSeparator::new('-', '+', '+', '+')) + .padding(1, 1) + .build(); + table.set_format(format); + + table.set_titles(Row::new(vec![Cell::new("Title 1"), Cell::new("Title 2")])); + table.add_row(Row::new(vec![Cell::new("Value 1"), Cell::new("Value 2")])); + table.add_row(Row::new(vec![Cell::new("Value three"), Cell::new("Value four")])); + + let out = "\ ++-------------+------------+ +| Title 1 | Title 2 | +| Value 1 | Value 2 | +| Value three | Value four | ++-------------+------------+ +"; + + println!("{}", out); + println!("____"); + println!("{}", table.to_string().replace("\r\n","\n")); + assert_eq!(out, table.to_string().replace("\r\n","\n")); + } + + #[test] + fn test_readme_format_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")])); + table.add_row(Row::new(vec![Cell::new("Value 1"), Cell::new("Value 2")])); + table.add_row(Row::new(vec![Cell::new("Value three"), Cell::new("Value four")])); + + let out = "\ ++-------------+------------+ +| Title 1 | Title 2 | ++-------------+------------+ +| Value 1 | Value 2 | +| Value three | Value four | ++-------------+------------+ +"; + println!("{}", out); + println!("____"); + println!("{}", table.to_string().replace("\r\n","\n")); + assert_eq!(out, table.to_string().replace("\r\n","\n")); + } + #[cfg(feature = "csv")] mod csv { use Table; |