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

use super::{parse_to_config_file, ConfigStructure, Flattenable};
use crate::sort;

use crate::CONFIG_FILE;

const fn default_true() -> bool {
    true
}
const fn default_scroll_offset() -> usize {
    6
}
const fn default_max_preview_size() -> u64 {
    2 * 1024 * 1024 // 2 MB
}
const fn default_column_ratio() -> (usize, usize, usize) {
    (1, 3, 4)
}

#[derive(Clone, Debug, Deserialize)]
struct SortRawOption {
    #[serde(default)]
    show_hidden: bool,
    #[serde(default = "default_true")]
    directories_first: bool,
    #[serde(default)]
    case_sensitive: bool,
    #[serde(default)]
    reverse: bool,
}

impl SortRawOption {
    pub fn into_sort_option(self, sort_method: sort::SortType) -> sort::SortOption {
        sort::SortOption {
            show_hidden: self.show_hidden,
            directories_first: self.directories_first,
            case_sensitive: self.case_sensitive,
            reverse: self.reverse,
            sort_method,
        }
    }
}

impl std::default::Default for SortRawOption {
    fn default() -> Self {
        Self {
            show_hidden: bool::default(),
            directories_first: default_true(),
            case_sensitive: bool::default(),
            reverse: bool::default(),
        }
    }
}

#[derive(Clone, Debug, Deserialize)]
pub struct JoshutoRawConfig {
    #[serde(default = "default_scroll_offset")]
    scroll_offset: usize,
    #[serde(default = "default_true")]
    tilde_in_titlebar: bool,
    #[serde(default = "default_true")]
    show_preview: bool,
    #[serde(default)]
    xdg_open: bool,
    #[serde(default = "default_max_preview_size")]
    max_preview_size: u64,
    column_ratio: Option<[usize; 3]>,
    sort_method: Option<String>,
    #[serde(default)]
    sort_option: SortRawOption,
}

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

        let sort_method = match self.sort_method {
            Some(s) => match sort::SortType::parse(s.as_str()) {
                Some(s) => s,
                None => sort::SortType::Natural,
            },
            None => sort::SortType::Natural,
        };
        let sort_option = self.sort_option.into_sort_option(sort_method);

        JoshutoConfig {
            scroll_offset: self.scroll_offset,
            tilde_in_titlebar: self.tilde_in_titlebar,
            show_preview: self.show_preview,
            xdg_open: self.xdg_open,
            max_preview_size: self.max_preview_size,
            column_ratio,
            sort_option,
        }
    }
}

#[derive(Debug, Clone)]
pub struct JoshutoConfig {
    pub scroll_offset: usize,
    pub tilde_in_titlebar: bool,
    pub show_preview: bool,
    pub xdg_open: bool,
    pub max_preview_size: u64,
    pub sort_option: sort::SortOption,
    pub column_ratio: (usize, usize, usize),
}

impl ConfigStructure for JoshutoConfig {
    fn get_config() -> Self {
        parse_to_config_file::<JoshutoRawConfig, JoshutoConfig>(CONFIG_FILE)
            .unwrap_or_else(Self::default)
    }
}

impl std::default::Default for JoshutoConfig {
    fn default() -> Self {
        let sort_option = sort::SortOption::default();

        Self {
            scroll_offset: default_scroll_offset(),
            tilde_in_titlebar: default_true(),
            show_preview: default_true(),
            xdg_open: false,
            max_preview_size: default_max_preview_size(),
            sort_option,
            column_ratio: default_column_ratio(),
        }
    }
}