writer/writer.rs
1//! This example demonstrates how to use the [write](TableFormatter::write) method of a [TableFormatter].
2//! The [write](TableFormatter::write) and [debug_write](TableFormatter::debug_write) methods allow you to
3//! write a table result to a [Write] object rather than just stdout. (In this example, the Write object is
4//! just stdout, but you get the idea)
5//!
6//! Also note that this example uses a non-debug writing method. Tuples and arrays have default
7//! implementations for [DisplayTableRow](kitty_table::DisplayTableRow) whenever their members implement
8//! [Display](std::fmt::Display). For other types, this will need to be manually implemented, unlike
9//! [DebugTableRow](kitty_table::DebugTableRow) which can be `derive`d.
10
11use std::io::{stdout, Write};
12
13use kitty_table::TableFormatter;
14
15pub fn main() {
16 let table = TableFormatter::from_style();
17
18 let data = [
19 [1, 0, 0, 0],
20 [0, 1, 0, 0],
21 [0, 0, 1, 0],
22 [0, 0, 0, 1]
23 ];
24
25 let mut out = stdout().lock();
26
27 if let Err(err) = table.write(&mut out, data) {
28 panic!("Failed to print table due to {}.", err.kind());
29 }
30
31 let _ = out.flush();
32}
33
34/*
35Expected result:
36
37┏━━━┯━━━┯━━━┯━━━┓
38┃ 1 │ 0 │ 0 │ 0 ┃
39┠───┼───┼───┼───┨
40┃ 0 │ 1 │ 0 │ 0 ┃
41┠───┼───┼───┼───┨
42┃ 0 │ 0 │ 1 │ 0 ┃
43┠───┼───┼───┼───┨
44┃ 0 │ 0 │ 0 │ 1 ┃
45┗━━━┷━━━┷━━━┷━━━┛
46*/