summaryrefslogtreecommitdiffstats
path: root/src/config/theme.rs
blob: 0c43ed8f35d61cba2491454128fd00d1ba896b38 (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
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
extern crate toml;
extern crate xdg;

use std::collections::HashMap;

use config::{parse_config_file, Flattenable};

#[derive(Debug, Deserialize, Clone)]
pub struct JoshutoColorPair {
    pub id: i16,
    pub fg: i16,
    pub bg: i16,
}

impl JoshutoColorPair {
    pub fn new(id: i16, fg: i16, bg: i16) -> Self {
        JoshutoColorPair { id, fg, bg }
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct JoshutoRawColorTheme {
    pub colorpair: i16,
    pub bold: Option<bool>,
    pub underline: Option<bool>,
    pub prefix: Option<String>,
    pub prefixsize: Option<usize>,
}

impl JoshutoRawColorTheme {
    pub fn flatten(self) -> JoshutoColorTheme {
        JoshutoColorTheme {
            colorpair: self.colorpair,
            bold: self.bold.unwrap_or(false),
            underline: self.underline.unwrap_or(false),
            prefix: self.prefix,
            prefixsize: self.prefixsize,
        }
    }
}

#[derive(Debug, Deserialize)]
pub struct JoshutoRawTheme {
    colorpair: Option<Vec<JoshutoColorPair>>,
    selection: Option<JoshutoRawColorTheme>,
    executable: Option<JoshutoRawColorTheme>,
    regular: Option<JoshutoRawColorTheme>,
    directory: Option<JoshutoRawColorTheme>,
    link: Option<JoshutoRawColorTheme>,
    socket: Option<JoshutoRawColorTheme>,
    ext: Option<HashMap<String, JoshutoRawColorTheme>>,
}

impl Flattenable<JoshutoTheme> for JoshutoRawTheme {
    fn flatten(self) -> JoshutoTheme {
        let colorpair = match self.colorpair {
            Some(s) => s,
            None => {
                let mut colorpair: Vec<JoshutoColorPair> = Vec::with_capacity(10);
                colorpair.push(JoshutoColorPair::new(2, 2, -1));
                colorpair.push(JoshutoColorPair::new(3, 3, -1));
                colorpair.push(JoshutoColorPair::new(4, 4, -1));
                colorpair.push(JoshutoColorPair::new(5, 5, -1));
                colorpair.push(JoshutoColorPair::new(6, 6, -1));
                colorpair
            }
        };

        let selection = self.selection.unwrap_or(JoshutoRawColorTheme {
            colorpair: 3,
            bold: Some(true),
            underline: None,
            prefix: None,
            prefixsize: None,
        });

        let executable = self.executable.unwrap_or(JoshutoRawColorTheme {
            colorpair: 2,
            bold: Some(true),
            underline: None,
            prefix: None,
            prefixsize: None,
        });

        let regular = self.regular.unwrap_or(JoshutoRawColorTheme {
            colorpair: 0,
            bold: None,
            underline: None,
            prefix: None,
            prefixsize: None,
        });

        let directory = self.directory.unwrap_or(JoshutoRawColorTheme {
            colorpair: 4,
            bold: Some(true),
            underline: None,
            prefix: None,
            prefixsize: None,
        });

        let link = self.link.unwrap_or(JoshutoRawColorTheme {
            colorpair: 6,
            bold: Some(true),
            underline: None,
            prefix: None,
            prefixsize: None,
        });

        let socket = self.socket.unwrap_or(JoshutoRawColorTheme {
            colorpair: 6,
            bold: Some(true),
            underline: None,
            prefix: None,
            prefixsize: None,
        });

        let mut extraw = self.ext.unwrap_or_default();
        let mut ext: HashMap<String, JoshutoColorTheme> = HashMap::new();
        for (k, v) in extraw.drain() {
            ext.insert(k, v.flatten());
        }

        JoshutoTheme {
            colorpair,
            regular: regular.flatten(),
            directory: directory.flatten(),
            selection: selection.flatten(),
            executable: executable.flatten(),
            link: link.flatten(),
            socket: socket.flatten(),
            ext,
        }
    }
}

#[derive(Debug, Clone)]
pub struct JoshutoColorTheme {
    pub colorpair: i16,
    pub bold: bool,
    pub underline: bool,
    pub prefix: Option<String>,
    pub prefixsize: Option<usize>,
}

#[derive(Debug, Clone)]
pub struct JoshutoTheme {
    pub colorpair: Vec<JoshutoColorPair>,
    pub regular: JoshutoColorTheme,
    pub selection: JoshutoColorTheme,
    pub directory: JoshutoColorTheme,
    pub executable: JoshutoColorTheme,
    pub link: JoshutoColorTheme,
    pub socket: JoshutoColorTheme,
    pub ext: HashMap<String, JoshutoColorTheme>,
}

impl JoshutoTheme {
    pub fn new() -> Self {
        let mut colorpair: Vec<JoshutoColorPair> = Vec::with_capacity(10);
        colorpair.push(JoshutoColorPair::new(2, 2, -1));
        colorpair.push(JoshutoColorPair::new(3, 3, -1));
        colorpair.push(JoshutoColorPair::new(4, 4, -1));
        colorpair.push(JoshutoColorPair::new(5, 5, -1));
        colorpair.push(JoshutoColorPair::new(6, 6, -1));

        let selection = JoshutoColorTheme {
            colorpair: 3,
            bold: true,
            underline: false,
            prefix: None,
            prefixsize: None,
        };

        let executable = JoshutoColorTheme {
            colorpair: 2,
            bold: true,
            underline: false,
            prefix: None,
            prefixsize: None,
        };

        let regular = JoshutoColorTheme {
            colorpair: 0,
            bold: false,
            underline: false,
            prefix: None,
            prefixsize: None,
        };

        let directory = JoshutoColorTheme {
            colorpair: 4,
            bold: true,
            underline: false,
            prefix: None,
            prefixsize: None,
        };

        let link = JoshutoColorTheme {
            colorpair: 6,
            bold: true,
            underline: false,
            prefix: None,
            prefixsize: None,
        };

        let socket = JoshutoColorTheme {
            colorpair: 6,
            bold: true,
            underline: false,
            prefix: None,
            prefixsize: None,
        };

        JoshutoTheme {
            colorpair,
            selection,
            executable,
            regular,
            directory,
            link,
            socket,
            ext: HashMap::new(),
        }
    }

    pub fn get_config() -> JoshutoTheme {
        parse_config_file::<JoshutoRawTheme, JoshutoTheme>(::THEME_FILE)
            .unwrap_or_else(JoshutoTheme::new)
    }
}