summaryrefslogtreecommitdiffstats
path: root/src/options.rs
blob: 1262df56560f78b361a00b60b9107c66f1aceb18 (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
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
use serde::Deserialize;

use crate::{
	app::{self, App, data_harvester},
	constants::*,
	utils::error::{self, BottomError},
};

#[derive(Default, Deserialize)]
pub struct Config {
    pub flags: Option<ConfigFlags>,
    pub colors: Option<ConfigColours>,
}

#[derive(Default, Deserialize)]
pub struct ConfigFlags {
    pub avg_cpu: Option<bool>,
    pub dot_marker: Option<bool>,
    pub temperature_type: Option<String>,
    pub rate: Option<u64>,
    pub left_legend: Option<bool>,
    pub current_usage: Option<bool>,
    pub group_processes: Option<bool>,
    pub case_sensitive: Option<bool>,
    pub whole_word: Option<bool>,
    pub regex: Option<bool>,
    pub default_widget: Option<String>,
    pub show_disabled_data: Option<bool>,
    pub basic_mode: Option<bool>,
    //disabled_cpu_cores: Option<Vec<u64>>, // TODO: [FEATURE] Enable disabling cores in config/flags
}

#[derive(Default, Deserialize)]
pub struct ConfigColours {
    pub table_header_color: Option<String>,
    pub avg_cpu_color: Option<String>,
    pub cpu_core_colors: Option<Vec<String>>,
    pub ram_color: Option<String>,
    pub swap_color: Option<String>,
    pub rx_color: Option<String>,
    pub tx_color: Option<String>,
    pub rx_total_color: Option<String>,
    pub tx_total_color: Option<String>,
    pub border_color: Option<String>,
    pub highlighted_border_color: Option<String>,
    pub text_color: Option<String>,
    pub selected_text_color: Option<String>,
    pub selected_bg_color: Option<String>,
    pub widget_title_color: Option<String>,
    pub graph_color: Option<String>,
}

pub fn get_update_rate_in_milliseconds(
    update_rate: &Option<&str>, config: &Config,
) -> error::Result<u128> {
    let update_rate_in_milliseconds = if let Some(update_rate) = update_rate {
        update_rate.parse::<u128>()?
    } else if let Some(flags) = &config.flags {
        if let Some(rate) = flags.rate {
            rate as u128
        } else {
            DEFAULT_REFRESH_RATE_IN_MILLISECONDS
        }
    } else {
        DEFAULT_REFRESH_RATE_IN_MILLISECONDS
    };

    if update_rate_in_milliseconds < 250 {
        return Err(BottomError::InvalidArg(
            "Please set your update rate to be greater than 250 milliseconds.".to_string(),
        ));
    } else if update_rate_in_milliseconds > u128::from(std::u64::MAX) {
        return Err(BottomError::InvalidArg(
            "Please set your update rate to be less than unsigned INT_MAX.".to_string(),
        ));
    }

    Ok(update_rate_in_milliseconds)
}

pub fn get_temperature_option(
    matches: &clap::ArgMatches<'static>, config: &Config,
) -> error::Result<data_harvester::temperature::TemperatureType> {
    if matches.is_present("FAHRENHEIT") {
        return Ok(data_harvester::temperature::TemperatureType::Fahrenheit);
    } else if matches.is_present("KELVIN") {
        return Ok(data_harvester::temperature::TemperatureType::Kelvin);
    } else if matches.is_present("CELSIUS") {
        return Ok(data_harvester::temperature::TemperatureType::Celsius);
    } else if let Some(flags) = &config.flags {
        if let Some(temp_type) = &flags.temperature_type {
            // Give lowest priority to config.
            return match temp_type.as_str() {
                "fahrenheit" | "f" => {
                    Ok(data_harvester::temperature::TemperatureType::Fahrenheit)
                }
                "kelvin" | "k" => {
                    Ok(data_harvester::temperature::TemperatureType::Kelvin)
                }
                "celsius" | "c" => {
                    Ok(data_harvester::temperature::TemperatureType::Celsius)
                }
                _ => {
                    Err(BottomError::ConfigError(
                        "Invalid temperature type.  Please have the value be of the form <kelvin|k|celsius|c|fahrenheit|f>".to_string()
                    ))
                }
            };
        }
    }
    Ok(data_harvester::temperature::TemperatureType::Celsius)
}

pub fn get_avg_cpu_option(matches: &clap::ArgMatches<'static>, config: &Config) -> bool {
    if matches.is_present("AVG_CPU") {
        return true;
    } else if let Some(flags) = &config.flags {
        if let Some(avg_cpu) = flags.avg_cpu {
            return avg_cpu;
        }
    }

    false
}

pub fn get_use_dot_option(matches: &clap::ArgMatches<'static>, config: &Config) -> bool {
    if matches.is_present("DOT_MARKER") {
        return true;
    } else if let Some(flags) = &config.flags {
        if let Some(dot_marker) = flags.dot_marker {
            return dot_marker;
        }
    }
    false
}

pub fn get_use_left_legend_option(matches: &clap::ArgMatches<'static>, config: &Config) -> bool {
    if matches.is_present("LEFT_LEGEND") {
        return true;
    } else if let Some(flags) = &config.flags {
        if let Some(left_legend) = flags.left_legend {
            return left_legend;
        }
    }

    false
}

pub fn get_use_current_cpu_total_option(
    matches: &clap::ArgMatches<'static>, config: &Config,
) -> bool {
    if matches.is_present("USE_CURR_USAGE") {
        return true;
    } else if let Some(flags) = &config.flags {
        if let Some(current_usage) = flags.current_usage {
            return current_usage;
        }
    }

    false
}

pub fn get_show_disabled_data_option(matches: &clap::ArgMatches<'static>, config: &Config) -> bool {
    if matches.is_present("SHOW_DISABLED_DATA") {
        return true;
    } else if let Some(flags) = &config.flags {
        if let Some(show_disabled_data) = flags.show_disabled_data {
            return show_disabled_data;
        }
    }

    false
}

pub fn get_use_basic_mode_option(matches: &clap::ArgMatches<'static>, config: &Config) -> bool {
    if matches.is_present("BASIC_MODE") {
        return true;
    } else if let Some(flags) = &config.flags {
        if let Some(basic_mode) = flags.basic_mode {
            return basic_mode;
        }
    }

    false
}

pub fn enable_app_grouping(matches: &clap::ArgMatches<'static>, config: &Config, app: &mut App) {
    if matches.is_present("GROUP_PROCESSES") {
        app.toggle_grouping();
    } else if let Some(flags) = &config.flags {
        if let Some(grouping) = flags.group_processes {
            if grouping {
                app.toggle_grouping();
            }
        }
    }
}

pub fn enable_app_case_sensitive(
    matches: &clap::ArgMatches<'static>, config: &Config, app: &mut App,
) {
    if matches.is_present("CASE_SENSITIVE") {
        app.process_search_state.search_toggle_ignore_case();
    } else if let Some(flags) = &config.flags {
        if let Some(case_sensitive) = flags.case_sensitive {
            if case_sensitive {
                app.process_search_state.search_toggle_ignore_case();
            }
        }
    }
}

pub fn enable_app_match_whole_word(
    matches: &clap::ArgMatches<'static>, config: &Config, app: &mut App,
) {
    if matches.is_present("WHOLE_WORD") {
        app.process_search_state.search_toggle_whole_word();
    } else if let Some(flags) = &config.flags {
        if let Some(whole_word) = flags.whole_word {
            if whole_word {
                app.process_search_state.search_toggle_whole_word();
            }
        }
    }
}

pub fn enable_app_use_regex(matches: &clap::ArgMatches<'static>, config: &Config, app: &mut App) {
    if matches.is_present("REGEX_DEFAULT")