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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! To see how color numbers map to actual colors in your terminal run
//! `cargo run --example colors`. Use tab to swap fg/bg colors.
use serde::Deserialize;
use std::env::home_dir;
use std::path::PathBuf;

#[derive(Debug, Clone, Deserialize)]
pub struct Server {
    /// Address of the server
    pub addr: String,

    /// Port of the server
    pub port: u16,

    /// Hostname to be used in connection registration
    pub hostname: String,

    /// Real name to be used in connection registration
    pub realname: String,

    /// Nicks to try when connecting to this server. tiny tries these sequentially, and starts
    /// adding trailing underscores to the last one if none of the nicks are available.
    pub nicks: Vec<String>,

    /// Commands to automatically run after joining to the server. Useful for e.g. registering the
    /// nick via nickserv or joining channels. Uses tiny command syntax.
    pub auto_cmds: Vec<String>,
}

/// Similar to `Server`, but used when connecting via the `/connect` command.
#[derive(Debug, Clone, Deserialize)]
pub struct Defaults {
    pub nicks: Vec<String>,
    pub hostname: String,
    pub realname: String,
    pub auto_cmds: Vec<String>
}

#[derive(Debug, Deserialize)]
pub struct Config {
    pub servers: Vec<Server>,
    pub defaults: Defaults,
    #[serde(default)]
    pub colors: Colors,
    pub log_dir: String,
}

pub fn get_config_path() -> PathBuf {
    let mut config_path = home_dir().unwrap();
    config_path.push(".tinyrc.yml");
    config_path
}

