Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions src/config/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ struct ConfigWrapper<'a> {
colors: Color<'a>,
#[serde(default)]
logo: super::LogoStyle<'a>,
#[serde(default)]
parameters: Option<InfoConfig<'a>>,
}

#[derive(Debug, Deserialize)]
struct InfoConfig<'a> {
#[serde(default, borrow)]
disks: Option<DisksInfoConfig<'a>>,
}

#[derive(Debug, Deserialize)]
struct DisksInfoConfig<'a> {
#[serde(default, borrow)]
exclude: Option<Vec<&'a str>>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -133,23 +147,27 @@ impl<'de: 'static> serde::Deserialize<'de> for super::Config {
header,
value: format,
separator,
} => super::Entry::Info {
kind,
fields: kind
.get_fields()
.iter()
.filter(|field| {
format
.unwrap_or_else(|| kind.default_format())
.contains(&field.to_string())
})
.copied()
.collect::<Vec<_>>(),
header: header
.unwrap_or_else(|| language_func(kind.default_header())),
format: format.unwrap_or_else(|| kind.default_format()),
separator: separator.unwrap_or_else(|| language_func("_colon_")),
},
} => {
let header =
header.unwrap_or_else(|| language_func(kind.default_header()));
let format = format.unwrap_or_else(|| kind.default_format());
super::Entry::Info {
kind,
fields: kind
.get_fields()
.iter()
.filter(|field| {
let field_str = field.as_str();
header.contains(field_str) || format.contains(field_str)
})
.copied()
.collect::<Vec<_>>(),
header,
format,
separator: separator
.unwrap_or_else(|| language_func("_colon_")),
}
}
Entry::Separator {
separator: content,
sizing: Some(sizing @ SeparatorSizing::Fixed(size)),
Expand Down Expand Up @@ -188,6 +206,17 @@ impl<'de: 'static> serde::Deserialize<'de> for super::Config {
.collect::<Vec<_>>()
})
.unwrap_or_else(|| super::default_entries(config.language)),
parameters: config
.parameters
.map(|info| super::InfoConfig {
disks: info
.disks
.map(|disks| super::DisksInfoConfig {
exclude: disks.exclude.unwrap_or_default(),
})
.unwrap_or_default(),
})
.unwrap_or_default(),
})
}
}
28 changes: 26 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ pub struct Config {
pub entries: Vec<Entry<'static>>,
pub colors: ColorOption,
pub logo: LogoStyle<'static>,
pub parameters: InfoConfig<'static>,
}

#[derive(Debug, Default, Decode, Encode)]
pub struct InfoConfig<'a> {
pub disks: DisksInfoConfig<'a>,
}

#[derive(Debug, Decode, Encode)]
pub struct DisksInfoConfig<'a> {
pub exclude: Vec<&'a str>,
}

impl<'a> Default for DisksInfoConfig<'a> {
fn default() -> Self {
Self {
exclude: vec!["/boot", "/etc", "/snapd", "/docker"],
}
}
}

