diff options
| -rw-r--r-- | Cargo.lock | 4 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/backend/bib.rs | 20 | ||||
| -rw-r--r-- | src/backend/search.rs | 1 | ||||
| -rw-r--r-- | src/frontend/entries.rs | 89 | ||||
| -rw-r--r-- | src/frontend/ui.rs | 21 | ||||
| -rw-r--r-- | src/main.rs | 6 | ||||
| -rw-r--r-- | tests/biblatex-test.bib | 2 |
8 files changed, 114 insertions, 31 deletions
@@ -636,9 +636,9 @@ dependencies = [ [[package]] name = "hayagriva" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3de14f15f4243222e84000669030b3a2fe3a0c6755fd4e38e8a92de039ec21ef" +checksum = "7a3635c2577f77499c9dc3dceeef2e64e6c146e711b1861507a0f15b20641348" dependencies = [ "biblatex", "ciborium", @@ -16,7 +16,7 @@ color-eyre = "0.6.3" crossterm = { version = "0.28.1", features = ["event-stream"] } editor-command = "0.1.1" futures = "0.3.30" -hayagriva = "0.7.0" +hayagriva = "0.8.0" itertools = "0.13.0" nucleo-matcher = "0.3.1" ratatui = { version = "0.28.1", features = ["unstable-rendered-line-info"]} diff --git a/src/backend/bib.rs b/src/backend/bib.rs index aca08b3..1f5d05d 100644 --- a/src/backend/bib.rs +++ b/src/backend/bib.rs @@ -17,6 +17,7 @@ use biblatex::{self, Bibliography}; use biblatex::{ChunksExt, Type}; +use itertools::Itertools; use std::{fs, path::PathBuf}; #[derive(Debug)] @@ -74,16 +75,13 @@ impl BibiMain { // since it is the entry point of the biblatex crate. pub fn get_citekeys(bibstring: &Bibliography) -> Vec<String> { let citekeys: Vec<String> = bibstring.keys().map(|k| k.to_owned()).collect(); - // let mut citekeys: Vec<String> = - // bibstring.iter().map(|entry| entry.to_owned().key).collect(); - // citekeys.sort_by_key(|name| name.to_lowercase()); citekeys } // collect all keywords present in the bibliography // sort them and remove duplicates // this list is for fast filtering entries by topics/keyowrds - pub fn collect_tag_list(citekeys: &Vec<String>, biblio: &Bibliography) -> Vec<String> { + pub fn collect_tag_list(citekeys: &[String], biblio: &Bibliography) -> Vec<String> { // Initialize vector collecting all keywords let mut keyword_list = vec![]; @@ -118,8 +116,8 @@ impl BibiMain { if biblio.get(&citekey).unwrap().author().is_ok() { let authors = biblio.get(&citekey).unwrap().author().unwrap(); if authors.len() > 1 { - let authors = format!("{} et al.", authors[0].name); - authors + let all_authors = authors.iter().map(|a| &a.name).join(", "); + all_authors } else if authors.len() == 1 { let authors = authors[0].name.to_string(); authors @@ -128,12 +126,14 @@ impl BibiMain { editors_authors } } else { - if biblio.get(&citekey).unwrap().editors().is_ok() { + if !biblio.get(&citekey).unwrap().editors().unwrap().is_empty() { let editors = biblio.get(&citekey).unwrap().editors().unwrap(); - if editors.len() > 1 { - let editors = format!("{} (ed.) et al.", editors[0].0[0].name); + 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.len() == 1 { + } else if editors[0].0.len() == 1 { let editors = format!("{} (ed.)", editors[0].0[0].name); editors } else { diff --git a/src/backend/search.rs b/src/backend/search.rs index 4576d2d..65d97d7 100644 --- a/src/backend/search.rs +++ b/src/backend/search.rs @@ -116,6 +116,7 @@ mod tests { fn test_vector_join() { let bibvec: EntryTableItem = EntryTableItem { authors: "Author".to_string(), + short_author: "".to_string(), title: "Title".to_string(), year: "1999".to_string(), pubtype: "article".to_string(), diff --git a/src/frontend/entries.rs b/src/frontend/entries.rs index 63a2a1b..f64e35e 100644 --- a/src/frontend/entries.rs +++ b/src/frontend/entries.rs @@ -17,7 +17,10 @@ use super::app::App; use super::tui::Tui; -use crate::backend::{bib::BibiMain, search::BibiSearch}; +use crate::backend::{ + bib::{BibiMain, FileFormat}, + search::BibiSearch, +}; use biblatex::Bibliography; use color_eyre::eyre::{Context, Ok, Result}; use core::panic; @@ -54,11 +57,12 @@ impl EntryTable { } } - pub fn set_entry_table(citekeys: &Vec<String>, biblio: &Bibliography) -> Vec<EntryTableItem> { + pub fn set_entry_table(citekeys: &[String], biblio: &Bibliography) -> Vec<EntryTableItem> { let mut entry_table: Vec<EntryTableItem> = citekeys .into_iter() .map(|key| EntryTableItem { authors: BibiMain::get_authors(&key, &biblio), + short_author: String::new(), title: BibiMain::get_title(&key, &biblio), year: BibiMain::get_year(&key, &biblio), pubtype: BibiMain::get_pubtype(&key, &biblio), @@ -114,6 +118,7 @@ impl EntryTable { #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct EntryTableItem { pub authors: String, + pub short_author: String, pub title: String, pub year: String, pub pubtype: String, @@ -127,8 +132,32 @@ pub struct EntryTableItem { impl EntryTableItem { // This functions decides which fields are rendered in the entry table // Fields which should be usable but not visible can be left out - pub fn ref_vec(&self) -> Vec<&String> { - vec![&self.authors, &self.title, &self.year, &self.pubtype] + pub fn ref_vec(&mut self) -> Vec<&str> { + self.short_author = match self.authors.split_once(",") { + Some((first, _rest)) => { + if self.authors.contains("(ed.)") { + let first_author = format!("{} et al. (ed.)", first); + first_author + } else { + let first_author = format!("{} et al.", first); + first_author + } + } + None => String::from(""), + }; + + vec![ + { + if self.short_author.is_empty() { + &self.authors + } else { + &self.short_author + } + }, + &self.title, + &self.year, + &self.pubtype, + ] } pub fn authors(&self) -> &str { @@ -150,6 +179,14 @@ impl EntryTableItem { pub fn citekey(&self) -> &str { &self.citekey } + + pub fn doi_url(&self) -> &str { + &self.doi_url + } + + pub fn filepath(&self) -> &str { + &self.filepath + } } impl App { @@ -355,6 +392,8 @@ impl App { #[cfg(test)] mod tests { + use super::EntryTableItem; + #[test] fn check_os() { let os = std::env::consts::OS; @@ -365,4 +404,46 @@ mod tests { std::env::consts::OS ) } + + #[test] + fn shorten_authors() { + let mut entry: EntryTableItem = EntryTableItem { + authors: "Miller, Schmitz, Bernard".to_string(), + short_author: "".to_string(), + title: "A title".to_string(), + year: "2000".to_string(), + pubtype: "article".to_string(), + keywords: "key1, key2".to_string(), + citekey: "miller_2000".to_string(), + abstract_text: "An abstract".to_string(), + doi_url: "www.text.org".to_string(), + filepath: "/home/test".to_string(), + }; + + let entry_vec = EntryTableItem::ref_vec(&mut entry); + + let mut entry_editors: EntryTableItem = EntryTableItem { + authors: "Miller, Schmitz, Bernard (ed.)".to_string(), + short_author: "".to_string(), + title: "A title".to_string(), + year: "2000".to_string(), + pubtype: "article".to_string(), + keywords: "key1, key2".to_string(), + citekey: "miller_2000".to_string(), + abstract_text: "An abstract".to_string(), + doi_url: "www.text.org".to_string(), + filepath: "/home/test".to_string(), + }; + + let entry_vec_editors = EntryTableItem::ref_vec(&mut entry_editors); + + assert_eq!( + entry_vec, + vec!["Miller et al.", "A title", "2000", "article"] + ); + assert_eq!( + entry_vec_editors, + vec!["Miller et al. (ed.)", "A title", "2000", "article"] + ) + } } diff --git a/src/frontend/ui.rs b/src/frontend/ui.rs index 1767cea..d94a654 100644 --- a/src/frontend/ui.rs +++ b/src/frontend/ui.rs @@ -28,7 +28,10 @@ use ratatui::{ }, }; -use crate::{frontend::app::App, frontend::keywords::TagListItem}; +use crate::{ + backend::bib::BibiMain, + frontend::{app::App, keywords::TagListItem}, +}; use super::app::{CurrentArea, FormerArea}; @@ -284,7 +287,7 @@ impl App { let rows = self .entry_table .entry_table_items - .iter() + .iter_mut() .enumerate() .map(|(_i, data)| { let item = data.ref_vec(); @@ -349,15 +352,16 @@ impl App { let mut lines = vec![]; lines.push(Line::from(vec![ Span::styled("Authors: ", style_value), - Span::styled(cur_entry.authors.clone(), Style::new().green()), + // Span::styled(cur_entry.authors.clone(), Style::new().green()), + Span::styled(cur_entry.authors(), Style::new().green()), ])); lines.push(Line::from(vec![ Span::styled("Title: ", style_value), - Span::styled(cur_entry.title.clone(), Style::new().magenta()), + Span::styled(cur_entry.title(), Style::new().magenta()), ])); lines.push(Line::from(vec![ Span::styled("Year: ", style_value), - Span::styled(cur_entry.year.clone(), Style::new().light_magenta()), + Span::styled(cur_entry.year(), Style::new().light_magenta()), ])); if !cur_entry.doi_url.is_empty() || !cur_entry.filepath.is_empty() { lines.push(Line::raw("")); @@ -366,7 +370,7 @@ impl App { lines.push(Line::from(vec![ Span::styled("DOI/URL: ", style_value_sec), Span::styled( - cur_entry.doi_url.clone(), + cur_entry.doi_url(), Style::default().fg(TEXT_FG_COLOR).underlined(), ), ])); @@ -374,10 +378,7 @@ impl App { if !cur_entry.filepath.is_empty() { lines.push(Line::from(vec![ Span::styled("File: ", style_value_sec), - Span::styled( - cur_entry.filepath.clone(), - Style::default().fg(TEXT_FG_COLOR), - ), + Span::styled(cur_entry.filepath(), Style::default().fg(TEXT_FG_COLOR)), ])); } lines.push(Line::from("")); diff --git a/src/main.rs b/src/main.rs index 6ef7ee1..b90aeea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,9 +42,9 @@ async fn main() -> Result<()> { std::process::exit(0); } - if !parsed_args.bibfilearg.is_file() { - panic!("No \'.bib\' file passed, aborting") - } + // if !parsed_args.bibfilearg.is_file() { + // panic!("No \'.bib\' file passed, aborting") + // } init_error_hooks()?; diff --git a/tests/biblatex-test.bib b/tests/biblatex-test.bib index 7a1c226..b89416b 100644 --- a/tests/biblatex-test.bib +++ b/tests/biblatex-test.bib @@ -15,7 +15,7 @@ publisher = {Gale}, volume = {61}, pages = {204--208}, - editor = {Matuz, Roger}, + editor = {Matuz, Roger and Miller, Helen}, keywords = {narration}, langid = {english}, langidopts = {variant=american}, |
