Skip to content

Commit

Permalink
WIP: Conditional map
Browse files Browse the repository at this point in the history
See #1774
  • Loading branch information
bim9262 committed Jul 28, 2023
1 parent 521ea25 commit 2e90330
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/blocks/amd_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let info = read_gpu_info(&config.device).await?;

widget.set_values(map! {
widget.set_values(map_required!(format, {
"icon" => Value::icon(api.get_icon("gpu")?),
"utilization" => Value::percents(info.utilization_percents),
"vram_total" => Value::bytes(info.vram_total_bytes),
"vram_used" => Value::bytes(info.vram_used_bytes),
"vram_used_percents" => Value::percents(info.vram_used_bytes / info.vram_total_bytes * 100.0),
});
}));

widget.state = match info.utilization_percents {
x if x > 90.0 => State::Critical,
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
1 => format_singular.clone(),
_ => format.clone(),
});
widget.set_values(map!(
widget.set_values(map_required!(format, {
"count" => Value::number(count),
"icon" => Value::icon(api.get_icon("update")?)
));
}));

let warning = warning_updates_regex
.as_ref()
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/backlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
if config.invert_icons {
icon_value = 1.0 - icon_value;
}
widget.set_values(map! {
widget.set_values(map_required!(format, {
"icon" => Value::icon(api.get_icon_in_progression("backlight", icon_value)?),
"brightness" => Value::percents((brightness * 100.0).round())
});
}));
api.set_widget(widget).await?;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/blocks/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,12 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
),
};

values.insert(
"icon".into(),
Value::icon(api.get_icon_in_progression(icon_name, icon_value)?),
);
if format.contains_key("icon") {
values.insert(
"icon".into(),
Value::icon(api.get_icon_in_progression(icon_name, icon_value)?),
);
}

widget.set_values(values);
widget.state = state;
Expand All @@ -213,7 +215,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let mut widget = Widget::new()
.with_format(missing_format.clone())
.with_state(State::Critical);
widget.set_values(map!("icon" => Value::icon(api.get_icon("bat_not_available")?)));
widget.set_values(map_required!(missing_format, {"icon" => Value::icon(api.get_icon("bat_not_available")?)}));
api.set_widget(widget).await?;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/blocks/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let mut widget = Widget::new();

let values = map! {
let values = map_required!(format, {
"icon" => Value::icon(api.get_icon(device.icon)?),
"name" => Value::text(device.name),
"available" => Value::flag(),
[if let Some(p) = device.battery_percentage] "percentage" => Value::percents(p),
[if let Some(p) = device.battery_percentage]
"battery_icon" => Value::icon(api.get_icon_in_progression("bat", p as f64 / 100.0)?),
};
});

if device.connected {
widget.set_format(format.clone());
Expand All @@ -135,7 +135,9 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
None => {
debug!("Showing device as unavailable");
let mut widget = Widget::new().with_format(disconnected_format.clone());
widget.set_values(map!("icon" => Value::icon(api.get_icon("bluetooth")?)));
widget.set_values(
map_required!(format, {"icon" => Value::icon(api.get_icon("bluetooth")?)}),
);
api.set_widget(widget).await?;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/blocks/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
Ok(Value::icon(api.get_icon(icon)?).with_instance(instance))
};

let values = map! {
let values = map_required!(format, {
"icon" => Value::icon(api.get_icon("music")?),
"next" => new_btn("music_next", NEXT_BTN, api)?,
"prev" => new_btn("music_prev", PREV_BTN, api)?,
};
});

let preferred_players = match config.player.clone() {
PlayerName::Single(name) => vec![name],
Expand Down Expand Up @@ -336,7 +336,9 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
}
None => {
let mut widget = Widget::new().with_format(format.clone());
widget.set_values(map!("icon" => Value::icon(api.get_icon("music")?)));
widget.set_values(
map_required!(format, {"icon" => Value::icon(api.get_icon("music")?)}),
);
api.set_widget(widget).await?;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ macro_rules! map {

pub use map;

#[macro_export]
macro_rules! map_required {
($format: ident, {$( $([$($cond_tokens:tt)*])? $key:literal => $value:expr ),* $(,)?}) => {{
#[allow(unused_mut)]
let mut m = ::std::collections::HashMap::new();
$(
if $format.contains_key($key){
map!(@insert m, $key, $value $(,$($cond_tokens)*)?);
}
)*
m
}};
}

pub use map_required;

macro_rules! regex {
($re:literal $(,)?) => {{
static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
Expand Down

0 comments on commit 2e90330

Please sign in to comment.