| 1 | //! How to handle config files and arguments. |
| 2 | |
| 3 | // TODO: Break this apart or do something a bit smarter. |
| 4 | |
| 5 | pub mod args; |
| 6 | pub mod config; |
| 7 | mod error; |
| 8 | |
| 9 | use std::{ |
| 10 | convert::TryInto, |
| 11 | fs, |
| 12 | io::Write, |
| 13 | path::{Path, PathBuf}, |
| 14 | str::FromStr, |
| 15 | time::{Duration, Instant}, |
| 16 | }; |
| 17 | |
| 18 | use anyhow::{Context, Result}; |
| 19 | pub use config::Config; |
| 20 | use config::style::Styles; |
| 21 | use data::TemperatureType; |
| 22 | pub(crate) use error::{OptionError, OptionResult}; |
| 23 | use indexmap::IndexSet; |
| 24 | use regex::Regex; |
| 25 | use rustc_hash::{FxHashMap, FxHashSet}; |
| 26 | #[cfg(feature = "battery")] |
| 27 | use starship_battery::Manager; |
| 28 | |
| 29 | use self::{ |
| 30 | args::BottomArgs, |
| 31 | config::{IgnoreList, StringOrNum, layout::Row}, |
| 32 | }; |
| 33 | use crate::{ |
| 34 | app::{filter::Filter, layout_manager::*, *}, |
| 35 | canvas::components::time_series::LegendPosition, |
| 36 | components::time_series::TimeseriesConfig, |
| 37 | constants::*, |
| 38 | utils::data_units::DataUnit, |
| 39 | widgets::*, |
| 40 | }; |
| 41 | |
| 42 | /// Macro to check whether a flag is enabled, either as an arg or in the config file. |
| 43 | macro_rules! is_flag_enabled { |
| 44 | ($flag_name:ident, $arg:expr, $config:expr) => { |
| 45 | if $arg.$flag_name { |
| 46 | true |
| 47 | } else if let Some(flags) = &$config.flags { |
| 48 | flags.$flag_name.unwrap_or(false) |
| 49 | } else { |
| 50 | false |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | ($cmd_flag:literal, $cfg_flag:ident, $matches:expr, $config:expr) => { |
| 55 | if $matches.get_flag($cmd_flag) { |
| 56 | true |
| 57 | } else if let Some(flags) = &$config.flags { |
| 58 | flags.$cfg_flag.unwrap_or(false) |
| 59 | } else { |
| 60 | false |
| 61 | } |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | /// A version of [`is_flag_enabled`] which instead expects the user to pass |
| 66 | /// in `config_section`, which is the section the flag is located, rather than |
| 67 | /// defaulting to `config.flags` where `config` is passed in. |
| 68 | macro_rules! is_flag_enabled_in { |
| 69 | ($flag_name:ident, $arg:expr, $config_section:expr) => { |
| 70 | if $arg.$flag_name { |
| 71 | true |
| 72 | } else if let Some(options) = &$config_section { |
| 73 | options.$flag_name.unwrap_or(false) |
| 74 | } else { |
| 75 | false |
| 76 | } |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | /// A version of [`is_flag_enabled`] which should be used to deprecate old config flags. |
| 81 | macro_rules! enabled_option_with_deprecated { |
| 82 | ($arg:expr, $config:expr, $section:ident . $field:ident, $flags:ident . $deprecated_flag:ident $(,)?) => { |
| 83 | if $arg { |
| 84 | true |
| 85 | } else if let Some(section) = &$config.$section { |
| 86 | section.$field.unwrap_or(false) |
| 87 | } else if let Some(flags) = &$config.flags { |
| 88 | flags |
| 89 | .$deprecated_flag |
| 90 | .inspect(|_| { |
| 91 | deprecated_warning(stringify!($deprecated_flag), stringify!($section.$field)) |
| 92 | }) |
| 93 | .unwrap_or(false) |
| 94 | } else { |
| 95 | false |
| 96 | } |
| 97 | }; |
| 98 | ($arg:expr, $config:expr, $section:ident . $field:ident, $flags:ident . $deprecated_flag:ident, $alias:literal $(,)?) => { |
| 99 | if $arg { |
| 100 | true |
| 101 | } else if let Some(section) = &$config.$section { |
| 102 | section.$field.unwrap_or(false) |
| 103 | } else if let Some(flags) = &$config.flags { |
| 104 | flags |
| 105 | .$deprecated_flag |
| 106 | .inspect(|_| { |
| 107 | deprecated_warning_with_alias( |
| 108 | stringify!($deprecated_flag), |
| 109 | stringify!($section.$field), |
| 110 | $alias, |
| 111 | ) |
| 112 | }) |
| 113 | .unwrap_or(false) |
| 114 | } else { |
| 115 | false |
| 116 | } |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | /// Get the value of a field for a specific section in the config file if set. If not set, the default is used. |
| 121 | macro_rules! config_or_default { |
| 122 | ($config:expr, $section:ident . $field:ident) => { |
| 123 | $config |
| 124 | .$section |
| 125 | .as_ref() |
| 126 | .map(|s| s.$field) |
| 127 | .unwrap_or_default() |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | /// Get the value of a field for a specific section in the config file if set. If not set, |
| 132 | /// the provided default is used. |
| 133 | macro_rules! config_or { |
| 134 | ($config:expr, $section:ident . $field:ident, $default:expr) => { |
| 135 | $config |
| 136 | .$section |
| 137 | .as_ref() |
| 138 | .and_then(|s| s.$field) |
| 139 | .unwrap_or($default) |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | /// The default config file sub-path. |
| 144 | const DEFAULT_CONFIG_FILE_LOCATION: &str = "bottom/bottom.toml"; |
| 145 | |
| 146 | /// Returns the config path to use. If `override_config_path` is specified, then |
| 147 | /// we will use that. If not, then return the "default" config path, which is: |
| 148 | /// |
| 149 | /// - If a path already exists at `<HOME>/bottom/bottom.toml`, then use that for |
| 150 | /// legacy reasons. |
| 151 | /// - Otherwise, use `<SYSTEM_CONFIG_FOLDER>/bottom/bottom.toml`. |
| 152 | /// |
| 153 | /// For more details on this, see [dirs](https://docs.rs/dirs/latest/dirs/fn.config_dir.html)' |
| 154 | /// documentation. |
| 155 | /// |
| 156 | /// XXX: For macOS, we additionally will manually check `$XDG_CONFIG_HOME` as |
| 157 | /// well first before falling back to `dirs`. |
| 158 | fn get_config_path(override_config_path: Option<&Path>) -> Option<PathBuf> { |
| 159 | if let Some(conf_loc) = override_config_path { |
| 160 | return Some(conf_loc.to_path_buf()); |
| 161 | } else if let Some(home_path) = dirs::home_dir() { |
| 162 | let mut old_home_path = home_path; |
| 163 | old_home_path.push(".config/"); |
| 164 | old_home_path.push(DEFAULT_CONFIG_FILE_LOCATION); |
| 165 | if let Ok(res) = old_home_path.try_exists() |
| 166 | && res |
| 167 | { |
| 168 | // We used to create it at `<HOME>/DEFAULT_CONFIG_FILE_PATH`, but changed it |
| 169 | // to be more correct later. However, for legacy reasons, if it already exists, |
| 170 | // use the old one. |
| 171 | return Some(old_home_path); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | let config_path = dirs::config_dir().map(|mut path| { |
| 176 | path.push(DEFAULT_CONFIG_FILE_LOCATION); |
| 177 | path |
| 178 | }); |
| 179 | |
| 180 | if cfg!(target_os = "macos") |
| 181 | && let Ok(xdg_config_path) = std::env::var("XDG_CONFIG_HOME") |
| 182 | && !xdg_config_path.is_empty() |
| 183 | { |
| 184 | // If XDG_CONFIG_HOME exists and is non-empty, _but_ we previously used the |
| 185 | // Library-based path for a config and it exists, then use that |
| 186 | // instead for backwards-compatibility. |
| 187 | if let Some(old_macos_path) = &config_path |
| 188 | && let Ok(res) = old_macos_path.try_exists() |
| 189 | && res |
| 190 | { |
| 191 | return config_path; |
| 192 | } |
| 193 | |
| 194 | // Otherwise, try and use the XDG_CONFIG_HOME-based path. |
| 195 | let mut cfg_path = PathBuf::new(); |
| 196 | cfg_path.push(xdg_config_path); |
| 197 | cfg_path.push(DEFAULT_CONFIG_FILE_LOCATION); |
| 198 | |
| 199 | return Some(cfg_path); |
| 200 | } |
| 201 | |
| 202 | config_path |
| 203 | } |
| 204 | |
| 205 | fn create_config_at_path(path: &Path) -> anyhow::Result<Config> { |
| 206 | if let Some(parent_path) = path.parent() { |
| 207 | fs::create_dir_all(parent_path)?; |
| 208 | } |
| 209 | |
| 210 | let mut file = fs::File::create(path)?; |
| 211 | file.write_all(CONFIG_TEXT.as_bytes())?; |
| 212 | |
| 213 | Ok(Config::default()) |
| 214 | } |
| 215 | |
| 216 | /// Get the config at `config_path`. If there is no config file at the specified |
| 217 | /// path, it will try to create a new file with the default settings, and return |
| 218 | /// the default config. |
| 219 | /// |
| 220 | /// We're going to use the following behaviour on when we'll return an error |
| 221 | /// rather than just "silently" continuing on: |
| 222 | /// - If the user passed in a path explicitly, then we will be loud and error |
| 223 | /// out. |
| 224 | /// - If the user does NOT pass in a path explicitly, then just show a warning, |
| 225 | /// but continue. This is in case they do not want to write a default config |
| 226 | /// file at the XDG locations, for example. |
| 227 | pub(crate) fn get_or_create_config(config_path: Option<&Path>) -> anyhow::Result<Config> { |
| 228 | let adjusted_config_path = get_config_path(config_path); |
| 229 | |
| 230 | match &adjusted_config_path { |
| 231 | Some(path) => { |
| 232 | if let Ok(config_string) = fs::read_to_string(path) { |
| 233 | Ok(toml_edit::de::from_str(&config_string)?) |
| 234 | } else { |
| 235 | match create_config_at_path(path) { |
| 236 | Ok(cfg) => Ok(cfg), |
| 237 | Err(err) => { |
| 238 | let path = path.display(); |
| 239 | if config_path.is_some() { |
| 240 | Err(err.context(format!( |
| 241 | "bottom could not create a new config file at '{path}'.", |
| 242 | ))) |
| 243 | } else { |
| 244 | indoc::eprintdoc!( |
| 245 | "Note: bottom couldn't create a default config file at '{path}', and the \ |
| 246 | application has fallen back to the default configuration. |
| 247 | |
| 248 | Caused by: |
| 249 | {err} |
| 250 | ", |
| 251 | ); |
| 252 | |
| 253 | Ok(Config::default()) |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | None => { |
| 260 | // If we somehow don't have any config path, then just assume the default config |
| 261 | // but don't write to any file. |
| 262 | // |
| 263 | // TODO: For now, just print a message to stderr indicating this. In the future, |
| 264 | // probably show in-app (too). |
| 265 | |
| 266 | eprintln!( |
| 267 | "Note: bottom couldn't find a location to create or read a config file, so \ |
| 268 | the application has fallen back to the default configuration. \ |
| 269 | This could be for a variety of reasons, such as issues with file permissions." |
| 270 | ); |
| 271 | |
| 272 | Ok(Config::default()) |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /// Initialize the app. |
| 278 | pub(crate) fn init_app(args: BottomArgs, config: Config) -> Result<(App, BottomLayout, Styles)> { |
| 279 | use BottomWidgetType::*; |
| 280 | |
| 281 | // Since everything takes a reference, but we want to take ownership here to |
| 282 | // drop matches/config later... |
| 283 | let args = &args; |
| 284 | let config = &config; |
| 285 | |
| 286 | let styling = Styles::new(args, config)?; |
| 287 | |
| 288 | let (widget_layout, default_widget_id, default_widget_type_option) = |
| 289 | get_widget_layout(args, config) |
| 290 | .context("Found an issue while trying to build the widget layout.")?; |
| 291 | |
| 292 | let retention_ms = get_retention(args, config)?; |
| 293 | let autohide_time = is_flag_enabled!(autohide_time, args.general, config); |
| 294 | let default_time_value = get_default_time_value(args, config, retention_ms)?; |
| 295 | |
| 296 | let use_basic_mode = is_flag_enabled!(basic, args.general, config); |
| 297 | let expanded = is_flag_enabled!(expanded, args.general, config); |
| 298 | |
| 299 | #[cfg(feature = "zfs")] |
| 300 | let free_arc = enabled_option_with_deprecated!( |
| 301 | args.memory.free_arc, |
| 302 | config, |
| 303 | memory_graph.free_arc, |
| 304 | flags.free_arc, |
| 305 | "memory.free_arc", |
| 306 | ); |
| 307 | |
| 308 | // For processes |
| 309 | let is_grouped = enabled_option_with_deprecated!( |
| 310 | args.process.group_processes, |
| 311 | config, |
| 312 | processes.default_grouped, |
| 313 | flags.group_processes, |
| 314 | ); |
| 315 | let is_case_sensitive = enabled_option_with_deprecated!( |
| 316 | args.process.case_sensitive, |
| 317 | config, |
| 318 | processes.case_sensitive, |
| 319 | flags.case_sensitive, |
| 320 | ); |
| 321 | let is_match_whole_word = enabled_option_with_deprecated!( |
| 322 | args.process.whole_word, |
| 323 | config, |
| 324 | processes.whole_word, |
| 325 | flags.whole_word, |
| 326 | ); |
| 327 | let is_use_regex = |
| 328 | enabled_option_with_deprecated!(args.process.regex, config, processes.regex, flags.regex,); |
| 329 | let is_default_tree = enabled_option_with_deprecated!( |
| 330 | args.process.tree, |
| 331 | config, |
| 332 | processes.default_tree, |
| 333 | flags.tree, |
| 334 | ); |
| 335 | let is_default_command = enabled_option_with_deprecated!( |
| 336 | args.process.process_command, |
| 337 | config, |
| 338 | processes.process_command, |
| 339 | flags.process_command, |
| 340 | ); |
| 341 | #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] |
| 342 | let is_advanced_kill = !(enabled_option_with_deprecated!( |
| 343 | args.process.disable_advanced_kill, |
| 344 | config, |
| 345 | processes.disable_advanced_kill, |
| 346 | flags.disable_advanced_kill, |
| 347 | )); |
| 348 | let is_read_only = is_flag_enabled!(read_only, args.process, config); |
| 349 | #[cfg(target_os = "linux")] |
| 350 | let hide_k_threads = enabled_option_with_deprecated!( |
| 351 | args.process.hide_k_threads, |
| 352 | config, |
| 353 | processes.hide_k_threads, |
| 354 | flags.hide_k_threads, |
| 355 | ); |
| 356 | |
| 357 | let process_memory_as_value = enabled_option_with_deprecated!( |
| 358 | args.process.process_memory_as_value, |
| 359 | config, |
| 360 | processes.default_memory_value, |
| 361 | flags.process_memory_as_value, |
| 362 | ); |
| 363 | let is_default_tree_collapsed = enabled_option_with_deprecated!( |
| 364 | args.process.tree_collapse, |
| 365 | config, |
| 366 | processes.tree_collapse, |
| 367 | flags.tree_collapse, |
| 368 | ); |
| 369 | |
| 370 | // For CPU |
| 371 | let default_cpu_selection = get_default_cpu_selection(args, config); |
| 372 | |
| 373 | let mut widget_map = FxHashMap::default(); |
| 374 | let mut cpu_state_map: FxHashMap<u64, CpuWidgetState> = FxHashMap::default(); |
| 375 | let mut mem_state_map: FxHashMap<u64, MemWidgetState> = FxHashMap::default(); |
| 376 | let mut net_state_map: FxHashMap<u64, NetWidgetState> = FxHashMap::default(); |
| 377 | let mut proc_state_map: FxHashMap<u64, ProcWidgetState> = FxHashMap::default(); |
| 378 | let mut temp_state_map: FxHashMap<u64, TempWidgetState> = FxHashMap::default(); |
| 379 | let mut temp_graph_state_map: FxHashMap<u64, TempGraphWidgetState> = FxHashMap::default(); |
| 380 | let mut disk_state_map: FxHashMap<u64, DiskTableWidget> = FxHashMap::default(); |
| 381 | let mut disk_io_graph_state_map: FxHashMap<u64, DiskIoGraphWidgetState> = FxHashMap::default(); |
| 382 | let mut battery_state_map: FxHashMap<u64, BatteryWidgetState> = FxHashMap::default(); |
| 383 | |
| 384 | let autohide_timer = if autohide_time { |
| 385 | Some(Instant::now()) |
| 386 | } else { |
| 387 | None |
| 388 | }; |
| 389 | |
| 390 | let mut initial_widget_id: u64 = default_widget_id; |
| 391 | let mut initial_widget_type = Proc; |
| 392 | let is_custom_layout = config.row.is_some(); |
| 393 | let mut used_widget_set = FxHashSet::default(); |
| 394 | |
| 395 | let network_unit_type = get_network_unit_type(args, config); |
| 396 | let network_scale_type = get_network_scale_type(args, config); |
| 397 | // Use + update this again after deprecation |
| 398 | // let network_use_binary_prefix = is_flag_enabled!(network_use_binary_prefix, args.network, config); |
| 399 | let network_use_binary_prefix = enabled_option_with_deprecated!( |
| 400 | args.network.network_use_binary_prefix, |
| 401 | config, |
| 402 | network_graph.use_binary_prefix, |
| 403 | flags.network_use_binary_prefix, |
| 404 | "network.use_binary_prefix", |
| 405 | ); |
| 406 | let network_show_packets = |
| 407 | is_flag_enabled_in!(show_packets, args.network, config.network_graph); |
| 408 | let network_start_zeroed = if args.network.network_start_zeroed { |
| 409 | true |
| 410 | } else if let Some(network_graph) = &config.network_graph { |
| 411 | network_graph.start_zeroed.unwrap_or(false) |
| 412 | } else { |
| 413 | false |
| 414 | }; |
| 415 | |
| 416 | let proc_columns: Option<IndexSet<ProcWidgetColumn>> = { |
| 417 | config.processes.as_ref().and_then(|cfg| { |
| 418 | if cfg.columns.is_empty() { |
| 419 | None |
| 420 | } else { |
| 421 | // TODO: Should we be using an indexmap? Or maybe allow dupes. |
| 422 | Some(IndexSet::from_iter( |
| 423 | cfg.columns.iter().map(ProcWidgetColumn::from), |
| 424 | )) |
| 425 | } |
| 426 | }) |
| 427 | }; |
| 428 | |
| 429 | let network_legend_position = get_network_legend_position(args, config)?; |
| 430 | let memory_legend_position = get_memory_legend_position(args, config)?; |
| 431 | let temperature_legend_position = get_temperature_legend_position(config)?; |
| 432 | let disk_io_legend_position = get_disk_io_legend_position(config)?; |
| 433 | let disk_io_name_filter = match &config.disk_io_graph { |
| 434 | Some(cfg) => get_ignore_list(&cfg.name_filter) |
| 435 | .context("Update 'disk_io_graph.name_filter' in your config file")?, |
| 436 | None => None, |
| 437 | }; |
| 438 | let disk_show_unmounted = config |
| 439 | .disk |
| 440 | .as_ref() |
| 441 | .and_then(|cfg| cfg.include_unmounted) |
| 442 | .unwrap_or(false); |
| 443 | let disk_io_graph_show_unmounted = config |
| 444 | .disk_io_graph |
| 445 | .as_ref() |
| 446 | .and_then(|cfg| cfg.include_unmounted) |
| 447 | .unwrap_or(false); |
| 448 | |
| 449 | let enable_cache_memory = get_enable_cache_memory(args, config); |
| 450 | let enable_gpu = get_enable_gpu(args, config); |
| 451 | |
| 452 | // TODO: Can probably just reuse the options struct. |
| 453 | let app_config_fields = AppConfigFields { |
| 454 | update_rate: get_update_rate(args, config)?, |
| 455 | temperature_type: get_temperature(args, config) |
| 456 | .context("Update 'temperature_type' in your config file.")?, |
| 457 | show_average_cpu: get_show_average_cpu(args, config), |
| 458 | show_cpu_decimal: config_or!(config, cpu.show_decimal, false), |
| 459 | use_dot: is_flag_enabled!(dot_marker, args.general, config), |
| 460 | cpu_left_legend: enabled_option_with_deprecated!( |
| 461 | args.cpu.cpu_left_legend, |
| 462 | config, |
| 463 | cpu.left_legend, |
| 464 | flags.cpu_left_legend, |
| 465 | ), |
| 466 | use_current_cpu_total: enabled_option_with_deprecated!( |
| 467 | args.process.current_usage, |
| 468 | config, |
| 469 | processes.current_usage, |
| 470 | flags.current_usage, |
| 471 | ), |
| 472 | unnormalized_cpu: enabled_option_with_deprecated!( |
| 473 | args.process.unnormalized_cpu, |
| 474 | config, |
| 475 | processes.unnormalized_cpu, |
| 476 | flags.unnormalized_cpu, |
| 477 | ), |
| 478 | get_process_threads: is_flag_enabled_in!(get_threads, args.process, config.processes), |
| 479 | use_basic_mode, |
| 480 | default_time_value, |
| 481 | time_interval: get_time_interval(args, config, retention_ms)?, |
| 482 | hide_time: is_flag_enabled!(hide_time, args.general, config), |
| 483 | autohide_time, |
| 484 | use_old_network_legend: is_flag_enabled!(use_old_network_legend, args.network, config), |
| 485 | table_gap: config_or_default!(config, flags.table_gap), |
| 486 | disable_click: is_flag_enabled!(disable_click, args.general, config), |
| 487 | disable_keys: is_flag_enabled!(disable_keys, args.general, config), |
| 488 | enable_gpu, |
| 489 | short_gpu_names: get_short_gpu_names(args, config), |
| 490 | enable_cache_memory, |
| 491 | show_table_scroll_position: is_flag_enabled!( |
| 492 | show_table_scroll_position, |
| 493 | args.general, |
| 494 | config |
| 495 | ), |
| 496 | show_table_scroll_bar: config_or!(config, flags.show_table_scroll_bar, false), |
| 497 | #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] |
| 498 | is_advanced_kill, |
| 499 | is_read_only, |
| 500 | #[cfg(target_os = "linux")] |
| 501 | hide_k_threads, |
| 502 | memory_legend_position, |
| 503 | network_legend_position, |
| 504 | network_scale_type, |
| 505 | network_unit_type, |
| 506 | network_use_binary_prefix, |
| 507 | network_show_packets, |
| 508 | network_start_zeroed, |
| 509 | retention_ms, |
| 510 | dedicated_average_row: enabled_option_with_deprecated!( |
| 511 | false, |
| 512 | config, |
| 513 | cpu.basic_average_cpu_row, |
| 514 | flags.average_cpu_row, |
| 515 | ), |
| 516 | default_tree_collapse: is_default_tree_collapsed, |
| 517 | #[cfg(feature = "zfs")] |
| 518 | free_arc, |
| 519 | default_temp_sort_column: config |
| 520 | .temperature |
| 521 | .as_ref() |
| 522 | .and_then(|cfg| cfg.default_sort.to_owned()), |
| 523 | default_temp_sort_order: config |
| 524 | .temperature |
| 525 | .as_ref() |
| 526 | .map(|cfg| cfg.sort_order) |
| 527 | .unwrap_or_default(), |
| 528 | default_disk_sort_column: config |
| 529 | .disk |
| 530 | .as_ref() |
| 531 | .and_then(|cfg| cfg.default_sort.to_owned()), |
| 532 | default_disk_sort_order: config |
| 533 | .disk |
| 534 | .as_ref() |
| 535 | .map(|cfg| cfg.sort_order) |
| 536 | .unwrap_or_default(), |
| 537 | temperature_legend_position, |
| 538 | disk_io_legend_position, |
| 539 | disk_show_unmounted, |
| 540 | disk_io_graph_show_unmounted, |
| 541 | }; |
| 542 | |
| 543 | let process_default_sort = match &args.process.process_default_sort { |
| 544 | Some(name) => Some(ProcColumn::parse_column_name(name).ok_or_else(|| { |
| 545 | OptionError::arg(format!( |
| 546 | "'{name}' is not a valid process column for '--process_default_sort'" |
| 547 | )) |
| 548 | })?), |
| 549 | None => config.processes.as_ref().and_then(|cfg| cfg.default_sort), |
| 550 | }; |
| 551 | |
| 552 | let ts_config = TimeseriesConfig { |
| 553 | time_interval: app_config_fields.time_interval, |
| 554 | retention_ms: app_config_fields.retention_ms, |
| 555 | autohide_time: app_config_fields.autohide_time, |
| 556 | default_time_value: app_config_fields.default_time_value, |
| 557 | }; |
| 558 | |
| 559 | let process_default_sort_order = config.processes.as_ref().and_then(|cfg| cfg.sort_order); |
| 560 | |
| 561 | let table_config = ProcTableConfig { |
| 562 | is_case_sensitive, |
| 563 | is_match_whole_word, |
| 564 | is_use_regex, |
| 565 | show_memory_as_values: process_memory_as_value, |
| 566 | is_command: is_default_command, |
| 567 | default_sort: process_default_sort, |
| 568 | sort_order: process_default_sort_order, |
| 569 | }; |
| 570 | |
| 571 | for row in &widget_layout.rows { |
| 572 | for col in &row.children { |
| 573 | for col_row in &col.children { |
| 574 | for widget in &col_row.children { |
| 575 | widget_map.insert(widget.widget_id, widget.clone()); |
| 576 | if let Some(default_widget_type) = &default_widget_type_option |
| 577 | && (!is_custom_layout || use_basic_mode) |
| 578 | { |
| 579 | match widget.widget_type { |
| 580 | BasicCpu => { |
| 581 | if let Cpu = *default_widget_type { |
| 582 | initial_widget_id = widget.widget_id; |
| 583 | initial_widget_type = Cpu; |
| 584 | } |
| 585 | } |
| 586 | BasicMem => { |
| 587 | if let Mem = *default_widget_type { |
| 588 | initial_widget_id = widget.widget_id; |
| 589 | initial_widget_type = Cpu; |
| 590 | } |
| 591 | } |
| 592 | BasicNet => { |
| 593 | if let Net = *default_widget_type { |
| 594 | initial_widget_id = widget.widget_id; |
| 595 | initial_widget_type = Cpu; |
| 596 | } |
| 597 | } |
| 598 | _ => { |
| 599 | if *default_widget_type == widget.widget_type { |
| 600 | initial_widget_id = widget.widget_id; |
| 601 | initial_widget_type = widget.widget_type.clone(); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | used_widget_set.insert(widget.widget_type.clone()); |
| 608 | |
| 609 | match widget.widget_type { |
| 610 | Cpu => { |
| 611 | cpu_state_map.insert( |
| 612 | widget.widget_id, |
| 613 | CpuWidgetState::new( |
| 614 | &app_config_fields, |
| 615 | default_cpu_selection, |
| 616 | autohide_timer, |
| 617 | &styling, |
| 618 | ), |
| 619 | ); |
| 620 | } |
| 621 | Mem => { |
| 622 | mem_state_map.insert( |
| 623 | widget.widget_id, |
| 624 | MemWidgetState::init(ts_config, autohide_timer), |
| 625 | ); |
| 626 | } |
| 627 | Net => { |
| 628 | net_state_map.insert( |
| 629 | widget.widget_id, |
| 630 | NetWidgetState::init(ts_config, autohide_timer), |
| 631 | ); |
| 632 | } |
| 633 | Proc => { |
| 634 | let mode = if is_grouped { |
| 635 | ProcWidgetMode::Grouped |
| 636 | } else if is_default_tree { |
| 637 | ProcWidgetMode::Tree(TreeCollapsed::new(is_default_tree_collapsed)) |
| 638 | } else { |
| 639 | ProcWidgetMode::Normal |
| 640 | }; |
| 641 | |
| 642 | proc_state_map.insert( |
| 643 | widget.widget_id, |
| 644 | ProcWidgetState::new( |
| 645 | &app_config_fields, |
| 646 | mode, |
| 647 | table_config, |
| 648 | &styling, |
| 649 | &proc_columns, |
| 650 | ), |
| 651 | ); |
| 652 | } |
| 653 | Disk => { |
| 654 | disk_state_map.insert( |
| 655 | widget.widget_id, |
| 656 | DiskTableWidget::new( |
| 657 | &app_config_fields, |
| 658 | &styling, |
| 659 | config.disk.as_ref().and_then(|cfg| cfg.columns.as_deref()), |
| 660 | disk_show_unmounted, |
| 661 | ), |
| 662 | ); |
| 663 | } |
| 664 | Temp => { |
| 665 | temp_state_map.insert( |
| 666 | widget.widget_id, |
| 667 | TempWidgetState::new(&app_config_fields, &styling), |
| 668 | ); |
| 669 | } |
| 670 | TempGraph => { |
| 671 | let upper_limit = config |
| 672 | .temperature_graph |
| 673 | .as_ref() |
| 674 | .and_then(|cfg| cfg.max_temp) |
| 675 | .map(|v| v as f32); |
| 676 | temp_graph_state_map.insert( |
| 677 | widget.widget_id, |
| 678 | TempGraphWidgetState::new(ts_config, autohide_timer, upper_limit), |
| 679 | ); |
| 680 | } |
| 681 | DiskIoGraph => { |
| 682 | let show_read = config |
| 683 | .disk_io_graph |
| 684 | .as_ref() |
| 685 | .and_then(|c| c.show_read) |
| 686 | .unwrap_or(true); |
| 687 | |
| 688 | let show_write = config |
| 689 | .disk_io_graph |
| 690 | .as_ref() |
| 691 | .and_then(|c| c.show_write) |
| 692 | .unwrap_or(true); |
| 693 | |
| 694 | let legend = config |
| 695 | .disk_io_graph |
| 696 | .as_ref() |
| 697 | .and_then(|c| c.legend.clone()) |
| 698 | .unwrap_or_default(); |
| 699 | |
| 700 | let use_log = config |
| 701 | .disk_io_graph |
| 702 | .as_ref() |
| 703 | .and_then(|c| c.use_log) |
| 704 | .unwrap_or(false); |
| 705 | |
| 706 | disk_io_graph_state_map.insert( |
| 707 | widget.widget_id, |
| 708 | DiskIoGraphWidgetState::new( |
| 709 | ts_config, |
| 710 | autohide_timer, |
| 711 | show_read, |
| 712 | show_write, |
| 713 | legend, |
| 714 | use_log, |
| 715 | ), |
| 716 | ); |
| 717 | } |
| 718 | Battery => { |
| 719 | battery_state_map |
| 720 | .insert(widget.widget_id, BatteryWidgetState::default()); |
| 721 | } |
| 722 | // FIXME: This is kind of a hack that we have these cases at all. |
| 723 | Empty | BasicCpu | BasicMem | BasicNet | BasicTables | CpuLegend |
| 724 | | ProcSort | ProcSearch => {} |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | let basic_table_widget_state = if use_basic_mode { |
| 732 | Some(match initial_widget_type { |
| 733 | Proc | Disk | Temp => BasicTableWidgetState { |
| 734 | currently_displayed_widget_type: initial_widget_type, |
| 735 | currently_displayed_widget_id: initial_widget_id, |
| 736 | left_tlc: None, |
| 737 | left_brc: None, |
| 738 | right_tlc: None, |
| 739 | right_brc: None, |
| 740 | }, |
| 741 | _ => BasicTableWidgetState { |
| 742 | currently_displayed_widget_type: Proc, |
| 743 | currently_displayed_widget_id: DEFAULT_WIDGET_ID, |
| 744 | left_tlc: None, |
| 745 | left_brc: None, |
| 746 | right_tlc: None, |
| 747 | right_brc: None, |
| 748 | }, |
| 749 | }) |
| 750 | } else { |
| 751 | None |
| 752 | }; |
| 753 | |
| 754 | let use_mem = used_widget_set.contains(&Mem) || used_widget_set.contains(&BasicMem); |
| 755 | let used_widgets = UsedWidgets { |
| 756 | use_cpu: used_widget_set.contains(&Cpu) || used_widget_set.contains(&BasicCpu), |
| 757 | use_mem, |
| 758 | use_cache: use_mem && enable_cache_memory, |
| 759 | use_gpu: enable_gpu, |
| 760 | use_net: used_widget_set.contains(&Net) || used_widget_set.contains(&BasicNet), |
| 761 | use_proc: used_widget_set.contains(&Proc), |
| 762 | use_disk: used_widget_set.contains(&Disk), |
| 763 | use_temp: used_widget_set.contains(&Temp), |
| 764 | use_temp_graph: used_widget_set.contains(&TempGraph), |
| 765 | use_disk_io_graph: used_widget_set.contains(&DiskIoGraph), |
| 766 | use_battery: used_widget_set.contains(&Battery), |
| 767 | }; |
| 768 | |
| 769 | let (disk_name_filter, disk_mount_filter) = { |
| 770 | match &config.disk { |
| 771 | Some(cfg) => { |
| 772 | let df = get_ignore_list(&cfg.name_filter) |
| 773 | .context("Update 'disk.name_filter' in your config file")?; |
| 774 | let mf = get_ignore_list(&cfg.mount_filter) |
| 775 | .context("Update 'disk.mount_filter' in your config file")?; |
| 776 | |
| 777 | (df, mf) |
| 778 | } |
| 779 | None => (None, None), |
| 780 | } |
| 781 | }; |
| 782 | let temp_sensor_filter = match &config.temperature { |
| 783 | Some(cfg) => get_ignore_list(&cfg.sensor_filter) |
| 784 | .context("Update 'temperature.sensor_filter' in your config file")?, |
| 785 | None => None, |
| 786 | }; |
| 787 | let temp_graph_sensor_filter = match &config.temperature_graph { |
| 788 | Some(cfg) => get_ignore_list(&cfg.sensor_filter) |
| 789 | .context("Update 'temperature_graph.sensor_filter' in your config file")?, |
| 790 | None => None, |
| 791 | }; |
| 792 | let net_interface_filter = match &config.network_graph { |
| 793 | Some(cfg) => get_ignore_list(&cfg.interface_filter) |
| 794 | .context("Update 'network_graph.interface_filter' in your config file")?, |
| 795 | None => None, |
| 796 | }; |
| 797 | |
| 798 | let states = AppWidgetStates { |
| 799 | cpu_state: CpuState::init(cpu_state_map), |
| 800 | mem_state: MemState::init(mem_state_map), |
| 801 | net_state: NetState::init(net_state_map), |
| 802 | proc_state: ProcState::init(proc_state_map), |
| 803 | temp_state: TempState::init(temp_state_map), |
| 804 | temp_graph_state: TempGraphStates::init(temp_graph_state_map), |
| 805 | disk_state: DiskState::init(disk_state_map), |
| 806 | disk_io_graph_state: DiskIoGraphStates::init(disk_io_graph_state_map), |
| 807 | battery_state: AppBatteryState::init(battery_state_map), |
| 808 | basic_table_widget_state, |
| 809 | }; |
| 810 | |
| 811 | let current_widget = widget_map |
| 812 | .get(&initial_widget_id) |
| 813 | .expect("widget map should have the initial widget ID") |
| 814 | .clone(); |
| 815 | let filters = DataFilters { |
| 816 | disk_filter: disk_name_filter, |
| 817 | mount_filter: disk_mount_filter, |
| 818 | temp_filter: temp_sensor_filter, |
| 819 | temp_graph_filter: temp_graph_sensor_filter, |
| 820 | disk_io_graph_filter: disk_io_name_filter, |
| 821 | net_filter: net_interface_filter, |
| 822 | }; |
| 823 | let is_expanded = expanded && !use_basic_mode; |
| 824 | |
| 825 | Ok(( |
| 826 | App::new( |
| 827 | app_config_fields, |
| 828 | states, |
| 829 | widget_map, |
| 830 | current_widget, |
| 831 | used_widgets, |
| 832 | filters, |
| 833 | is_expanded, |
| 834 | ), |
| 835 | widget_layout, |
| 836 | styling, |
| 837 | )) |
| 838 | } |
| 839 | |
| 840 | fn get_widget_layout( |
| 841 | args: &BottomArgs, config: &Config, |
| 842 | ) -> OptionResult<(BottomLayout, u64, Option<BottomWidgetType>)> { |
| 843 | let cpu_left_legend = enabled_option_with_deprecated!( |
| 844 | args.cpu.cpu_left_legend, |
| 845 | config, |
| 846 | cpu.left_legend, |
| 847 | flags.cpu_left_legend, |
| 848 | ); |
| 849 | |
| 850 | let (default_widget_type, mut default_widget_count) = |
| 851 | get_default_widget_and_count(args, config)?; |
| 852 | let mut default_widget_id = 1; |
| 853 | |
| 854 | let bottom_layout = if is_flag_enabled!(basic, args.general, config) { |
| 855 | default_widget_id = DEFAULT_WIDGET_ID; |
| 856 | |
| 857 | BottomLayout::init_basic_default(get_use_battery(args, config)) |
| 858 | } else { |
| 859 | let ref_row: Vec<Row>; // Required to handle reference |
| 860 | let rows = match &config.row { |
| 861 | Some(r) => r, |
| 862 | None => { |
| 863 | ref_row = toml_edit::de::from_str::<Config>(if get_use_battery(args, config) { |
| 864 | DEFAULT_BATTERY_LAYOUT |
| 865 | } else { |
| 866 | DEFAULT_LAYOUT |
| 867 | })? |
| 868 | .row |
| 869 | .expect("This cannot (like it really shouldn't) fail!"); |
| 870 | &ref_row |
| 871 | } |
| 872 | }; |
| 873 | |
| 874 | let mut iter_id = 0; // A lazy way of forcing unique IDs *shrugs* |
| 875 | let mut total_height_ratio = 0; |
| 876 | |
| 877 | let mut ret_bottom_layout = BottomLayout { |
| 878 | rows: rows |
| 879 | .iter() |
| 880 | .map(|row| { |
| 881 | row.convert_row_to_bottom_row( |
| 882 | &mut iter_id, |
| 883 | &mut total_height_ratio, |
| 884 | &mut default_widget_id, |
| 885 | &default_widget_type, |
| 886 | &mut default_widget_count, |
| 887 | cpu_left_legend, |
| 888 | ) |
| 889 | .map_err(|err| OptionError::config(err.to_string())) |
| 890 | }) |
| 891 | .collect::<OptionResult<Vec<_>>>()?, |
| 892 | total_row_height_ratio: total_height_ratio, |
| 893 | }; |
| 894 | |
| 895 | // Confirm that we have at least ONE widget left - if not, error out! |
| 896 | if iter_id > 0 { |
| 897 | ret_bottom_layout.get_movement_mappings(); |
| 898 | ret_bottom_layout |
| 899 | } else { |
| 900 | return Err(OptionError::config( |
| 901 | "have at least one widget under the '[[row]]' section.", |
| 902 | )); |
| 903 | } |
| 904 | }; |
| 905 | |
| 906 | Ok((bottom_layout, default_widget_id, default_widget_type)) |
| 907 | } |
| 908 | |
| 909 | #[inline] |
| 910 | fn try_parse_ms(s: &str) -> Result<u64, ()> { |
| 911 | Ok(if let Ok(val) = humantime::parse_duration(s) { |
| 912 | val.as_millis().try_into().map_err(|_| ())? |
| 913 | } else if let Ok(val) = s.parse::<u64>() { |
| 914 | val |
| 915 | } else { |
| 916 | return Err(()); |
| 917 | }) |
| 918 | } |
| 919 | |
| 920 | macro_rules! parse_arg_value { |
| 921 | ($to_try:expr, $flag:literal) => { |
| 922 | $to_try.map_err(|_| OptionError::invalid_arg_value($flag)) |
| 923 | }; |
| 924 | } |
| 925 | |
| 926 | macro_rules! parse_config_value { |
| 927 | ($to_try:expr, $setting:literal) => { |
| 928 | $to_try.map_err(|_| OptionError::invalid_config_value($setting)) |
| 929 | }; |
| 930 | } |
| 931 | |
| 932 | macro_rules! parse_ms_option { |
| 933 | ($arg_expr:expr, $config_expr:expr, $default_value:expr, $setting:literal, $low:expr, $high:expr $(,)?) => {{ |
| 934 | use humantime::format_duration; |
| 935 | |
| 936 | if let Some(to_parse) = $arg_expr { |
| 937 | let value = parse_arg_value!(try_parse_ms(to_parse), $setting)?; |
| 938 | |
| 939 | if let Some(limit) = $low { |
| 940 | if value < limit { |
| 941 | return Err(OptionError::arg(format!( |
| 942 | "'--{}' must be greater than {}", |
| 943 | $setting, |
| 944 | format_duration(Duration::from_millis(limit)) |
| 945 | ))); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | if let Some(limit) = $high { |
| 950 | if value > limit { |
| 951 | return Err(OptionError::arg(format!( |
| 952 | "'--{}' must be less than {}", |
| 953 | $setting, |
| 954 | format_duration(Duration::from_millis(limit)) |
| 955 | ))); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | Ok(value) |
| 960 | } else if let Some(to_parse) = $config_expr { |
| 961 | let value = match to_parse { |
| 962 | StringOrNum::String(s) => parse_config_value!(try_parse_ms(s), $setting)?, |
| 963 | StringOrNum::Num(n) => *n, |
| 964 | }; |
| 965 | |
| 966 | if let Some(limit) = $low { |
| 967 | if value < limit { |
| 968 | return Err(OptionError::arg(format!( |
| 969 | "'{}' must be greater than {}", |
| 970 | $setting, |
| 971 | format_duration(Duration::from_millis(limit)) |
| 972 | ))); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | if let Some(limit) = $high { |
| 977 | if value > limit { |
| 978 | return Err(OptionError::arg(format!( |
| 979 | "'{}' must be less than {}", |
| 980 | $setting, |
| 981 | format_duration(Duration::from_millis(limit)) |
| 982 | ))); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | Ok(value) |
| 987 | } else { |
| 988 | Ok($default_value) |
| 989 | } |
| 990 | }}; |
| 991 | } |
| 992 | |
| 993 | /// Mark a config option field as deprecated, and what to use instead. |
| 994 | #[inline] |
| 995 | fn deprecated_warning(deprecated_field: &str, new_field: &str) { |
| 996 | eprintln!( |
| 997 | "Warning: The config option '{deprecated_field}' is deprecated and will eventually be removed. Please use '{new_field}' instead.", |
| 998 | ); |
| 999 | } |
| 1000 | |
| 1001 | /// Mark a config option field as deprecated, and what to use instead with an alias. |
| 1002 | #[inline] |
| 1003 | fn deprecated_warning_with_alias(deprecated_field: &str, new_field: &str, alias: &str) { |
| 1004 | eprintln!( |
| 1005 | "Warning: The config option '{deprecated_field}' is deprecated and will eventually be removed. Please use '{new_field}' or '{alias}' instead.", |
| 1006 | ); |
| 1007 | } |
| 1008 | |
| 1009 | /// How quickly we update data. |
| 1010 | #[inline] |
| 1011 | fn get_update_rate(args: &BottomArgs, config: &Config) -> OptionResult<u64> { |
| 1012 | const DEFAULT_REFRESH_RATE_IN_MILLISECONDS: u64 = 1000; |
| 1013 | |
| 1014 | parse_ms_option!( |
| 1015 | &args.general.rate, |
| 1016 | config.flags.as_ref().and_then(|flags| flags.rate.as_ref()), |
| 1017 | DEFAULT_REFRESH_RATE_IN_MILLISECONDS, |
| 1018 | "rate", |
| 1019 | Some(250), |
| 1020 | None, |
| 1021 | ) |
| 1022 | } |
| 1023 | |
| 1024 | fn get_temperature(args: &BottomArgs, config: &Config) -> OptionResult<TemperatureType> { |
| 1025 | if args.temperature.fahrenheit { |
| 1026 | return Ok(TemperatureType::Fahrenheit); |
| 1027 | } else if args.temperature.kelvin { |
| 1028 | return Ok(TemperatureType::Kelvin); |
| 1029 | } else if args.temperature.celsius { |
| 1030 | return Ok(TemperatureType::Celsius); |
| 1031 | } else if let Some(flags) = &config.flags |
| 1032 | && let Some(temp_type) = &flags.temperature_type |
| 1033 | { |
| 1034 | return parse_config_value!(TemperatureType::from_str(temp_type), "temperature_type"); |
| 1035 | } |
| 1036 | Ok(TemperatureType::Celsius) |
| 1037 | } |
| 1038 | |
| 1039 | /// Yes, this function gets whether to show average CPU (true) or not (false). |
| 1040 | fn get_show_average_cpu(args: &BottomArgs, config: &Config) -> bool { |
| 1041 | if args.cpu.hide_avg_cpu { |
| 1042 | return false; |
| 1043 | } else if let Some(cpu) = &config.cpu { |
| 1044 | return !cpu.hide_avg_cpu.unwrap_or(false); |
| 1045 | } else if let Some(flags) = &config.flags |
| 1046 | && let Some(hide) = flags.hide_avg_cpu |
| 1047 | { |
| 1048 | deprecated_warning("hide_avg_cpu", "cpu.hide_avg_cpu"); |
| 1049 | return !hide; |
| 1050 | } |
| 1051 | |
| 1052 | true |
| 1053 | } |
| 1054 | |
| 1055 | // I hate this too. |
| 1056 | fn get_default_cpu_selection(args: &BottomArgs, config: &Config) -> config::cpu::CpuDefault { |
| 1057 | match &args.cpu.default_cpu_entry { |
| 1058 | Some(default) => match default { |
| 1059 | args::CpuDefault::All => config::cpu::CpuDefault::All, |
| 1060 | args::CpuDefault::Average => config::cpu::CpuDefault::Average, |
| 1061 | }, |
| 1062 | None => config.cpu.as_ref().map(|c| c.default).unwrap_or_default(), |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | #[inline] |
| 1067 | fn get_default_time_value( |
| 1068 | args: &BottomArgs, config: &Config, retention_ms: u64, |
| 1069 | ) -> OptionResult<u64> { |
| 1070 | const DEFAULT_TIME_MILLISECONDS: u64 = 60 * 1000; // Defaults to 1 min. |
| 1071 | |
| 1072 | parse_ms_option!( |
| 1073 | &args.general.default_time_value, |
| 1074 | config |
| 1075 | .flags |
| 1076 | .as_ref() |
| 1077 | .and_then(|flags| flags.default_time_value.as_ref()), |
| 1078 | DEFAULT_TIME_MILLISECONDS, |
| 1079 | "default_time_value", |
| 1080 | Some(30000), |
| 1081 | Some(retention_ms), |
| 1082 | ) |
| 1083 | } |
| 1084 | |
| 1085 | #[inline] |
| 1086 | fn get_time_interval(args: &BottomArgs, config: &Config, retention_ms: u64) -> OptionResult<u64> { |
| 1087 | const TIME_CHANGE_MILLISECONDS: u64 = 15 * 1000; // How much to increment each time |
| 1088 | |
| 1089 | parse_ms_option!( |
| 1090 | &args.general.time_delta, |
| 1091 | config |
| 1092 | .flags |
| 1093 | .as_ref() |
| 1094 | .and_then(|flags| flags.time_delta.as_ref()), |
| 1095 | TIME_CHANGE_MILLISECONDS, |
| 1096 | "time_delta", |
| 1097 | Some(1000), |
| 1098 | Some(retention_ms), |
| 1099 | ) |
| 1100 | } |
| 1101 | |
| 1102 | fn get_default_widget_and_count( |
| 1103 | args: &BottomArgs, config: &Config, |
| 1104 | ) -> OptionResult<(Option<BottomWidgetType>, u64)> { |
| 1105 | let widget_type = if let Some(widget_type) = &args.general.default_widget_type { |
| 1106 | let parsed_widget = parse_arg_value!(widget_type.parse(), "default_widget_type")?; |
| 1107 | if let BottomWidgetType::Empty = parsed_widget { |
| 1108 | None |
| 1109 | } else { |
| 1110 | Some(parsed_widget) |
| 1111 | } |
| 1112 | } else if let Some(flags) = &config.flags { |
| 1113 | if let Some(widget_type) = &flags.default_widget_type { |
| 1114 | let parsed_widget = parse_config_value!(widget_type.parse(), "default_widget_type")?; |
| 1115 | if let BottomWidgetType::Empty = parsed_widget { |
| 1116 | None |
| 1117 | } else { |
| 1118 | Some(parsed_widget) |
| 1119 | } |
| 1120 | } else { |
| 1121 | None |
| 1122 | } |
| 1123 | } else { |
| 1124 | None |
| 1125 | }; |
| 1126 | |
| 1127 | let widget_count: Option<u128> = if let Some(widget_count) = args.general.default_widget_count { |
| 1128 | Some(widget_count.into()) |
| 1129 | } else { |
| 1130 | config.flags.as_ref().and_then(|flags| { |
| 1131 | flags |
| 1132 | .default_widget_count |
| 1133 | .map(|widget_count| widget_count.into()) |
| 1134 | }) |
| 1135 | }; |
| 1136 | |
| 1137 | match (widget_type, widget_count) { |
| 1138 | (Some(widget_type), Some(widget_count)) => { |
| 1139 | let widget_count = widget_count.try_into().map_err(|_| OptionError::other( |
| 1140 | "set your widget count to be at most 18446744073709551615.".to_string() |
| 1141 | ))?; |
| 1142 | Ok((Some(widget_type), widget_count)) |
| 1143 | } |
| 1144 | (Some(widget_type), None) => Ok((Some(widget_type), 1)), |
| 1145 | (None, Some(_widget_count)) => Err(OptionError::other( |
| 1146 | "cannot set 'default_widget_count' by itself, it must be used with 'default_widget_type'.".to_string(), |
| 1147 | )), |
| 1148 | (None, None) => Ok((None, 1)) |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | #[cfg(feature = "battery")] |
| 1153 | fn get_use_battery(args: &BottomArgs, config: &Config) -> bool { |
| 1154 | // TODO: Move this so it's dynamic in the app itself and automatically hide if |
| 1155 | // there are no batteries? |
| 1156 | if let Ok(battery_manager) = Manager::new() |
| 1157 | && let Ok(batteries) = battery_manager.batteries() |
| 1158 | && batteries.count() == 0 |
| 1159 | { |
| 1160 | return false; |
| 1161 | } |
| 1162 | |
| 1163 | if args.battery.battery { |
| 1164 | return true; |
| 1165 | } else if let Some(flags) = &config.flags |
| 1166 | && let Some(battery) = flags.battery |
| 1167 | { |
| 1168 | return battery; |
| 1169 | } |
| 1170 | |
| 1171 | false |
| 1172 | } |
| 1173 | |
| 1174 | #[cfg(not(feature = "battery"))] |
| 1175 | fn get_use_battery(_args: &BottomArgs, _config: &Config) -> bool { |
| 1176 | false |
| 1177 | } |
| 1178 | |
| 1179 | #[cfg(feature = "gpu")] |
| 1180 | fn get_enable_gpu(args: &BottomArgs, config: &Config) -> bool { |
| 1181 | if args.gpu.disable_gpu { |
| 1182 | return false; |
| 1183 | } |
| 1184 | |
| 1185 | !config |
| 1186 | .flags |
| 1187 | .as_ref() |
| 1188 | .and_then(|f| f.disable_gpu) |
| 1189 | .unwrap_or(false) |
| 1190 | } |
| 1191 | |
| 1192 | #[cfg(not(feature = "gpu"))] |
| 1193 | fn get_enable_gpu(_: &BottomArgs, _: &Config) -> bool { |
| 1194 | false |
| 1195 | } |
| 1196 | |
| 1197 | #[cfg(feature = "gpu")] |
| 1198 | fn get_short_gpu_names(args: &BottomArgs, config: &Config) -> bool { |
| 1199 | if args.memory.short_gpu_names { |
| 1200 | return true; |
| 1201 | } |
| 1202 | |
| 1203 | config |
| 1204 | .memory_graph |
| 1205 | .as_ref() |
| 1206 | .and_then(|m| m.short_gpu_names) |
| 1207 | .unwrap_or(false) |
| 1208 | } |
| 1209 | |
| 1210 | #[cfg(not(feature = "gpu"))] |
| 1211 | fn get_short_gpu_names(_: &BottomArgs, _: &Config) -> bool { |
| 1212 | false |
| 1213 | } |
| 1214 | |
| 1215 | #[cfg(not(target_os = "windows"))] |
| 1216 | fn get_enable_cache_memory(args: &BottomArgs, config: &Config) -> bool { |
| 1217 | enabled_option_with_deprecated!( |
| 1218 | args.memory.enable_cache_memory, |
| 1219 | config, |
| 1220 | memory_graph.cache_memory, |
| 1221 | flags.enable_cache_memory, |
| 1222 | "memory.cache_memory", |
| 1223 | ) |
| 1224 | } |
| 1225 | |
| 1226 | #[cfg(target_os = "windows")] |
| 1227 | fn get_enable_cache_memory(_args: &BottomArgs, _config: &Config) -> bool { |
| 1228 | false |
| 1229 | } |
| 1230 | |
| 1231 | fn get_ignore_list(ignore_list: &Option<IgnoreList>) -> OptionResult<Option<Filter>> { |
| 1232 | if let Some(ignore_list) = ignore_list { |
| 1233 | let list: Result<Vec<_>, _> = ignore_list |
| 1234 | .list |
| 1235 | .iter() |
| 1236 | .map(|name| { |
| 1237 | let escaped_string: String; |
| 1238 | let res = format!( |
| 1239 | "{}{}{}{}", |
| 1240 | if ignore_list.whole_word { "^" } else { "" }, |
| 1241 | if ignore_list.case_sensitive { |
| 1242 | "" |
| 1243 | } else { |
| 1244 | "(?i)" |
| 1245 | }, |
| 1246 | if ignore_list.regex { |
| 1247 | name |
| 1248 | } else { |
| 1249 | escaped_string = regex::escape(name); |
| 1250 | &escaped_string |
| 1251 | }, |
| 1252 | if ignore_list.whole_word { "$" } else { "" }, |
| 1253 | ); |
| 1254 | |
| 1255 | Regex::new(&res) |
| 1256 | }) |
| 1257 | .collect(); |
| 1258 | |
| 1259 | let list = list.map_err(|err| OptionError::config(err.to_string()))?; |
| 1260 | |
| 1261 | Ok(Some(Filter::new(ignore_list.is_list_ignored, list))) |
| 1262 | } else { |
| 1263 | Ok(None) |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | fn get_network_unit_type(args: &BottomArgs, config: &Config) -> DataUnit { |
| 1268 | if args.network.network_use_bytes { |
| 1269 | return DataUnit::Byte; |
| 1270 | } else if let Some(use_bytes) = config.network_graph.as_ref().and_then(|cfg| cfg.use_bytes) { |
| 1271 | if use_bytes { |
| 1272 | return DataUnit::Byte; |
| 1273 | } |
| 1274 | } else if let Some(flags) = &config.flags |
| 1275 | && let Some(network_use_bytes) = flags.network_use_bytes |
| 1276 | { |
| 1277 | deprecated_warning_with_alias( |
| 1278 | "network_use_bytes", |
| 1279 | "network_graph.use_bytes", |
| 1280 | "network.use_bytes", |
| 1281 | ); |
| 1282 | |
| 1283 | if network_use_bytes { |
| 1284 | return DataUnit::Byte; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | DataUnit::Bit |
| 1289 | } |
| 1290 | |
| 1291 | fn get_network_scale_type(args: &BottomArgs, config: &Config) -> AxisScaling { |
| 1292 | if args.network.network_use_log { |
| 1293 | return AxisScaling::Log; |
| 1294 | } else if let Some(use_log) = config.network_graph.as_ref().and_then(|cfg| cfg.use_log) { |
| 1295 | if use_log { |
| 1296 | return AxisScaling::Log; |
| 1297 | } |
| 1298 | } else if let Some(flags) = &config.flags |
| 1299 | && let Some(network_use_log) = flags.network_use_log |
| 1300 | { |
| 1301 | deprecated_warning_with_alias( |
| 1302 | "network_use_log", |
| 1303 | "network_graph.use_log", |
| 1304 | "network.use_log", |
| 1305 | ); |
| 1306 | |
| 1307 | if network_use_log { |
| 1308 | return AxisScaling::Log; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | AxisScaling::Linear |
| 1313 | } |
| 1314 | |
| 1315 | fn get_retention(args: &BottomArgs, config: &Config) -> OptionResult<u64> { |
| 1316 | const DEFAULT_RETENTION_MS: u64 = 600 * 1000; // Keep 10 minutes of data. |
| 1317 | |
| 1318 | parse_ms_option!( |
| 1319 | &args.general.retention, |
| 1320 | config |
| 1321 | .flags |
| 1322 | .as_ref() |
| 1323 | .and_then(|flags| flags.retention.as_ref()), |
| 1324 | DEFAULT_RETENTION_MS, |
| 1325 | "retention", |
| 1326 | None, |
| 1327 | None, |
| 1328 | ) |
| 1329 | } |
| 1330 | |
| 1331 | #[inline] |
| 1332 | fn parse_legend_position( |
| 1333 | arg: Option<&String>, cfg: Option<&String>, deprecated_cfg: Option<(&String, &'static str)>, |
| 1334 | setting: &'static str, alias: Option<&'static str>, |
| 1335 | ) -> OptionResult<Option<LegendPosition>> { |
| 1336 | #[inline] |
| 1337 | fn parse_position_or_err<F: FnOnce(&'static str) -> OptionError>( |
| 1338 | s: &str, setting: &'static str, err_fn: F, |
| 1339 | ) -> OptionResult<Option<LegendPosition>> { |
| 1340 | Ok(match s.to_ascii_lowercase().trim() { |
| 1341 | "none" => None, |
| 1342 | position => Some(position.parse().map_err(|_| err_fn(setting))?), |
| 1343 | }) |
| 1344 | } |
| 1345 | |
| 1346 | if let Some(s) = arg { |
| 1347 | parse_position_or_err(s, setting, OptionError::invalid_arg_value) |
| 1348 | } else if let Some(s) = cfg { |
| 1349 | parse_position_or_err(s, setting, OptionError::invalid_config_value) |
| 1350 | } else if let Some((s, name)) = deprecated_cfg { |
| 1351 | // Remove the deprecated args/code paths and copy back to the function above later. |
| 1352 | if let Some(alias) = alias { |
| 1353 | deprecated_warning_with_alias(name, setting, alias); |
| 1354 | } else { |
| 1355 | deprecated_warning(name, setting); |
| 1356 | } |
| 1357 | parse_position_or_err(s, setting, OptionError::invalid_config_value) |
| 1358 | } else { |
| 1359 | Ok(Some(LegendPosition::default())) |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | fn get_network_legend_position( |
| 1364 | args: &BottomArgs, config: &Config, |
| 1365 | ) -> OptionResult<Option<LegendPosition>> { |
| 1366 | parse_legend_position( |
| 1367 | args.network.network_legend.as_ref(), |
| 1368 | config |
| 1369 | .network_graph |
| 1370 | .as_ref() |
| 1371 | .and_then(|cfg| cfg.legend_position.as_ref()), |
| 1372 | config |
| 1373 | .flags |
| 1374 | .as_ref() |
| 1375 | .and_then(|flags| flags.network_legend.as_ref()) |
| 1376 | .map(|s| (s, "network_legend")), |
| 1377 | "network_graph.legend_position", |
| 1378 | Some("network.legend_position"), |
| 1379 | ) |
| 1380 | } |
| 1381 | |
| 1382 | fn get_memory_legend_position( |
| 1383 | args: &BottomArgs, config: &Config, |
| 1384 | ) -> OptionResult<Option<LegendPosition>> { |
| 1385 | parse_legend_position( |
| 1386 | args.memory.memory_legend.as_ref(), |
| 1387 | config |
| 1388 | .memory_graph |
| 1389 | .as_ref() |
| 1390 | .and_then(|cfg| cfg.legend_position.as_ref()), |
| 1391 | config |
| 1392 | .flags |
| 1393 | .as_ref() |
| 1394 | .and_then(|flags| flags.memory_legend.as_ref()) |
| 1395 | .map(|s| (s, "memory_legend")), |
| 1396 | "memory_graph.legend_position", |
| 1397 | Some("memory.legend_position"), |
| 1398 | ) |
| 1399 | } |
| 1400 | |
| 1401 | fn get_temperature_legend_position(config: &Config) -> OptionResult<Option<LegendPosition>> { |
| 1402 | parse_legend_position( |
| 1403 | None, |
| 1404 | config |
| 1405 | .temperature_graph |
| 1406 | .as_ref() |
| 1407 | .and_then(|settings| settings.legend_position.as_ref()), |
| 1408 | None, |
| 1409 | "legend_position", |
| 1410 | None, |
| 1411 | ) |
| 1412 | } |
| 1413 | |
| 1414 | fn get_disk_io_legend_position(config: &Config) -> OptionResult<Option<LegendPosition>> { |
| 1415 | parse_legend_position( |
| 1416 | None, |
| 1417 | config |
| 1418 | .disk_io_graph |
| 1419 | .as_ref() |
| 1420 | .and_then(|settings| settings.legend_position.as_ref()), |
| 1421 | None, |
| 1422 | "disk_io_graph.legend_position", |
| 1423 | None, |
| 1424 | ) |
| 1425 | } |
| 1426 | |
| 1427 | #[cfg(test)] |
| 1428 | mod test { |
| 1429 | use clap::Parser; |
| 1430 | |
| 1431 | use super::*; |
| 1432 | use crate::{ |
| 1433 | app::App, |
| 1434 | args::BottomArgs, |
| 1435 | canvas::components::time_series::LegendPosition, |
| 1436 | options::{ |
| 1437 | OptionError, config::flags::GeneralConfig, get_default_time_value, get_retention, |
| 1438 | get_update_rate, parse_legend_position, try_parse_ms, |
| 1439 | }, |
| 1440 | }; |
| 1441 | |
| 1442 | #[test] |
| 1443 | fn verify_try_parse_ms() { |
| 1444 | let a = "100s"; |
| 1445 | let b = "100"; |
| 1446 | let c = "1 min"; |
| 1447 | let d = "1 hour 1 min"; |
| 1448 | |
| 1449 | assert_eq!(try_parse_ms(a), Ok(100 * 1000)); |
| 1450 | assert_eq!(try_parse_ms(b), Ok(100)); |
| 1451 | assert_eq!(try_parse_ms(c), Ok(60 * 1000)); |
| 1452 | assert_eq!(try_parse_ms(d), Ok(3660 * 1000)); |
| 1453 | |
| 1454 | let a_bad = "1 test"; |
| 1455 | let b_bad = "-100"; |
| 1456 | |
| 1457 | assert!(try_parse_ms(a_bad).is_err()); |
| 1458 | assert!(try_parse_ms(b_bad).is_err()); |
| 1459 | } |
| 1460 | |
| 1461 | #[test] |
| 1462 | fn verify_parse_legend_position() { |
| 1463 | let setting = "network_legend"; |
| 1464 | |
| 1465 | // No arg, no config. |
| 1466 | assert_eq!( |
| 1467 | parse_legend_position(None, None, None, setting, None), |
| 1468 | Ok(Some(LegendPosition::default())) |
| 1469 | ); |
| 1470 | |
| 1471 | // Arg takes precedence and is parsed (case-insensitive, trimmed). |
| 1472 | let arg = " ToP-lEfT ".to_string(); |
| 1473 | let cfg = "bottom-right".to_string(); |
| 1474 | assert_eq!( |
| 1475 | parse_legend_position(Some(&arg), Some(&cfg), None, setting, None), |
| 1476 | Ok(Some(LegendPosition::TopLeft)) |
| 1477 | ); |
| 1478 | |
| 1479 | // "none" disables the legend, from either source. |
| 1480 | let none_arg = "None".to_string(); |
| 1481 | assert_eq!( |
| 1482 | parse_legend_position(Some(&none_arg), None, None, setting, None), |
| 1483 | Ok(None) |
| 1484 | ); |
| 1485 | let none_cfg = "none".to_string(); |
| 1486 | assert_eq!( |
| 1487 | parse_legend_position(None, Some(&none_cfg), None, setting, None), |
| 1488 | Ok(None) |
| 1489 | ); |
| 1490 | |
| 1491 | // Config value is used when no arg is provided. |
| 1492 | let cfg_only = "left".to_string(); |
| 1493 | assert_eq!( |
| 1494 | parse_legend_position(None, Some(&cfg_only), None, setting, None), |
| 1495 | Ok(Some(LegendPosition::Left)) |
| 1496 | ); |
| 1497 | |
| 1498 | // Invalid arg value. |
| 1499 | let bad_arg = "bad".to_string(); |
| 1500 | assert_eq!( |
| 1501 | parse_legend_position(Some(&bad_arg), None, None, setting, None), |
| 1502 | Err(OptionError::invalid_arg_value(setting)) |
| 1503 | ); |
| 1504 | |
| 1505 | // Invalid config value. |
| 1506 | let bad_cfg = "bad".to_string(); |
| 1507 | assert_eq!( |
| 1508 | parse_legend_position(None, Some(&bad_cfg), None, setting, None), |
| 1509 | Err(OptionError::invalid_config_value(setting)) |
| 1510 | ); |
| 1511 | } |
| 1512 | |
| 1513 | #[test] |
| 1514 | fn matches_human_times() { |
| 1515 | let config = Config::default(); |
| 1516 | |
| 1517 | { |
| 1518 | let delta_args = vec!["btm", "--time_delta", "2 min"]; |
| 1519 | let args = BottomArgs::parse_from(delta_args); |
| 1520 | |
| 1521 | assert_eq!( |
| 1522 | get_time_interval(&args, &config, 60 * 60 * 1000), |
| 1523 | Ok(2 * 60 * 1000) |
| 1524 | ); |
| 1525 | } |
| 1526 | |
| 1527 | { |
| 1528 | let default_time_args = vec!["btm", "--default_time_value", "300s"]; |
| 1529 | let args = BottomArgs::parse_from(default_time_args); |
| 1530 | |
| 1531 | assert_eq!( |
| 1532 | get_default_time_value(&args, &config, 60 * 60 * 1000), |
| 1533 | Ok(5 * 60 * 1000) |
| 1534 | ); |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | #[test] |
| 1539 | fn matches_number_times() { |
| 1540 | let config = Config::default(); |
| 1541 | |
| 1542 | { |
| 1543 | let delta_args = vec!["btm", "--time_delta", "120000"]; |
| 1544 | let args = BottomArgs::parse_from(delta_args); |
| 1545 | |
| 1546 | assert_eq!( |
| 1547 | get_time_interval(&args, &config, 60 * 60 * 1000), |
| 1548 | Ok(2 * 60 * 1000) |
| 1549 | ); |
| 1550 | } |
| 1551 | |
| 1552 | { |
| 1553 | let default_time_args = vec!["btm", "--default_time_value", "300000"]; |
| 1554 | let args = BottomArgs::parse_from(default_time_args); |
| 1555 | |
| 1556 | assert_eq!( |
| 1557 | get_default_time_value(&args, &config, 60 * 60 * 1000), |
| 1558 | Ok(5 * 60 * 1000) |
| 1559 | ); |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | #[test] |
| 1564 | fn config_human_times() { |
| 1565 | let args = BottomArgs::parse_from(["btm"]); |
| 1566 | |
| 1567 | let mut config = Config::default(); |
| 1568 | let flags = GeneralConfig { |
| 1569 | time_delta: Some("2 min".to_string().into()), |
| 1570 | default_time_value: Some("300s".to_string().into()), |
| 1571 | rate: Some("1s".to_string().into()), |
| 1572 | retention: Some("10m".to_string().into()), |
| 1573 | ..Default::default() |
| 1574 | }; |
| 1575 | |
| 1576 | config.flags = Some(flags); |
| 1577 | |
| 1578 | assert_eq!( |
| 1579 | get_time_interval(&args, &config, 60 * 60 * 1000), |
| 1580 | Ok(2 * 60 * 1000) |
| 1581 | ); |
| 1582 | |
| 1583 | assert_eq!( |
| 1584 | get_default_time_value(&args, &config, 60 * 60 * 1000), |
| 1585 | Ok(5 * 60 * 1000) |
| 1586 | ); |
| 1587 | |
| 1588 | assert_eq!(get_update_rate(&args, &config), Ok(1000)); |
| 1589 | |
| 1590 | assert_eq!(get_retention(&args, &config), Ok(600000)); |
| 1591 | } |
| 1592 | |
| 1593 | #[test] |
| 1594 | fn config_number_times_as_string() { |
| 1595 | let args = BottomArgs::parse_from(["btm"]); |
| 1596 | |
| 1597 | let mut config = Config::default(); |
| 1598 | let flags = GeneralConfig { |
| 1599 | time_delta: Some("120000".to_string().into()), |
| 1600 | default_time_value: Some("300000".to_string().into()), |
| 1601 | rate: Some("1000".to_string().into()), |
| 1602 | retention: Some("600000".to_string().into()), |
| 1603 | ..Default::default() |
| 1604 | }; |
| 1605 | |
| 1606 | config.flags = Some(flags); |
| 1607 | |
| 1608 | assert_eq!( |
| 1609 | get_time_interval(&args, &config, 60 * 60 * 1000), |
| 1610 | Ok(2 * 60 * 1000) |
| 1611 | ); |
| 1612 | |
| 1613 | assert_eq!( |
| 1614 | get_default_time_value(&args, &config, 60 * 60 * 1000), |
| 1615 | Ok(5 * 60 * 1000) |
| 1616 | ); |
| 1617 | |
| 1618 | assert_eq!(get_update_rate(&args, &config), Ok(1000)); |
| 1619 | |
| 1620 | assert_eq!(get_retention(&args, &config), Ok(600000)); |
| 1621 | } |
| 1622 | |
| 1623 | #[test] |
| 1624 | fn config_number_times_as_num() { |
| 1625 | let args = BottomArgs::parse_from(["btm"]); |
| 1626 | |
| 1627 | let mut config = Config::default(); |
| 1628 | let flags = GeneralConfig { |
| 1629 | time_delta: Some(120000.into()), |
| 1630 | default_time_value: Some(300000.into()), |
| 1631 | rate: Some(1000.into()), |
| 1632 | retention: Some(600000.into()), |
| 1633 | ..Default::default() |
| 1634 | }; |
| 1635 | |
| 1636 | config.flags = Some(flags); |
| 1637 | |
| 1638 | assert_eq!( |
| 1639 | get_time_interval(&args, &config, 60 * 60 * 1000), |
| 1640 | Ok(2 * 60 * 1000) |
| 1641 | ); |
| 1642 | |
| 1643 | assert_eq!( |
| 1644 | get_default_time_value(&args, &config, 60 * 60 * 1000), |
| 1645 | Ok(5 * 60 * 1000) |
| 1646 | ); |
| 1647 | |
| 1648 | assert_eq!(get_update_rate(&args, &config), Ok(1000)); |
| 1649 | |
| 1650 | assert_eq!(get_retention(&args, &config), Ok(600000)); |
| 1651 | } |
| 1652 | |
| 1653 | fn create_app(args: BottomArgs) -> App { |
| 1654 | let config = Config::default(); |
| 1655 | super::init_app(args, config).unwrap().0 |
| 1656 | } |
| 1657 | |
| 1658 | // TODO: There's probably a better way to create clap options AND unify together |
| 1659 | // to avoid the possibility of typos/mixing up. Use proc macros to unify on |
| 1660 | // one struct? |
| 1661 | #[test] |
| 1662 | fn verify_cli_options_build() { |
| 1663 | let app = crate::args::build_cmd(); |
| 1664 | |
| 1665 | let default_app = create_app(BottomArgs::parse_from(["btm"])); |
| 1666 | |
| 1667 | // Skip battery since it's tricky to test depending on the platform/features |
| 1668 | // we're testing with. |
| 1669 | let skip = ["help", "version", "celsius", "battery", "generate_schema"]; |
| 1670 | |
| 1671 | for arg in app.get_arguments().collect::<Vec<_>>() { |
| 1672 | let arg_name = arg |
| 1673 | .get_long_and_visible_aliases() |
| 1674 | .unwrap() |
| 1675 | .first() |
| 1676 | .unwrap() |
| 1677 | .to_owned(); |
| 1678 | |
| 1679 | if !arg.get_action().takes_values() && !skip.contains(&arg_name) { |
| 1680 | let arg = format!("--{arg_name}"); |
| 1681 | |
| 1682 | let arguments = vec!["btm", &arg]; |
| 1683 | let args = BottomArgs::parse_from(arguments); |
| 1684 | let testing_app = create_app(args); |
| 1685 | |
| 1686 | if (default_app.app_config_fields == testing_app.app_config_fields) |
| 1687 | && default_app.is_expanded == testing_app.is_expanded |
| 1688 | && default_app |
| 1689 | .states |
| 1690 | .proc_state |
| 1691 | .widget_states |
| 1692 | .iter() |
| 1693 | .zip(testing_app.states.proc_state.widget_states.iter()) |
| 1694 | .all(|(a, b)| a.1.test_equality(b.1)) |
| 1695 | { |
| 1696 | panic!("failed on {arg_name}"); |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | /// This one has slightly more complex behaviour due to `dirs` not |
| 1703 | /// respecting XDG on macOS, so we manually handle it. However, to |
| 1704 | /// ensure backwards-compatibility, we also have to do some special cases. |
| 1705 | #[cfg(target_os = "macos")] |
| 1706 | #[test] |
| 1707 | fn test_get_config_path_macos() { |
| 1708 | use std::path::PathBuf; |
| 1709 | |
| 1710 | use super::{DEFAULT_CONFIG_FILE_LOCATION, get_config_path}; |
| 1711 | |
| 1712 | // Case three: no previous config, no XDG var. |
| 1713 | // SAFETY: This is fine, this is just a test, and no other test affects env |
| 1714 | // vars. |
| 1715 | unsafe { |
| 1716 | std::env::remove_var("XDG_CONFIG_HOME"); |
| 1717 | } |
| 1718 | |
| 1719 | let case_1 = dirs::config_dir() |
| 1720 | .map(|mut path| { |
| 1721 | path.push(DEFAULT_CONFIG_FILE_LOCATION); |
| 1722 | path |
| 1723 | }) |
| 1724 | .unwrap(); |
| 1725 | |
| 1726 | // Skip this test if the file already exists. |
| 1727 | if !case_1.exists() { |
| 1728 | assert_eq!(get_config_path(None), Some(case_1)); |
| 1729 | } |
| 1730 | |
| 1731 | // Case two: no previous config, XDG var exists. |
| 1732 | // SAFETY: This is fine, this is just a test, and no other test affects env |
| 1733 | // vars. |
| 1734 | unsafe { |
| 1735 | std::env::set_var("XDG_CONFIG_HOME", "/tmp"); |
| 1736 | } |
| 1737 | let mut case_2 = PathBuf::new(); |
| 1738 | case_2.push("/tmp"); |
| 1739 | case_2.push(DEFAULT_CONFIG_FILE_LOCATION); |
| 1740 | |
| 1741 | // Skip this test if the file already exists. |
| 1742 | if !case_2.exists() { |
| 1743 | assert_eq!(get_config_path(None), Some(case_2)); |
| 1744 | } |
| 1745 | |
| 1746 | // Case one: old non-XDG exists already, XDG var exists. |
| 1747 | // let case_3 = case_1; |
| 1748 | // assert_eq!(get_config_path(None), Some(case_1)); |
| 1749 | } |
| 1750 | } |