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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/// The style of the table
///
/// Examples shown will have a header line and two content lines
#[derive(Clone)]
pub enum Style {
    /// ```text
    /// item      qty
    /// spam       42
    /// eggs      451
    /// ```
    Plain,
    /// ```text
    /// item      qty
    /// ------  -----
    /// spam       42
    /// eggs      451
    /// ```
    Simple,
    /// ```text
    /// | item   |   qty |
    /// |--------|-------|
    /// | spam   |    42 |
    /// | eggs   |   451 |
    /// ```
    Github,
    /// ```text
    /// +--------+-------+
    /// | item   |   qty |
    /// +========+=======+
    /// | spam   |    42 |
    /// +--------+-------+
    /// | eggs   |   451 |
    /// +--------+-------+
    /// ```
    Grid,
    /// ```text
    /// ╒════════╤═══════╕
    /// │ item   │   qty │
    /// ╞════════╪═══════╡
    /// │ spam   │    42 │
    /// ├────────┼───────┤
    /// │ eggs   │   451 │
    /// ╘════════╧═══════╛
    /// ```
    Fancy,
    /// ```text
    /// item   |   qty
    ///--------+-------
    /// spam   |    42
    /// eggs   |   451
    /// ```
    Presto,
    /// ```text
    /// │ item   │   qty │
    /// ├────────┼───────┤
    /// │ spam   │    42 │
    /// │ eggs   │   451 │
    /// ```
    FancyGithub,
    /// ```text
    /// item   │   qty
    /// ───────┼──────
    /// spam   │    42
    /// eggs   │   451
    /// ```
    FancyPresto,
}

impl Style {
    /// Get Style from &str
    pub fn from(s: &str) -> Option<Self> {
        match s {
            "plain" => Some(Self::Plain),
            "simple" => Some(Self::Simple),
            "github" => Some(Self::Github),
            "grid" => Some(Self::Grid),
            "fancy" => Some(Self::Fancy),
            "presto" => Some(Self::Presto),
            "fancygithub" => Some(Self::FancyGithub),
            "fancypresto" => Some(Self::FancyPresto),
            _ => None,
        }
    }

    /// Returns the corresponding format
    pub fn to_format(&self) -> TableFormat {
        let basicrow = DataRow::new("", "  ", "");
        let emptyformat = TableFormat {
            lineabove: None,
            linebelowheader: None,
            linebetweenrows: None,
            linebelow: None,
            headerrow: basicrow.clone(),
            datarow: basicrow,
            padding: 0,
            hidelineaboveifheader: false,
            hidelinebelowifheader: false,
        };
        let basicline = Line::new("", "-", "  ", "");
        let piperow = DataRow::new("| ", " | ", " |");
        let single_line = Line::new("", "─", "─┼─", "");
        let single_line_with_ends = Line::new("├─", "─", "─┼─", "─┤");
        let row_line = DataRow::new("", " │ ", "");
        let row_line_with_ends = DataRow::new("│ ", " │ ", " │");
        match self {
            Self::Plain => emptyformat,
            Self::Simple => TableFormat {
                lineabove: Some(basicline.clone()),
                linebelowheader: Some(basicline.clone()),
                linebelow: Some(basicline),
                hidelineaboveifheader: true,
                hidelinebelowifheader: true,
                ..emptyformat
            },
            Self::Github => {
                let line = Line::new("|-", "-", "-|-", "-|");
                TableFormat {
                    lineabove: Some(line.clone()),
                    linebelowheader: Some(line),
                    headerrow: piperow.clone(),
                    datarow: piperow,
                    padding: 1,
                    hidelineaboveifheader: true,
                    ..emptyformat
                }
            }
            Self::Grid => {
                let line = Line::new("+-", "-", "-+-", "-+");
                TableFormat {
                    lineabove: Some(line.clone()),
                    linebelowheader: Some(Line::new("+=", "=", "=+=", "=+")),
                    linebetweenrows: Some(line.clone()),
                    linebelow: Some(line),
                    headerrow: piperow.clone(),
                    datarow: piperow,
                    padding: 1,
                    ..emptyformat
                }
            }
            Self::Fancy => TableFormat {
                lineabove: Some(Line::new("╒═", "═", "═╤═", "═╕")),
                linebelowheader: Some(Line::new("╞═", "═", "═╪═", "═╡")),
                linebetweenrows: Some(single_line_with_ends),
                linebelow: Some(Line::new("╘═", "═", "═╧═", "═╛")),
                headerrow: row_line_with_ends.clone(),
                datarow: row_line_with_ends,
                padding: 1,
                ..emptyformat
            },
            Self::Presto => {
                let row = DataRow::new(" ", " | ", " ");
                TableFormat {
                    linebelowheader: Some(Line::new("-", "-", "-+-", "-")),
                    headerrow: row.clone(),
                    datarow: row,
                    padding: 1,
                    ..emptyformat
                }
            }
            Self::FancyGithub => TableFormat {
                linebelowheader: Some(single_line_with_ends),
                headerrow: row_line_with_ends.clone(),
                datarow: row_line_with_ends,
                padding: 1,
                ..emptyformat
            },
            Self::FancyPresto => TableFormat {
                linebelowheader: Some(single_line),
                headerrow: row_line.clone(),
                datarow: row_line,
                padding: 1,
                ..emptyformat
            },
        }
    }
}

