File tree Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,16 @@ fn main() {
4141 table. printstd ( ) ;
4242 println ! ( "" ) ;
4343
44+ // Print
45+ // | Title 1 | Title 2 |
46+ // |-------------|------------|
47+ // | Value 1 | Value 2 |
48+ // | Value three | Value four |
49+ println ! ( "FORMAT_MARKDOWN :" ) ;
50+ table. set_format ( * format:: consts:: FORMAT_MARKDOWN ) ;
51+ table. printstd ( ) ;
52+ println ! ( "" ) ;
53+
4454 // Custom format can be implemented using `prettytable::format::FormatBuilder`
4555 // Example to print
4656 // +-------------+------------+
Original file line number Diff line number Diff line change @@ -353,6 +353,9 @@ pub mod consts {
353353 use super :: { TableFormat , LineSeparator , FormatBuilder , LinePosition } ;
354354
355355 lazy_static ! {
356+
357+ /// A line separator made of `-` and `|`
358+ static ref MINUS_PIPE_SEP : LineSeparator = LineSeparator :: new( '-' , '|' , '|' , '|' ) ;
356359 /// A line separator made of `-` and `+`
357360 static ref MINUS_PLUS_SEP : LineSeparator = LineSeparator :: new( '-' , '+' , '+' , '+' ) ;
358361 /// A line separator made of `=` and `+`
@@ -555,5 +558,21 @@ pub mod consts {
555558 '┘' ) )
556559 . padding( 1 , 1 )
557560 . build( ) ;
561+
562+ /// A markdown table
563+ ///
564+ /// # Example
565+ /// ```text
566+ /// | Title 1 | Title 2 |
567+ /// |-------------|------------|
568+ /// | Value 1 | Value 2 |
569+ /// | Value three | Value four |
570+ /// ```
571+ pub static ref FORMAT_MARKDOWN : TableFormat = FormatBuilder :: new( )
572+ . padding( 1 , 1 )
573+ . borders( '|' )
574+ . separator( LinePosition :: Title , * MINUS_PIPE_SEP )
575+ . column_separator( '|' )
576+ . build( ) ;
558577 }
559578}
Original file line number Diff line number Diff line change @@ -975,6 +975,28 @@ mod tests {
975975 assert_eq ! ( 6 , table. print( & mut StringWriter :: new( ) ) . unwrap( ) ) ;
976976 }
977977
978+ #[ test]
979+ fn test_markdown_format_with_title ( ) {
980+ let mut table = Table :: new ( ) ;
981+ table. set_format ( * format:: consts:: FORMAT_MARKDOWN ) ;
982+
983+ table. set_titles ( Row :: new ( vec ! [ Cell :: new( "Title 1" ) , Cell :: new( "Title 2" ) ] ) ) ;
984+ table. add_row ( Row :: new ( vec ! [ Cell :: new( "Value 1" ) , Cell :: new( "Value 2" ) ] ) ) ;
985+ table. add_row ( Row :: new ( vec ! [ Cell :: new( "Value three" ) , Cell :: new( "Value four" ) ] ) ) ;
986+
987+ let out = "\
988+ | Title 1 | Title 2 |
989+ |-------------|------------|
990+ | Value 1 | Value 2 |
991+ | Value three | Value four |
992+ " ;
993+ println ! ( "{}" , out) ;
994+ println ! ( "____" ) ;
995+ println ! ( "{}" , table. to_string( ) . replace( "\r \n " , "\n " ) ) ;
996+ assert_eq ! ( out, table. to_string( ) . replace( "\r \n " , "\n " ) ) ;
997+ assert_eq ! ( 4 , table. print( & mut StringWriter :: new( ) ) . unwrap( ) ) ;
998+ }
999+
9781000 #[ test]
9791001 fn test_horizontal_span ( ) {
9801002 let mut table = Table :: new ( ) ;
You can’t perform that action at this time.
0 commit comments