diff options
Diffstat (limited to 'src/cliargs.rs')
| -rw-r--r-- | src/cliargs.rs | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/src/cliargs.rs b/src/cliargs.rs index 71ba38c..3c302f4 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -17,12 +17,13 @@ use color_eyre::eyre::Result; use color_eyre::owo_colors::OwoColorize; +use dirs::{config_dir, home_dir}; use lexopt::prelude::*; use std::env; use std::path::PathBuf; use walkdir::WalkDir; -use crate::tui::colors::AppColors; +use crate::app; // struct for CLIArgs #[derive(Debug, Default, Clone)] @@ -30,10 +31,8 @@ pub struct CLIArgs { pub helparg: bool, pub versionarg: bool, pub pos_args: Vec<PathBuf>, - pub files: Vec<PathBuf>, - // INFO: AppColors struct later should be moved to config/app struct - // when config file is implemented - pub colors: AppColors, + pub cfg_path: PathBuf, + pub light_theme: bool, } impl CLIArgs { @@ -41,22 +40,25 @@ impl CLIArgs { let mut args = CLIArgs::default(); let mut parser = lexopt::Parser::from_env(); + // Default config + args.cfg_path = if config_dir().is_some() { + config_dir().unwrap().join("bibiman/bibiman.toml") + } else { + home_dir().unwrap().join(".config/bibiman/bibiman.toml") + }; + while let Some(arg) = parser.next()? { match arg { Short('h') | Long("help") => args.helparg = true, Short('v') | Long("version") => args.versionarg = true, - Long("light-terminal") => { - args.colors.light_colors(); - args.colors.toggle_color_scheme() - } + Short('c') | Long("config-file") => args.cfg_path = parser.value()?.parse()?, + Long("light-terminal") => args.light_theme = true, // Value(pos_arg) => parse_files(&mut args, pos_arg), Value(pos_arg) => args.pos_args.push(pos_arg.into()), _ => return Err(arg.unexpected()), } } - args.files = parse_files(args.pos_args.clone()); - Ok(args) } } @@ -64,10 +66,16 @@ impl CLIArgs { /// This function maps a vector containing paths to another vector containing paths. /// But it will walk all entries of the first vec which are directories /// and put only valid file paths with `.bib` ending to the resulting vec. -fn parse_files(args: Vec<PathBuf>) -> Vec<PathBuf> { +pub fn parse_files(args: Vec<PathBuf>) -> Vec<PathBuf> { let mut files: Vec<PathBuf> = Vec::new(); // If pos arg is file, just push it to path vec for i in args { + // Expand tilde to /home/user + let i = if i.starts_with("~") { + app::expand_home(&i) + } else { + i + }; if i.is_file() { files.push(i); // If pos arg is dir, walk dir and collect bibfiles @@ -114,6 +122,8 @@ POSITIONAL ARGS: FLAGS: -h, --help Show this help and exit -v, --version Show the version and exit + -c, --config-file Path to config file used for current session. + Takes precedence over standard config file. --light-terminal Enable color mode for light terminal background", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), |
