tabled/macros/col.rs
1/// Creates a [`Table`] with [`Display`] arguments nested within.
2///
3/// The macros allows several tables to be displayed vertically.
4///
5/// Companion to [`row!`].
6///
7/// # Examples
8/// ```rust,no_run
9/// # use tabled::{row, col, Table};
10/// # let (table1, table2, table3) = (Table::new(&[String::new()]), Table::new(&[String::new()]), Table::new(&[String::new()]));
11/// let new_table = col![table1, table2];
12/// let new_table_of_clones = col![table1; 3];
13/// let columns_and_rows = col![
14/// table1,
15/// row![table2, table3]
16/// ];
17/// ```
18///
19/// [`row!`]: crate::row
20/// [`Table`]: crate::Table
21/// [`Display`]: std::fmt::Display
22#[macro_export]
23#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
24macro_rules! col {
25 // Vertical
26 ( $($table:expr), * $(,)? ) => {{
27 let mut builder = $crate::builder::Builder::default();
28
29 $(
30 builder.add_record([$table.to_string()]);
31 )*
32
33 builder.build()
34 }};
35
36 // Duplicate single item
37 ( $table:expr; $N:expr) => {{
38 let mut builder = $crate::builder::Builder::default();
39
40 let n = $N;
41 if n > 0 {
42 let t = $table.to_string();
43 for _ in 0..$N {
44 builder.add_record([t.clone()]);
45 }
46 }
47
48 builder.build()
49 }};
50}