/// The column alignments
///
/// Numbers are only considered as non-text when align is `Decimal`.
#[derive(PartialEq)]
pub enum Align {
    /// Left aligned text
    Left,
    /// Centered text
    Center,
    /// Right aligned text
    Right,
    /// Numbers right aligned and aligned with their fractional dot
    Decimal,
}

#[derive(Clone)]
pub struct Line {
    pub begin: String,
    pub hline: String,
    pub sep: String,
    pub end: String,
}
impl Line {
    fn new(begin: &str, hline: &str, sep: &str, end: &str) -> Self {
        Self {
            begin: String::from(begin),
            hline: String::from(hline),
            sep: String::from(sep),
            end: String::from(end),
        }
    }

    #[cfg(feature = "ansi_term_style")]
    /// Apply style to line
    pub fn apply_style(&mut self, style: ansi_term::Style) {
        self.begin = paint(&self.begin, style);
        self.hline = paint(&self.hline, style);
        self.sep = paint(&self.sep, style);
        self.end = paint(&self.end, style);
    }
}

#[derive(Clone)]
pub struct DataRow {
    pub begin: String,
    pub sep: String,
    pub end: String,
}
impl DataRow {
    fn new(begin: &str, sep: &str, end: &str) -> Self {
        Self {
            begin: String::from(begin),
            sep: String::from(sep),
            end: String::from(end),
        }
    }

    #[cfg(feature = "ansi_term_style")]
    /// Apply style to datarow
    pub fn apply_style(&mut self, style: ansi_term::Style) {
        self.begin = paint(&self.begin, style);
        self.sep = paint(&self.sep, style);
        self.end = paint(&self.end, style);
    }
}

#[cfg(feature = "ansi_term_style")]
fn paint(s: &str, style: ansi_term::Style) -> String {
    style.paint(s).to_string()
}

// A table is structured like so:
//     --- lineabove ---------
//         headerrow
//     --- linebelowheader ---
//         datarow
//     --- linebetweenrows ---
//     ... (more datarows) ...
//     --- linebewteenrows ---
//         last datarow
//     --- linebelow ---------
#[derive(Clone)]
pub struct TableFormat {
    pub lineabove: Option<Line>,
    pub linebelowheader: Option<Line>,
    pub linebetweenrows: Option<Line>,
    pub linebelow: Option<Line>,
    pub headerrow: DataRow,
    pub datarow: DataRow,
    pub padding: u32,
    pub hidelineaboveifheader: bool,
    pub hidelinebelowifheader: bool,
}

#[cfg(feature = "ansi_term_style")]
impl TableFormat {
    /// Apply the style to all the Strings in the TableFormat
    pub fn apply_style(&mut self, style: ansi_term::Style) {
        if let Some(la) = self.lineabove.as_mut() {
            la.apply_style(style);
        }
        if let Some(lbh) = self.linebelowheader.as_mut() {
            lbh.apply_style(style);
        }
        if let Some(lbr) = self.linebetweenrows.as_mut() {
            lbr.apply_style(style);
        }
        if let Some(lb) = self.linebelow.as_mut() {
            lb.apply_style(style);
        }
        self.headerrow.apply_style(style);
        self.datarow.apply_style(style);
    }
}