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