Play audio alert on progress in Pomodoro cycle
This commit is contained in:
61
src/lib.rs
61
src/lib.rs
@@ -1,6 +1,38 @@
|
||||
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};
|
||||
|
||||
#[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 load<'a, 'b, P: AsRef<Path>>(&'a mut self, _path: &'b P) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn play(&self) {
|
||||
let file = File::open(self.path).unwrap();
|
||||
let source = Decoder::new(BufReader::new(file)).unwrap();
|
||||
|
||||
rodio::play_raw(self.device, source.convert_samples());
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub enum State {
|
||||
Work,
|
||||
@@ -13,33 +45,43 @@ pub mod pomodoro {
|
||||
pub work_time: u64,
|
||||
pub short_break: u64,
|
||||
pub long_break: u64,
|
||||
pub data_directory: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
let dirs = ProjectDirs::from("moe", "paoda", "domasi").unwrap();
|
||||
|
||||
let data_directory = dirs.data_dir().to_owned();
|
||||
|
||||
if !data_directory.exists() {
|
||||
fs::create_dir_all(&data_directory).unwrap();
|
||||
}
|
||||
|
||||
Config {
|
||||
work_time: 1,
|
||||
short_break: 1,
|
||||
long_break: 1,
|
||||
data_directory,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Pomodoro {
|
||||
pub count: u64,
|
||||
pub state: State,
|
||||
pub struct Pomodoro<'a> {
|
||||
count: u64,
|
||||
state: State,
|
||||
alert: Alert<'a>,
|
||||
}
|
||||
|
||||
impl Default for Pomodoro {
|
||||
fn default() -> Self {
|
||||
impl Pomodoro<'_> {
|
||||
pub fn new<'a>(alert: &'a Alert) -> Pomodoro<'a> {
|
||||
Pomodoro {
|
||||
count: 0,
|
||||
state: State::Inactive,
|
||||
alert: *alert,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Pomodoro {
|
||||
fn next(&mut self) {
|
||||
match self.state {
|
||||
State::Work => {
|
||||
@@ -59,16 +101,17 @@ pub mod pomodoro {
|
||||
|
||||
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;
|
||||
async_std::task::sleep(duration).await;
|
||||
}
|
||||
|
||||
pub async fn start(&mut self, config: Config) {
|
||||
loop {
|
||||
self.next();
|
||||
|
||||
self.alert.play();
|
||||
|
||||
match self.state {
|
||||
State::Work => {
|
||||
println!("Start Work.");
|
||||
|
||||
19
src/main.rs
19
src/main.rs
@@ -1,6 +1,8 @@
|
||||
use async_std::task;
|
||||
use clap::{App, ArgMatches, SubCommand};
|
||||
use domasi::pomodoro::Config;
|
||||
use domasi::pomodoro::{Alert, Config};
|
||||
use domasi::Pomodoro;
|
||||
|
||||
fn main() {
|
||||
let matches = App::new("Domasi")
|
||||
.version("0.1.0")
|
||||
@@ -10,15 +12,20 @@ fn main() {
|
||||
.get_matches();
|
||||
|
||||
match matches.subcommand() {
|
||||
("start", Some(args)) => start(args),
|
||||
("start", Some(sub_matches)) => start(sub_matches),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn start(_args: &ArgMatches) {
|
||||
let mut pomodoro = Pomodoro::default();
|
||||
pub fn start(_args: &ArgMatches) {
|
||||
let config = Config::default();
|
||||
|
||||
pomodoro.start(config).await;
|
||||
let audio_path = config.data_directory.join("sound/alert.ogg");
|
||||
let default_device = rodio::default_output_device().unwrap();
|
||||
let alert = Alert::new(&audio_path, &default_device);
|
||||
let mut pomodoro = Pomodoro::new(&alert);
|
||||
|
||||
task::block_on(async {
|
||||
pomodoro.start(config).await;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user