2022-07-07 11:39:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
|
|
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use structopt::StructOpt;
|
|
|
|
use structopt::clap::AppSettings;
|
2022-07-08 08:26:01 +00:00
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::fs;
|
2022-07-07 11:39:19 +00:00
|
|
|
|
|
|
|
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<Command>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
2022-07-08 11:47:08 +00:00
|
|
|
let data_dir:PathBuf = match env::var("XDG_DATA_HOME") {
|
|
|
|
Ok(s) => [ s, "htracker".to_string() ].iter().collect(),
|
|
|
|
Err(_e) => PathBuf::from("./"),
|
|
|
|
};
|
2022-07-08 08:26:01 +00:00
|
|
|
|
2022-07-08 15:24:59 +00:00
|
|
|
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()
|
2022-07-08 08:26:01 +00:00
|
|
|
{
|
|
|
|
println!("Creating {} data directory.", data_dir.display());
|
|
|
|
fs::create_dir_all(data_dir)
|
|
|
|
.unwrap_or_else(|err| { panic!("Filesystem error: {}", err) });
|
|
|
|
}
|
|
|
|
|
2022-07-07 11:39:19 +00:00
|
|
|
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!(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|