Implement Pomodoro Technique
This commit is contained in:
		
							
								
								
									
										97
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,97 @@
 | 
			
		||||
pub use pomodoro::Pomodoro;
 | 
			
		||||
 | 
			
		||||
pub mod pomodoro {
 | 
			
		||||
    #[derive(PartialEq, Eq)]
 | 
			
		||||
    pub enum State {
 | 
			
		||||
        Work,
 | 
			
		||||
        ShortBreak,
 | 
			
		||||
        LongBreak,
 | 
			
		||||
        Inactive,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub struct Config {
 | 
			
		||||
        pub work_time: u64,
 | 
			
		||||
        pub short_break: u64,
 | 
			
		||||
        pub long_break: u64,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    impl Default for Config {
 | 
			
		||||
        fn default() -> Self {
 | 
			
		||||
            Config {
 | 
			
		||||
                work_time: 1,
 | 
			
		||||
                short_break: 1,
 | 
			
		||||
                long_break: 1,
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub struct Pomodoro {
 | 
			
		||||
        pub count: u64,
 | 
			
		||||
        pub state: State,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    impl Default for Pomodoro {
 | 
			
		||||
        fn default() -> Self {
 | 
			
		||||
            Pomodoro {
 | 
			
		||||
                count: 0,
 | 
			
		||||
                state: State::Inactive,
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    impl Pomodoro {
 | 
			
		||||
        fn next(&mut self) {
 | 
			
		||||
            match self.state {
 | 
			
		||||
                State::Work => {
 | 
			
		||||
                    self.count += 1;
 | 
			
		||||
 | 
			
		||||
                    if (self.count % 4) == 0 {
 | 
			
		||||
                        self.state = State::LongBreak;
 | 
			
		||||
                    } else {
 | 
			
		||||
                        self.state = State::ShortBreak;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                State::LongBreak => self.state = State::Inactive,
 | 
			
		||||
                State::ShortBreak => self.state = State::Work,
 | 
			
		||||
                State::Inactive => self.state = State::Work,
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        async fn wait(minutes: u64) {
 | 
			
		||||
            use std::time::Duration;
 | 
			
		||||
            use tokio::time::delay_for;
 | 
			
		||||
 | 
			
		||||
            let duration = Duration::from_secs(minutes * 5);
 | 
			
		||||
            delay_for(duration).await;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        pub async fn start(&mut self, config: Config) {
 | 
			
		||||
            loop {
 | 
			
		||||
                self.next();
 | 
			
		||||
 | 
			
		||||
                match self.state {
 | 
			
		||||
                    State::Work => {
 | 
			
		||||
                        println!("Start Work.");
 | 
			
		||||
                        Self::wait(config.work_time).await;
 | 
			
		||||
                    }
 | 
			
		||||
                    State::ShortBreak => {
 | 
			
		||||
                        println!("Start Short Break.");
 | 
			
		||||
                        Self::wait(config.short_break).await;
 | 
			
		||||
                    }
 | 
			
		||||
                    State::LongBreak => {
 | 
			
		||||
                        println!("Start Long Break.");
 | 
			
		||||
                        Self::wait(config.long_break).await;
 | 
			
		||||
                    }
 | 
			
		||||
                    State::Inactive => {
 | 
			
		||||
                        println!("Pomodoro Cycle is complete");
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        pub fn completed(&self) -> u64 {
 | 
			
		||||
            self.count / 4
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -1,3 +1,24 @@
 | 
			
		||||
use clap::{App, ArgMatches, SubCommand};
 | 
			
		||||
use domasi::pomodoro::Config;
 | 
			
		||||
use domasi::Pomodoro;
 | 
			
		||||
fn main() {
 | 
			
		||||
    println!("Hello, world!");
 | 
			
		||||
    let matches = App::new("Domasi")
 | 
			
		||||
        .version("0.1.0")
 | 
			
		||||
        .author("paoda <musukarekai@gmail.com>")
 | 
			
		||||
        .about("Yet another pomodoro timer.")
 | 
			
		||||
        .subcommand(SubCommand::with_name("start").about("Start the Pomodoro Timer"))
 | 
			
		||||
        .get_matches();
 | 
			
		||||
 | 
			
		||||
    match matches.subcommand() {
 | 
			
		||||
        ("start", Some(args)) => start(args),
 | 
			
		||||
        _ => {}
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
pub async fn start(_args: &ArgMatches) {
 | 
			
		||||
    let mut pomodoro = Pomodoro::default();
 | 
			
		||||
    let config = Config::default();
 | 
			
		||||
 | 
			
		||||
    pomodoro.start(config).await;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user