11extern crate clap;
22extern crate pad;
33
4- use clap:: { App , Arg } ;
5- use pad:: PadStr ;
4+ mod config;
65
6+ use pad:: PadStr ;
77use std:: io:: { self , Result , BufRead , BufReader } ;
88use std:: fs:: { self , File } ;
99use std:: cmp:: max;
10-
11- type TableData = Vec < Vec < String > > ;
12-
13- struct Config {
14- minimize : bool ,
15- separator : String ,
16- file : Option < String > ,
17- outfile : Option < String > ,
18- }
19-
20- fn get_config ( ) -> Config {
21- let args = App :: new ( "mdtable" )
22- . version ( "1.0.1" )
23- . author ( "Axel Lindeberg" )
24- . about ( "Makes creating tables in markdown much easier!" )
25- . arg ( Arg :: with_name ( "minimize" )
26- . help ( "Minimizes table output" )
27- . long ( "minimize" )
28- . short ( "m" )
29- )
30- . arg ( Arg :: with_name ( "file" )
31- . help ( "Reads table values from this if given, stdin otherwise." )
32- . long ( "file" )
33- . short ( "f" )
34- . takes_value ( true )
35- )
36- . arg ( Arg :: with_name ( "outfile" )
37- . help ( "Prints output to this if given, stdout otherwise." )
38- . long ( "out" )
39- . short ( "o" )
40- . takes_value ( true )
41- )
42- . arg ( Arg :: with_name ( "separator" )
43- . help ( "String that separates values." )
44- . long ( "separator" )
45- . short ( "s" )
46- . default_value ( "," )
47- )
48- . get_matches ( ) ;
49-
50- Config {
51- minimize : args. is_present ( "minimize" ) ,
52- separator : args. value_of ( "separator" ) . map ( String :: from) . unwrap ( ) ,
53- file : args. value_of ( "file" ) . map ( String :: from) ,
54- outfile : args. value_of ( "outfile" ) . map ( String :: from) ,
55- }
56- }
10+ use config:: Config ;
5711
5812fn read_lines ( file : & Option < String > ) -> Result < Vec < String > > {
5913 match file {
@@ -74,8 +28,8 @@ fn read_lines(file: &Option<String>) -> Result<Vec<String>> {
7428 }
7529}
7630
77- fn parse_table_data ( lines : & Vec < String > , separator : & String ) -> TableData {
78- let mut rows: TableData = lines. iter ( )
31+ fn parse_table_data ( lines : & Vec < String > , separator : & String ) -> Vec < Vec < String > > {
32+ let mut rows: Vec < Vec < String > > = lines. iter ( )
7933 . map ( |line| line
8034 . split ( separator)
8135 . map ( |word| word. trim ( ) )
@@ -86,14 +40,14 @@ fn parse_table_data(lines: &Vec<String>, separator: &String) -> TableData {
8640 let max_len = rows. iter ( )
8741 . map ( |row| row. len ( ) )
8842 . max ( )
89- . unwrap ( ) ;
43+ . unwrap_or ( 0 ) ;
9044 for row in & mut rows {
9145 row. resize ( max_len, String :: new ( ) ) ;
9246 }
9347 rows
9448}
9549
96- fn format_minimized ( rows : & TableData ) -> String {
50+ fn format_minimized ( rows : & Vec < Vec < String > > ) -> String {
9751 [
9852 rows[ 0 ] . join ( "|" ) ,
9953 vec ! [ "---" ; rows[ 0 ] . len( ) ] . join ( "|" ) ,
@@ -104,7 +58,7 @@ fn format_minimized(rows: &TableData) -> String {
10458 ] . join ( "\n " )
10559}
10660
107- fn format_pretty ( data : & TableData ) -> String {
61+ fn format_pretty ( data : & Vec < Vec < String > > ) -> String {
10862 let lengths = data. iter ( ) . fold (
10963 vec ! [ 1 ; data[ 0 ] . len( ) ] ,
11064 |lens, row| row. iter ( )
@@ -136,12 +90,12 @@ fn format_pretty(data: &TableData) -> String {
13690}
13791
13892fn main ( ) -> Result < ( ) > {
139- let config = get_config ( ) ;
93+ let config = Config :: from_args ( ) ;
14094 let lines = read_lines ( & config. file ) ?;
14195 let data = parse_table_data ( & lines, & config. separator ) ;
14296
14397 if data. len ( ) < 2 || data[ 0 ] . len ( ) == 0 {
144- eprintln ! ( "Table requires at least 2 rows (including header) and 1 column." ) ;
98+ eprintln ! ( "Bad Input: Table requires at least 2 rows (including header) and 1 column." ) ;
14599 std:: process:: exit ( 1 ) ;
146100 }
147101
0 commit comments