htracker/src/main.rs

137 lines
3.5 KiB
Rust
Raw Normal View History

2022-07-07 13:39:19 +02: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 10:26:01 +02:00
use std::env;
use std::path::PathBuf;
use std::fs;
2022-07-07 13:39:19 +02: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
{
2022-07-19 22:56:37 +02:00
#[structopt(help = "Name of the new habit")]
2022-07-07 13:39:19 +02:00
name:String,
2022-07-19 22:56:37 +02:00
#[structopt(long, short, help = "Days the new habit is active",
default_value = "mon,tue,wed,thu,fri,sat,sun")]
2022-07-07 13:39:19 +02:00
days:String,
2022-07-19 22:56:37 +02:00
#[structopt(long, help = "Assign habit as a bad habit (negative points)")]
2022-07-07 13:39:19 +02:00
bad:bool,
2022-08-04 10:12:46 +02:00
#[structopt(short,
long,
help = "Priority of the new habit (L, M, or H)",
default_value = "M")]
priority:char,
2022-07-07 13:39:19 +02:00
},
Commit { },
2022-07-19 15:06:21 +02:00
#[structopt(alias = "del")]
Delete {
#[structopt(help = "ID of the habit to delete")]
id:usize,
},
#[structopt(alias = "i")]
Info {
2022-07-19 22:56:37 +02:00
#[structopt(help = "ID of the habit to show information for")]
2022-07-19 15:06:21 +02:00
id:usize,
},
2022-07-07 13:39:19 +02:00
#[structopt(alias = "ls")]
List
{
2022-07-19 22:56:37 +02:00
#[structopt(short, long, help = "List all active habits")]
2022-07-07 13:39:19 +02:00
all:bool,
},
#[structopt(alias = "mod")]
2022-07-19 15:06:21 +02:00
Modify {
#[structopt(help = "ID of the habit to modify")]
id:usize,
2022-07-19 22:56:37 +02:00
#[structopt(short, long, help = "New name for the habit")]
2022-07-19 15:06:21 +02:00
name:Option<String>,
2022-08-04 10:12:46 +02:00
#[structopt(short, long, help = "New priority for the habit (L, M, or H)")]
priority:Option<char>,
2022-07-19 22:56:37 +02:00
#[structopt(long, short, help = "New days the habit is active")]
2022-07-19 15:06:21 +02:00
days:Option<String>,
2022-07-19 22:56:37 +02:00
#[structopt(long, help = "Toggle the 'bad' value of the habit")]
2022-07-19 15:06:21 +02:00
toggle_bad:bool,
},
2022-07-19 22:56:37 +02:00
Stats { },
2022-08-19 14:13:53 +02:00
#[structopt(alias = "vac")]
Vacation { },
2022-07-07 13:39:19 +02:00
}
fn main()
{
let opts = Opts::from_args();
2022-07-08 18:27:32 +02:00
let data_dir =
PathBuf::from(
env::var("XDG_DATA_HOME")
2022-07-13 10:29:54 +02:00
.unwrap_or_else(|e| { panic!("Error: {}", e); }))
2022-07-08 18:27:32 +02:00
.join("htracker");
2022-07-08 10:26:01 +02:00
if data_dir.exists() && data_dir.is_file()
{
2022-07-08 18:27:32 +02:00
panic!("Error: file exists at '{}', please (re)move it.",
data_dir.display());
}
else if !data_dir.exists()
2022-07-08 10:26:01 +02:00
{
2022-07-08 18:27:32 +02:00
println!("First run: files will be stored in {}", data_dir.display());
2022-07-08 23:07:31 +02:00
fs::create_dir_all(&data_dir)
2022-07-13 10:29:54 +02:00
.unwrap_or_else(|e| { panic!("Filesystem error: {}", e); });
2022-07-08 10:26:01 +02:00
}
2022-07-12 23:49:38 +02:00
let mut hmgr = HabitMgr::new(&data_dir);
2022-07-08 18:27:32 +02:00
2022-07-07 13:39:19 +02:00
match opts.cmd
{
2022-07-19 22:56:37 +02:00
None => hmgr.list(false),
2022-07-07 13:39:19 +02:00
Some(c) =>
match c
{
2022-08-04 10:12:46 +02:00
Command::Add { name, days, bad, priority } =>
hmgr.add(name, bad, priority, days),
Command::Delete { id } =>
hmgr.delete(id),
2022-07-19 15:06:21 +02:00
Command::Info { id } =>
hmgr.habit_info(id),
2022-07-19 22:56:37 +02:00
Command::List { all } =>
hmgr.list(all),
2022-08-04 10:12:46 +02:00
Command::Modify { id, name, priority, days, toggle_bad } =>
hmgr.modify(id, name, toggle_bad, priority, days),
2022-07-07 13:39:19 +02:00
_ => todo!(),
},
}
}