Skip to content

Commit

Permalink
feat: add argument to change data dir
Browse files Browse the repository at this point in the history
see issue blinry#24 for a description of the problem this commit solves. using
the new `--habit-dir` command line argument, you can specify the
directory where habitctl should store your data.
  • Loading branch information
korrat committed Mar 11, 2021
1 parent ab6ebb6 commit dc2c382
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 91 deletions.
104 changes: 23 additions & 81 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ chrono = "0.4.10"
rprompt = "1.0.5"
clap = "2.33.0"
open = "1.3.2"
dirs = "3.0.1"
libmath = "0.2.1"
shellexpand = "2.1.0"
20 changes: 11 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process;
use std::process::Command;

Expand Down Expand Up @@ -42,6 +42,7 @@ fn ask_prompt() -> String {
fn main() {
let matches = app_from_crate!()
.template("{bin} {version}\n{author}\n\n{about}\n\nUSAGE:\n {usage}\n\nFLAGS:\n{flags}\n\nSUBCOMMANDS:\n{subcommands}")
.arg(Arg::with_name("habit-dir").long("habit-dir").default_value("~/.habitctl/"))
.subcommand(
SubCommand::with_name("ask")
.about("Ask for status of all habits for a day")
Expand All @@ -57,7 +58,10 @@ fn main() {
.subcommand(SubCommand::with_name("edith").about("Edit list of current habits"))
.get_matches();

let mut habitctl = HabitCtl::new();
let expanded = shellexpand::tilde(matches.value_of("habit-dir").unwrap());
let habitctl_dir = Path::new(expanded.as_ref());

let mut habitctl = HabitCtl::new(&habitctl_dir);
habitctl.load();

let ago: i64 = if habitctl.first_date().is_some() {
Expand Down Expand Up @@ -128,29 +132,27 @@ enum DayStatus {
}

impl HabitCtl {
fn new() -> HabitCtl {
let mut habitctl_dir = dirs::home_dir().unwrap();
habitctl_dir.push(".habitctl");
fn new(habitctl_dir: &Path) -> HabitCtl {
if !habitctl_dir.is_dir() {
println!("Welcome to habitctl!\n");
fs::create_dir(&habitctl_dir).unwrap();
}

let mut habits_file = habitctl_dir.clone();
let mut habits_file = habitctl_dir.to_owned();
habits_file.push("habits");
if !habits_file.is_file() {
fs::File::create(&habits_file).unwrap();
fs::write(&habits_file, HABIT_FILE_TEMPLATE).unwrap();

println!(
"Created {}. This file will list your currently tracked habits.",
habits_file.to_str().unwrap()
);
}

let mut log_file = habitctl_dir;
let mut log_file = habitctl_dir.to_owned();
log_file.push("log");
if !log_file.is_file() {
fs::write(&log_file, HABIT_FILE_TEMPLATE).unwrap();
fs::write(&log_file, "").unwrap();

println!(
"Created {}. This file will contain your habit log.\n",
Expand Down

0 comments on commit dc2c382

Please sign in to comment.