diff options
| author | lukeflo | 2024-11-25 11:49:25 +0100 |
|---|---|---|
| committer | lukeflo | 2024-11-25 11:49:25 +0100 |
| commit | bfe4624508f893fdf14a94fffbe4377aabf8c90f (patch) | |
| tree | f09ffe6e083b9f9d78afc68a57ec2078b7379120 /src | |
| parent | b638d1bb534ef165864bfe54405c73a0e1bb0385 (diff) | |
| download | bibiman-bfe4624508f893fdf14a94fffbe4377aabf8c90f.tar.gz bibiman-bfe4624508f893fdf14a94fffbe4377aabf8c90f.zip | |
handling of errors and ui for multi file case
Diffstat (limited to 'src')
| -rw-r--r-- | src/bibiman.rs | 12 | ||||
| -rw-r--r-- | src/bibiman/bibisetup.rs | 15 | ||||
| -rw-r--r-- | src/cliargs.rs | 56 | ||||
| -rw-r--r-- | src/tui/ui.rs | 13 |
4 files changed, 61 insertions, 35 deletions
diff --git a/src/bibiman.rs b/src/bibiman.rs index b8f04c8..bba3eec 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -78,7 +78,7 @@ impl Bibiman { // Constructs a new instance of [`App`]. pub fn new(args: &CLIArgs) -> Result<Self> { // let main_bibfiles = args.fileargs.clone(); - let main_biblio = BibiSetup::new(&args.fileargs); + let main_biblio = BibiSetup::new(&args.files); let tag_list = TagList::new(main_biblio.keyword_list.clone()); let search_struct = BibiSearch::default(); let entry_table = EntryTable::new(&main_biblio.entry_list); @@ -123,7 +123,7 @@ impl Bibiman { } pub fn update_lists(&mut self, args: &CLIArgs) { - self.main_biblio = BibiSetup::new(&args.fileargs); + self.main_biblio = BibiSetup::new(&args.files); self.tag_list = TagList::new(self.main_biblio.keyword_list.clone()); self.entry_table = EntryTable::new(&self.main_biblio.entry_list); } @@ -312,17 +312,17 @@ impl Bibiman { // Check if multiple files were passed to bibiman and // return the correct file path - let filepath = if args.fileargs.len() == 1 { - args.fileargs.first().unwrap().as_os_str() + let filepath = if args.files.len() == 1 { + args.files.first().unwrap().as_os_str() } else { let mut idx = 0; - for f in &args.fileargs { + for f in &args.files { if search::search_pattern_in_file(&citekey_pattern, &f).is_some() { break; } idx += 1; } - args.fileargs[idx].as_os_str() + args.files[idx].as_os_str() }; let filecontent = fs::read_to_string(&filepath).unwrap(); diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index ad413a4..b0049c6 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -83,8 +83,19 @@ impl BibiSetup { std::process::exit(1) } else { // Loop over all files and check for the correct extension - 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()) + main_bibfiles.iter().for_each(|f| { + if f.extension().is_some() && f.extension().unwrap() != "bib" { + println!( + "{}\n{}", + "The passed file has no valid extension. You need a \'.bib\' file:" + .red() + .bold(), + f.as_os_str().to_string_lossy().bright_red().italic() + ); + println!(); + println!("{}", cliargs::help_func()); + std::process::exit(1) + } }); } } diff --git a/src/cliargs.rs b/src/cliargs.rs index 02fc8df..6224b7b 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -19,7 +19,6 @@ use color_eyre::eyre::Result; use color_eyre::owo_colors::OwoColorize; use lexopt::prelude::*; use std::env; -use std::ffi::OsString; use std::path::PathBuf; use walkdir::WalkDir; @@ -28,7 +27,8 @@ use walkdir::WalkDir; pub struct CLIArgs { pub helparg: bool, pub versionarg: bool, - pub fileargs: Vec<PathBuf>, + pub pos_args: Vec<PathBuf>, + pub files: Vec<PathBuf>, } impl CLIArgs { @@ -40,40 +40,48 @@ impl CLIArgs { match arg { Short('h') | Long("help") => args.helparg = true, Short('v') | Long("version") => args.versionarg = true, - Value(pos_arg) => parse_files(&mut args, pos_arg), + // 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) } } -pub fn parse_files(args: &mut CLIArgs, pos_arg: OsString) { +fn parse_files(args: Vec<PathBuf>) -> Vec<PathBuf> { // convert to PathBuf to use methods for testing the path - let path = PathBuf::from(pos_arg); + // let path = PathBuf::from(pos_arg); + let mut files: Vec<PathBuf> = Vec::new(); // If pos arg is file, just push it to path vec - if path.is_file() { - args.fileargs.push(path); - // If pos arg is dir, walk dir and collect bibfiles - } else if path.is_dir() { - for file in WalkDir::new(path) { - let f = file.unwrap().into_path(); - if f.is_file() && f.extension().unwrap() == "bib" { - args.fileargs.push(f) + for i in args { + if i.is_file() { + files.push(i); + // If pos arg is dir, walk dir and collect bibfiles + } else if i.is_dir() { + for file in WalkDir::new(i) { + let f = file.unwrap().into_path(); + if f.is_file() && f.extension().unwrap_or_default() == "bib" { + files.push(f) + } } + } else { + println!( + "{}\n{}", + "The positional argument is neither a valid file, nor a directory:" + .red() + .bold(), + i.as_os_str().to_string_lossy().bright_red().italic() + ); + println!(); + println!("{}", help_func()); + std::process::exit(1) } - } else { - println!( - "{}\n{}", - "The positional argument is neither a valid file, nor a directory:" - .red() - .bold(), - path.as_os_str().to_string_lossy().bright_red().italic() - ); - println!(); - println!("{}", help_func()); - std::process::exit(1) } + files } pub fn help_func() -> String { diff --git a/src/tui/ui.rs b/src/tui/ui.rs index ddb9080..45dfbcc 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -367,16 +367,23 @@ pub fn render_file_info(app: &mut App, args: &CLIArgs, frame: &mut Frame, rect: .horizontal_margin(1) .areas(rect); - let file_info = if args.fileargs.len() == 1 { + let file_info = if args.pos_args.len() == 1 && args.pos_args.first().unwrap().is_file() { Line::from(vec![ Span::raw("File: ").bold(), - Span::raw(args.fileargs[0].file_name().unwrap().to_string_lossy()).bold(), + Span::raw(args.pos_args[0].file_name().unwrap().to_string_lossy()).bold(), + ]) + .bg(HEADER_FOOTER_BG) + } else if args.pos_args.len() == 1 && args.pos_args.first().unwrap().is_dir() { + Line::from(vec![ + Span::raw("Directory: ").bold(), + Span::raw(args.pos_args[0].file_name().unwrap().to_string_lossy()).bold(), + Span::raw("/*.bib").bold(), ]) .bg(HEADER_FOOTER_BG) } else { Line::from(vec![ Span::raw("Multiple files (").bold(), - Span::raw(count_files(&args.fileargs).to_string()).bold(), + Span::raw(count_files(&args.files).to_string()).bold(), Span::raw(")"), ]) .bg(HEADER_FOOTER_BG) |
