aboutsummaryrefslogtreecommitdiff
path: root/src/cliargs.rs
diff options
context:
space:
mode:
authorlukeflo2025-02-24 15:29:47 +0000
committerlukeflo2025-02-24 15:29:47 +0000
commit5da77a2f812a0bb6e0057f7b2e2c642142fca125 (patch)
tree6be886693445a54e4cbd7b98151555eec220e863 /src/cliargs.rs
parentdd8dd9611771491e723a49b41cf27b1e9090664d (diff)
parentaff7c398da005029a293178e487cf5323e507fb4 (diff)
downloadbibiman-5da77a2f812a0bb6e0057f7b2e2c642142fca125.tar.gz
bibiman-5da77a2f812a0bb6e0057f7b2e2c642142fca125.zip
Merge pull request 'Implement config file' (#15) from implement-config into main
Reviewed-on: https://codeberg.org/lukeflo/bibiman/pulls/15
Diffstat (limited to 'src/cliargs.rs')
-rw-r--r--src/cliargs.rs34
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"),