From 0eae6de6df392fb3b8fa9d39dde42cecff97d240 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Fri, 30 May 2025 13:39:24 +0200 Subject: impl open_popup function for all popup cases --- src/app.rs | 23 +++++++++--------- src/bibiman.rs | 73 +++++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 64 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 23230ba..92d89ba 100644 --- a/src/app.rs +++ b/src/app.rs @@ -15,7 +15,7 @@ // along with this program. If not, see . ///// -use crate::bibiman::{CurrentArea, FormerArea}; +use crate::bibiman::CurrentArea; use crate::config::BibiConfig; use color_eyre::eyre::{Context, Ok, Result}; // use super::Event; @@ -142,13 +142,14 @@ impl App { || doi.starts_with("http://doi.org") || doi.starts_with("http://dx.doi.org") { - self.bibiman.handle_new_entry_submission(doi); + self.bibiman.handle_new_entry_submission(doi)?; } else { - self.bibiman.popup_area.popup_message( - "No valid DOI pattern: ", - doi, - false, - ); + self.bibiman.open_popup( + PopupKind::MessageError, + Some("No valid DOI pattern: "), + Some(doi), + None, + )?; } } _ => {} @@ -317,7 +318,7 @@ impl App { // self.bibiman.current_area = CurrentArea::PopupArea; // self.bibiman.popup_area.popup_state.select(Some(0)); self.bibiman - .open_popup(PopupKind::YankItem, None, None, Some(items)); + .open_popup(PopupKind::YankItem, None, None, Some(items))?; } } CmdAction::EditFile => { @@ -355,14 +356,14 @@ impl App { // self.bibiman.popup_area.popup_state.select(Some(0)) self.bibiman - .open_popup(PopupKind::OpenRes, None, None, Some(items)); + .open_popup(PopupKind::OpenRes, None, None, Some(items))?; } else { self.bibiman.open_popup( PopupKind::MessageError, Some("Selected entry has no connected ressources: "), Some(&entry.citekey), None, - ); + )?; // self.bibiman.popup_area.popup_message( // "Selected entry has no connected ressources: ", // &entry.citekey, @@ -378,7 +379,7 @@ impl App { } } CmdAction::ShowHelp => { - self.bibiman.open_popup(PopupKind::Help, None, None, None); + self.bibiman.open_popup(PopupKind::Help, None, None, None)?; } CmdAction::Exit => { self.quit(); diff --git a/src/bibiman.rs b/src/bibiman.rs index 9606c19..e3509a9 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -24,7 +24,7 @@ use crate::tui::Tui; use crate::{app, cliargs}; use crate::{bibiman::entries::EntryTable, bibiman::keywords::TagList}; use arboard::Clipboard; -use color_eyre::eyre::Result; +use color_eyre::eyre::{Error, Result}; use editor_command::EditorBuilder; use ratatui::widgets::ScrollbarState; use regex::Regex; @@ -147,16 +147,19 @@ impl Bibiman { /// values `MessageConfirm`, `MessageError` and `YankItem`. If not needed, set it /// to `None`. /// - `items`: A vector of items which are needed if a selectable list is rendered. - /// The vector consists of tuples including a pair of `&str`. The second item of + /// The vector consists of tuples including a pair of `String`. The second item of /// the tuple is considered kind of an object which can be used e.g. to open /// the given filepath etc. If not needed, set it to `None`. + /// + /// The function will panic if a needed argument for the particular `PopupKind` + /// is missing pub fn open_popup( &mut self, popup_kind: PopupKind, message: Option<&str>, object: Option<&str>, items: Option>, - ) { + ) -> Result<()> { if let CurrentArea::EntryArea = self.current_area { self.former_area = Some(FormerArea::EntryArea); } else if let CurrentArea::TagArea = self.current_area { @@ -168,42 +171,69 @@ impl Bibiman { match popup_kind { PopupKind::Help => { self.popup_area.popup_kind = Some(PopupKind::Help); + Ok(()) } PopupKind::MessageConfirm => { self.popup_area.popup_kind = Some(PopupKind::MessageConfirm); if object.is_some() && message.is_some() { - self.popup_area.popup_message = message.unwrap().to_owned() + object.unwrap() + self.popup_area.popup_message = message.unwrap().to_owned() + object.unwrap(); + Ok(()) } else if object.is_none() && message.is_some() { - self.popup_area.popup_message = message.unwrap().to_owned() + self.popup_area.popup_message = message.unwrap().to_owned(); + Ok(()) } else { - return; + Err(Error::msg("You need to past at least a message via Some(&str) to create a message popup")) } } PopupKind::MessageError => { self.popup_area.popup_kind = Some(PopupKind::MessageError); if object.is_some() && message.is_some() { - self.popup_area.popup_message = message.unwrap().to_owned() + object.unwrap() + self.popup_area.popup_message = message.unwrap().to_owned() + object.unwrap(); + Ok(()) } else if object.is_none() && message.is_some() { - self.popup_area.popup_message = message.unwrap().to_owned() + self.popup_area.popup_message = message.unwrap().to_owned(); + Ok(()) } else { - return; + Err(Error::msg("You need to past at least a message via Some(&str) to create a message popup")) } } PopupKind::OpenRes => { - self.popup_area.popup_kind = Some(PopupKind::OpenRes); - self.popup_area.popup_selection(items.unwrap()); - self.popup_area.popup_state.select(Some(0)); + if items.is_some() { + self.popup_area.popup_kind = Some(PopupKind::OpenRes); + self.popup_area.popup_selection(items.unwrap()); + self.popup_area.popup_state.select(Some(0)); + Ok(()) + } else { + Err(Error::msg( + "No Vec<(String, String)> passed as argument to generate the items list", + )) + } } PopupKind::AppendToFile => { - self.popup_area.popup_kind = Some(PopupKind::AppendToFile); + if items.is_some() { + self.popup_area.popup_kind = Some(PopupKind::AppendToFile); + Ok(()) + } else { + Err(Error::msg( + "No Vec<(String, String)> passed as argument to generate the items list", + )) + } } PopupKind::AddEntry => { self.popup_area.popup_kind = Some(PopupKind::AddEntry); + Ok(()) } PopupKind::YankItem => { - self.popup_area.popup_kind = Some(PopupKind::YankItem); - self.popup_area.popup_selection(items.unwrap()); - self.popup_area.popup_state.select(Some(0)); + if items.is_some() { + self.popup_area.popup_kind = Some(PopupKind::YankItem); + self.popup_area.popup_selection(items.unwrap()); + self.popup_area.popup_state.select(Some(0)); + Ok(()) + } else { + Err(Error::msg( + "No Vec<(String, String)> passed as argument to generate the items list", + )) + } } } } @@ -493,7 +523,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, doi_string: &str) { + pub fn handle_new_entry_submission(&mut self, doi_string: &str) -> Result<()> { let doi_string = if doi_string.starts_with("10.") { "https://doi.org/".to_string() + doi_string } else { @@ -522,10 +552,11 @@ impl Bibiman { Some("Can't find DOI: "), Some(&doi_string), None, - ); + )?; // self.popup_area // .popup_message("Can't find DOI: ", &doi_string, false); } + Ok(()) } pub fn append_to_file(&mut self) { @@ -677,7 +708,7 @@ impl Bibiman { Some("Yanked citekey to clipboard: "), Some(citekey.clone().as_str()), None, - ); + )?; // self.popup_area.popup_message( // "Yanked citekey to clipboard: ", // citekey, // self.bibiman.get_selected_citekey(), @@ -693,7 +724,7 @@ impl Bibiman { Some("Yanked weblink to clipboard: "), Some(l.clone().as_str()), None, - ); + )?; // self.popup_area.popup_message( // "Yanked weblink to clipboard: ", // l, // self.bibiman.get_selected_link(), @@ -714,7 +745,7 @@ impl Bibiman { Some("Yanked filepath to clipboard: "), Some(p), None, - ); + )?; // self.popup_area.popup_message( // "Yanked filepath to clipboard: ", // p, // self.bibiman.get_selected_link(), -- cgit v1.2.3