diff --git a/ewwtilities/src/calendar_background.rs b/ewwtilities/src/calendar_background.rs index d4e66f0..6bab721 100644 --- a/ewwtilities/src/calendar_background.rs +++ b/ewwtilities/src/calendar_background.rs @@ -16,11 +16,11 @@ pub fn run_calendar(args: &CliArgs) { } let (mut moonrise, mut moonset, mut moonday, mut sunrise, mut sunset): ( - Option<(u32, u32)>, - Option<(u32, u32)>, - Option<(u32, u32)>, - Option<(u32, u32)>, - Option<(u32, u32)>, + Option, + Option, + Option, + Option, + Option, ) = (None, None, None, None, None); let moonphase: Option = None; @@ -79,55 +79,51 @@ pub fn run_calendar(args: &CliArgs) { let mut output_state = CalandarState { has_bg: false, is_sun: false, - gradient_angle: 0, + gradient_angle_percentage: 0.0, }; - let (now_hr, now_min) = (Local::now().hour(), Local::now().minute()); + let now_mins = (Local::now().hour() * 60 +Local::now().minute()) as i32; if sunrise.is_none() { - sunrise = Some((8, 0)); + sunrise = Some(8 * 60 + 0); } if sunset.is_none() { - sunset = Some((18, 0)); + sunset = Some(18 * 60 + 0); } let sun_rise = sunrise.clone().unwrap(); let sun_set = sunset.clone().unwrap(); - if now_hr > sun_rise.0 && now_min > sun_rise.1 && now_hr < sun_set.0 && now_min < sun_set.1 { + if now_mins > sun_rise && now_mins < sun_set { // it is after sunrise, and before sunset output_state.has_bg = true; output_state.is_sun = true; - output_state.gradient_angle = { - let sunrise_mins = sun_rise.0 * 60 + sun_rise.1; - (((now_hr * 60 + now_min) - sunrise_mins) as f32 - / ((sun_set.0 * 60 + sun_set.1) - sunrise_mins) as f32) - .floor() as i32 + output_state.gradient_angle_percentage = { + (now_mins - sun_rise) as f32 + / (sun_set - sun_rise) as f32 }; } else { // Check if the moon is up if moonrise.is_some() { - let (moonrise_hr, moonrise_min) = moonrise.clone().unwrap(); + let moon_rise = moonrise.clone().unwrap(); if moonset.is_some() { - let (moonset_hr, moonset_min) = moonset.clone().unwrap(); - if now_hr > moonrise_hr - && now_min > moonrise_min - && now_hr < moonset_hr - && now_min < moonset_min + let moon_set = moonset.clone().unwrap(); + if now_mins > moon_rise + && now_mins < moon_set { // moon is up output_state.has_bg = true; output_state.is_sun = false; } } else { - if now_hr > moonrise_hr && now_min > moonrise_hr { + if now_mins > moon_rise { output_state.has_bg = true; output_state.is_sun = false; } } } else { if moonset.is_some() { - let (moonset_hr, moonset_min) = moonset.clone().unwrap(); - if now_hr < moonset_hr && now_min < moonset_min { + let moon_set = moonset.clone().unwrap(); + if now_mins < moon_set { // moon is up output_state.has_bg = true; output_state.is_sun = false; @@ -138,19 +134,19 @@ pub fn run_calendar(args: &CliArgs) { if output_state.has_bg == true && output_state.is_sun == false { // moon is up, get the position if moonrise.is_some() { - let (moonrise_hr, moonrise_min) = moonrise.clone().unwrap(); - let (moonday_hrs, moonday_mins) = moonday.clone().unwrap(); + let moon_rise = moonrise.clone().unwrap(); + let moon_day = moonday.clone().unwrap(); - output_state.gradient_angle = { - (((now_hr * 60 + now_min) - (moonrise_hr * 60 + moonrise_min)) as f32 - / (moonday_hrs * 60 + moonday_mins) as f32) as i32 + output_state.gradient_angle_percentage = { + (now_mins - moon_rise) as f32 + / moon_day as f32 }; } else { - let (moonset_hr, moonset_min) = moonset.clone().unwrap(); - let (moonday_hrs, moonday_mins) = moonday.clone().unwrap(); - let begin_mins: i32 = (moonset_hr as i32 - moonday_hrs as i32) * 60 + (moonset_min as i32 - moonday_mins as i32); - let time_since_rise = (now_hr * 60 + now_min) as i32 + (begin_mins.abs()); - output_state.gradient_angle = (time_since_rise as f32 / (moonset_hr * 60 + moonset_min) as f32) as i32; + let moon_set = moonset.clone().unwrap(); + let moon_day = moonday.clone().unwrap(); + let begin_mins: i32 = moon_set - moon_day; + let time_since_rise = now_mins + (begin_mins.abs()); + output_state.gradient_angle_percentage = time_since_rise as f32 / moon_set as f32; } } @@ -159,34 +155,28 @@ pub fn run_calendar(args: &CliArgs) { println!("{}", serde_json::to_string(&output_state).unwrap()); } -fn parse_time(i: &str, do_debug: bool) -> Option<(u32, u32)> { - if do_debug { - debug!("parsing: {}", i); - } +fn parse_time(i: &str, do_debug: bool) -> Option { + debug!("parsing: {}", i); let hr = match i[0..2].parse::() { - Ok(val) => val, + Ok(val) => val as i32, Err(e) => { - if do_debug { - debug!("Error parsing: {e}"); - } + debug!("Error parsing: {e}"); return None; } }; let min = match i[3..5].parse::() { - Ok(val) => val, + Ok(val) => val as i32, Err(e) => { - if do_debug { - debug!("Error parsing: {e}"); - } + debug!("Error parsing: {e}"); return None; } }; - Some((hr, min)) + Some(hr * 60 + min) } #[derive(Serialize, Deserialize)] struct CalandarState { pub has_bg: bool, pub is_sun: bool, - pub gradient_angle: i32, + pub gradient_angle_percentage: f32, }