2020-05-21 23:59:38 -05:00

227 lines
6.9 KiB
Rust

pub use pomodoro::Pomodoro;
pub mod pomodoro {
use directories::ProjectDirs;
use rodio::{Decoder, Device, Source};
use std::fs::{self, File};
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::sync::mpsc::Sender;
use std::time::{Duration, Instant};
#[derive(Copy, Clone)]
pub struct Alert<'a> {
path: &'a Path,
device: &'a Device,
}
impl Alert<'_> {
pub fn new<'a, P: AsRef<Path>>(path: &'a P, device: &'a Device) -> Alert<'a> {
Alert {
path: path.as_ref(),
device,
}
}
pub fn play(&self) {
if self.path.exists() {
let file = File::open(self.path).unwrap();
let source = Decoder::new(BufReader::new(file)).unwrap();
rodio::play_raw(self.device, source.convert_samples());
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum State {
Work,
ShortBreak,
LongBreak,
Inactive,
}
impl std::fmt::Display for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
State::Work => f.write_str("Working: "),
State::ShortBreak => f.write_str("Resting: "),
State::LongBreak => f.write_str("REALLY Resting: "),
State::Inactive => f.write_str("Inactive: "),
}
}
}
pub struct Config {
pub work_time: Duration,
pub short_break: Duration,
pub long_break: Duration,
pub data_directory: PathBuf,
}
impl Config {
pub fn new(work: u64, short_break: u64, long_break: u64) -> Config {
let data_directory = Self::get_data_directory();
if !data_directory.exists() {
fs::create_dir_all(&data_directory).unwrap();
}
let seconds_in_minutes = 60;
Config {
work_time: Duration::from_secs(work * seconds_in_minutes),
short_break: Duration::from_secs(short_break * seconds_in_minutes),
long_break: Duration::from_secs(long_break * seconds_in_minutes),
data_directory,
}
}
pub fn with_data_directory<P: AsRef<Path>>(mut self, path: &P) -> Config {
self.data_directory = path.as_ref().to_owned();
self
}
fn get_data_directory() -> PathBuf {
let dirs = ProjectDirs::from("moe", "paoda", "domasi").unwrap();
dirs.data_dir().to_owned()
}
}
impl Default for Config {
fn default() -> Self {
Config::new(20, 5, 10)
}
}
#[derive(Copy, Clone)]
pub struct Pomodoro<'a> {
count: u64,
state: State,
alert: Alert<'a>,
}
impl Pomodoro<'_> {
pub fn new<'a>(alert: &'a Alert) -> Pomodoro<'a> {
Pomodoro {
count: 0,
state: State::Inactive,
alert: *alert,
}
}
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(duration: Duration) {
async_std::task::sleep(duration).await;
}
pub async fn start(&mut self, config: Config, tx: Sender<Status>) {
loop {
self.next();
self.alert.play();
match self.state {
State::Work => {
Self::send_to_clock(&tx, self.state, config.work_time);
Self::wait(config.work_time).await;
}
State::ShortBreak => {
Self::send_to_clock(&tx, self.state, config.short_break);
Self::wait(config.short_break).await;
}
State::LongBreak => {
Self::send_to_clock(&tx, self.state, config.long_break);
Self::wait(config.long_break).await;
}
State::Inactive => {
println!("Pomodoro Cycle is complete");
break;
}
}
}
}
fn send_to_clock(tx: &Sender<Status>, state: State, length: Duration) {
let status = Status {
start: Instant::now(),
length,
state,
};
tx.send(status).unwrap();
}
}
#[derive(Copy, Clone, Debug)]
pub struct Status {
pub start: Instant,
pub length: Duration,
pub state: State,
}
pub struct Clock;
impl Clock {
pub fn get_formatted_string(status: Status) -> (u64, String) {
let now = Instant::now();
let elapsed = now.checked_duration_since(status.start);
match elapsed {
Some(duration) => {
let remaining = status.length.as_secs() - duration.as_secs();
let seconds = remaining;
let hours = seconds / 3600;
let minutes = (seconds - (hours * 3600)) / 60;
let seconds = seconds - (hours * 3600) - (minutes * 60);
let mut clock = String::new();
clock.push_str(&format!("{}", status.state));
if hours > 0 {
// We don't want o bother with the hours part if there is none
if hours < 10 {
clock.push_str(&format!("0{}:", hours));
} else {
clock.push_str(&format!("{}:", hours));
}
}
if minutes < 10 {
clock.push_str(&format!("0{}:", minutes));
} else {
clock.push_str(&format!("{}:", minutes));
}
if seconds < 10 {
clock.push_str(&format!("0{}", seconds));
} else {
clock.push_str(&format!("{}", seconds));
}
(remaining, clock)
}
None => (0, "??:??:??".to_string()), // This will break the loop
}
}
pub fn get_polling_interval() -> Duration {
Duration::from_millis(500)
}
}
}