domasi/src/main.rs

134 lines
3.5 KiB
Rust
Raw Normal View History

use async_std::task;
2020-05-22 05:21:27 +00:00
use clap::{App, Arg, ArgMatches, SubCommand};
2020-05-25 06:51:07 +00:00
use crossterm::{
cursor,
event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
terminal::{disable_raw_mode, enable_raw_mode, Clear, 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};
2020-05-22 05:21:27 +00:00
use std::path::{Path, PathBuf};
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.")
2020-05-22 05:21:27 +00:00
.subcommand(
SubCommand::with_name("start")
.about("Start the Pomodoro Timer")
.arg(
Arg::with_name("alert")
.short("a")
.long("alert")
.value_name("FILE")
.takes_value(true)
.help("Aloud Sound. (Supports WAV, MP3, Vorbis, FLAC)"),
),
)
2020-05-21 06:03:38 +00:00
.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
}
}
2020-05-22 05:21:27 +00:00
pub fn start(args: &ArgMatches) {
2020-05-21 06:03:38 +00:00
let config = Config::default();
2020-05-22 05:21:27 +00:00
let audio_path: PathBuf;
if let Some(path) = args.value_of("alert") {
audio_path = Path::new(path).to_path_buf();
} else {
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);
2020-05-25 06:51:07 +00:00
let (tx, rx): (Sender<Status>, Receiver<Status>) = channel();
2020-05-25 06:51:07 +00:00
// UI Thread
thread::spawn(move || loop {
if let Ok(status) = rx.recv() {
loop {
let (remaining, string) = Clock::get_formatted_string(status);
print_overwrite(&string);
2020-05-25 06:51:07 +00:00
// Super fun race condition that you gotta handle better :)
if remaining < 1 {
break;
}
2020-05-25 06:51:07 +00:00
thread::sleep(Clock::get_polling_interval());
}
2020-05-25 06:51:07 +00:00
}
});
// User Input Thread
thread::spawn(|| {
setup_user_input().unwrap();
});
2020-05-25 06:51:07 +00:00
task::block_on(async {
pomodoro.start(config, tx).await;
});
2020-05-21 04:04:31 +00:00
}
2020-05-25 06:51:07 +00:00
fn setup_user_input() -> crossterm::Result<()> {
enable_raw_mode()?;
get_user_input()?;
disable_raw_mode()
}
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();
}
2020-05-25 06:51:07 +00:00
fn get_user_input() -> crossterm::Result<()> {
loop {
if poll(Clock::get_polling_interval())? {
if let Event::Key(event) = read()? {
handle_key_event(event);
}
}
}
}
fn handle_key_event(event: KeyEvent) {
match event.code {
KeyCode::Char('c') => {
if let KeyModifiers::CONTROL = event.modifiers {
exit_domasi(1)
}
}
KeyCode::Esc | KeyCode::Char('q') => exit_domasi(0),
_ => {}
}
}
fn exit_domasi(code: i32) {
disable_raw_mode().unwrap();
std::process::exit(code);
}