aboutsummaryrefslogtreecommitdiffstats
path: root/src/render.rs
blob: 8a116a0d2087a4de90dad4f51c83459857eb6ca5 (plain) (blame)
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
use std::{collections::BTreeMap, sync::Arc};

use ansi_term::{Colour, Colour::Fixed, Colour::RGB, Style};
use zellij_tile::prelude::bail;

use crate::{widgets::widget::Widget, ZellijState};

#[derive(Clone, Debug, PartialEq)]
pub struct FormattedPart {
    pub fg: Option<Colour>,
    pub bg: Option<Colour>,
    pub bold: bool,
    pub italic: bool,
    pub content: String,
}

impl FormattedPart {
    pub fn from_format_string(format: String) -> Self {
        let mut format = format;
        if format.starts_with("#[") {
            format = format.strip_prefix("#[").unwrap().to_string();
        }
        let mut result = FormattedPart::default();

        let format_content_split = format.split(']');

        if format_content_split.clone().count() == 1 {
            result.content = format;

            return result;
        }

        let format_content_split = format_content_split.collect::<Vec<&str>>();
        result.content = format_content_split[1].to_string();

        let parts = format_content_split[0].split(',');
        for part in parts {
            if part.starts_with("fg=") {
                result.fg = parse_color(part.strip_prefix("fg=").unwrap());
            }

            if part.starts_with("bg=") {
                result.bg = parse_color(part.strip_prefix("bg=").unwrap());
            }

            if part.eq("bold") {
                result.bold = true;
            }

            if part.eq("italic") {
                result.italic = true;
            }
        }

        result
    }

    pub fn format_string(&self, text: String) -> String {
        let mut style = match self.fg {
            Some(color) => Style::new().fg(color),
            None => Style::new(),
        };

        style.background = self.bg;
        style.is_italic = self.italic;
        style.is_bold = self.bold;

        let style = style.paint(text);

        format!("{}", style)
    }

    pub fn format_string_with_widgets(
        &self,
        widgets: BTreeMap<String, Arc<dyn Widget>>,
        state: ZellijState,
    ) -> String {
        let mut output = self.content.clone();

        for key in widgets.keys() {
            let token = format!("{{{key}}}");
            if !output.contains(token.as_str()) {
                continue;
            }

            let result = match widgets.get(key) {
                Some(widget) => widget.process(state.clone()),
                None => "Use of uninitialized widget".to_string(),
            };

            output = output.replace(token.as_str(), &result);
        }

        self.format_string(output)
    }
}

impl Default for FormattedPart {
    fn default() -> Self {
        Self {
            fg: None,
            bg: None,
            bold: false,
            italic: false,
            content: "".to_string(),
        }
    }
}

fn hex_to_rgb(s: &str) -> anyhow::Result<Vec<u8>> {
    if s.len() != 6 {
        bail!("wrong hex color length");
    }

    (0..s.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(&s[i..i + 2], 16).map_err(|e| anyhow::Error::from(e)))
        .collect()
}

fn parse_color(color: &str) -> Option<Colour> {
    if color.starts_with('#') {
        let rgb = hex_to_rgb(color.strip_prefix('#').unwrap());
        if rgb.is_err() {
            return None;
        }

        let rgb = rgb.unwrap();
        if rgb.len() != 3 {
            return None;
        }

        return Some(RGB(
            *rgb.first().unwrap(),
            *rgb.get(1).unwrap(),
            *rgb.get(2).unwrap(),
        ));
    }

    if let Ok(result) = color.parse::<u8>() {
        return Some(Fixed(result));
    }

    None
}

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

    #[test]
    fn test_hex_to_rgb() {
        let result = hex_to_rgb("010203");
        let expected = Vec::from([1, 2, 3]);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), expected);
    }

    #[test]
    fn test_hex_to_rgb_with_invalid_input() {
        let result = hex_to_rgb("#010203");
        assert!(result.is_err());

        let result = hex_to_rgb(" 010203");
        assert!(result.is_err());

        let result = hex_to_rgb("010");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_color() {
        let result = parse_color("#010203");
        let expected = Colour::RGB(1, 2, 3);
        assert_eq!(result, Some(expected));

        let result = parse_color("255");
        let expected = Colour::Fixed(255);
        assert_eq!(result, Some(expected));

        let result = parse_color("365");
        assert_eq!(result, None);

        let result = parse_color("#365");
        assert_eq!(result, None);
    }

}