Compare commits

..

No commits in common. "850c629eca7471d20191e2ea088b744c7be61b20" and "6cbd5a48ca98e55825490bf45cf7d2134c290923" have entirely different histories.

View file

@ -16,13 +16,13 @@ pub fn run_calendar(args: &CliArgs) {
}
let (mut moonrise, mut moonset, mut moonday, mut sunrise, mut sunset): (
Option<i32>,
Option<i32>,
Option<i32>,
Option<i32>,
Option<i32>,
Option<(u32, u32)>,
Option<(u32, u32)>,
Option<(u32, u32)>,
Option<(u32, u32)>,
Option<(u32, u32)>,
) = (None, None, None, None, None);
let mut moonphase: Option<u32> = None;
let moonphase: Option<u32> = None;
let mut false_empty_count = 0;
loop {
@ -42,13 +42,13 @@ pub fn run_calendar(args: &CliArgs) {
let value = buffer[21..].to_string();
if value.starts_with("Moonrise") {
moonrise = parse_time(value[8..].trim());
moonrise = parse_time(value[8..].trim(), args.debug);
} else if value.starts_with("Moonset") {
moonset = parse_time(value[7..].trim());
moonset = parse_time(value[7..].trim(), args.debug);
} else if value.starts_with("Moonday") {
moonday = parse_time(value[7..].trim());
moonday = parse_time(value[7..].trim(), args.debug);
} else if value.starts_with("Moonphase") {
moonphase = match value[9..].trim()[0..2].parse::<u32>() {
let moonphase = match value[9..].trim()[0..2].parse::<u32>() {
Ok(val) => Some(val),
Err(e) => {
if args.debug {
@ -58,9 +58,9 @@ pub fn run_calendar(args: &CliArgs) {
}
};
} else if value.starts_with("Sunrise") {
sunrise = parse_time(value[7..].trim());
sunrise = parse_time(value[7..].trim(), args.debug);
} else if value.starts_with("Sunset") {
sunset = parse_time(value[6..].trim());
sunset = parse_time(value[6..].trim(), args.debug);
} else {
continue;
}
@ -79,56 +79,55 @@ pub fn run_calendar(args: &CliArgs) {
let mut output_state = CalandarState {
has_bg: false,
is_sun: false,
gradient_angle_percentage: 0.0,
moonphase: 0,
gradient_angle: 0,
};
if moonphase.is_some() {
output_state.moonphase = moonphase.unwrap();
}
let now_mins = (Local::now().hour() * 60 +Local::now().minute()) as i32;
let (now_hr, now_min) = (Local::now().hour(), Local::now().minute());
if sunrise.is_none() {
sunrise = Some(8 * 60 + 0);
sunrise = Some((8, 0));
}
if sunset.is_none() {
sunset = Some(18 * 60 + 0);
sunset = Some((18, 0));
}
let sun_rise = sunrise.clone().unwrap();
let sun_set = sunset.clone().unwrap();
if now_mins > sun_rise && now_mins < sun_set {
if now_hr > sun_rise.0 && now_min > sun_rise.1 && now_hr < sun_set.0 && now_min < sun_set.1 {
// it is after sunrise, and before sunset
output_state.has_bg = true;
output_state.is_sun = true;
output_state.gradient_angle_percentage = {
(now_mins - sun_rise) as f32
/ (sun_set - sun_rise) as f32
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
};
} else {
// Check if the moon is up
if moonrise.is_some() {
let moon_rise = moonrise.clone().unwrap();
let (moonrise_hr, moonrise_min) = moonrise.clone().unwrap();
if moonset.is_some() {
let moon_set = moonset.clone().unwrap();
if now_mins > moon_rise
&& now_mins < moon_set
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
{
// moon is up
output_state.has_bg = true;
output_state.is_sun = false;
}
} else {
if now_mins > moon_rise {
if now_hr > moonrise_hr && now_min > moonrise_hr {
output_state.has_bg = true;
output_state.is_sun = false;
}
}
} else {
if moonset.is_some() {
let moon_set = moonset.clone().unwrap();
if now_mins < moon_set {
let (moonset_hr, moonset_min) = moonset.clone().unwrap();
if now_hr < moonset_hr && now_min < moonset_min {
// moon is up
output_state.has_bg = true;
output_state.is_sun = false;
@ -139,19 +138,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 moon_rise = moonrise.clone().unwrap();
let moon_day = moonday.clone().unwrap();
let (moonrise_hr, moonrise_min) = moonrise.clone().unwrap();
let (moonday_hrs, moonday_mins) = moonday.clone().unwrap();
output_state.gradient_angle_percentage = {
(now_mins - moon_rise) as f32
/ moon_day as f32
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
};
} else {
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;
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;
}
}
@ -160,29 +159,34 @@ pub fn run_calendar(args: &CliArgs) {
println!("{}", serde_json::to_string(&output_state).unwrap());
}
fn parse_time(i: &str) -> Option<i32> {
fn parse_time(i: &str, do_debug: bool) -> Option<(u32, u32)> {
if do_debug {
debug!("parsing: {}", i);
}
let hr = match i[0..2].parse::<u32>() {
Ok(val) => val as i32,
Ok(val) => val,
Err(e) => {
if do_debug {
debug!("Error parsing: {e}");
}
return None;
}
};
let min = match i[3..5].parse::<u32>() {
Ok(val) => val as i32,
Ok(val) => val,
Err(e) => {
if do_debug {
debug!("Error parsing: {e}");
}
return None;
}
};
Some(hr * 60 + min)
Some((hr, min))
}
#[derive(Serialize, Deserialize)]
struct CalandarState {
pub has_bg: bool,
pub is_sun: bool,
pub gradient_angle_percentage: f32,
pub moonphase: u32,
pub gradient_angle: i32,
}