summaryrefslogtreecommitdiff
path: root/utils/src/display/table.rs
blob: ae745d8acbb84689836bc0cfbc8621e8a3483847 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
pub struct Table {
    items: Vec<String>,
    line: Vec<Vec<String>>,
    length: Vec<usize>,
    padding: usize,
}

impl Table {
    /// Create a new Table
    pub fn new(items: Vec<impl Into<String>>) -> Self {
        Self::new_with_padding(items, 2)
    }

    /// Create a new Table with padding
    pub fn new_with_padding(items: Vec<impl Into<String>>, padding: usize) -> Self {
        let items: Vec<String> = items.into_iter().map(|v| v.into()).collect();
        let mut length = Vec::with_capacity(items.len());

        for item in &items {
            length.push(display_width(item));
        }

        Table {
            items,
            padding,
            line: Vec::new(),
            length,
        }
    }

    /// Push a new row of items to the table
    pub fn push_item(&mut self, items: Vec<impl Into<String>>) {
        let items: Vec<String> = items.into_iter().map(|v| v.into()).collect();

        let mut processed_items = Vec::with_capacity(self.items.len());

        for i in 0..self.items.len() {
            if i < items.len() {
                processed_items.push(items[i].clone());
            } else {
                processed_items.push(String::new());
            }
        }

        for (i, d) in processed_items.iter().enumerate() {
            let d_len = display_width(d);
            if d_len > self.length[i] {
                self.length[i] = d_len;
            }
        }

        self.line.push(processed_items);
    }

    /// Insert a new row of items at the specified index
    pub fn insert_item(&mut self, index: usize, items: Vec<impl Into<String>>) {
        let items: Vec<String> = items.into_iter().map(|v| v.into()).collect();

        let mut processed_items = Vec::with_capacity(self.items.len());

        for i in 0..self.items.len() {
            if i < items.len() {
                processed_items.push(items[i].clone());
            } else {
                processed_items.push(String::new());
            }
        }

        for (i, d) in processed_items.iter().enumerate() {
            let d_len = display_width(d);
            if d_len > self.length[i] {
                self.length[i] = d_len;
            }
        }

        self.line.insert(index, processed_items);
    }

    /// Get the current maximum column widths
    fn get_column_widths(&self) -> &[usize] {
        &self.length
    }
}

impl std::fmt::Display for Table {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let column_widths = self.get_column_widths();

        // Build the header row
        let header: Vec<String> = self
            .items
            .iter()
            .enumerate()
            .map(|(i, item)| {
                let target_width = column_widths[i] + self.padding;
                let current_width = display_width(item);
                let space_count = target_width - current_width;
                let space = " ".repeat(space_count);
                let result = format!("{}{}", item, space);
                result
            })
            .collect();
        writeln!(f, "{}", header.join(""))?;

        // Build each data row
        for row in &self.line {
            let formatted_row: Vec<String> = row
                .iter()
                .enumerate()
                .map(|(i, cell)| {
                    let target_width = column_widths[i] + self.padding;
                    let current_width = display_width(cell);
                    let space_count = target_width - current_width;
                    let spaces = " ".repeat(space_count);
                    let result = format!("{}{}", cell, spaces);
                    result
                })
                .collect();
            writeln!(f, "{}", formatted_row.join(""))?;
        }

        Ok(())
    }
}

pub fn display_width(s: &str) -> usize {
    // Filter out ANSI escape sequences before calculating width
    let filtered_bytes = strip_ansi_escapes::strip(s);
    let filtered_str = match std::str::from_utf8(&filtered_bytes) {
        Ok(s) => s,
        Err(_) => s, // Fallback to original string if UTF-8 conversion fails
    };

    let mut width = 0;
    for c in filtered_str.chars() {
        if c.is_ascii() {
            width += 1;
        } else {
            width += 2;
        }
    }
    width
}