Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add format_alt to temperature and remove collapsed #1640

Merged
merged 9 commits into from
Oct 13, 2022
41 changes: 20 additions & 21 deletions src/blocks/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
//! Key | Values | Default
//! ----|--------|--------
//! `format` | A string to customise the output of this block. See below for available placeholders | <code>" $icon{ $average avg, $max max&vert;} "</code>
madhur marked this conversation as resolved.
Show resolved Hide resolved
//! `format_alt` | If set, block will switch between `format` and `format_alt` on every click | `None`
//! `interval` | Update interval in seconds | `5`
//! `collapsed` | Whether the block will be collapsed by default | `true`
//! `scale` | Either `"celsius"` or `"fahrenheit"` | `"celsius"`
//! `good` | Maximum temperature to set state to good | `20` °C (`68` °F)
//! `idle` | Maximum temperature to set state to idle | `45` °C (`113` °F)
Expand Down Expand Up @@ -64,10 +64,9 @@ const DEFAULT_WARN: f64 = 80.0;
#[serde(deny_unknown_fields, default)]
struct TemperatureConfig {
format: FormatConfig,
format_alt: Option<FormatConfig>,
#[default(5.into())]
interval: Seconds,
#[default(true)]
collapsed: bool,
scale: TemperatureScale,
good: Option<f64>,
idle: Option<f64>,
Expand Down Expand Up @@ -97,12 +96,13 @@ impl TemperatureScale {

pub async fn run(config: toml::Value, mut api: CommonApi) -> Result<()> {
let config = TemperatureConfig::deserialize(config).config_error()?;
let mut collapsed = config.collapsed;
let mut widget = Widget::new().with_format(
config
.format
.with_default(" $icon{ $average avg, $max max|} ")?,
);

let mut format = config.format.with_default(" $icon{ $average avg, $max max|} ")?;
madhur marked this conversation as resolved.
Show resolved Hide resolved
let mut format_alt = match config.format_alt {
Some(f) => Some(f.with_default("")?),
None => None,
};
let mut widget = Widget::new().with_format(format.clone());

let good = config
.good
Expand Down Expand Up @@ -183,16 +183,12 @@ pub async fn run(config: toml::Value, mut api: CommonApi) -> Result<()> {
};

'outer: loop {
if collapsed {
widget.set_values(map!("icon" => Value::icon(api.get_icon("thermometer")?)));
} else {
widget.set_values(map! {
"icon" => Value::icon(api.get_icon("thermometer")?),
"average" => Value::degrees(avg_temp),
"min" => Value::degrees(min_temp),
"max" => Value::degrees(max_temp),
});
}
widget.set_values(map! {
"icon" => Value::icon(api.get_icon("thermometer")?),
"average" => Value::degrees(avg_temp),
"min" => Value::degrees(min_temp),
"max" => Value::degrees(max_temp),
});

api.set_widget(&widget).await?;

Expand All @@ -203,8 +199,11 @@ pub async fn run(config: toml::Value, mut api: CommonApi) -> Result<()> {
UpdateRequest => break 'outer,
Click(click) => {
if click.button == MouseButton::Left {
collapsed = !collapsed;
break;
if let Some(ref mut format_alt) = format_alt {
std::mem::swap(format_alt, &mut format);
widget.set_format(format.clone());
break;
}
}
}
}
Expand Down