//! How to handle config files and arguments.
// TODO: Break this apart or do something a bit smarter.
pub mod args;
pub mod colours;
pub mod config;
use std::{
convert::TryInto,
str::FromStr,
time::{Duration, Instant},
};
use anyhow::{Context, Result};
use clap::ArgMatches;
pub use colours::ConfigColours;
pub use config::Config;
use hashbrown::{HashMap, HashSet};
use indexmap::IndexSet;
use regex::Regex;
#[cfg(feature = "battery")]
use starship_battery::Manager;
use self::{
args::BottomArgs,
config::{layout::Row, IgnoreList, StringOrNum},
};
use crate::{
app::{filter::Filter, layout_manager::*, *},
canvas::{components::time_chart::LegendPosition, styling::CanvasStyling, ColourScheme},
constants::*,
data_collection::temperature::TemperatureType,
utils::{
data_units::DataUnit,
error::{self, BottomError},
},
widgets::*,
};
macro_rules! is_flag_enabled {
($flag_name:ident, $matches:expr, $config:expr) => {
if $matches.get_flag(stringify!($flag_name)) {
true
} else if let Some(flags) = &$config.flags {
flags.$flag_name.unwrap_or(false)
} else {
false
}
};
($arg:expr, $config:expr, $cfg_flag:ident) => {
if let Some(flag) = $arg {
flag
} else if let Some(flags) = &$config.flags {
flags.$cfg_flag.unwrap_or(false)
} else {
false
}
};
($cmd_flag:literal, $cfg_flag:ident, $matches:expr, $config:expr) => {
if $matches.get_flag($cmd_flag) {
true
} else if let Some(flags) = &$config.flags {
flags.$cfg_flag.unwrap_or(false)
} else {
false
}
};
}
pub fn init_app(
matches: ArgMatches, config: Config, widget_layout: &BottomLayout, default_widget_id: u64,
default_widget_type_option: &Option<BottomWidgetType>, styling: &CanvasStyling,
) -> Result<App> {
use BottomWidgetType::*;
// Since everything takes a reference, but we want to take ownership here to drop matches/config later...
let matches = &matches;
let config = &config;
let retention_ms =
get_retention(matches, config).context("Update `retention` in your config file.")?;
let autohide_time = is_flag_enabled!(autohide_time, matches, config);
let default_time_value = get_default_time_value(matches, config, retention_ms)
.context("Update 'default_time_value' in your config file.")?;
let use_basic_mode = is_flag_enabled!(basic, matches, config);
let expanded = is_flag_enabled!(expanded, matches, config);
// For processes
let is_grouped = is_flag_enabled!(group_processes, matches, config);
let is_case_sensitive = is_flag_enabled!(case_sensitive, matches, config);
let is_match_whole_word = is_flag_enabled!(whole_word, matches, config);
let is_use_regex = is_flag_enabled!(regex, matches, config);
let mut widget_map = HashMap::new();
let mut cpu_state_map: HashMap<u64, CpuWidgetState> = HashMap::new();
let mut mem_state_map: HashMap<u64,<