nifun

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

main.rs (1391B)


      1 use crossterm::event::{self, Event, KeyCode};
      2 use jiff::{Span, Timestamp};
      3 use notify_rust::Notification;
      4 use std::{io::Write, io::stdout, process::Command, thread, time::Duration};
      5 
      6 fn main() -> Result<(), Box<dyn std::error::Error>> {
      7     let interval: Span = Span::new().minutes(2);
      8     let start_time = Timestamp::now();
      9     let mut next_notif_time = start_time.checked_add(interval).unwrap();
     10     println!("press 'q' to quit");
     11     loop {
     12         let now = Timestamp::now();
     13         if now >= next_notif_time {
     14             notif();
     15             next_notif_time = next_notif_time.checked_add(interval).unwrap();
     16         } else {
     17             if event::poll(Duration::from_millis(50))?
     18                 && let Event::Key(key_event) = event::read()?
     19                 && key_event.code == KeyCode::Char('q')
     20             {
     21                 println!("\n'q' detected. Exiting.");
     22                 break;
     23             }
     24             print!("\r\x1b[2K{}", next_notif_time - now);
     25             stdout().flush().unwrap();
     26             thread::sleep(std::time::Duration::from_millis(100));
     27         }
     28     }
     29     Ok(())
     30 }
     31 
     32 fn notif() {
     33     Command::new("powershell")
     34         .args(["-c", "[console]::beep()"])
     35         .status()
     36         .expect("failed to execute process");
     37 
     38     Notification::new()
     39         .summary("COMing!!!")
     40         .body("Picture will be taken soon!")
     41         .show()
     42         .unwrap();
     43 }