pretty_table/
prelude.rs

1pub use crate::error::Error;
2pub use crate::print_table;
3use crate::table::generate_table_string_vec;
4pub use crate::TableOptions;
5use std::{fs::File, io::Write};
6
7/// This is a simple macro that takes 1 or more number of Vectors of any type that's coersable into `String` type, and print them as
8/// pretty-formatted table in the standard output.
9///
10/// ## Example
11///
12/// ```rust
13/// use pretty_table::prelude::*;
14///
15/// fn main() {
16///     // usage with individual rows
17///     print_table!(
18///         vec!["Name", "Age", "Salary"], // header
19///         vec!["Altmann", "45", "11.0k"],
20///         vec!["Bezos", "32", "99.34k"],
21///         vec!["Pichai", "56", "9.9m"],
22///         vec!["Cook", "43", "8.2m"]
23///     );
24///
25///     // usage with table data
26///     print_table!(
27///         vec![
28///             vec!["Name", "Age", "Salary"], // header
29///             vec!["Altmann", "45", "11.0k"],
30///             vec!["Bezos", "32", "99.34k"],
31///             vec!["Pichai", "56", "9.9m"],
32///             vec!["Cook", "43", "8.2m"]
33///         ]
34///     );
35/// }
36/// ```
37#[macro_export]
38macro_rules! print_table {
39    ($rows:expr) => {
40        {
41            use pretty_table::table::generate_table_string_vec;
42
43            let table_rows: Vec<Vec<String>> = $rows.into_iter().map(|row| {
44                row.into_iter().map(|data| data.into()).collect::<Vec<String>>()
45            }).collect::<Vec<Vec<String>>>();
46
47            for row in generate_table_string_vec(table_rows, None).into_iter() {
48                println!("{}", row);
49            }
50        }
51    };
52    ($($row:expr), +) => {
53        {
54            use pretty_table::table::generate_table_string_vec;
55
56
57            let table_rows: Vec<Vec<String>> = vec![
58                $($row.into_iter().map(String::from).collect()),+
59            ];
60
61            for row in generate_table_string_vec(table_rows, None).into_iter() {
62                println!("{}", row);
63            }
64        }
65    }
66}
67
68/// This function is used to write the table into a file with specified filename. The function takes filename and table data as
69/// 2-D Vector of any type that can be coerse into a `String` type.
70///
71/// ## Example
72///
73/// ```rust
74/// use pretty_table::prelude::*;
75///
76/// fn main() {
77///     let table_data = vec![
78///         vec!["Name", "Age", "Salary"], // header
79///         vec!["Altmann", "45", "11.0k"],
80///         vec!["Bezos", "32", "99.34k"],
81///         vec!["Pichai", "56", "9.9m"],
82///         vec!["Cook", "43", "8.2m"],
83///     ];
84///
85///     write_table_to_file("table.txt", table_data).unwrap();
86/// }
87/// ```
88pub fn write_table_to_file<S>(filename: S, table_rows: Vec<Vec<S>>) -> Result<(), Error>
89where
90    S: Into<String>,
91{
92    let filename = filename.into();
93    let rows = table_rows
94        .into_iter()
95        .map(|row| {
96            row.into_iter()
97                .map(|field| field.into())
98                .collect::<Vec<String>>()
99        })
100        .collect::<Vec<Vec<String>>>();
101
102    let prettified_table_rows_vec = generate_table_string_vec(rows, None);
103
104    let mut file = File::create(&filename)?;
105
106    for row in prettified_table_rows_vec.into_iter() {
107        writeln!(file, "{}", row)?;
108    }
109
110    Ok(())
111}