aboutsummaryrefslogtreecommitdiff
path: root/src/frontend
diff options
context:
space:
mode:
authorlukeflo2024-10-18 17:07:25 +0200
committerlukeflo2024-10-18 17:07:25 +0200
commitb5538d8c8408f1afbcfecb2a3b0407c3ad53ebe5 (patch)
tree2371188efc845614d91332f631cd6dd523af0c75 /src/frontend
parent917b9c522635a304ff7c70499f05903a694696a3 (diff)
downloadbibiman-b5538d8c8408f1afbcfecb2a3b0407c3ad53ebe5.tar.gz
bibiman-b5538d8c8408f1afbcfecb2a3b0407c3ad53ebe5.zip
Handle short and long author fields
Diffstat (limited to 'src/frontend')
-rw-r--r--src/frontend/entries.rs89
-rw-r--r--src/frontend/ui.rs21
2 files changed, 96 insertions, 14 deletions
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(""));