From bb17a820ec61b24e937b3d13f443d48ae7fb0c8b Mon Sep 17 00:00:00 2001 From: Sirio Negri Date: Thu, 2 May 2024 13:30:30 +0200 Subject: [PATCH 1/2] Adds chrono and updates dependencies --- Cargo.toml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 16c27be..2208a6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,10 +9,11 @@ edition = "2021" config = "0.14.0" crossterm = "0.27.0" dirs = "5.0.1" -ratatui = "0.26.1" -reqwest = {version = "0.12.1", features = ["blocking","json"]} +ratatui = "0.26.2" +reqwest = {version = "0.12.4", features = ["blocking","json"]} scraper = "0.19.0" -serde = "1.0.197" -serde_derive = "1.0.197" -serde_json = "1.0.115" -serde_repr = "0.1.18" +serde = "1.0.200" +serde_derive = "1.0.200" +serde_json = "1.0.116" +serde_repr = "0.1.19" +chrono = "0.4.38" From f18d84ff55e0f68aa2e8ad02ad74b81c401dfc0e Mon Sep 17 00:00:00 2001 From: Sirio Negri Date: Thu, 2 May 2024 14:17:10 +0200 Subject: [PATCH 2/2] Formats time and fixes rh2m visualization --- src/main.rs | 2 -- src/ui.rs | 18 +++++++++++++- src/weather.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index b1ea668..c27ad87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,6 @@ use ratatui::Terminal; use std::io; fn main() -> AppResult<()> { - println!("{:?}", dirs::config_local_dir()); - // Create an application. let mut app = App::new(); diff --git a/src/ui.rs b/src/ui.rs index ac770fb..984f1ca 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,3 +1,4 @@ +use chrono::{Duration, NaiveDateTime}; use ratatui::{ layout::Alignment, style::{Color, Style}, @@ -155,6 +156,18 @@ fn render_scheduling_menu( ); } +fn weather_time(time_init: &str, delta_t: i8) -> String { + // Parse the initial time string into a NaiveDateTime + let time_start: NaiveDateTime = + NaiveDateTime::parse_from_str(time_init, "%Y%m%d%H%M").expect("Invalid time format"); + + // Add the delta_t hours to the initial time + let time = time_start + Duration::hours(delta_t as i64); + + // Format the resulting time as a string + time.format("%d/%m %H:%M").to_string() +} + fn create_table(data: &ForecastResponse) -> Table { // Create the table header let header = vec![ @@ -172,6 +185,9 @@ fn create_table(data: &ForecastResponse) -> Table { .map(Cell::from) .collect::() .style(Style::default().add_modifier(Modifier::BOLD)); + let mut time_start: String = data.init.clone(); + let minutes: &str = "00"; + time_start.push_str(minutes); // Create table rows let rows = data @@ -179,7 +195,7 @@ fn create_table(data: &ForecastResponse) -> Table { .iter() .map(|forecast| { Row::new(vec![ - Cell::from(forecast.timepoint.to_string()), + Cell::from(weather_time(&time_start, forecast.timepoint)), Cell::from(forecast.cloud_cover.to_string()), // Assuming CloudCover, Seeing, Transparency, Wind10m have a to_string() method Cell::from(forecast.seeing.to_string()), Cell::from(forecast.transparency.to_string()), diff --git a/src/weather.rs b/src/weather.rs index da982b2..f8ef296 100644 --- a/src/weather.rs +++ b/src/weather.rs @@ -21,7 +21,7 @@ pub struct Forecast { pub seeing: Seeing, pub transparency: Transparency, pub lifted_index: LiftedIndex, - pub rh2m: i8, + pub rh2m: RH2m, pub wind10m: Wind10m, pub temp2m: i8, pub prec_type: String, @@ -30,7 +30,7 @@ pub struct Forecast { #[derive(Debug, Deserialize)] pub struct ForecastResponse { product: String, - init: String, + pub init: String, pub dataseries: Vec, } @@ -218,6 +218,65 @@ pub fn get_rh2m_value(index: i8) -> Option<&'static str> { ]); rh2m.get(&index).cloned() } +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize_repr)] +#[repr(i8)] +pub enum RH2m { + ZeroFive = -4, + FiveTen = -3, + TenFifteen = -2, + FifteenTwenty = -1, + TwentyTwentyFive = 0, + TwentyFiveThirty = 1, + ThirtyThirtyFive = 2, + ThirtyFiveForty = 3, + FortyFortyFive = 4, + FortyFiveFifty = 5, + FiftyFiftyFive = 6, + FiftyFiveSixty = 7, + SixtySixtyFive = 8, + SixtyFiveSeventy = 9, + SeventySeventyFive = 10, + SeventyFiveEighty = 11, + EightyEightyFive = 12, + EightyFiveNinety = 13, + NinetyNinetyFive = 14, + NinetyFiveNinetyNine = 15, + NinetyNineHundred = 16, +} + +impl RH2m { + pub const fn to_str(self) -> &'static str { + match self { + RH2m::ZeroFive => "0%-5%", + RH2m::FiveTen => "5%-10%", + RH2m::TenFifteen => "10%-15%", + RH2m::FifteenTwenty => "15%-20%", + RH2m::TwentyTwentyFive => "20%-25%", + RH2m::TwentyFiveThirty => "25%-30%", + RH2m::ThirtyThirtyFive => "30%-35%", + RH2m::ThirtyFiveForty => "35%-40%", + RH2m::FortyFortyFive => "40%-45%", + RH2m::FortyFiveFifty => "45%-50%", + RH2m::FiftyFiftyFive => "50%-55%", + RH2m::FiftyFiveSixty => "55%-60%", + RH2m::SixtySixtyFive => "60%-65%", + RH2m::SixtyFiveSeventy => "65%-70%", + RH2m::SeventySeventyFive => "70%-75%", + RH2m::SeventyFiveEighty => "75%-80%", + RH2m::EightyEightyFive => "80%-85%", + RH2m::EightyFiveNinety => "85%-90%", + RH2m::NinetyNinetyFive => "90%-95%", + RH2m::NinetyFiveNinetyNine => "95%-99%", + RH2m::NinetyNineHundred => "100%", + } + } +} + +impl Display for RH2m { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.to_str()) + } +} #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize_repr)] #[repr(u8)] @@ -296,14 +355,12 @@ mod test { #[test] fn test_get_forecast() { - println!("{}", get_forecast()); assert!(get_forecast().contains("astro")); } #[test] fn test_prepare_data() { let data = prepare_data().unwrap(); - println!("{:?}", data); assert_eq!(data.product, "astro"); } }