/* * Copyright (C) 2022 Ortega Froysa, Nicolás * Author: Ortega Froysa, Nicolás * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ use structopt::StructOpt; use structopt::clap::AppSettings; use std::env; use std::path::PathBuf; use std::fs; mod habit; mod habitmgr; use habitmgr::HabitMgr; #[derive(StructOpt)] #[structopt( setting = AppSettings::InferSubcommands, about = "A CLI tool to help form good habits and lose bad ones.")] struct Opts { #[structopt(subcommand)] cmd:Option, } #[derive(StructOpt)] enum Command { #[structopt(alias = "a")] Add { #[structopt(help = "name of the new habit")] name:String, #[structopt(long, short, default_value = "mon,tue,wed,thu,fri,sat,sun")] days:String, #[structopt(long)] bad:bool, #[structopt(short, long, default_value = "5")] weight:u8, }, Commit { }, #[structopt(alias = "ls")] List { #[structopt(short, long, help = "list all active habits")] all:bool, #[structopt(short, long, help = "show UUIDs")] verbose:bool, }, #[structopt(alias = "mod")] Modify { }, #[structopt(alias = "rm")] Remove { }, #[structopt(alias = "stats")] Statistics { }, } fn main() { let opts = Opts::from_args(); let mut hmgr = HabitMgr::new(); let data_dir:PathBuf = match env::var("XDG_DATA_HOME") { Ok(s) => [ s, "htracker".to_string() ].iter().collect(), Err(_e) => PathBuf::from("./"), }; if data_dir.exists() && data_dir.is_file() { panic!("Error: {} file already exists! Please (re)move it.", data_dir.display()); } else if !data_dir.exists() { println!("Creating {} data directory.", data_dir.display()); fs::create_dir_all(data_dir) .unwrap_or_else(|err| { panic!("Filesystem error: {}", err) }); } match opts.cmd { None => hmgr.list(false, false), Some(c) => match c { Command::Add { name, days, bad, weight } => hmgr.add(name, bad, weight, days), Command::List { all, verbose } => hmgr.list(all, verbose), _ => todo!(), }, } }