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
use colored::Color;

#[derive(Debug, Clone)]
pub enum CellWidth {
    // The cell width is fixed
    Fixed(usize),
    // The cell is always at least a minimum width
    Minimum(usize),
    // The cell takes on the width of its content
    Content,
}

impl CellWidth {
    pub fn default() -> CellWidth {
        CellWidth::Content
    }
}

/// Describes how content should be aligned.
#[derive(Debug, Clone)]
#[derive(PartialEq)]
pub enum Alignment {
    Left,
    Center,
    Right
}

impl Alignment {
    fn from_token(token: char) -> Option<Alignment> {
        match token {
            '<' => Some(Alignment::Left),
            '^' => Some(Alignment::Center),
            '>' => Some(Alignment::Right),
            _ => None
        }
    }
}

/// Describes whether content will wrap or truncate.
#[derive(Debug, Clone)]
pub enum Wrap {
    /// Content will be truncated when over-width
    Truncate,
    // Content will wrap when over-width
    Wrap
}

impl Wrap {
    fn from_token(token: char) -> Option<Wrap> {
        match token {
            ';' => Some(Wrap::Wrap),
            '.' => Some(Wrap::Truncate),
            _ => None
        }
    }
}

#[allow(unused_macros)]
#[macro_export]
macro_rules! content_style {
    ( $style:literal ) => {
        ContentStyle::from_format($style)
    }
}

/// Represents the style to apply to a line of content.
#[derive(Debug, Clone)]
pub struct ContentStyle {
    pub foreground_color: Option<Color>,
    pub background_color: Option<Color>,
    pub alignment: Alignment,
    pub wrap: Wrap,
    pub width: CellWidth
}

impl ContentStyle {
    #[must_use]
    pub fn default() -> ContentStyle {
        ContentStyle {
            foreground_color: None,
            background_color: None,
            alignment: Alignment::Left,
            wrap: Wrap::Truncate,
            width: CellWidth::Content,
        }
    }

    #[must_use]
    pub fn new(
        foreground_color: Option<Color>,
        background_color: Option<Color>,
        alignment: Alignment,
        wrap: Wrap,
        width: CellWidth
    ) -> ContentStyle {
        ContentStyle {
            foreground_color,
            background_color,
            alignment,
            wrap,
            width,
        }
    }

    /// Returns a `ContentStyle` from a format string.
    ///
    /// # Arguments
    ///
    /// * `format` - The format string to parse.
    ///
    /// # Panics
    ///
    /// If width specifiers are not well formatted.
    #[must_use]
    pub fn from_format(format: &str) -> ContentStyle {
        // Start with defaults
        let mut style = ContentStyle::default();

        // Iterate tokens
        let tokens: Vec<char> = format[1..format.len() - 1].chars().collect();
        let mut token_ix = 0;
        while token_ix < tokens.len() {
            let token = tokens[token_ix];

            // Foreground color
            if let Some(color) = ContentStyle::color_from_token(token) {
                style.foreground_color = Some(color)
            }
            // Alignment
            if let Some(alignment) = Alignment::from_token(token) {
                style.alignment = alignment
            }
            // Wrap
            if let Some(wrap) = Wrap::from_token(token) {
                style.wrap = wrap
            }

            // Background color (consumes two tokens)
            if token == '-' {
                // Avoid problem if - is last token (with no color code)
                if tokens.len() > token_ix + 1 {
                    style.background_color = 
                        ContentStyle::color_from_token(tokens[token_ix + 1]);
                    // Consume next token (to skip the background color code)
                    token_ix += 1;
                }
            }
            token_ix += 1;

            // Width specifier (consumes until matching token)
            if token == ':' {
                // TODO: Clean up this logic (should be a common width fn)
                if let Some(ix) = format[token_ix+1..=tokens.len()].find(':') {
                    let width = format[token_ix+1..=token_ix+ix].parse::<usize>().unwrap();
                    style.width = CellWidth::Fixed(width);
                    token_ix += ix + 1;
                }
            }

            // Width specifier (consumes until matching token)
            if token == '|' {
                // TODO: Clean up this logic (should be a common width fn)

                if let Some(ix) = format[token_ix+1..=tokens.len()].find('|') {
                        let width = format[token_ix+1..=token_ix+ix].parse::<usize>().unwrap();
                        style.width = CellWidth::Minimum(width);
                        token_ix += ix + 1;
                }
            }
        }

        style
    }

    fn color_from_token(
        token: char
    ) -> Option<Color> {
        match token {
            'w' => Some(Color::White),
            'l' => Some(Color::Black),
            'r' => Some(Color::Red),
            'g' => Some(Color::Green),
            'y' => Some(Color::Yellow),
            'b' => Some(Color::Blue),
            'm' => Some(Color::Magenta),
            'c' => Some(Color::Cyan),
            'W' => Some(Color::BrightWhite),
            'L' => Some(Color::BrightBlack),
            'R' => Some(Color::BrightRed),
            'G' => Some(Color::BrightGreen),
            'Y' => Some(Color::BrightYellow),
            'B' => Some(Color::BrightBlue),
            'M' => Some(Color::BrightMagenta),
            'C' => Some(Color::BrightCyan),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use colored::Color;

   #[test]
   fn from_format_fixed_width() {
        let style = ContentStyle::from_format("{c^;:15:}");
   
        let expected = 
            ContentStyle {
                foreground_color: Some(Color::Cyan),
                background_color: None,
                alignment: Alignment::Center,
                wrap: Wrap::Wrap,
                width: CellWidth::Fixed(15)
            };

        assert_eq!(
            format!("{:?}", style),
            format!("{:?}", expected)
        );
    }

}