#[derive(Debug, Decode, Encode)]
Expand Down Expand Up @@ -88,15 +107,19 @@ impl<'a> Entry<'a> {
separator: Option<&'a str>,
) -> Self {
let format = kind.default_format();
let header = header.unwrap_or_else(|| language_func(kind.default_header()));
Self::Info {
kind,
header: header.unwrap_or_else(|| language_func(kind.default_header())),
header,
format,
separator: separator.unwrap_or_else(|| language_func("_colon_")),
fields: kind
.get_fields()
.iter()
.filter(|field| format.contains(&field.to_string()))
.filter(|field| {
let field_str = field.as_str();
format.contains(field_str) || header.contains(field_str)
})
.copied()
.collect(),
}
Expand Down Expand Up @@ -162,6 +185,7 @@ impl Default for Config {
entries: default_entries(Locale::default()),
colors: ColorOption::default(),
logo: LogoStyle::default(),
parameters: InfoConfig::default(),
}
}
}
Expand Down
41 changes: 25 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use afetch::error::{ErrorType, FetchInfosError};
use afetch::logos::get_logo;
use afetch::system::battery::get_battery;
use afetch::system::cpu::get_cpu;
use afetch::system::disk::get_disk;
use afetch::system::disks::get_disks;
use afetch::system::host::get_hostname;
use afetch::system::kernel::get_kernel;
use afetch::system::loadavg::get_loadavg;
Expand All @@ -27,18 +29,20 @@ fn main() -> Result<(), FetchInfosError> {
.entries
.par_iter()
.filter_map(|element| match element {
Entry::Info { kind, .. } => match kind {
InfoKind::Battery => Some(get_battery as InfoFunction),
InfoKind::Cpu => Some(get_cpu as InfoFunction),
InfoKind::Host => Some(get_hostname as InfoFunction),
InfoKind::Kernel => Some(get_kernel as InfoFunction),
InfoKind::Uptime => Some(get_uptime as InfoFunction),
InfoKind::Memory => Some(get_memory as InfoFunction),
InfoKind::Loadavg => Some(get_loadavg as InfoFunction),
Entry::Info { kind, fields, .. } => match kind {
InfoKind::Battery => Some((get_battery as InfoFunction, fields)),
InfoKind::Cpu => Some((get_cpu as InfoFunction, fields)),
InfoKind::Disk => Some((get_disk as InfoFunction, fields)),
InfoKind::Disks => Some((get_disks as InfoFunction, fields)),
InfoKind::Host => Some((get_hostname as InfoFunction, fields)),
InfoKind::Kernel => Some((get_kernel as InfoFunction, fields)),
InfoKind::Uptime => Some((get_uptime as InfoFunction, fields)),
InfoKind::Memory => Some((get_memory as InfoFunction, fields)),
InfoKind::Loadavg => Some((get_loadavg as InfoFunction, fields)),
},
_ => None,
})
.map(|f| f(language_func))
.map(|(f, fields)| f(language_func, fields, &config))
.collect();

let logo = if supports_unicode() {
Expand Down Expand Up @@ -78,8 +82,9 @@ fn main() -> Result<(), FetchInfosError> {

let mut output: String = String::default();
let mut last_info_len = 0;
let mut i = 0;
let mut i2 = 0;
for (i, entry) in config.entries.iter().enumerate() {
for entry in &config.entries {
let mut write_entry = |entry: String| {
#[cfg(feature = "image")]
if matches!(config.logo, LogoStyle::Image { .. }) {
Expand All @@ -98,6 +103,8 @@ fn main() -> Result<(), FetchInfosError> {
if logo.is_none() {
writeln!(output, "{entry}").ok();
}

i += 1;
};

match entry {
Expand All @@ -122,19 +129,21 @@ fn main() -> Result<(), FetchInfosError> {
};

let mut format_and_write = |info: &InfoGroup| {
let mut formatted_header = (*header).to_owned();
let mut formatted_info = (*value).to_owned();
for value in &info.values {
formatted_info =
formatted_info.replace(&format!("{{{}}}", value.field), &value.value);
let placeholder = format!("{{{}}}", value.field);
formatted_header = formatted_header.replace(&placeholder, &value.value);
formatted_info = formatted_info.replace(&placeholder, &value.value);
}

last_info_len = count_str_length(header)
last_info_len = count_str_length(&formatted_header)
+ count_str_length(separator)
+ count_str_length(&formatted_info);

formatted_info = format!(
"{}{}{}",
header.custom_color_wrapper(header_color).bold(),
formatted_header.custom_color_wrapper(header_color).bold(),
separator.custom_color_wrapper(header_separator_color),
formatted_info.custom_color_wrapper(info_color)
);
Expand Down Expand Up @@ -183,8 +192,8 @@ fn main() -> Result<(), FetchInfosError> {
}

if let Some((_, _, lines)) = &logo {
if config.entries.len() < lines.len() {
for logo_line in &lines[config.entries.len()..] {
if i < lines.len() {
for logo_line in &lines[i..] {
writeln!(output, " {}{}", logo_line, "".white()).ok();
}
}
Expand Down
Loading