diff options
| author | lukeflo | 2024-12-23 21:03:48 +0100 |
|---|---|---|
| committer | lukeflo | 2024-12-23 21:03:48 +0100 |
| commit | 57126d7f42b871aa1835b056fbe74179c13e53b0 (patch) | |
| tree | bf81db50446aa7a8e06553f4e8aff97ea0cfb816 /src/app.rs | |
| parent | ad5c2cb586616eca99fc1db0efaaa0ff5aa97144 (diff) | |
| parent | 9a33a794167d60ce35030f007674f6e9424b1ff3 (diff) | |
| download | bibiman-57126d7f42b871aa1835b056fbe74179c13e53b0.tar.gz bibiman-57126d7f42b871aa1835b056fbe74179c13e53b0.zip | |
Merge branch 'add-entry-via-doi'
+ implement the functionality to add an entry via DOI
+ responsive error messages if resolving of DOI don't work
+ keep changes to choosen file to minimum
Diffstat (limited to 'src/app.rs')
| -rw-r--r-- | src/app.rs | 116 |
1 files changed, 72 insertions, 44 deletions
@@ -23,6 +23,7 @@ use crate::tui::commands::InputCmdAction; use crate::tui::popup::PopupKind; use crate::tui::{self, Tui}; use crate::{bibiman::Bibiman, tui::commands::CmdAction}; +use core::panic; use std::ffi::OsStr; use std::path::PathBuf; use std::process::{Command, Stdio}; @@ -58,7 +59,7 @@ impl App { }) } - pub async fn run(&mut self, args: &CLIArgs) -> Result<()> { + pub async fn run(&mut self, args: &mut CLIArgs) -> Result<()> { let mut tui = tui::Tui::new()?; tui.enter()?; @@ -109,29 +110,61 @@ impl App { self.running = false; } - pub fn run_command(&mut self, cmd: CmdAction, args: &CLIArgs, tui: &mut Tui) -> Result<()> { + pub fn run_command(&mut self, cmd: CmdAction, args: &mut CLIArgs, tui: &mut Tui) -> Result<()> { match cmd { CmdAction::Input(cmd) => match cmd { InputCmdAction::Nothing => {} InputCmdAction::Handle(event) => { self.input.handle_event(&event); - self.bibiman.search_list_by_pattern(&self.input); + if let CurrentArea::SearchArea = self.bibiman.current_area { + self.bibiman.search_list_by_pattern(&self.input); + } } InputCmdAction::Enter => { self.input_mode = true; // Logic for TABS to be added - self.bibiman.enter_search_area(); + // self.bibiman.enter_search_area(); } InputCmdAction::Confirm => { + // Logic for TABS to be added + if let CurrentArea::SearchArea = self.bibiman.current_area { + self.bibiman.confirm_search(); + } else if let CurrentArea::PopupArea = self.bibiman.current_area { + match self.bibiman.popup_area.popup_kind { + Some(PopupKind::AddEntry) => { + let doi = self.input.value(); + self.bibiman.close_popup(); + self.input_mode = false; + // Check if the DOI pattern is valid. If not, show warning and break + if doi.starts_with("10.") + || doi.starts_with("https://doi.org") + || doi.starts_with("https://dx.doi.org") + || doi.starts_with("http://doi.org") + || doi.starts_with("http://dx.doi.org") + { + self.bibiman.handle_new_entry_submission(args, doi); + } else { + self.bibiman.popup_area.popup_message( + "No valid DOI pattern: ", + doi, + false, + ); + } + } + _ => {} + } + } self.input = Input::default(); self.input_mode = false; - // Logic for TABS to be added - self.bibiman.confirm_search(); } InputCmdAction::Exit => { self.input = Input::default(); self.input_mode = false; - self.bibiman.break_search(); + if let CurrentArea::SearchArea = self.bibiman.current_area { + self.bibiman.break_search(); + } else if let CurrentArea::PopupArea = self.bibiman.current_area { + self.bibiman.close_popup(); + } } }, CmdAction::SelectNextRow(amount) => match self.bibiman.current_area { @@ -142,13 +175,15 @@ impl App { CurrentArea::TagArea => { self.bibiman.select_next_tag(amount); } - CurrentArea::PopupArea => { - if let Some(PopupKind::Help) = self.bibiman.popup_area.popup_kind { + CurrentArea::PopupArea => match self.bibiman.popup_area.popup_kind { + Some(PopupKind::Help) => { self.bibiman.popup_area.popup_scroll_down(); - } else if let Some(PopupKind::Selection) = self.bibiman.popup_area.popup_kind { + } + Some(PopupKind::OpenRes) | Some(PopupKind::AppendToFile) => { self.bibiman.popup_area.popup_state.scroll_down_by(1) } - } + _ => {} + }, _ => {} }, CmdAction::SelectPrevRow(amount) => match self.bibiman.current_area { @@ -159,13 +194,15 @@ impl App { CurrentArea::TagArea => { self.bibiman.select_previous_tag(amount); } - CurrentArea::PopupArea => { - if let Some(PopupKind::Help) = self.bibiman.popup_area.popup_kind { + CurrentArea::PopupArea => match self.bibiman.popup_area.popup_kind { + Some(PopupKind::Help) => { self.bibiman.popup_area.popup_scroll_up(); - } else if let Some(PopupKind::Selection) = self.bibiman.popup_area.popup_kind { + } + Some(PopupKind::OpenRes) | Some(PopupKind::AppendToFile) => { self.bibiman.popup_area.popup_state.scroll_up_by(1) } - } + _ => {} + }, _ => {} }, CmdAction::SelectNextCol => { @@ -205,14 +242,20 @@ impl App { CmdAction::ToggleArea => { self.bibiman.toggle_area(); } - CmdAction::SearchList => {} + CmdAction::SearchList => { + self.input_mode = true; + self.bibiman.enter_search_area(); + } CmdAction::Reset => { if let CurrentArea::PopupArea = self.bibiman.current_area { if let Some(PopupKind::Help) = self.bibiman.popup_area.popup_kind { self.bibiman.popup_area.popup_scroll_pos = 0; self.bibiman.close_popup() - } else if let Some(PopupKind::Selection) = self.bibiman.popup_area.popup_kind { + } else if let Some(PopupKind::OpenRes) = self.bibiman.popup_area.popup_kind { self.bibiman.close_popup() + } else if let Some(PopupKind::AppendToFile) = self.bibiman.popup_area.popup_kind + { + self.bibiman.close_popup(); } } else { self.bibiman.reset_current_list(); @@ -224,33 +267,11 @@ impl App { } else if let CurrentArea::PopupArea = self.bibiman.current_area { if let Some(PopupKind::Help) = self.bibiman.popup_area.popup_kind { self.bibiman.close_popup(); - } else if let Some(PopupKind::Selection) = self.bibiman.popup_area.popup_kind { - // Index of selected entry - let entry_idx = self - .bibiman - .entry_table - .entry_table_state - .selected() - .unwrap(); - - // Index of selected popup field - let popup_idx = self.bibiman.popup_area.popup_state.selected().unwrap(); - - // Choose ressource depending an selected popup field - if self.bibiman.popup_area.popup_list[popup_idx].contains("Weblink") { - let object = - self.bibiman.entry_table.entry_table_items[entry_idx].doi_url(); - let url = prepare_weblink(object); - open_connected_link(&url)?; - } else if self.bibiman.popup_area.popup_list[popup_idx].contains("File") { - let object = - self.bibiman.entry_table.entry_table_items[entry_idx].filepath(); - open_connected_file(object)?; - } else { - eprintln!("Unable to find ressource to open"); - }; - // run command to open file/Url - self.bibiman.close_popup() + } else if let Some(PopupKind::OpenRes) = self.bibiman.popup_area.popup_kind { + self.bibiman.open_connected_res()?; + } else if let Some(PopupKind::AppendToFile) = self.bibiman.popup_area.popup_kind + { + self.bibiman.append_entry_to_file(args)? } } } @@ -299,6 +320,7 @@ impl App { if entry.filepath.is_some() { items.push("File (PDF/EPUB)".to_owned()) } + self.bibiman.popup_area.popup_kind = Some(PopupKind::OpenRes); self.bibiman.popup_area.popup_selection(items); self.bibiman.former_area = Some(FormerArea::EntryArea); self.bibiman.current_area = CurrentArea::PopupArea; @@ -312,6 +334,12 @@ impl App { } } } + CmdAction::AddEntry => { + if let CurrentArea::EntryArea = self.bibiman.current_area { + self.input_mode = true; + self.bibiman.add_entry(); + } + } CmdAction::ShowHelp => { self.bibiman.show_help(); } |
