1use tabled::{assert::assert_table, settings::style::Style, tables::CompactTable};
2
3fn main() {
4 let data = [
5 ["Debian", "1.1.1.1", "true"],
6 ["Arch", "127.1.1.1", "true"],
7 ["Manjaro", "Arch", "true"],
8 ];
9
10 let table = CompactTable::from(data).with(Style::modern());
11 let mut buf = [0; 1024 * 10];
12 let mut w = Writer::new(&mut buf);
13
14 table.fmt(&mut w).unwrap();
15
16 assert_table!(w.as_str(),
17 "┌─────────┬───────────┬──────┐"
18 "│ Debian │ 1.1.1.1 │ true │"
19 "│─────────┼───────────┼──────│"
20 "│ Arch │ 127.1.1.1 │ true │"
21 "│─────────┼───────────┼──────│"
22 "│ Manjaro │ Arch │ true │"
23 "└─────────┴───────────┴──────┘"
24 );
25}
26
27struct Writer<'a> {
28 buf: &'a mut [u8],
29 cursor: usize,
30}
31
32impl<'a> Writer<'a> {
33 fn new(buf: &'a mut [u8]) -> Self {
34 Self { buf, cursor: 0 }
35 }
36
37 fn as_str(&self) -> &str {
38 core::str::from_utf8(&self.buf[0..self.cursor]).unwrap()
39 }
40}
41
42impl core::fmt::Write for Writer<'_> {
43 fn write_str(&mut self, s: &str) -> core::fmt::Result {
44 let cap = self.buf.len();
45
46 for (i, &b) in self.buf[self.cursor..cap]
47 .iter_mut()
48 .zip(s.as_bytes().iter())
49 {
50 *i = b;
51 }
52
53 self.cursor = usize::min(cap, self.cursor + s.len());
54
55 Ok(())
56 }
57}