domasi/src/main.rs

82 lines
2.3 KiB
Rust

use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg, SubCommand};
use directories::ProjectDirs;
use domasi::Alert;
use domasi::Domasi;
use std::fs;
use std::path::PathBuf;
fn main() {
let app = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.arg(
Arg::with_name("create-alert-dir")
.short("C")
.long("create-alert-dir")
.help("Creates the alert directory"),
)
.subcommand(SubCommand::with_name("start").about("Start Pomodoro Timer"));
let matches = app.get_matches();
if matches.is_present("create-alert-dir") {
// Create the Alert Directory
match create_alert_directory() {
Ok(path) => println!("Created {:?}", path),
Err(err) => eprintln!("Failed to create Alert Dir: {}", err),
}
return; // Exit main
}
match matches.subcommand() {
("start", Some(_)) => {
let mut domasi: Domasi = Default::default();
match get_alert() {
Some(alert) => domasi.start(Some(alert)).unwrap(),
None => domasi.start(None).unwrap(),
}
}
_ => println!("No argument or subcommand provided."),
}
}
fn create_alert_directory() -> std::io::Result<PathBuf> {
let alert_dir = ProjectDirs::from("moe", "paoda", "domasi")
.unwrap()
.data_dir()
.to_path_buf()
.join("alert");
fs::create_dir_all(&alert_dir)?;
Ok(alert_dir)
}
fn get_alert() -> Option<Alert> {
let alert_dir = ProjectDirs::from("moe", "paoda", "domasi")
.unwrap()
.data_dir()
.to_path_buf()
.join("alert");
let file = find_alert_file(alert_dir)?;
Some(Alert::new(file))
}
fn find_alert_file(path: PathBuf) -> Option<PathBuf> {
let items = fs::read_dir(path).ok()?;
for entry_result in items {
if let Ok(entry) = entry_result {
if let Some(ext) = entry.path().extension() {
if let Some("mp3") | Some("wav") | Some("ogg") | Some("flac") = ext.to_str() {
return Some(entry.path());
}
}
}
}
None
}