diff --git a/examples/formatting.rs b/examples/formatting.rs index 430568783..22404923f 100644 --- a/examples/formatting.rs +++ b/examples/formatting.rs @@ -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 // +-------------+------------+ diff --git a/src/format.rs b/src/format.rs index 5ddaeb104..0169d017b 100644 --- a/src/format.rs +++ b/src/format.rs @@ -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 `+` @@ -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(); } } diff --git a/src/lib.rs b/src/lib.rs index 5b5d8e4cd..daa64e33a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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();