aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlukeflo2025-05-30 17:04:42 +0200
committerlukeflo2025-05-30 17:04:42 +0200
commitfbcd12fb9e215852a12e0c3f5963aa52996b26aa (patch)
tree19f5ef411175cac554e004df3d05ed7cac1a7a7d /src
parent0eae6de6df392fb3b8fa9d39dde42cecff97d240 (diff)
downloadbibiman-fbcd12fb9e215852a12e0c3f5963aa52996b26aa.tar.gz
bibiman-fbcd12fb9e215852a12e0c3f5963aa52996b26aa.zip
better UI for popups, some testings
Diffstat (limited to 'src')
-rw-r--r--src/app.rs31
-rw-r--r--src/bibiman.rs86
-rw-r--r--src/tui/ui.rs29
3 files changed, 60 insertions, 86 deletions
diff --git a/src/app.rs b/src/app.rs
index 92d89ba..f751716 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -306,10 +306,13 @@ impl App {
items.push(("Weblink: ".into(), entry.doi_url.unwrap().clone()))
}
if entry.filepath.is_some() {
- items.push((
- "Filepath: ".into(),
- entry.filepath.unwrap()[0].clone().into_string().unwrap(),
- ))
+ entry.filepath.unwrap().iter().for_each(|p| {
+ items.push(("Filepath: ".into(), p.clone().into_string().unwrap()))
+ });
+ // items.push((
+ // "Filepath: ".into(),
+ // entry.filepath.unwrap()[0].clone().into_string().unwrap(),
+ // ))
}
// self.bibiman.popup_area.popup_kind = Some(PopupKind::YankItem);
@@ -344,16 +347,13 @@ impl App {
))
}
if entry.filepath.is_some() {
- items.push((
- "File (PDF/EPUB): ".into(),
- entry.filepath.unwrap()[0].clone().into_string().unwrap(),
- ))
+ entry.filepath.unwrap().iter().for_each(|p| {
+ items.push((
+ "File (PDF/EPUB): ".into(),
+ p.clone().into_string().unwrap(),
+ ))
+ });
}
- // 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;
- // self.bibiman.popup_area.popup_state.select(Some(0))
self.bibiman
.open_popup(PopupKind::OpenRes, None, None, Some(items))?;
@@ -364,11 +364,6 @@ impl App {
Some(&entry.citekey),
None,
)?;
- // self.bibiman.popup_area.popup_message(
- // "Selected entry has no connected ressources: ",
- // &entry.citekey,
- // false,
- // )
}
}
}
diff --git a/src/bibiman.rs b/src/bibiman.rs
index e3509a9..583d849 100644
--- a/src/bibiman.rs
+++ b/src/bibiman.rs
@@ -28,6 +28,7 @@ use color_eyre::eyre::{Error, Result};
use editor_command::EditorBuilder;
use ratatui::widgets::ScrollbarState;
use regex::Regex;
+use std::ffi::OsString;
use std::fs::{self, read_to_string};
use std::fs::{File, OpenOptions};
use std::io::Write;
@@ -666,6 +667,7 @@ impl Bibiman {
// Index of selected popup field
let popup_idx = self.popup_area.popup_state.selected().unwrap();
+ let popup_entry = self.popup_area.popup_list[popup_idx].1.clone();
// Choose ressource depending an selected popup field
if self.popup_area.popup_list[popup_idx].0.contains("Weblink") {
@@ -674,8 +676,9 @@ impl Bibiman {
app::open_connected_link(cfg, &url)?;
} else if self.popup_area.popup_list[popup_idx].0.contains("File") {
// TODO: Selection for multiple files
- let object = self.entry_table.entry_table_items[entry_idx].filepath()[0];
- app::open_connected_file(cfg, object)?;
+ // let object = self.entry_table.entry_table_items[entry_idx].filepath()[0];
+ let object: OsString = popup_entry.into();
+ app::open_connected_file(cfg, &object)?;
} else {
eprintln!("Unable to find ressource to open");
};
@@ -686,76 +689,27 @@ impl Bibiman {
}
pub fn yank_entry_field(&mut self) -> Result<()> {
- // self.close_popup();
- // Index of selected entry
- let entry_idx = self.entry_table.entry_table_state.selected().unwrap();
-
// Index of selected popup field
let popup_idx = self.popup_area.popup_state.selected().unwrap();
+ let popup_entry = self.popup_area.popup_list[popup_idx].1.clone();
- match self.popup_area.popup_list[popup_idx]
+ let kind = self.popup_area.popup_list[popup_idx]
.0
.to_lowercase()
- .as_str()
- .split_whitespace()
+ .split(":")
.next()
- {
- Some("citekey:") => {
- let citekey = &self.entry_table.entry_table_items[entry_idx].citekey;
- Bibiman::yank_text(citekey);
- self.open_popup(
- PopupKind::MessageConfirm,
- 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(),
- // true,
- // );
- }
- Some("weblink:") => {
- let link = &self.entry_table.entry_table_items[entry_idx].doi_url;
- if let Some(l) = link {
- Bibiman::yank_text(l);
- self.open_popup(
- PopupKind::MessageConfirm,
- 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(),
- // true,
- // );
- }
- }
- Some("filepath:") => {
- let path = self.entry_table.entry_table_items[entry_idx]
- .filepath
- .clone();
- if let Some(p) = path {
- let p = p[0].as_os_str().to_str();
- if let Some(p) = p {
- Bibiman::yank_text(p);
- self.open_popup(
- PopupKind::MessageConfirm,
- Some("Yanked filepath to clipboard: "),
- Some(p),
- None,
- )?;
- // self.popup_area.popup_message(
- // "Yanked filepath to clipboard: ",
- // p, // self.bibiman.get_selected_link(),
- // true,
- // );
- }
- }
- }
- _ => {}
- };
+ .unwrap()
+ .to_owned();
+
+ let msg = format!("Yanked {} to clipboard: ", &kind);
+
+ Bibiman::yank_text(&popup_entry);
+ self.open_popup(
+ PopupKind::MessageConfirm,
+ Some(&msg),
+ Some(&popup_entry),
+ None,
+ )?;
Ok(())
}
diff --git a/src/tui/ui.rs b/src/tui/ui.rs
index 9cbd075..a998bc7 100644
--- a/src/tui/ui.rs
+++ b/src/tui/ui.rs
@@ -279,7 +279,14 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) {
.popup_area
.popup_list
.iter()
- .map(|(mes, obj)| ListItem::from(mes.to_owned() + obj))
+ .map(
+ |(mes, obj)| {
+ ListItem::from(Line::from(vec![
+ Span::styled(mes, Style::new().bold()),
+ Span::raw(obj),
+ ]))
+ }, // ListItem::from(mes.to_owned() + obj)
+ )
.collect();
let title = if let Some(PopupKind::OpenRes) = app.bibiman.popup_area.popup_kind {
@@ -311,7 +318,25 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) {
.add_modifier(Modifier::REVERSED),
);
- let popup_width = frame.area().width / 2;
+ // To find the longest line, we need to collect the chars of every item
+ // and add.
+ let list_widths: Vec<u16> = app
+ .bibiman
+ .popup_area
+ .popup_list
+ .iter()
+ .map(|(m, o)| m.chars().count() as u16 + o.chars().count() as u16)
+ .collect();
+
+ // Now take the max number for the width of the popup
+ let max_item = list_widths.iter().max().unwrap().to_owned();
+
+ // Check if the popup would exceed the terminal frame width
+ let popup_width = if max_item + 2 > frame.area().width - 2 {
+ frame.area().width - 2
+ } else {
+ max_item + 2
+ };
let popup_heigth = list.len() + 2;
let popup_area = popup_area(frame.area(), popup_width, popup_heigth as u16);