Skip to content

Commit

Permalink
allow using area fields in custom indicator, fix #67
Browse files Browse the repository at this point in the history
  • Loading branch information
Yo'av Moshe committed Nov 8, 2024
1 parent eca4a1a commit 2bef52f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::prelude::*;
use serde_json::{Map, Value};
use serde_json::Value;
use std::collections::HashMap;

use crate::lang::Lang;
Expand Down Expand Up @@ -70,16 +70,23 @@ pub fn format_ampm_time(day: &serde_json::Value, key: &str, ampm: bool) -> Strin
}
pub fn format_indicator(
weather_conditions: &Value,
area: &Value,
expression: String,
weather_icon: &&str,
) -> String {
if !weather_conditions.is_object() {
return String::new();
}
let default_map = Map::new();
let weather_conditions_map = weather_conditions.as_object().unwrap_or(&default_map);

let (weather_map, area_map) = match (weather_conditions.as_object(), area.as_object()) {
(Some(w), Some(a)) => (w, a),
_ => return String::new(),
};
let mut combined_map = weather_map.clone();
combined_map.extend(area_map.clone());

let mut formatted_indicator = expression.to_string();
weather_conditions_map
combined_map
.iter()
.map(|condition| ("{".to_owned() + condition.0 + "}", condition.1))
.for_each(|condition| {
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn main() {
.expect(format!("Unable to write cache file at {}", cachefile).as_str());
}
let current_condition = &weather["current_condition"][0];
let nearest_area = &weather["nearest_area"][0];
let feels_like = if args.fahrenheit {
current_condition["FeelsLikeF"].as_str().unwrap()
} else {
Expand All @@ -115,7 +116,9 @@ fn main() {
format!("{} {}", weather_icon, indicator)
}
}
Some(expression) => format_indicator(current_condition, expression, weather_icon),
Some(expression) => {
format_indicator(current_condition, nearest_area, expression, weather_icon)
}
};
data.insert("text", text);

Expand Down

0 comments on commit 2bef52f

Please sign in to comment.