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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use lazy_static;
use regex::Regex;
use std::cmp;
use std::collections::HashSet;

use unicode_width::UnicodeWidthChar;
use unicode_width::UnicodeWidthStr;

/// Represents the horizontal alignment of content within a cell.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Alignment {
    Left,
    Right,
    Center,
}

///A table cell containing some str data.
///
///A cell may span multiple columns by setting the value of `col_span`.
///
///`pad_content` will add a space to either side of the cell's content.AsRef
#[derive(Debug, Clone)]
pub struct TableCell {
    pub data: String,
    pub col_span: usize,
    pub alignment: Alignment,
    pub pad_content: bool,
}

impl TableCell {
    pub fn new<T>(data: T) -> TableCell
    where
        T: ToString,
    {
        Self {
            data: data.to_string(),
            col_span: 1,
            alignment: Alignment::Left,
            pad_content: true,
        }
    }

    pub fn builder<T>(data: T) -> TableCellBuilder
    where
        T: ToString,
    {
        TableCellBuilder::new(data.to_string())
    }

    #[deprecated(since = "1.4.0", note = "Use builder instead")]
    pub fn new_with_col_span<T>(data: T, col_span: usize) -> TableCell
    where
        T: ToString,
    {
        Self {
            data: data.to_string(),
            alignment: Alignment::Left,
            pad_content: true,
            col_span,
        }
    }

    #[deprecated(since = "1.4.0", note = "Use builder instead")]
    pub fn new_with_alignment<T>(data: T, col_span: usize, alignment: Alignment) -> TableCell
    where
        T: ToString,
    {
        Self {
            data: data.to_string(),
            pad_content: true,
            col_span,
            alignment,
        }
    }

    #[deprecated(since = "1.4.0", note = "Use builder instead")]
    pub fn new_with_alignment_and_padding<T>(
        data: T,
        col_span: usize,
        alignment: Alignment,
        pad_content: bool,
    ) -> TableCell
    where
        T: ToString,
    {
        Self {
            data: data.to_string(),
            col_span,
            alignment,
            pad_content,
        }
    }

    /// Calculates the width of the cell.
    ///
    /// New line characters are taken into account during the calculation.
    pub fn width(&self) -> usize {
        let wrapped = self.wrapped_content(std::usize::MAX);
        let mut max = 0;
        for s in wrapped {
            let str_width = string_width(&s);
            max = cmp::max(max, str_width);
        }
        max
    }

    /// The width of the cell's content divided by its `col_span` value.
    pub fn split_width(&self) -> f32 {
        let res = self.width() as f32 / self.col_span as f32;
        res
    }

    /// The minium width required to display the cell properly
    pub fn min_width(&self) -> usize {
        let mut max_char_width: usize = 0;
        for c in self.data.chars() {
            max_char_width = cmp::max(max_char_width, c.width().unwrap_or(1) as usize);
        }

        if self.pad_content {
            max_char_width + ' '.width().unwrap_or(1) as usize * 2
        } else {
            max_char_width
        }
    }

    /// Wraps the cell's content to the provided width.
    ///
    /// New line characters are taken into account.
    pub fn wrapped_content(&self, width: usize) -> Vec<String> {
        let pad_char = if self.pad_content { ' ' } else { '\0' };
        let hidden: HashSet<usize> = STRIP_ANSI_RE
            .find_iter(&self.data)
            .flat_map(|m| m.start()..m.end())
            .collect();
        let mut res: Vec<String> = Vec::new();
        let mut buf = String::new();
        buf.push(pad_char);
        let mut byte_index = 0;
        for c in self.data.chars() {
            if !hidden.contains(&byte_index)
                && (string_width(&buf) >= width - pad_char.width().unwrap_or(1) || c == '\n')
            {
                buf.push(pad_char);
                res.push(buf);
                buf = String::new();
                buf.push(pad_char);
                if c == '\n' {
                    byte_index += 1;
                    continue;
                }
            }
            byte_index += c.len_utf8();
            buf.push(c);
        }
        buf.push(pad_char);
        res.push(buf);

        res
    }
}

impl<T> From<T> for TableCell
where
    T: ToString,
{
    fn from(other: T) -> Self {
        TableCell::new(other)
    }
}

pub struct TableCellBuilder {
    data: String,
    col_span: usize,
    alignment: Alignment,
    pad_content: bool,
}

impl Into<TableCell> for TableCellBuilder {
    fn into(self) -> TableCell {
        self.build()
    }
}

impl Into<TableCell> for &mut TableCellBuilder {
    fn into(self) -> TableCell {
        self.build()
    }
}

impl TableCellBuilder {
    fn new(data: String) -> TableCellBuilder {
        TableCellBuilder {
            data,
            col_span: 1,
            alignment: Alignment::Left,
            pad_content: true,
        }
    }

    pub fn col_span(&mut self, col_span: usize) -> &mut Self {
        self.col_span = col_span;
        self
    }

    pub fn alignment(&mut self, alignment: Alignment) -> &mut Self {
        self.alignment = alignment;
        self
    }

    pub fn pad_content(&mut self, pad_content: bool) -> &mut Self {
        self.pad_content = pad_content;
        self
    }

    pub fn build(&self) -> TableCell {
        TableCell {
            data: self.data.clone(),
            col_span: self.col_span,
            alignment: self.alignment,
            pad_content: self.pad_content,
        }
    }
}

// Taken from https://github.com/mitsuhiko/console
lazy_static! {
    static ref STRIP_ANSI_RE: Regex =
        Regex::new(r"[\x1b\x9b][\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]")
            .unwrap();
}

// The width of a string. Strips ansi characters
pub fn string_width(string: &str) -> usize {
    let stripped = STRIP_ANSI_RE.replace_all(string, "");
    stripped.width()
}