Skip to content

Commit

Permalink
Merge pull request #10 from ziriuz84/8-format-weather-time
Browse files Browse the repository at this point in the history
8 format weather time
  • Loading branch information
ziriuz84 authored May 2, 2024
2 parents 5cdb72b + f18d84f commit 7b36c6b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 13 deletions.
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
18 changes: 17 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{Duration, NaiveDateTime};
use ratatui::{
layout::Alignment,
style::{Color, Style},
Expand Down Expand Up @@ -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![
Expand All @@ -172,14 +185,17 @@ fn create_table(data: &ForecastResponse) -> Table {
.map(Cell::from)
.collect::<Row>()
.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
.dataseries
.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()),
Expand Down
65 changes: 61 additions & 4 deletions src/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,7 +30,7 @@ pub struct Forecast {
#[derive(Debug, Deserialize)]
pub struct ForecastResponse {
product: String,
init: String,
pub init: String,
pub dataseries: Vec<Forecast>,
}

Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 7b36c6b

Please sign in to comment.