diff options
| author | lukeflo | 2025-05-18 22:44:36 +0200 |
|---|---|---|
| committer | lukeflo | 2025-05-24 14:24:40 +0200 |
| commit | 7fe5acb85b9e665ec83f590942ee78b8c90d6aae (patch) | |
| tree | 979fc66155ca60ee9bc167cf32348fa8bbdf8c31 | |
| parent | 1dcb24c381990bdead5fa3df2fb95d74b176a8c1 (diff) | |
| download | bibiman-7fe5acb85b9e665ec83f590942ee78b8c90d6aae.tar.gz bibiman-7fe5acb85b9e665ec83f590942ee78b8c90d6aae.zip | |
first tests for pdf folder
| -rw-r--r-- | src/bibiman/bibisetup.rs | 22 | ||||
| -rw-r--r-- | src/cliargs.rs | 41 |
2 files changed, 60 insertions, 3 deletions
diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index cba1536..92731b5 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -22,7 +22,7 @@ use itertools::Itertools; use std::ffi::{OsStr, OsString}; use std::{fs, path::PathBuf}; -use crate::cliargs; +use crate::cliargs::{self, CLIArgs}; // Set necessary fields // TODO: can surely be made more efficient/simpler @@ -328,9 +328,11 @@ impl BibiSetup { } } - pub fn get_filepath(citekey: &str, biblio: &Bibliography) -> Option<OsString> { + pub fn get_filepath(citekey: &str, biblio: &Bibliography, args: &CLIArgs) -> Option<OsString> { if biblio.get(citekey).unwrap().file().is_ok() { Some(biblio.get(citekey).unwrap().file().unwrap().trim().into()) + } else if args.pdf_files.is_some() { + Self::merge_filepath_or_none(&citekey, &biblio, &args) } else { None } @@ -350,4 +352,20 @@ impl BibiSetup { None } } + + fn merge_filepath_or_none( + citekey: &str, + biblio: &Bibliography, + args: &CLIArgs, + ) -> Option<OsString> { + // Oder n Loop??? + let pdf_file = { + let filename = citekey.to_owned() + ".pdf"; + for f in args.pdf_files.unwrap().iter() { + if f.file_name().unwrap().to_str().unwrap() == &filename { + break f; + } + } + }; + } } diff --git a/src/cliargs.rs b/src/cliargs.rs index d4fac46..6fd30e1 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -33,6 +33,7 @@ pub struct CLIArgs { pub pos_args: Vec<PathBuf>, pub cfg_path: Option<PathBuf>, pub light_theme: bool, + pub pdf_files: Option<Vec<PathBuf>>, } impl CLIArgs { @@ -55,6 +56,10 @@ impl CLIArgs { Short('v') | Long("version") => args.versionarg = true, Short('c') | Long("config-file") => args.cfg_path = Some(parser.value()?.parse()?), Long("light-terminal") => args.light_theme = true, + Long("merge-pdf-paths") => { + let pdf_dir: PathBuf = parser.value()?.parse()?; + args.pdf_files = collect_pdf_files(pdf_dir); + } // Value(pos_arg) => parse_files(&mut args, pos_arg), Value(pos_arg) => args.pos_args.push(pos_arg.into()), _ => return Err(arg.unexpected()), @@ -65,6 +70,37 @@ impl CLIArgs { } } +/// This function walks the given dir and collects all pdf files into a `Vec` +pub fn collect_pdf_files(pdf_dir: PathBuf) -> Option<Vec<PathBuf>> { + let mut files: Vec<PathBuf> = Vec::new(); + + // Expand tilde to /home/user + let pdf_dir = if pdf_dir.starts_with("~") { + app::expand_home(&pdf_dir) + } else { + pdf_dir + }; + + // Walk the passed dir and collect all pdf files into vec + if pdf_dir.is_dir() { + for file in WalkDir::new(pdf_dir) { + let f = file.unwrap().into_path(); + if f.is_file() + && f.extension().is_some() + && f.extension().unwrap_or_default().to_ascii_lowercase() == "pdf" + { + files.push(f); + } + } + } + + if files.is_empty() { + None + } else { + Some(files) + } +} + /// 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. @@ -126,7 +162,10 @@ FLAGS: -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", + --light-terminal Enable color mode for light terminal background + --merge-pdf-paths Merge PDF files named by citekey at the given path into + the `file` field of the entry matching the citekey + (might not work with citekeys containing special chars)", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), ); |
