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

Add markdown table formatting #118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions examples/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ fn main() {
table.printstd();
println!("");

// Print
// | Title 1 | Title 2 |
// |-------------|------------|
// | Value 1 | Value 2 |
// | Value three | Value four |
println!("FORMAT_MARKDOWN :");
table.set_format(*format::consts::FORMAT_MARKDOWN);
table.printstd();
println!("");

// Custom format can be implemented using `prettytable::format::FormatBuilder`
// Example to print
// +-------------+------------+
Expand Down
19 changes: 19 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ pub mod consts {
use super::{FormatBuilder, LinePosition, LineSeparator, TableFormat};

lazy_static! {

/// A line separator made of `-` and `|`
static ref MINUS_PIPE_SEP: LineSeparator = LineSeparator::new('-', '|', '|', '|');
/// A line separator made of `-` and `+`
static ref MINUS_PLUS_SEP: LineSeparator = LineSeparator::new('-', '+', '+', '+');
/// A line separator made of `=` and `+`
Expand Down Expand Up @@ -562,5 +565,21 @@ pub mod consts {
'┘'))
.padding(1, 1)
.build();

/// A markdown table
///
/// # Example
/// ```text
/// | Title 1 | Title 2 |
/// |-------------|------------|
/// | Value 1 | Value 2 |
/// | Value three | Value four |
/// ```
pub static ref FORMAT_MARKDOWN: TableFormat = FormatBuilder::new()
.padding(1, 1)
.borders('|')
.separator(LinePosition::Title, *MINUS_PIPE_SEP)
.column_separator('|')
.build();
}
}
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,27 @@ mod tests {
assert_eq!(6, table.print(&mut StringWriter::new()).unwrap());
}

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

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_empty_table_with_title() {
let mut table = Table::new();
Expand Down