diff options
| author | lukeflo | 2024-11-14 15:54:58 +0100 |
|---|---|---|
| committer | lukeflo | 2024-11-14 15:54:58 +0100 |
| commit | f251969044036ecfbc2cd4774c9ecb97b1992bb3 (patch) | |
| tree | a5d3fc06f4eeee62280dced32eca47d297577935 | |
| parent | ca51eea300b84a6fa865c6f3e00823a8099bb9bc (diff) | |
| download | bibiman-f251969044036ecfbc2cd4774c9ecb97b1992bb3.tar.gz bibiman-f251969044036ecfbc2cd4774c9ecb97b1992bb3.zip | |
refined complicated code parts
| -rw-r--r-- | src/app.rs | 4 | ||||
| -rw-r--r-- | src/bibiman.rs | 24 | ||||
| -rw-r--r-- | src/bibiman/bibisetup.rs | 181 | ||||
| -rw-r--r-- | src/bibiman/entries.rs | 8 | ||||
| -rw-r--r-- | src/bibiman/search.rs | 33 | ||||
| -rw-r--r-- | src/cliargs.rs | 6 | ||||
| -rw-r--r-- | src/tui/popup.rs | 49 | ||||
| -rw-r--r-- | src/tui/ui.rs | 29 |
8 files changed, 120 insertions, 214 deletions
@@ -228,9 +228,9 @@ impl App { } CmdAction::YankItem => { if let CurrentArea::EntryArea = self.bibiman.current_area { - Bibiman::yank_text(&self.bibiman.get_selected_citekey()); + Bibiman::yank_text(self.bibiman.get_selected_citekey()); self.bibiman.popup_area.popup_message( - "Yanked citekey to clipboard:", + "Yanked citekey to clipboard:".to_owned(), self.bibiman.get_selected_citekey().to_string(), ); } diff --git a/src/bibiman.rs b/src/bibiman.rs index ed6a66f..a677132 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -303,8 +303,7 @@ impl Bibiman { // Get the citekey of the selected entry pub fn get_selected_citekey(&self) -> &str { let idx = self.entry_table.entry_table_state.selected().unwrap(); - let citekey = &self.entry_table.entry_table_items[idx].citekey; - citekey + &self.entry_table.entry_table_items[idx].citekey } pub fn run_editor(&mut self, tui: &mut Tui) -> Result<()> { @@ -317,10 +316,10 @@ impl Bibiman { let mut line_count = 0; for line in filecontent.lines() { - line_count = line_count + 1; + line_count += 1; // if reaching the citekey break the loop // if reaching end of lines without match, reset to 0 - if line.contains(&citekey) { + if line.contains(citekey) { break; } else if line_count == filecontent.len() { eprintln!( @@ -364,7 +363,7 @@ impl Bibiman { { break; } - idx_count = idx_count + 1 + idx_count += 1 } // Set selected entry to vec-index of match @@ -379,7 +378,7 @@ impl Bibiman { // so deleting a char, will show former entries too let orig_list = self.entry_table.entry_table_at_search_start.clone(); let filtered_list = - BibiSearch::search_entry_list(&mut self.search_struct.search_string, orig_list.clone()); + BibiSearch::search_entry_list(&self.search_struct.search_string, orig_list.clone()); self.entry_table.entry_table_items = filtered_list; self.entry_table.sort_entry_table(false); self.entry_table.entry_scroll_state = ScrollbarState::content_length( @@ -406,7 +405,7 @@ impl Bibiman { // Pass filepath as argument, pipe stdout and stderr to /dev/null // to keep the TUI clean (where is it piped on Windows???) let _ = Command::new(&cmd) - .arg(&filepath.as_ref().unwrap()) + .arg(filepath.as_ref().unwrap()) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn() @@ -427,10 +426,10 @@ impl Bibiman { // terminal is not blocked by the resolving process let url = if web_adress.starts_with("10.") { let prefix = "https://doi.org/".to_string(); - prefix + &web_adress + prefix + web_adress } else if web_adress.starts_with("www.") { let prefix = "https://".to_string(); - prefix + &web_adress + prefix + web_adress } else { web_adress.to_string() }; @@ -496,8 +495,7 @@ impl Bibiman { pub fn get_selected_tag(&self) -> &str { let idx = self.tag_list.tag_list_state.selected().unwrap(); - let keyword = &self.tag_list.tag_list_items[idx]; - keyword + &self.tag_list.tag_list_items[idx] } pub fn search_tags(&mut self) { @@ -529,7 +527,7 @@ impl Bibiman { } } - filtered_keywords.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); + filtered_keywords.sort_by_key(|a| a.to_lowercase()); filtered_keywords.dedup(); self.search_struct.filtered_tag_list = filtered_keywords.clone(); @@ -546,7 +544,7 @@ impl Bibiman { pub fn filter_for_tags(&mut self) { let orig_list = &self.entry_table.entry_table_items; let keyword = self.get_selected_tag(); - let filtered_list = BibiSearch::filter_entries_by_tag(&keyword, &orig_list); + let filtered_list = BibiSearch::filter_entries_by_tag(keyword, orig_list); // self.tag_list.selected_keyword = keyword.to_string(); self.tag_list.selected_keywords.push(keyword.to_string()); self.entry_table.entry_table_items = filtered_list; diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index 027e217..e12aa8f 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -98,7 +98,7 @@ impl BibiSetup { .red() .bold() ); - println!(""); + println!(); println!("{}", cliargs::help_func()); // panic!("No file passed as argument. Please choose a .bib file.") std::process::exit(1) @@ -107,18 +107,18 @@ impl BibiSetup { fn create_entry_list(citekeys: &[String], bibliography: &Bibliography) -> Vec<BibiData> { citekeys - .into_iter() + .iter() .map(|k| BibiData { - authors: Self::get_authors(&k, &bibliography), - title: Self::get_title(&k, &bibliography), - year: Self::get_year(&k, &bibliography), - pubtype: Self::get_pubtype(&k, &bibliography), - keywords: Self::get_keywords(&k, &bibliography), + authors: Self::get_authors(k, bibliography), + title: Self::get_title(k, bibliography), + year: Self::get_year(k, bibliography), + pubtype: Self::get_pubtype(k, bibliography), + keywords: Self::get_keywords(k, bibliography), citekey: k.to_owned(), - abstract_text: Self::get_abstract(&k, &bibliography), - doi_url: Self::get_weblink(&k, &bibliography), - filepath: Self::get_filepath(&k, &bibliography), - subtitle: Self::get_subtitle(&k, &bibliography), + abstract_text: Self::get_abstract(k, bibliography), + doi_url: Self::get_weblink(k, bibliography), + filepath: Self::get_filepath(k, bibliography), + subtitle: Self::get_subtitle(k, bibliography), }) .collect() } @@ -140,13 +140,8 @@ impl BibiSetup { // Loop over entries and collect all keywords for i in citekeys { - if biblio.get(&i).unwrap().keywords().is_ok() { - let items = biblio - .get(&i) - .unwrap() - .keywords() - .unwrap() - .format_verbatim(); + if biblio.get(i).unwrap().keywords().is_ok() { + let items = biblio.get(i).unwrap().keywords().unwrap().format_verbatim(); // Split keyword string into slices, trim leading and trailing // whitespaces, remove empty slices, and collect them let mut key_vec: Vec<String> = items @@ -160,123 +155,91 @@ impl BibiSetup { } // Sort the vector and remove duplicates - keyword_list.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); + // keyword_list.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); + keyword_list.sort_by_key(|a| a.to_lowercase()); keyword_list.dedup(); keyword_list } pub fn get_authors(citekey: &str, biblio: &Bibliography) -> String { - if biblio.get(&citekey).unwrap().author().is_ok() { - let authors = biblio.get(&citekey).unwrap().author().unwrap(); + if biblio.get(citekey).unwrap().author().is_ok() { + let authors = biblio.get(citekey).unwrap().author().unwrap(); if authors.len() > 1 { - let all_authors = authors.iter().map(|a| &a.name).join(", "); - all_authors + authors.iter().map(|a| &a.name).join(", ") } else if authors.len() == 1 { - let authors = authors[0].name.to_string(); - authors + authors[0].name.to_string() } else { - let editors_authors = format!("empty"); - editors_authors + "empty".to_string() } - } else { - if !biblio.get(&citekey).unwrap().editors().unwrap().is_empty() { - let editors = biblio.get(&citekey).unwrap().editors().unwrap(); - if editors[0].0.len() > 1 { - // let editors = format!("{} (ed.) et al.", editors[0].0[0].name); - let mut editors = editors[0].0.iter().map(|e| &e.name).join(", "); - editors.push_str(" (ed.)"); - editors - } else if editors[0].0.len() == 1 { - let editors = format!("{} (ed.)", editors[0].0[0].name); - editors - } else { - let editors_authors = format!("empty"); - editors_authors - } + } else if !biblio.get(citekey).unwrap().editors().unwrap().is_empty() { + let editors = biblio.get(citekey).unwrap().editors().unwrap(); + if editors[0].0.len() > 1 { + format!("{} (ed.)", editors[0].0.iter().map(|e| &e.name).join(", ")) + } else if editors[0].0.len() == 1 { + format!("{} (ed.)", editors[0].0[0].name) } else { - let editors_authors = format!("empty"); - editors_authors + "empty".to_string() } + } else { + "empty".to_string() } } pub fn get_title(citekey: &str, biblio: &Bibliography) -> String { - let title = { - if biblio.get(&citekey).unwrap().title().is_ok() { - let title = biblio - .get(&citekey) - .unwrap() - .title() - .unwrap() - .format_verbatim(); - title - } else { - let title = format!("no title"); - title - } - }; - title + if biblio.get(citekey).unwrap().title().is_ok() { + biblio + .get(citekey) + .unwrap() + .title() + .unwrap() + .format_verbatim() + } else { + "no title".to_string() + } } pub fn get_year(citekey: &str, biblio: &Bibliography) -> String { - let bib = biblio.get(&citekey).unwrap(); - // let year = biblio.get(&citekey).unwrap(); - let year = { - if bib.date().is_ok() { - let year = bib.date().unwrap().to_chunks().format_verbatim(); - let year = year[..4].to_string(); - year - } else { - let year = format!("n.d."); - year - } - }; - year + let bib = biblio.get(citekey).unwrap(); + if bib.date().is_ok() { + let year = bib.date().unwrap().to_chunks().format_verbatim(); + year[..4].to_string() + } else { + "n.d.".to_string() + } } pub fn get_pubtype(citekey: &str, biblio: &Bibliography) -> String { - let pubtype = biblio.get(&citekey).unwrap().entry_type.to_string(); - pubtype + biblio.get(citekey).unwrap().entry_type.to_string() } pub fn get_keywords(citekey: &str, biblio: &Bibliography) -> String { - let keywords = { - if biblio.get(&citekey).unwrap().keywords().is_ok() { - let keywords = biblio - .get(&citekey) - .unwrap() - .keywords() - .unwrap() - .format_verbatim(); - keywords - } else { - let keywords = String::from(""); - keywords - } - }; - keywords + if biblio.get(citekey).unwrap().keywords().is_ok() { + biblio + .get(citekey) + .unwrap() + .keywords() + .unwrap() + .format_verbatim() + } else { + "".to_string() + } } pub fn get_abstract(citekey: &str, biblio: &Bibliography) -> String { - let text = { - if biblio.get(&citekey).unwrap().abstract_().is_ok() { - let abstract_text = biblio - .get(&citekey) - .unwrap() - .abstract_() - .unwrap() - .format_verbatim(); - abstract_text - } else { - let abstract_text = format!("No abstract"); - abstract_text - } - }; - text + if biblio.get(citekey).unwrap().abstract_().is_ok() { + biblio + .get(citekey) + .unwrap() + .abstract_() + .unwrap() + .format_verbatim() + } else { + "no abstract".to_string() + } } pub fn get_weblink(citekey: &str, biblio: &Bibliography) -> Option<String> { - let bib = biblio.get(&citekey).unwrap(); + let bib = biblio.get(citekey).unwrap(); if bib.doi().is_ok() { Some(bib.doi().unwrap()) } else if bib.url().is_ok() { @@ -287,18 +250,18 @@ impl BibiSetup { } pub fn get_filepath(citekey: &str, biblio: &Bibliography) -> Option<String> { - if biblio.get(&citekey).unwrap().file().is_ok() { - Some(biblio.get(&citekey).unwrap().file().unwrap()) + if biblio.get(citekey).unwrap().file().is_ok() { + Some(biblio.get(citekey).unwrap().file().unwrap()) } else { None } } pub fn get_subtitle(citekey: &str, biblio: &Bibliography) -> Option<String> { - if biblio.get(&citekey).unwrap().subtitle().is_ok() { + if biblio.get(citekey).unwrap().subtitle().is_ok() { Some( biblio - .get(&citekey) + .get(citekey) .unwrap() .subtitle() .unwrap() diff --git a/src/bibiman/entries.rs b/src/bibiman/entries.rs index d8fadcc..75c7db5 100644 --- a/src/bibiman/entries.rs +++ b/src/bibiman/entries.rs @@ -64,7 +64,7 @@ impl EntryTable { pub fn set_entry_table(entry_list: &[BibiData]) -> Vec<EntryTableItem> { let mut entry_table: Vec<EntryTableItem> = entry_list - .into_iter() + .iter() .map(|e| EntryTableItem { authors: e.authors.clone(), short_author: String::new(), @@ -196,15 +196,15 @@ impl EntryTableItem { } pub fn doi_url(&self) -> &str { - &self.doi_url.as_ref().unwrap() + self.doi_url.as_ref().unwrap() } pub fn filepath(&self) -> &str { - &self.filepath.as_ref().unwrap() + self.filepath.as_ref().unwrap() } pub fn subtitle(&self) -> &str { - &self.subtitle.as_ref().unwrap() + self.subtitle.as_ref().unwrap() } } diff --git a/src/bibiman/search.rs b/src/bibiman/search.rs index 896d1a4..b6b1b79 100644 --- a/src/bibiman/search.rs +++ b/src/bibiman/search.rs @@ -22,38 +22,25 @@ use nucleo_matcher::{ }; use std::collections::HashMap; -#[derive(Debug)] +#[derive(Debug, Default)] pub struct BibiSearch { pub search_string: String, // Search string show in footer, used for search pub inner_search: bool, // True, if we trigger a search for already filtered list pub filtered_tag_list: Vec<String>, } -impl Default for BibiSearch { - fn default() -> Self { - Self { - search_string: String::new(), - inner_search: false, - filtered_tag_list: Vec::new(), - } - } -} - impl BibiSearch { // Stringify EntryTableItem by joining/concat fn convert_to_string(inner_vec: &EntryTableItem) -> String { - let entry_table_item_str = { - format!( - "{} {} {} {} {} {}", - &inner_vec.authors, - &inner_vec.title, - &inner_vec.year, - &inner_vec.pubtype, - &inner_vec.keywords, - &inner_vec.citekey - ) - }; - entry_table_item_str + format!( + "{} {} {} {} {} {}", + &inner_vec.authors, + &inner_vec.title, + &inner_vec.year, + &inner_vec.pubtype, + &inner_vec.keywords, + &inner_vec.citekey + ) } // Return a filtered entry list diff --git a/src/cliargs.rs b/src/cliargs.rs index 9de2e7f..7fd512e 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -37,6 +37,12 @@ pub struct CLIArgs { pub bibfilearg: PathBuf, } +impl Default for CLIArgs { + fn default() -> Self { + Self::new() + } +} + impl CLIArgs { pub fn new() -> Self { let (cli_args, pos_args) = ArgumentsCLI::parse().expect("Could not parse CLI arguments"); diff --git a/src/tui/popup.rs b/src/tui/popup.rs index fe76956..9011a3b 100644 --- a/src/tui/popup.rs +++ b/src/tui/popup.rs @@ -30,7 +30,7 @@ pub enum PopupKind { Selection, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct PopupArea { pub is_popup: bool, pub popup_kind: Option<PopupKind>, @@ -40,19 +40,6 @@ pub struct PopupArea { pub popup_state: ListState, } -impl Default for PopupArea { - fn default() -> Self { - PopupArea { - is_popup: false, - popup_kind: None, - popup_message: String::new(), - popup_scroll_pos: 0, - popup_list: Vec::new(), - popup_state: ListState::default(), - } - } -} - impl PopupArea { pub fn popup_help<'a>() -> Text<'a> { let help = [ @@ -78,36 +65,11 @@ impl PopupArea { ("ENTER: ", "Filter by selected keyword"), ("Search", "sub"), ("↓,↑,←,→: ", "Move cursor"), + ("BACKSPACE: ", "Delete Character"), ("ENTER: ", "Confirm search"), ("ESC: ", "Abort search"), ]; - // let help_text: Vec<Line<'_>> = help - // .into_iter() - // .map(|(keys, help)| { - // if help == "first" { - // Line::from( - // Span::raw(keys) - // .bold() - // .fg(Color::Indexed(MAIN_BLUE_COLOR_INDEX)), - // ) - // } else if help == "sub" { - // Line::from( - // Span::raw(keys) - // .bold() - // .fg(Color::Indexed(MAIN_BLUE_COLOR_INDEX)), - // ) - // } else { - // Line::from(vec![ - // Span::raw(keys) - // .bold() - // .fg(Color::Indexed(MAIN_PURPLE_COLOR_INDEX)), - // Span::raw(help), - // ]) - // } - // }) - // .collect(); - let mut helptext: Vec<Line<'_>> = vec![]; for (keys, help) in help { @@ -134,13 +96,12 @@ impl PopupArea { } } - let text = Text::from(helptext); - text + Text::from(helptext) } - pub fn popup_message(&mut self, message: &str, object: String) { + pub fn popup_message(&mut self, message: String, object: String) { if object.is_empty() { - self.popup_message = message.into(); + self.popup_message = message; } else { self.popup_message = format!("{} \"{}\"", message, object); } diff --git a/src/tui/ui.rs b/src/tui/ui.rs index dd08291..fbfd40a 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -107,8 +107,8 @@ pub const fn color_list(list_item: i32, sel_item: i32, highlight: u8, max_diff: Color::Indexed(highlight) } else if (list_item - sel_item) > max_diff || (sel_item - list_item) > max_diff - || (-1 * (list_item - sel_item)) > max_diff - || (-1 * (sel_item - list_item)) > max_diff + || -(list_item - sel_item) > max_diff + || -(sel_item - list_item) > max_diff { Color::Indexed(highlight - max_diff as u8) } else if list_item < sel_item { @@ -152,7 +152,7 @@ pub fn render_ui(app: &mut App, frame: &mut Frame) { pub fn render_popup(app: &mut App, frame: &mut Frame) { if let Some(PopupKind::Help) = app.bibiman.popup_area.popup_kind { - let text: Text = Text::from(PopupArea::popup_help()); + let text: Text = PopupArea::popup_help(); // Calculate max scroll position depending on hight of terminal window let scroll_pos = if app.bibiman.popup_area.popup_scroll_pos @@ -175,7 +175,7 @@ pub fn render_popup(app: &mut App, frame: &mut Frame) { }; let popup = Popup::new(sized_par) - .title(" Keybindings ".bold().into_centered_line()) + .title(" Keybindings (j,k|↓,↑)".bold().into_centered_line()) .style(POPUP_HELP_BOX) .border_style(Style::new().fg(MAIN_BLUE)); @@ -204,18 +204,9 @@ pub fn render_footer(app: &mut App, frame: &mut Frame, rect: Rect) { CurrentArea::SearchArea => { let search_title = { match app.bibiman.former_area { - Some(FormerArea::EntryArea) => { - let search_title = " Search Entries ".to_string(); - search_title - } - Some(FormerArea::TagArea) => { - let search_title = " Search Keywords ".to_string(); - search_title - } - _ => { - let search_title = " Search ".to_string(); - search_title - } + Some(FormerArea::EntryArea) => " Search Entries ".to_string(), + Some(FormerArea::TagArea) => " Search Keywords ".to_string(), + _ => " Search ".to_string(), } }; @@ -520,7 +511,7 @@ pub fn render_entrytable(app: &mut App, frame: &mut Frame, rect: Rect) { .map(|(i, data)| { let item = data.ref_vec(); item.into_iter() - .map(|content| Cell::from(Text::from(format!("{content}")))) + .map(|content| Cell::from(Text::from(content.to_string()))) .collect::<Row>() .style( Style::new().fg(color_list( @@ -529,7 +520,7 @@ pub fn render_entrytable(app: &mut App, frame: &mut Frame, rect: Rect) { .entry_table .entry_table_state .selected() - .unwrap() as i32, + .unwrap_or(0) as i32, TEXT_HIGHLIGHT_COLOR_INDEX, 20, )), @@ -681,7 +672,7 @@ pub fn render_selected_item(app: &mut App, frame: &mut Frame, rect: Rect) { } else { let lines = vec![ Line::from(" "), - Line::from("No entry selected".bold().into_centered_line().red()), + "No entry selected".bold().into_centered_line().red(), ]; lines } |
