From 2f8676533eadb8fd941a6b8d6892b77fbdd11605 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 12 Dec 2021 19:35:54 +0000 Subject: [PATCH] Load file from .config/today/main.today by default --- Cargo.lock | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/cli.rs | 16 ++++++++++++--- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c891b6..32750b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "chrono" version = "0.4.19" @@ -116,6 +122,26 @@ dependencies = [ "generic-array", ] +[[package]] +name = "directories" +version = "4.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "fake-simd" version = "0.1.2" @@ -131,6 +157,17 @@ dependencies = [ "typenum", ] +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "heck" version = "0.3.3" @@ -277,6 +314,25 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_users" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" +dependencies = [ + "getrandom", + "redox_syscall", +] + [[package]] name = "sha-1" version = "0.8.2" @@ -377,6 +433,7 @@ dependencies = [ "anyhow", "chrono", "computus", + "directories", "pest", "pest_derive", "structopt", diff --git a/Cargo.toml b/Cargo.toml index 6138e05..5a25931 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" anyhow = "1.0.45" chrono = "0.4.19" computus = "1.0.0" +directories = "4.0.1" pest = "2.1.3" pest_derive = "2.1.0" structopt = "0.3.25" diff --git a/src/cli.rs b/src/cli.rs index ea2a550..62dc7ce 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use chrono::NaiveDate; +use directories::ProjectDirs; use structopt::StructOpt; use crate::eval::{DateRange, EntryMode}; @@ -14,14 +15,23 @@ mod render; #[derive(Debug, StructOpt)] pub struct Opt { - #[structopt(parse(from_os_str))] - file: PathBuf, + /// File to load + #[structopt(short, long, parse(from_os_str))] + file: Option, +} + +fn default_file() -> PathBuf { + ProjectDirs::from("", "", "today") + .expect("could not determine config dir") + .config_dir() + .join("main.today") } pub fn run() -> anyhow::Result<()> { let opt = Opt::from_args(); - let files = Files::load(&opt.file)?; + let file = opt.file.unwrap_or_else(default_file); + let files = Files::load(&file)?; let now = files.now().naive_local(); let range = DateRange::new(