pub fn get_default_config_yaml() -> String {
    let mut log_dir = home_dir().unwrap();
    log_dir.push("tiny_logs");
    format!("\
# Servers to auto-connect
servers:
    - addr: irc.mozilla.org
      port: 6667
      hostname: yourhost
      realname: yourname
      nicks: [tiny_user]
      auto_cmds:
          - 'msg NickServ identify hunter2'
          - 'join #tiny'

# Defaults used when connecting to servers via the /connect command
defaults:
    nicks: [tiny_user]
    hostname: yourhost
    realname: yourname
    auto_cmds: []

# Where to put log files
log_dir: '{}'

# Color theme based on 256 colors (if supported). Colors can be defined as color
# indices (0-255) or with their names.
#
# Accepted color names are:
# default (0), black (0), maroon (1), green (2), olive (3), navy (4),
# purple (5), teal (6), silver (7), gray (8), red (9), lime (10),
# yellow (11), blue (12), magenta (13), cyan (14), white (15)
#
# Attributes can be combined (e.g [bold, underline]), and valid values are bold
# and underline
colors:
    nick: [ 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14 ]

    clear:
        fg: default
        bg: default

    user_msg:
        fg: black
        bg: default

    err_msg:
        fg: black
        bg: maroon
        attrs: [bold]

    topic:
        fg: cyan
        bg: default
        attrs: [bold]

    cursor:
        fg: black
        bg: default

    join:
        fg: lime
        bg: default
        attrs: [bold]

    part:
        fg: maroon
        bg: default
        attrs: [bold]

    nick_change:
        fg: lime
        bg: default
        attrs: [bold]

    faded:
        fg: 242
        bg: default

    exit_dialogue:
        fg: default
        bg: navy

    highlight:
        fg: red
        bg: default
        attrs: [bold]

    completion:
        fg: 84
        bg: default

    timestamp:
        fg: 242
        bg: default

    tab_active:
        fg: default
        bg: default
        attrs: [bold]

    tab_normal:
        fg: gray
        bg: default

    tab_new_msg:
        fg: purple
        bg: default

    tab_highlight:
        fg: red
        bg: default
        attrs: [bold]\n", log_dir.as_path().to_str().unwrap())
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Colors
////////////////////////////////////////////////////////////////////////////////////////////////////

pub use termbox_simple::*;
use serde::de::{self, Deserializer, Visitor, MapAccess};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Style {
    /// Termbox fg.
    pub fg: u16,

    /// Termbox bg.
    pub bg: u16,
}


#[derive(Debug, Deserialize)]
#[serde(default)]
pub struct Colors {
    pub nick: Vec<u8>,
    pub clear: Style,
    pub user_msg: Style,
    pub err_msg: Style,
    pub topic: Style,
    pub cursor: Style,
    pub join: Style,
    pub part: Style,
    pub nick_change: Style,
    pub faded: Style,
    pub exit_dialogue: Style,
    pub highlight: Style,
    pub completion: Style,
    pub timestamp: Style,
    pub tab_active: Style,
    pub tab_normal: Style,
    pub tab_new_msg: Style,
    pub tab_highlight: Style,
}

impl Default for Colors {
    fn default() -> Self {
        Colors {
            nick: vec![ 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14 ],
            clear: Style { fg: TB_DEFAULT, bg: TB_DEFAULT },
            user_msg: Style { fg: 0, bg: TB_DEFAULT },
            err_msg: Style { fg: 0 | TB_BOLD, bg: 1 },
            topic: Style { fg: 14 | TB_BOLD, bg: TB_DEFAULT },
            cursor: Style { fg: 0, bg: TB_DEFAULT },
            join: Style { fg: 10 | TB_BOLD, bg: TB_DEFAULT },
            part: Style { fg: 1 | TB_BOLD, bg: TB_DEFAULT },
            nick_change: Style { fg: 10 | TB_BOLD, bg: TB_DEFAULT },
            faded: Style { fg: 242, bg: TB_DEFAULT },
            exit_dialogue: Style { fg: TB_DEFAULT, bg: 4 },
            highlight: Style { fg: 9 | TB_BOLD, bg: TB_DEFAULT },
            completion: Style { fg: 84, bg: TB_DEFAULT },
            timestamp: Style { fg: 242, bg: TB_DEFAULT },
            tab_active: Style { fg: 0 | TB_BOLD, bg: 0 },
            tab_normal: Style { fg: 8, bg: 0 },
            tab_new_msg: Style { fg: 5, bg: 0 },
            tab_highlight: Style { fg: 9 | TB_BOLD, bg: 0 },
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Color parsing
////////////////////////////////////////////////////////////////////////////////////////////////////

// Color names are taken from https://en.wikipedia.org/wiki/List_of_software_palettes
const COLORS: [(&'static str, u16); 17] =
[
    ("default", TB_DEFAULT), // Default fg/bg color of the terminal
    ("black",   0),
    ("maroon",  1),
    ("green",   2),
    ("olive",   3),
    ("navy",    4),
    ("purple",  5),
    ("teal",    6),
    ("silver",  7),
    ("gray",    8),
    ("red",     9),
    ("lime",    10),
    ("yellow",  11),
    ("blue",    12),
    ("magenta", 13),
    ("cyan",    14),
    ("white",   15)
];

const ATTRS: [(&'static str, u16); 2] =
[
    ("bold",      TB_BOLD),
    ("underline", TB_UNDERLINE)
];

fn parse_color(val: String) -> Option<u16> {
    for &(name, color) in &COLORS {
        if val == name {
            return Some(color);
        }
    }

    // If color name doesn't match try get a number
    val.parse().ok()
}

fn parse_attr(val: String) -> u16 {
    for &(name, attr) in &ATTRS {
        if name == val {
            return attr;
        }
    }

    return 0;
}

impl<'de> Deserialize<'de> for Style {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "lowercase")]
        enum Field { Fg, Bg, Attrs }

        use std::fmt;

        struct StyleVisitor;
        impl<'de> Visitor<'de> for StyleVisitor {
            type Value = Style;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                let colors = COLORS.iter().map(|&(name, _)| name).collect::<Vec<&str>>().join(", ");
                let attrs = ATTRS.iter().map(|&(name, _)| name).collect::<Vec<&str>>().join(", ");

                writeln!(formatter,
                         "fg: 0-255 or color name\n\
                         bg: 0-255 or color name\n\
                         attrs: [{}]\n\n\
                         color names: {}", attrs, colors)
            }

            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
                where M: MapAccess<'de>
            {
                let mut fg: Option<u16> = None;
                let mut bg: Option<u16> = None;
                let mut attr: u16 = 0;

                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Fg => {
                             let color = parse_color(map.next_value()?)
                                 .ok_or_else(|| de::Error::invalid_value(de::Unexpected::UnitVariant, &self))?;

                             fg = Some(color);
                        },

                        Field::Bg => {
                            let color = parse_color(map.next_value()?)
                                .ok_or_else(|| de::Error::invalid_value(de::Unexpected::UnitVariant, &self))?;

                            bg = Some(color);
                        },

                        Field::Attrs => {
                            let attrs: Vec<String> = map.next_value()?;
                            attr = attrs.into_iter()
                                .map(parse_attr)
                                .fold(0, |style, a| style | a);
                        }
                    }
                }

                let fg = fg.ok_or_else(|| de::Error::missing_field("fg"))?;
                let bg = bg.ok_or_else(|| de::Error::missing_field("bg"))?;

                Ok(Style { fg: fg | attr, bg })
            }
        }

        deserializer.deserialize_map(StyleVisitor)
    }
}

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

    #[test]
    fn parse_default_config() {
        match serde_yaml::from_str(&get_default_config_yaml()) {
            Err(yaml_err) => {
                println!("{}", yaml_err);
                assert!(false);
            }
            Ok(Config { .. }) => {}
        }
    }

    #[test]
    fn parse_pre_color_config() {
        // Parse config file without a theme field. Important to be able to
        // parse old config files.
        let config = "\
# Servers to auto-connect
servers:
    - addr: irc.mozilla.org
      port: 6667
      hostname: yourhost
      realname: yourname
      nicks: [tiny_user]
      auto_cmds:
          - 'msg NickServ identify hunter2'
          - 'join #tiny'

# Defaults used when connecting to servers via the /connect command
defaults:
    nicks: [tiny_user]
    hostname: yourhost
    realname: yourname
    auto_cmds: []

# Where to put log files
log_dir: path";
        match serde_yaml::from_str(config) {
            Err(yaml_err) => {
                println!("{}", yaml_err);
                assert!(false);
            }
            Ok(Config { .. }) => {}
        }
    }
}