82 lines
2.2 KiB
Rust
82 lines
2.2 KiB
Rust
use clap::{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("Domasi")
|
|
.version("0.1.0")
|
|
.author("Rekai Musuka <rekai@musuka.dev>")
|
|
.about("CLI Pomodoro Timer")
|
|
.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(_sub_match)) => {
|
|
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
|
|
}
|