1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
use super::Text;
/// render a table with arbitrary data
#[derive(Debug, Clone)]
pub struct Table {
contents: Vec<Vec<Text>>,
}
impl Table {
pub fn new() -> Self {
Table { contents: vec![] }
}
pub fn add_row(mut self, row: Vec<impl ToString>) -> Self {
self.contents
.push(row.iter().map(|c| Text::new(c.to_string())).collect());
self
}
pub fn add_styled_row(mut self, row: Vec<Text>) -> Self {
self.contents.push(row);
self
}
pub fn serialize(&self) -> String {
let columns = self
.contents
.get(0)
.map(|first_row| first_row.len())
.unwrap_or(0);
let rows = self.contents.len();
let contents = self
.contents
.iter()
.flatten()
.map(|t| t.serialize())
.collect::<Vec<_>>()
.join(";");
format!("{};{};{}\u{1b}\\", columns, rows, contents)
}
}
pub fn print_table(table: Table) {
print!("\u{1b}Pztable;{}", table.serialize())
}
pub fn print_table_with_coordinates(
table: Table,
x: usize,
y: usize,
width: Option<usize>,
height: Option<usize>,
) {
let width = width.map(|w| w.to_string()).unwrap_or_default();
let height = height.map(|h| h.to_string()).unwrap_or_default();
print!(
"\u{1b}Pztable;{}/{}/{}/{};{}\u{1b}\\",
x,
y,
width,
height,
table.serialize()
)
}
pub fn serialize_table(table: &Table) -> String {
format!("\u{1b}Pztable;{}", table.serialize())
}
pub fn serialize_table_with_coordinates(
table: &Table,
x: usize,
y: usize,
width: Option<usize>,
height: Option<usize>,
) -> String {
let width = width.map(|w| w.to_string()).unwrap_or_default();
let height = height.map(|h| h.to_string()).unwrap_or_default();
format!(
"\u{1b}Pztable;{}/{}/{}/{};{}\u{1b}\\",
x,
y,
width,
height,
table.serialize()
)
}
|