aboutsummaryrefslogtreecommitdiff
path: root/src/bibiman.rs
diff options
context:
space:
mode:
authorlukeflo2025-05-30 13:39:24 +0200
committerlukeflo2025-05-30 13:39:24 +0200
commit0eae6de6df392fb3b8fa9d39dde42cecff97d240 (patch)
tree347c0b89587d68e0341e483571438d334cd91d00 /src/bibiman.rs
parent5ab55a263a5ae9cc5cbadf52f6000621e2552f85 (diff)
downloadbibiman-0eae6de6df392fb3b8fa9d39dde42cecff97d240.tar.gz
bibiman-0eae6de6df392fb3b8fa9d39dde42cecff97d240.zip
impl open_popup function for all popup cases
Diffstat (limited to 'src/bibiman.rs')
-rw-r--r--src/bibiman.rs73
1 files changed, 52 insertions, 21 deletions
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<Vec<(String, String)>>,
- ) {
+ ) -> 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(),