use console::Term;
#[allow(deprecated)]
use std::thread::sleep_ms;
use timemachine::{Clock, TimeMachine};
#[derive(Debug, PartialEq, Clone)]
pub enum State {
NightDark,
DuskDawnRed,
DayWhite,
}
impl Default for State {
fn default() -> State {
State::DayWhite
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_client = napchart::api::BlockingClient::default();
let napchart = api_client.get("3tbkt")?;
let tm: TimeMachine<Option<napchart::ElementData>> =
TimeMachine::from_napchart(&napchart.lanes[0]);
let tm: TimeMachine<State> = tm.map_states_or_default(|elem| {
if let Some(e) = elem {
match e.color.as_str() {
"red" => Some(State::DuskDawnRed),
"gray" => Some(State::NightDark),
_ => None, }
} else {
None
}
});
let term = Term::stdout();
term.write_line("")?;
term.hide_cursor()?;
for t in Clock::minutes() {
term.write_line(&format!("The time is {:#.2} ", &t))?;
term.write_str(match tm.get_state(&t).ok().unwrap() {
State::NightDark => "it is dark ",
State::DuskDawnRed => "the red light is on ",
State::DayWhite => "the white light is on",
})?;
#[allow(deprecated)]
sleep_ms(25);
term.move_cursor_up(1)?;
term.move_cursor_left(21)?;
}
term.show_cursor()?;
term.move_cursor_down(2)?;
Ok(())
}