domasi/src/main.rs

69 lines
2.1 KiB
Rust
Raw Normal View History

use async_std::task;
2020-05-21 06:03:38 +00:00
use clap::{App, ArgMatches, SubCommand};
use crossterm::{cursor, terminal::Clear, terminal::ClearType, QueueableCommand};
use domasi::pomodoro::{Alert, Clock, Config, Status};
2020-05-21 06:03:38 +00:00
use domasi::Pomodoro;
use std::io::{stdout, Write};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread;
2020-05-21 04:04:31 +00:00
fn main() {
2020-05-21 06:03:38 +00:00
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(sub_matches)) => start(sub_matches),
// _ => {}
// }
if let ("start", Some(sub_matches)) = matches.subcommand() {
start(sub_matches);
2020-05-21 06:03:38 +00:00
}
}
pub fn start(_args: &ArgMatches) {
2020-05-21 06:03:38 +00:00
let config = Config::default();
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 {
let (tx, rx): (Sender<Status>, Receiver<Status>) = channel();
thread::spawn(move || loop {
if let Ok(status) = rx.recv() {
loop {
let (remaining, string) = Clock::get_formatted_string(status);
print_overwrite(&string);
// Super fun race condition that you gotta handle better :)
if remaining < 1 {
break;
}
thread::sleep(Clock::get_polling_interval());
}
}
});
pomodoro.start(config, tx).await;
});
2020-05-21 04:04:31 +00:00
}
fn print_overwrite(text: &str) {
let mut stdout = stdout();
stdout.queue(Clear(ClearType::CurrentLine)).unwrap();
stdout.queue(cursor::SavePosition).unwrap();
stdout.write_all(text.as_bytes()).unwrap();
stdout.queue(cursor::RestorePosition).unwrap();
stdout.flush().unwrap();
}