cli_table/lib.rs
1#![forbid(unsafe_code)]
2#![cfg_attr(not(feature = "doc"), forbid(unstable_features))]
3#![deny(missing_docs)]
4#![cfg_attr(feature = "doc", feature(doc_cfg))]
5//! Rust crate for printing tables on command line.
6//!
7//! # Usage
8//!
9//! Add `cli-table` in your `Cargo.toml`'s `dependencies` section
10//!
11//! ```toml
12//! [dependencies]
13//! cli-table = "0.5"
14//! ```
15//!
16//! ## Simple usage
17//!
18//! ```rust
19//! use cli_table::{format::Justify, print_stdout, Cell, Style, Table};
20//!
21//! let table = vec![
22//! vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
23//! vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
24//! vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
25//! ]
26//! .table()
27//! .title(vec![
28//! "Name".cell().bold(true),
29//! "Age (in years)".cell().bold(true),
30//! ])
31//! .bold(true);
32//!
33//! assert!(print_stdout(table).is_ok());
34//! ```
35//!
36//! Below is the output of the table we created just now:
37//!
38//! ```markdown
39//! +------------+----------------+
40//! | Name | Age (in years) | <-- This row and all the borders/separators
41//! +------------+----------------+ will appear in bold
42//! | Tom | 10 |
43//! +------------+----------------+
44//! | Jerry | 15 |
45//! +------------+----------------+
46//! | Scooby Doo | 25 |
47//! +------------+----------------+
48//! ```
49//!
50//! ## `Display` trait implementation
51//!
52//! To get a `Display` trait implementation of `TableStruct`, use `display()` function on the struct to get an instance
53//! of `TableDisplay` which implements `Display` trait.
54//!
55//! ```rust
56//! use cli_table::{format::Justify, Cell, Style, Table};
57//!
58//! let table = vec![
59//! vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
60//! vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
61//! vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
62//! ]
63//! .table()
64//! .title(vec![
65//! "Name".cell().bold(true),
66//! "Age (in years)".cell().bold(true),
67//! ])
68//! .bold(true);
69//!
70//! let table_display = table.display().unwrap();
71//!
72//! println!("{}", table_display);
73//! ```
74//!
75//! Below is the output of the table we created just now:
76//!
77//! ```markdown
78//! +------------+----------------+
79//! | Name | Age (in years) | <-- This row and all the borders/separators
80//! +------------+----------------+ will appear in bold
81//! | Tom | 10 |
82//! +------------+----------------+
83//! | Jerry | 15 |
84//! +------------+----------------+
85//! | Scooby Doo | 25 |
86//! +------------+----------------+
87//! ```
88//!
89//! ## Derive macro
90//!
91//! `#[derive(Table)]` can also be used to print a `Vec` or slice of `struct`s as table.
92//!
93//! ```rust
94//! use cli_table::{format::Justify, print_stdout, Table, WithTitle};
95//!
96//! #[derive(Table)]
97//! struct User {
98//! #[table(title = "ID", justify = "Justify::Right")]
99//! id: u64,
100//! #[table(title = "First Name")]
101//! first_name: &'static str,
102//! #[table(title = "Last Name")]
103//! last_name: &'static str,
104//! }
105//!
106//! let users = vec![
107//! User {
108//! id: 1,
109//! first_name: "Scooby",
110//! last_name: "Doo",
111//! },
112//! User {
113//! id: 2,
114//! first_name: "John",
115//! last_name: "Cena",
116//! },
117//! ];
118//!
119//! assert!(print_stdout(users.with_title()).is_ok());
120//! ```
121//!
122//! Below is the output of the table we created using derive macro:
123//!
124//! ```markdown
125//! +----+------------+-----------+
126//! | ID | First Name | Last Name | <-- This row will appear in bold
127//! +----+------------+-----------+
128//! | 1 | Scooby | Doo |
129//! +----+------------+-----------+
130//! | 2 | John | Cena |
131//! +----+------------+-----------+
132//! ```
133//!
134//! ### Field attributes
135//!
136//! - `title` | `name`: Used to specify title of a column. Usage: `#[table(title = "Title")]`
137//! - `justify`: Used to horizontally justify the contents of a column. Usage: `#[table(justify = "Justify::Right")]`
138//! - `align`: Used to vertically align the contents of a column. Usage: `#[table(align = "Align::Top")]`
139//! - `color`: Used to specify color of contents of a column. Usage: `#[table(color = "Color::Red")]`
140//! - `bold`: Used to specify boldness of contents of a column. Usage: `#[table(bold)]`
141//! - `order`: Used to order columns in a table while printing. Usage: `#[table(order = <usize>)]`. Here, columns will
142//! be sorted based on their order. For e.g., column with `order = 0` will be displayed on the left followed by
143//! column with `order = 1` and so on.
144//! - `display_fn`: Used to print types which do not implement `Display` trait. Usage `#[table(display_fn = "<func_name>")]`.
145//! Signature of provided function should be `fn <func_name>(value: &<type>) -> impl Display`.
146//! - `customize_fn`: Used to customize style of a cell. Usage `#[table(customize_fn = "<func_name>")]`. Signature of
147//! provided function should be `fn <func_name>(cell: CellStruct, value: &<type>) -> CellStruct`. This attribute can
148//! be used when you want to change the formatting/style of a cell based on its contents. Note that this will
149//! overwrite all the style settings done by other attributes.
150//! - `skip`: Used to skip a field from table. Usage: `#[table(skip)]`
151//!
152//! For more information on configurations available on derive macro, go to `cli-table/examples/struct.rs`.
153//!
154//! ## CSV
155//!
156//! This crate also integrates with [`csv`](https://crates.io/crates/csv) crate. On enabling `"csv"` feature, you can
157//! use `TryFrom<&mut Reader> for TableStruct` trait implementation to convert `csv::Reader` to `TableStruct`.
158//!
159//! For more information on handling CSV values, go to `cli-table/examples/csv.rs`.
160//!
161//! # Styling
162//!
163//! Style of a table/cell can be modified by calling functions of [`Style`] trait. It is implementated by both
164//! [`TableStruct`] and [`CellStruct`].
165//!
166//! For individually formatting each cell of a table, `justify`, `align` and `padding` functions can be used from
167//! `CellStruct`.
168//!
169//! In addition to this, borders and separators of a table can be customized by calling `border` and `separator`
170//! functions in `TableStruct`. For example, to create a borderless table:
171//!
172//! ```rust
173//! use cli_table::{Cell, Table, TableStruct, format::{Justify, Border}, print_stdout};
174//!
175//! fn get_table() -> TableStruct {
176//! vec![
177//! vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
178//! vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
179//! vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
180//! ]
181//! .table()
182//! }
183//!
184//! let table = get_table().border(Border::builder().build()); // Attaches an empty border to the table
185//! assert!(print_stdout(table).is_ok());
186//! ```
187//!
188//! # Features
189//!
190//! - `derive`: Enables derive macro for creating tables using structs. **Enabled** by default.
191//! - `csv`: Enables support for printing tables using [`csv`](https://crates.io/crates/csv). **Enabled** by default.
192mod buffers;
193mod cell;
194#[cfg(feature = "csv")]
195mod csv;
196mod display;
197mod row;
198mod style;
199mod table;
200#[cfg(any(feature = "title", feature = "derive"))]
201mod title;
202mod utils;
203
204pub mod format;
205
206pub use termcolor::{Color, ColorChoice};
207
208#[cfg(feature = "derive")]
209#[cfg_attr(feature = "doc", doc(cfg(feature = "derive")))]
210pub use cli_table_derive::Table;
211
212pub use self::{
213 cell::{Cell, CellStruct},
214 display::TableDisplay,
215 row::{Row, RowStruct},
216 style::Style,
217 table::{Table, TableStruct},
218};
219
220#[cfg(any(feature = "title", feature = "derive"))]
221pub use self::title::{Title, WithTitle};
222
223use std::io::Result;
224
225/// Prints a table to `stdout`
226pub fn print_stdout<T: Table>(table: T) -> Result<()> {
227 table.table().print_stdout()
228}
229
230/// Prints a table to `stderr`
231pub fn print_stderr<T: Table>(table: T) -> Result<()> {
232 table.table().print_stderr()
233}