aboutsummaryrefslogtreecommitdiff
path: root/src/bibiman.rs
diff options
context:
space:
mode:
authorlukeflo2025-02-24 15:29:47 +0000
committerlukeflo2025-02-24 15:29:47 +0000
commit5da77a2f812a0bb6e0057f7b2e2c642142fca125 (patch)
tree6be886693445a54e4cbd7b98151555eec220e863 /src/bibiman.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/bibiman.rs')
-rw-r--r--src/bibiman.rs71
1 files changed, 42 insertions, 29 deletions
diff --git a/src/bibiman.rs b/src/bibiman.rs
index 71288ce..e36d268 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,19 @@ 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();
+ if cfg.general.bibfiles.is_some() {
+ main_bibfiles.append(cfg.general.bibfiles.as_mut().unwrap())
+ };
+ 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 +133,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());
}
@@ -325,7 +330,7 @@ impl Bibiman {
self.entry_table.entry_table_state.select(Some(idx_count));
}
- pub fn run_editor(&mut self, args: &CLIArgs, tui: &mut Tui) -> Result<()> {
+ pub fn run_editor(&mut self, cfg: &BibiConfig, tui: &mut Tui) -> Result<()> {
// get filecontent and citekey for calculating line number
let citekey: &str = &self.entry_table.entry_table_items
[self.entry_table.entry_table_state.selected().unwrap()]
@@ -338,17 +343,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();
@@ -376,6 +381,7 @@ impl Bibiman {
tui.exit()?;
// Use VISUAL or EDITOR. Set "vi" as last fallback
let mut cmd: Command = EditorBuilder::new()
+ .source(cfg.general.editor.as_ref())
.environment()
.source(Some("vi"))
.build()
@@ -391,7 +397,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);
@@ -412,7 +418,7 @@ impl Bibiman {
///the new entry via `append_to_file()` function. If not, show error popup
///
///The method needs two arguments: the CLIArgs struct and the `str` containing the DOI
- pub fn handle_new_entry_submission(&mut self, args: &CLIArgs, doi_string: &str) {
+ pub fn handle_new_entry_submission(&mut self, doi_string: &str) {
let doi_string = if doi_string.starts_with("10.") {
"https://doi.org/".to_string() + doi_string
} else {
@@ -431,7 +437,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,19 +447,26 @@ 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);
}
- pub fn append_entry_to_file(&mut self, args: &mut CLIArgs) -> Result<()> {
+ pub fn append_entry_to_file(&mut self) -> Result<()> {
// Index of selected popup field
let popup_idx = self.popup_area.popup_state.selected().unwrap();
@@ -472,8 +485,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 +495,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 +535,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
@@ -531,7 +544,7 @@ impl Bibiman {
Ok(())
}
- pub fn open_connected_res(&mut self) -> Result<()> {
+ pub fn open_connected_res(&mut self, cfg: &BibiConfig) -> Result<()> {
// Index of selected entry
let entry_idx = self.entry_table.entry_table_state.selected().unwrap();
@@ -542,10 +555,10 @@ impl Bibiman {
if self.popup_area.popup_list[popup_idx].contains("Weblink") {
let object = self.entry_table.entry_table_items[entry_idx].doi_url();
let url = app::prepare_weblink(object);
- app::open_connected_link(&url)?;
+ app::open_connected_link(cfg, &url)?;
} else if self.popup_area.popup_list[popup_idx].contains("File") {
let object = self.entry_table.entry_table_items[entry_idx].filepath();
- app::open_connected_file(object)?;
+ app::open_connected_file(cfg, object)?;
} else {
eprintln!("Unable to find ressource to open");
};