diff options
| author | lukeflo | 2024-11-22 14:04:42 +0100 |
|---|---|---|
| committer | lukeflo | 2024-11-22 14:04:42 +0100 |
| commit | e5274a2e0c3c6ba62d12420d487e3725e154cee4 (patch) | |
| tree | 59300d8c6586b878584bb61b8dbe2ceba2c14e6e /src | |
| parent | 2621a347a0e6f2a2e1b625ca26aad3a0cf8e58f5 (diff) | |
| download | bibiman-e5274a2e0c3c6ba62d12420d487e3725e154cee4.tar.gz bibiman-e5274a2e0c3c6ba62d12420d487e3725e154cee4.zip | |
open multiple files: per CLI possible
Diffstat (limited to 'src')
| -rw-r--r-- | src/bibiman.rs | 14 | ||||
| -rw-r--r-- | src/bibiman/bibisetup.rs | 58 | ||||
| -rw-r--r-- | src/cliargs.rs | 16 | ||||
| -rw-r--r-- | src/tui/ui.rs | 4 |
4 files changed, 54 insertions, 38 deletions
diff --git a/src/bibiman.rs b/src/bibiman.rs index e65d5bc..4d0aa60 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -55,7 +55,7 @@ pub enum FormerArea { #[derive(Debug)] pub struct Bibiman { // main bib file - pub main_bibfile: PathBuf, + pub main_bibfiles: Vec<PathBuf>, // main bibliography pub main_biblio: BibiSetup, // search struct: @@ -77,14 +77,14 @@ pub struct Bibiman { impl Bibiman { // Constructs a new instance of [`App`]. pub fn new(args: CLIArgs) -> Result<Self> { - let main_bibfile = args.bibfilearg; - let main_biblio = BibiSetup::new(&main_bibfile); + let main_bibfiles = args.bibfilearg; + let main_biblio = BibiSetup::new(&main_bibfiles); let tag_list = TagList::new(main_biblio.keyword_list.clone()); let search_struct = BibiSearch::default(); let entry_table = EntryTable::new(&main_biblio.entry_list); let current_area = CurrentArea::EntryArea; Ok(Self { - main_bibfile, + main_bibfiles, main_biblio, tag_list, search_struct, @@ -123,7 +123,7 @@ impl Bibiman { } pub fn update_lists(&mut self) { - self.main_biblio = BibiSetup::new(&self.main_bibfile); + self.main_biblio = BibiSetup::new(&self.main_bibfiles); self.tag_list = TagList::new(self.main_biblio.keyword_list.clone()); self.entry_table = EntryTable::new(&self.main_biblio.entry_list); } @@ -306,7 +306,9 @@ impl Bibiman { .citekey; // create independent copy of citekey for finding entry after updating list let saved_key = citekey.to_owned(); - let filepath = self.main_bibfile.as_os_str(); + // TODO: Only for testing purposes, needs better logic to find correct file + // when using multi file approach + let filepath = self.main_bibfiles[0].as_os_str(); let filecontent: &str = &self.main_biblio.bibfilestring; let mut line_count = 0; diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index 85438b3..227d0b2 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -17,6 +17,7 @@ use biblatex::{self, Bibliography}; use biblatex::{ChunksExt, Type}; +use color_eyre::eyre::Result; use color_eyre::owo_colors::OwoColorize; use itertools::Itertools; use std::ffi::OsString; @@ -24,18 +25,11 @@ use std::{fs, path::PathBuf}; use crate::cliargs; -#[derive(Debug)] -pub enum FileFormat { - BibLatex, - Hayagriva, -} - // Set necessary fields // TODO: can surely be made more efficient/simpler #[derive(Debug)] pub struct BibiSetup { // pub bibfile: PathBuf, // path to bibfile - pub bibfile_format: FileFormat, // Format of passed file pub bibfilestring: String, // content of bibfile as string pub bibliography: Bibliography, // parsed bibliography pub citekeys: Vec<String>, // list of all citekeys @@ -58,17 +52,16 @@ pub struct BibiData { } impl BibiSetup { - pub fn new(main_bibfile: &PathBuf) -> Self { + pub fn new(main_bibfiles: &[PathBuf]) -> Self { // TODO: Needs check for config file path as soon as config file is impl - let bibfile_format = Self::check_file_format(main_bibfile); - let bibfilestring = fs::read_to_string(main_bibfile).unwrap(); + Self::check_files(main_bibfiles).expect("Something went wrong checking files"); + let bibfilestring = Self::bibfiles_to_string(main_bibfiles); let bibliography = biblatex::Bibliography::parse(&bibfilestring).unwrap(); let citekeys = Self::get_citekeys(&bibliography); let keyword_list = Self::collect_tag_list(&citekeys, &bibliography); let entry_list = Self::create_entry_list(&citekeys, &bibliography); Self { // bibfile, - bibfile_format, bibfilestring, bibliography, citekeys, @@ -78,21 +71,8 @@ impl BibiSetup { } // Check which file format the passed file has - fn check_file_format(main_bibfile: &PathBuf) -> FileFormat { - if main_bibfile.exists() { - let extension = main_bibfile.extension().unwrap().to_str(); - - match extension { - // Some("yml") => FileFormat::Hayagriva, - // Some("yaml") => FileFormat::Hayagriva, - Some("bib") => FileFormat::BibLatex, - Some(_) => panic!( - "The extension \".{:?}\" is no valid bibfile", - extension.unwrap() - ), - None => panic!("The given path {:?} holds no valid file", main_bibfile), - } - } else { + fn check_files(main_bibfiles: &[PathBuf]) -> Result<()> { + if main_bibfiles.is_empty() { println!( "{}", "No bibfile passed as argument. Please select a valid file." @@ -103,7 +83,33 @@ impl BibiSetup { println!("{}", cliargs::help_func()); // panic!("No file passed as argument. Please choose a .bib file.") std::process::exit(1) + } else { + main_bibfiles.iter().for_each(|f| if f.extension().is_some() && f.extension().unwrap() != "bib" { + panic!("File \'{}\' has no valid extension. Please select a file with \'.bib\' extension", f.to_str().unwrap()) + }); } + + Ok(()) + } + + fn bibfiles_to_string(main_bibfiles: &[PathBuf]) -> String { + // let file_strings = String::new(); + + // for f in main_bibfiles { + // file_strings. + // } + + // main_bibfiles + // .iter() + // .for_each(|f| format!("{}\n{}", file_strings, fs::read_to_string(f).unwrap())); + + // file_strings + let file_strings: Vec<String> = main_bibfiles + .iter() + .map(|f| fs::read_to_string(f).unwrap()) + .collect(); + + file_strings.join("\n") } fn create_entry_list(citekeys: &[String], bibliography: &Bibliography) -> Vec<BibiData> { diff --git a/src/cliargs.rs b/src/cliargs.rs index 7fd512e..a298f65 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -34,7 +34,7 @@ sarge! { pub struct CLIArgs { pub helparg: bool, pub versionarg: bool, - pub bibfilearg: PathBuf, + pub bibfilearg: Vec<PathBuf>, } impl Default for CLIArgs { @@ -47,10 +47,9 @@ impl CLIArgs { pub fn new() -> Self { let (cli_args, pos_args) = ArgumentsCLI::parse().expect("Could not parse CLI arguments"); let bibfilearg = if pos_args.len() > 1 { - PathBuf::from(&pos_args[1]) - // pos_args[1].to_string() + parse_files(pos_args) } else { - PathBuf::new() + panic!("No bibfile provided") }; Self { helparg: cli_args.help, @@ -60,6 +59,15 @@ impl CLIArgs { } } +pub fn parse_files(args: Vec<String>) -> Vec<PathBuf> { + let mut files: Vec<PathBuf> = Vec::new(); + for f in args { + files.push(PathBuf::from(f)) + } + files.remove(0); + files +} + pub fn help_func() -> String { let help = format!( "\ diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 74b10d7..fd0fc6f 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -366,8 +366,8 @@ pub fn render_file_info(app: &mut App, frame: &mut Frame, rect: Rect) { let file_info = Line::from(vec![ Span::raw("File: ").bold(), Span::raw( - app.bibiman - .main_bibfile + // TODO: Only for testing! Need to replace with dir or files vec + app.bibiman.main_bibfiles[0] .file_name() .unwrap() .to_string_lossy(), |
