diff options
| author | lukeflo | 2025-02-17 20:55:46 +0100 |
|---|---|---|
| committer | lukeflo | 2025-02-17 20:55:46 +0100 |
| commit | d443843d352d740b895c4d622eb9af9567aa7423 (patch) | |
| tree | 5b61ec01b2d54461c423b97e2a702e7ec7daded9 /src | |
| parent | 059591a1be6b887eaca9b114fdb5b350a65bae43 (diff) | |
| download | bibiman-d443843d352d740b895c4d622eb9af9567aa7423.tar.gz bibiman-d443843d352d740b895c4d622eb9af9567aa7423.zip | |
improve file handling
+ If config file **and** CLI args have different files/dirs, concat them and open all
+ Adapt UI to show which files are choosen
+ TODO: Flag for ignoring config file
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.rs | 5 | ||||
| -rw-r--r-- | src/bibiman.rs | 56 | ||||
| -rw-r--r-- | src/bibiman/bibisetup.rs | 2 | ||||
| -rw-r--r-- | src/cliargs.rs | 6 | ||||
| -rw-r--r-- | src/config.rs | 15 | ||||
| -rw-r--r-- | src/main.rs | 8 | ||||
| -rw-r--r-- | src/tui/ui.rs | 30 |
7 files changed, 70 insertions, 52 deletions
@@ -16,6 +16,7 @@ ///// use crate::bibiman::{CurrentArea, FormerArea}; +use crate::config::BibiConfig; use color_eyre::eyre::{Context, Ok, Result}; // use super::Event; use crate::cliargs::CLIArgs; @@ -46,11 +47,11 @@ pub struct App { impl App { // Constructs a new instance of [`App`]. - pub fn new(args: &CLIArgs) -> Result<Self> { + pub fn new(args: &mut CLIArgs, cfg: &mut BibiConfig) -> Result<Self> { // Self::default() let running = true; let input = Input::default(); - let bibiman = Bibiman::new(args)?; + let bibiman = Bibiman::new(args, cfg)?; Ok(Self { running, bibiman, diff --git a/src/bibiman.rs b/src/bibiman.rs index 71288ce..6aa138d 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -15,12 +15,13 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. ///// -use crate::app; use crate::bibiman::entries::EntryTableColumn; use crate::bibiman::{bibisetup::*, search::BibiSearch}; use crate::cliargs::CLIArgs; +use crate::config::BibiConfig; use crate::tui::popup::{PopupArea, PopupKind}; use crate::tui::Tui; +use crate::{app, cliargs}; use crate::{bibiman::entries::EntryTable, bibiman::keywords::TagList}; use arboard::Clipboard; use color_eyre::eyre::Result; @@ -61,7 +62,7 @@ pub enum FormerArea { #[derive(Debug)] pub struct Bibiman { // main bib file - // pub main_bibfiles: Vec<PathBuf>, + pub main_bibfiles: Vec<PathBuf>, // main bibliography pub main_biblio: BibiSetup, // search struct: @@ -82,15 +83,17 @@ pub struct Bibiman { 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.files); + pub fn new(args: &mut CLIArgs, cfg: &mut BibiConfig) -> Result<Self> { + let mut main_bibfiles: Vec<PathBuf> = args.pos_args.clone(); + main_bibfiles.append(&mut cfg.general.bibfiles); + let main_bibfiles = cliargs::parse_files(main_bibfiles); + 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.clone()); let current_area = CurrentArea::EntryArea; Ok(Self { - // main_bibfiles, + main_bibfiles, main_biblio, tag_list, search_struct, @@ -128,8 +131,8 @@ impl Bibiman { self.former_area = None; } - pub fn update_lists(&mut self, args: &CLIArgs) { - self.main_biblio = BibiSetup::new(&args.files); + pub fn update_lists(&mut self) { + 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.clone()); } @@ -338,17 +341,17 @@ impl Bibiman { // Check if multiple files were passed to bibiman and // return the correct file path - let filepath = if args.files.len() == 1 { - args.files.first().unwrap().as_os_str() + let filepath = if self.main_bibfiles.len() == 1 { + self.main_bibfiles.first().unwrap().as_os_str() } else { let mut idx = 0; - for f in &args.files { + for f in &self.main_bibfiles { if search::search_pattern_in_file(&citekey_pattern, &f).is_some() { break; } idx += 1; } - args.files[idx].as_os_str() + self.main_bibfiles[idx].as_os_str() }; let filecontent = fs::read_to_string(&filepath).unwrap(); @@ -391,7 +394,7 @@ impl Bibiman { tui.terminal.clear()?; // Update the database and the lists to show changes - Self::update_lists(self, args); + Self::update_lists(self); // Select entry which was selected before entering editor self.select_entry_by_citekey(citekey); @@ -431,7 +434,7 @@ impl Bibiman { .expect("Couldn't parse fetched entry into string"); self.popup_area.popup_sel_item = entry; self.popup_area.popup_kind = Some(PopupKind::AppendToFile); - self.append_to_file(args); + self.append_to_file(); self.former_area = Some(FormerArea::EntryArea); self.current_area = CurrentArea::PopupArea; self.popup_area.popup_state.select(Some(0)) @@ -441,14 +444,21 @@ impl Bibiman { } } - pub fn append_to_file(&mut self, args: &CLIArgs) { + pub fn append_to_file(&mut self) { let mut items = vec!["Create new file".to_owned()]; - if args.files.len() > 1 { - for f in args.files.clone() { + if self.main_bibfiles.len() > 1 { + for f in self.main_bibfiles.clone() { items.push(f.to_str().unwrap().to_owned()); } } else { - items.push(args.files.first().unwrap().to_str().unwrap().to_owned()); + items.push( + self.main_bibfiles + .first() + .unwrap() + .to_str() + .unwrap() + .to_owned(), + ); } self.popup_area.popup_selection(items); } @@ -472,8 +482,8 @@ impl Bibiman { let mut file = if self.popup_area.popup_list[popup_idx].contains("Create new file") { let citekey = PathBuf::from(&citekey); // Get path of current files - let path: PathBuf = if args.files[0].is_file() { - args.files[0].parent().unwrap().to_owned() + let path: PathBuf = if self.main_bibfiles[0].is_file() { + self.main_bibfiles[0].parent().unwrap().to_owned() } else { dirs::home_dir().unwrap() // home dir as fallback }; @@ -482,11 +492,11 @@ impl Bibiman { let newfile = path.join(citekey); - args.files.push(newfile.clone()); + self.main_bibfiles.push(newfile.clone()); File::create_new(newfile).unwrap() } else { - let file_path = &args.files[popup_idx - 1]; + let file_path = &self.main_bibfiles[popup_idx - 1]; // Check if similar citekey already exists let file_string = read_to_string(&file_path).unwrap(); @@ -522,7 +532,7 @@ impl Bibiman { // Write content to file file.write_all(self.popup_area.popup_sel_item.as_bytes())?; // Update the database and the lists to reflect the new content - self.update_lists(args); + self.update_lists(); self.close_popup(); // Select newly created entry diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index 21b3c4b..3f64d9c 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -144,7 +144,7 @@ impl BibiSetup { if main_bibfiles.is_empty() { println!( "{}", - "No bibfile passed as argument. Please select a valid file." + "No bibfile passed as argument or through config file. Please select a valid file." .red() .bold() ); diff --git a/src/cliargs.rs b/src/cliargs.rs index ecec93e..50ed6f5 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -31,7 +31,7 @@ pub struct CLIArgs { pub helparg: bool, pub versionarg: bool, pub pos_args: Vec<PathBuf>, - pub files: Vec<PathBuf>, + // pub files: Vec<PathBuf>, pub cfg_path: PathBuf, // INFO: AppColors struct later should be moved to config/app struct // when config file is implemented @@ -65,7 +65,7 @@ impl CLIArgs { } } - args.files = parse_files(args.pos_args.clone()); + // args.files = parse_files(args.pos_args.clone()); Ok(args) } @@ -74,7 +74,7 @@ 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 { diff --git a/src/config.rs b/src/config.rs index 2ef296a..a80cc13 100644 --- a/src/config.rs +++ b/src/config.rs @@ -30,16 +30,25 @@ pub struct BibiConfig { #[derive(Debug, Clone, Deserialize)] pub struct General { pub bibfiles: Vec<PathBuf>, - pub editor: String, + pub editor: Option<String>, } impl BibiConfig { - pub fn new(args: &mut CLIArgs) -> Result<Self, ConfigError> { + pub fn default(args: &CLIArgs) -> Self { + Self { + general: General { + bibfiles: args.pos_args.clone(), + editor: None, + }, + } + } + + pub fn new(args: &CLIArgs) -> Result<Self, ConfigError> { let mut cfg = config::Config::builder(); cfg = cfg.add_source( config::File::from(args.cfg_path.clone()) .format(FileFormat::Toml) - .required(true), + .required(false), ); cfg.build()?.try_deserialize() } diff --git a/src/main.rs b/src/main.rs index a0f69d1..94f5042 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,12 +45,16 @@ async fn main() -> Result<()> { std::process::exit(0); } - let cfg = BibiConfig::new(&mut parsed_args)?; + let mut cfg = if parsed_args.cfg_path.is_file() { + BibiConfig::new(&parsed_args)? + } else { + BibiConfig::default(&parsed_args) + }; init_error_hooks()?; // Create an application. - let mut app = App::new(&parsed_args)?; + let mut app = App::new(&mut parsed_args, &mut cfg)?; app.run(&mut parsed_args).await?; Ok(()) diff --git a/src/tui/ui.rs b/src/tui/ui.rs index d85f318..52d2d9a 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -410,27 +410,21 @@ pub fn render_file_info(app: &mut App, args: &CLIArgs, frame: &mut Frame, rect: .horizontal_margin(1) .areas(rect); - let file_info = if args.pos_args.len() == 1 && args.pos_args.first().unwrap().is_file() { + let file_info = if app.bibiman.main_bibfiles.len() == 1 + && app.bibiman.main_bibfiles.first().unwrap().is_file() + { Line::from(vec![ Span::raw("File: ") .fg(Color::Indexed(args.colors.main_text_color)) .bold(), - Span::raw(args.pos_args[0].file_name().unwrap().to_string_lossy()) - .fg(Color::Indexed(args.colors.main_text_color)) - .bold(), - ]) - .bg(Color::Indexed(args.colors.bar_bg_color)) - } else if args.pos_args.len() == 1 && args.pos_args.first().unwrap().is_dir() { - Line::from(vec![ - Span::raw("Directory: ") - .bold() - .fg(Color::Indexed(args.colors.main_text_color)), - Span::raw(args.pos_args[0].file_name().unwrap().to_string_lossy()) - .fg(Color::Indexed(args.colors.main_text_color)) - .bold(), - Span::raw("/*.bib") - .fg(Color::Indexed(args.colors.main_text_color)) - .bold(), + Span::raw( + app.bibiman.main_bibfiles[0] + .file_name() + .unwrap() + .to_string_lossy(), + ) + .fg(Color::Indexed(args.colors.main_text_color)) + .bold(), ]) .bg(Color::Indexed(args.colors.bar_bg_color)) } else { @@ -438,7 +432,7 @@ pub fn render_file_info(app: &mut App, args: &CLIArgs, frame: &mut Frame, rect: Span::raw("Multiple files (") .fg(Color::Indexed(args.colors.main_text_color)) .bold(), - Span::raw(count_files(&args.files).to_string()) + Span::raw(count_files(&app.bibiman.main_bibfiles).to_string()) .fg(Color::Indexed(args.colors.main_text_color)) .bold(), Span::raw(")") |
