summaryrefslogtreecommitdiffstats
path: root/src/config/config.rs
blob: 2caa1b55d2e093623332065ba23d82a775476281 (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
use serde_derive::Deserialize;

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

use crate::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_method: 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_method: 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 sort_method: sort::SortType = match self.sort_method {
                Some(s) => match s.as_str() {
                    "mtime" => sort::SortType::SortMtime,
                    _ => sort::SortType::SortNatural,
                },
                _ => sort::SortType::SortNatural,
            };

        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,
                sort_method,
            };

        JoshutoConfig {
            scroll_offset,
            tilde_in_titlebar,
            column_ratio,
            sort_option
        }
    }
}

#[derive(Debug, Clone)]
pub struct JoshutoConfig {
    pub scroll_offset: usize,
    pub tilde_in_titlebar: bool,
    pub sort_option: sort::SortOption,
    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,
            sort_method: sort::SortType::SortNatural,
        };


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

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