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

use config::{parse_config_file, Flattenable};
use sort;

use CONFIG_FILE;

#[derive(Clone, Debug, Deserialize)]
pub struct SortRawOption {
    pub show_hidden: Option<bool>,
    pub directories_first: Option<bool>,
    pub case_sensitive: Option<bool>,
    pub reverse: Option<bool>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct JoshutoRawConfig {
    scroll_offset: Option<usize>,
    tilde_in_titlebar: Option<bool>,
    sort_type: Option<String>,
    sort_option: Option<SortRawOption>,
    column_ratio: Option<[usize; 3]>,
}

impl JoshutoRawConfig {
    #[allow(dead_code)]
    pub fn new() -> Self {
        JoshutoRawConfig {
            scroll_offset: None,
            tilde_in_titlebar: None,
            sort_type: None,
            sort_option: None,
            column_ratio: None,
        }
    }
}

impl Flattenable<JoshutoConfig> for JoshutoRawConfig {
    fn flatten(self) -> JoshutoConfig {
        let column_ratio = match self.column_ratio {
            Some(s) => (s[0], s[1], s[2]),
            None => (1, 3, 4),
        };

        let scroll_offset: usize = self.scroll_offset.unwrap_or(6);
        let tilde_in_titlebar: bool = self.tilde_in_titlebar.unwrap_or(true);

        let show_hidden: bool;
        let case_sensitive: bool;
        let reverse: bool;
        let directories_first: bool;

        match self.sort_option {
            Some(s) => {
                show_hidden = s.show_hidden.unwrap_or(false);
                case_sensitive = s.case_sensitive.unwrap_or(false);
                reverse = s.reverse.unwrap_or(false);
                directories_first = s.directories_first.unwrap_or(true);
            }
            None => {
                show_hidden = false;
                case_sensitive = false;
                reverse = false;
                directories_first = true;
            }
        }

        let sort_option = sort::SortOption {
            show_hidden,
            directories_first,
            case_sensitive,
            reverse,
        };

        let sort_type: sort::SortType = match self.sort_type {
            Some(s) => match s.as_str() {
                "natural" => sort::SortType::SortNatural(sort_option),
                "mtime" => sort::SortType::SortMtime(sort_option),
                _ => sort::SortType::SortNatural(sort_option),
            },
            _ => sort::SortType::SortNatural(sort_option),
        };

        JoshutoConfig {
            scroll_offset,
            tilde_in_titlebar,
            sort_type,
            column_ratio,
        }
    }
}

#[derive(Debug, Clone)]
pub struct JoshutoConfig {
    pub scroll_offset: usize,
    pub tilde_in_titlebar: bool,
    pub sort_type: sort::SortType,
    pub column_ratio: (usize, usize, usize),
}

impl JoshutoConfig {
    pub fn new() -> Self {
        let sort_option = sort::SortOption {
            show_hidden: false,
            directories_first: true,
            case_sensitive: false,
            reverse: false,
        };
        let sort_type = sort::SortType::SortNatural(sort_option);

        JoshutoConfig {
            scroll_offset: 6,
            tilde_in_titlebar: true,
            sort_type,
            column_ratio: (1, 3, 4),
        }
    }

    pub fn get_config() -> JoshutoConfig {
        parse_config_file::<JoshutoRawConfig, JoshutoConfig>(CONFIG_FILE)
            .unwrap_or_else(JoshutoConfig::new)
    }
}