From fdfbf550144ef69ecc6003760fbb39466ec08637 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 29 Jun 2025 12:26:27 +0200 Subject: Some basic function rewriting for matching different file types --- src/bibiman/bibisetup.rs | 137 +++++++++++++++++++++++++++-------------------- src/config.rs | 12 +++++ 2 files changed, 91 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index bf5baf5..8d8de53 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -55,6 +55,7 @@ pub struct BibiData { pub filepath: Option>, pub file_field: bool, pub subtitle: Option, + pub notes: Option>, } impl BibiData { @@ -200,7 +201,7 @@ impl BibiSetup { cfg: &BibiConfig, ) -> Vec { let mut pdf_files = if cfg.general.pdf_path.is_some() { - collect_pdf_file_paths(cfg.general.pdf_path.as_ref().unwrap()) + collect_file_paths(cfg.general.pdf_path.as_ref().unwrap(), Some(vec!["pdf"])) } else { None }; @@ -225,6 +226,7 @@ impl BibiSetup { filepath: filepaths.0, file_field: filepaths.1, subtitle: Self::get_subtitle(k, bibliography), + notes: None, } }) .collect() @@ -373,12 +375,21 @@ impl BibiSetup { true, ) } else if pdf_files.is_some() { - (Self::merge_filepath_or_none(&citekey, pdf_files), false) + ( + Self::merge_filepath_or_none_two(&citekey, pdf_files, vec!["pdf"]), + false, + ) } else { (None, false) } } + // pub fn get_notepath( + // citekey: &str, + // note_files: &mut Option>>, + // ) -> Option> { + // } + pub fn get_subtitle(citekey: &str, biblio: &Bibliography) -> Option { if biblio.get(citekey).unwrap().subtitle().is_ok() { Some( @@ -399,53 +410,7 @@ impl BibiSetup { pdf_files: &mut Option>>, ) -> Option> { let pdf_file = { - // let mut idx = 0; let citekey = citekey.to_owned().to_ascii_lowercase() + ".pdf"; - // let filename = citekey.to_owned() + ".pdf"; - // for f in args.pdf_files.unwrap().iter() { - // if f.file_name().unwrap().to_str().unwrap() == &filename { - // break f; - // } - // } - - // loop { - // if idx + 1 > pdf_files.as_ref().unwrap().len() { - // break None; - // } - // let cur_entry = pdf_files.as_ref().unwrap()[idx].clone(); - // if cur_entry.is_file() - // && cur_entry - // .file_name() - // .unwrap() - // .to_ascii_lowercase() - // .to_str() - // .unwrap() - // == citekey - // { - // let path = cur_entry.to_owned().into_os_string(); - // pdf_files.as_mut().unwrap().swap_remove(idx); - // break Some(path); - // } else { - // idx += 1 - // } - // } - - // for file in pdf_files.as_ref().unwrap().iter() { - // let filename = file.file_name().unwrap().to_ascii_lowercase(); - // if filename.to_str().unwrap() == citekey { - // break; - // } else if pdf_files.as_ref().unwrap().len() > idx { - // break; - // } else { - // idx += 1; - // } - // } - - // if pdf_files.as_ref().unwrap()[idx].is_file() { - // Some(pdf_files.as_ref().unwrap()[idx].to_owned().into_os_string()) - // } else { - // None - // } if pdf_files.as_ref().unwrap().contains_key(&citekey) { let path_vec = pdf_files @@ -462,31 +427,74 @@ impl BibiSetup { pdf_file } + + fn merge_filepath_or_none_two( + citekey: &str, + files: &mut Option>>, + extensions: Vec<&str>, + ) -> Option> { + let mut file = Vec::new(); + + for e in extensions.iter() { + let basename = citekey.to_owned().to_ascii_lowercase() + "." + e; + if files.as_ref().unwrap().contains_key(&basename) { + let _ = files + .as_ref() + .unwrap() + .get(&basename) + .unwrap() + .to_owned() + .into_iter() + .for_each(|p| file.push(p.into_os_string())); + } + } + + if file.is_empty() { + None + } else { + Some(file) + } + } } -/// This function walks the given dir and collects all pdf files into a `HashMap` -/// of the format `[String, Vec]`, where `String` represents the basename -/// of the file and the `Vec` holds all filepaths ending with this basename. +/// This function walks the given dir and collects all files matching one of the +/// passed extensions into a `HashMap` of the format `[String, Vec]`, +/// where `String` represents the basename of the file and the `Vec` holds +/// all filepaths ending with this basename. /// /// In most cases the latter is only a single path, but there might be some concepts /// with subdirs were some entries have multiple files associated with them. -pub fn collect_pdf_file_paths(pdf_dir: &PathBuf) -> Option>> { +/// +/// Passing [`None`] as argument for extensions will result in collecting all files +/// from the given directory and its subdirectories! +pub fn collect_file_paths( + file_dir: &PathBuf, + extensions: Option>, +) -> Option>> { let mut files: HashMap> = HashMap::new(); // Expand tilde to /home/user - let pdf_dir = if pdf_dir.starts_with("~") { - &app::expand_home(&pdf_dir) + let file_dir = if file_dir.starts_with("~") { + &app::expand_home(&file_dir) } else { - pdf_dir + file_dir }; // Walk the passed dir and collect all pdf files into hashmap - if pdf_dir.is_dir() { - for file in WalkDir::new(pdf_dir) { + if file_dir.is_dir() { + for file in WalkDir::new(file_dir) { let f = file.unwrap().into_path(); if f.is_file() && f.extension().is_some() - && f.extension().unwrap_or_default().to_ascii_lowercase() == "pdf" + && extensions.as_ref().is_some_and(|v| { + v.contains( + &f.extension() + .unwrap_or_default() + .to_ascii_lowercase() + .to_str() + .unwrap_or_default(), + ) + }) { let filename = f .file_name() @@ -495,6 +503,19 @@ pub fn collect_pdf_file_paths(pdf_dir: &PathBuf) -> Option, pub pdf_path: Option, + pub note_path: Option, + pub note_extensions: Option>, } /// Substruct [colors] in config.toml @@ -117,6 +125,8 @@ impl Default for BibiConfig { url_opener: select_opener(), file_prefix: None, pdf_path: None, + note_path: None, + note_extensions: None, }, colors: Self::dark_colors(), } @@ -133,6 +143,8 @@ impl BibiConfig { url_opener: select_opener(), file_prefix: None, pdf_path: None, + note_path: None, + note_extensions: None, }, colors: if args.light_theme { Self::light_colors() -- cgit v1.2.3 From 40629e0c2dc62bfb1786cb1c18bc68ed4c23e9ac Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 29 Jun 2025 14:20:10 +0200 Subject: working note-opening action. needs refinement: errors and ui --- src/app.rs | 8 +++- src/bibiman.rs | 15 ++++++ src/bibiman/bibisetup.rs | 117 ++++++++++++++++++++++++++++++++++------------- src/bibiman/entries.rs | 2 + src/bibiman/search.rs | 1 + 5 files changed, 111 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index d10e4f8..5f2d16b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -343,7 +343,8 @@ impl App { .unwrap(); let entry = self.bibiman.entry_table.entry_table_items[idx].clone(); let mut items: Vec<(String, String)> = vec![]; - if entry.filepath.is_some() || entry.doi_url.is_some() { + if entry.filepath.is_some() || entry.doi_url.is_some() || entry.notes.is_some() + { if entry.doi_url.is_some() { items.push(( "Weblink (DOI/URL): ".into(), @@ -370,6 +371,11 @@ impl App { )) }); } + if entry.notes.is_some() { + entry.notes.unwrap().iter().for_each(|n| { + items.push(("Note: ".into(), n.clone().into_string().unwrap())); + }); + } self.bibiman .open_popup(PopupKind::OpenRes, None, None, Some(items))?; diff --git a/src/bibiman.rs b/src/bibiman.rs index 21601e3..96a733c 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -692,6 +692,21 @@ impl Bibiman { None, )?; } + } + if self.popup_area.popup_list[popup_idx].0.contains("Note") { + let file = expand_home(&PathBuf::from(popup_entry.clone())); + // let object: OsString = popup_entry.into(); + if file.is_file() { + app::open_connected_file(cfg, &file.into_os_string())?; + self.close_popup(); + } else { + self.open_popup( + PopupKind::MessageError, + Some("No valid file path: "), + Some(file.to_str().unwrap()), + None, + )?; + } } else { eprintln!("Unable to find ressource to open"); }; diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index 8d8de53..e79960c 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -201,10 +201,26 @@ impl BibiSetup { cfg: &BibiConfig, ) -> Vec { let mut pdf_files = if cfg.general.pdf_path.is_some() { - collect_file_paths(cfg.general.pdf_path.as_ref().unwrap(), Some(vec!["pdf"])) + collect_file_paths(cfg.general.pdf_path.as_ref().unwrap(), &Some(vec!["pdf"])) } else { None }; + let ext: Option> = + if cfg.general.note_path.is_some() && cfg.general.note_extensions.is_some() { + let mut ext: Vec<&str> = Vec::new(); + for e in cfg.general.note_extensions.as_ref().unwrap().iter() { + ext.push(e); + } + Some(ext) + } else { + None + }; + let mut note_files = + if cfg.general.note_path.is_some() && cfg.general.note_extensions.is_some() { + collect_file_paths(cfg.general.note_path.as_ref().unwrap(), &ext) + } else { + None + }; citekeys .iter() .enumerate() @@ -226,7 +242,11 @@ impl BibiSetup { filepath: filepaths.0, file_field: filepaths.1, subtitle: Self::get_subtitle(k, bibliography), - notes: None, + notes: if note_files.is_some() { + Self::get_notepath(k, &mut note_files, &ext) + } else { + None + }, } }) .collect() @@ -384,11 +404,17 @@ impl BibiSetup { } } - // pub fn get_notepath( - // citekey: &str, - // note_files: &mut Option>>, - // ) -> Option> { - // } + pub fn get_notepath( + citekey: &str, + note_files: &mut Option>>, + ext: &Option>, + ) -> Option> { + if let Some(e) = ext { + Self::merge_filepath_or_none_two(citekey, note_files, e.to_vec()) + } else { + None + } + } pub fn get_subtitle(citekey: &str, biblio: &Bibliography) -> Option { if biblio.get(citekey).unwrap().subtitle().is_ok() { @@ -405,29 +431,9 @@ impl BibiSetup { } } - fn merge_filepath_or_none( - citekey: &str, - pdf_files: &mut Option>>, - ) -> Option> { - let pdf_file = { - let citekey = citekey.to_owned().to_ascii_lowercase() + ".pdf"; - - if pdf_files.as_ref().unwrap().contains_key(&citekey) { - let path_vec = pdf_files - .as_ref() - .unwrap() - .get(&citekey) - .unwrap() - .to_owned(); - Some(path_vec.into_iter().map(|p| p.into_os_string()).collect()) - } else { - None - } - }; - - pdf_file - } - + /// Check if there exists files with the basename of the format + /// "citekey.extension" in the passed hashmap. If so, return all matches + /// as `Option`, otherwise return `None` fn merge_filepath_or_none_two( citekey: &str, files: &mut Option>>, @@ -469,7 +475,7 @@ impl BibiSetup { /// from the given directory and its subdirectories! pub fn collect_file_paths( file_dir: &PathBuf, - extensions: Option>, + extensions: &Option>, ) -> Option>> { let mut files: HashMap> = HashMap::new(); @@ -531,3 +537,52 @@ pub fn collect_file_paths( Some(files) } } + +#[cfg(test)] +mod tests { + use std::{collections::HashMap, ffi::OsString, path::PathBuf}; + + use super::BibiSetup; + + #[test] + fn check_file_matching() { + let mut files: HashMap> = HashMap::new(); + files.insert( + "citekey.md".to_string(), + vec![ + PathBuf::from("/one/note/citekey.md"), + PathBuf::from("/one/other/citekey.md"), + ], + ); + files.insert( + "citekey.pdf".to_string(), + vec![ + PathBuf::from("/one/note/citekey.pdf"), + PathBuf::from("/one/other/citekey.pdf"), + ], + ); + files.insert( + "citekey2.pdf".to_string(), + vec![ + PathBuf::from("/one/note/citekey2.pdf"), + PathBuf::from("/one/other/citekey2.pdf"), + ], + ); + + let matches = + BibiSetup::merge_filepath_or_none_two("citekey", &mut Some(files), vec!["md", "pdf"]); + + assert_eq!( + matches.clone().unwrap().iter().next().unwrap().to_owned(), + OsString::from("/one/note/citekey.md") + ); + assert_eq!( + matches.clone().unwrap().last().unwrap().to_owned(), + OsString::from("/one/other/citekey.pdf") + ); + assert!(!matches + .clone() + .unwrap() + .contains(&OsString::from("/one/other/citekey2.pdf"))); + } +} diff --git a/src/bibiman/entries.rs b/src/bibiman/entries.rs index 88a1583..e0c230b 100644 --- a/src/bibiman/entries.rs +++ b/src/bibiman/entries.rs @@ -159,6 +159,7 @@ mod tests { filepath: None, file_field: false, subtitle: None, + notes: None, }; let entry_vec = BibiData::ref_vec(&mut entry); @@ -177,6 +178,7 @@ mod tests { filepath: None, file_field: false, subtitle: None, + notes: None, }; let entry_vec_editors = BibiData::ref_vec(&mut entry_editors); diff --git a/src/bibiman/search.rs b/src/bibiman/search.rs index f391aed..0e32f63 100644 --- a/src/bibiman/search.rs +++ b/src/bibiman/search.rs @@ -137,6 +137,7 @@ mod tests { filepath: Some(vec![OsString::from("/home/file/path.pdf")]), file_field: true, subtitle: None, + notes: None, }; let joined_vec = BibiSearch::convert_to_string(&bibvec); -- cgit v1.2.3 From ae1667410b0a812fff8d464251548f23f88ae024 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 29 Jun 2025 20:31:41 +0200 Subject: some more tests for notes, need to elaborate function for opening notes --- src/app.rs | 22 +++++++++++++--------- src/bibiman.rs | 5 ++--- tests/note-files/aristotle:poetics.txt | 0 tests/note-files/aristotle:rhetoric.md | 0 tests/note-files/bertram.txt | 1 + tests/note-files/betram.txt | 1 - tests/test-config.toml | 2 +- 7 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 tests/note-files/aristotle:poetics.txt create mode 100644 tests/note-files/aristotle:rhetoric.md create mode 100644 tests/note-files/bertram.txt delete mode 100644 tests/note-files/betram.txt (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 5f2d16b..f015494 100644 --- a/src/app.rs +++ b/src/app.rs @@ -410,15 +410,6 @@ impl App { pub fn open_connected_file(cfg: &BibiConfig, file: &OsStr) -> Result<()> { // Build command to execute pdf-reader. 'xdg-open' is Linux standard let cmd = &cfg.general.pdf_opener; - // If necessary, replace ~ with /home dir - // let file = if cfg.general.file_prefix.is_some() { - // cfg.general.file_prefix.clone().unwrap().join(file) - // } else { - // PathBuf::from(file) - // }; - // let file = PathBuf::from(file); - - // let file = expand_home(&file).into_os_string(); // Pass filepath as argument, pipe stdout and stderr to /dev/null // to keep the TUI clean (where is it piped on Windows???) @@ -432,6 +423,19 @@ pub fn open_connected_file(cfg: &BibiConfig, file: &OsStr) -> Result<()> { Ok(()) } +pub fn open_connected_note(cfg: &BibiConfig, file: &OsStr) -> Result<()> { + // let cmd = cfg.general.editor.as_ref().unwrap(); + + let _ = Command::new("xdg-open") + .arg(file) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .wrap_err("Opening file not possible"); + + Ok(()) +} + pub fn open_connected_link(cfg: &BibiConfig, link: &str) -> Result<()> { // Build command to execute pdf-reader. 'xdg-open' is Linux standard let cmd = &cfg.general.url_opener; diff --git a/src/bibiman.rs b/src/bibiman.rs index 96a733c..1f19b24 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -692,12 +692,11 @@ impl Bibiman { None, )?; } - } - if self.popup_area.popup_list[popup_idx].0.contains("Note") { + } else if self.popup_area.popup_list[popup_idx].0.contains("Note") { let file = expand_home(&PathBuf::from(popup_entry.clone())); // let object: OsString = popup_entry.into(); if file.is_file() { - app::open_connected_file(cfg, &file.into_os_string())?; + app::open_connected_note(cfg, &file.into_os_string())?; self.close_popup(); } else { self.open_popup( diff --git a/tests/note-files/aristotle:poetics.txt b/tests/note-files/aristotle:poetics.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/note-files/aristotle:rhetoric.md b/tests/note-files/aristotle:rhetoric.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/note-files/bertram.txt b/tests/note-files/bertram.txt new file mode 100644 index 0000000..54d31e5 --- /dev/null +++ b/tests/note-files/bertram.txt @@ -0,0 +1 @@ +A simple text file with notes about this Betram dude diff --git a/tests/note-files/betram.txt b/tests/note-files/betram.txt deleted file mode 100644 index 54d31e5..0000000 --- a/tests/note-files/betram.txt +++ /dev/null @@ -1 +0,0 @@ -A simple text file with notes about this Betram dude diff --git a/tests/test-config.toml b/tests/test-config.toml index fd39b29..99d1d00 100644 --- a/tests/test-config.toml +++ b/tests/test-config.toml @@ -4,7 +4,7 @@ bibfiles = [ "tests/biblatex-test.bib" ] ## Default editor to use when editing files. Arguments are possible -# editor = "vim" # with args: "vim -y" +editor = "vim" # with args: "vim -y" ## Default app to open PDFs/Epubs # pdf_opener = "xdg-open" -- cgit v1.2.3 From b10615ade6bb2710cf6716f05cc496cb082d24ad Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 30 Jun 2025 10:43:38 +0200 Subject: opening notes in set editor --- src/app.rs | 16 +------ src/bibiman.rs | 47 +++++++++++++++++++-- tests/biblatex-test.bib | 12 +++--- tests/note-files/aristotle:poetics.txt | 0 tests/note-files/aristotle:rhetoric.md | 0 tests/note-files/aristotle_poetics.txt | 1 + tests/note-files/aristotle_rhetoric.md | 0 .../annotated-pdfs/ARIStotle:rheTORIC.PDF | Bin 25294 -> 0 bytes .../annotated-pdfs/ARIStotle_rheTORIC.PDF | Bin 0 -> 25294 bytes tests/pdf-files/aristotle:physics.pdf | Bin 25294 -> 0 bytes tests/pdf-files/aristotle:rhetoric.pdf | Bin 25294 -> 0 bytes tests/pdf-files/aristotle:rhetoric.txt | 0 tests/pdf-files/aristotle_physics.pdf | Bin 0 -> 25294 bytes tests/pdf-files/aristotle_rhetoric.pdf | Bin 0 -> 25294 bytes tests/pdf-files/aristotle_rhetoric.txt | 0 15 files changed, 53 insertions(+), 23 deletions(-) delete mode 100644 tests/note-files/aristotle:poetics.txt delete mode 100644 tests/note-files/aristotle:rhetoric.md create mode 100644 tests/note-files/aristotle_poetics.txt create mode 100644 tests/note-files/aristotle_rhetoric.md delete mode 100644 tests/pdf-files/annotated-pdfs/ARIStotle:rheTORIC.PDF create mode 100644 tests/pdf-files/annotated-pdfs/ARIStotle_rheTORIC.PDF delete mode 100644 tests/pdf-files/aristotle:physics.pdf delete mode 100644 tests/pdf-files/aristotle:rhetoric.pdf delete mode 100644 tests/pdf-files/aristotle:rhetoric.txt create mode 100644 tests/pdf-files/aristotle_physics.pdf create mode 100644 tests/pdf-files/aristotle_rhetoric.pdf create mode 100644 tests/pdf-files/aristotle_rhetoric.txt (limited to 'src') diff --git a/src/app.rs b/src/app.rs index f015494..496896a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -18,6 +18,7 @@ use crate::bibiman::CurrentArea; use crate::config::BibiConfig; use color_eyre::eyre::{Context, Ok, Result}; +use editor_command::EditorBuilder; // use super::Event; use crate::cliargs::CLIArgs; use crate::tui::commands::InputCmdAction; @@ -279,7 +280,7 @@ impl App { if let Some(PopupKind::Help) = self.bibiman.popup_area.popup_kind { self.bibiman.close_popup(); } else if let Some(PopupKind::OpenRes) = self.bibiman.popup_area.popup_kind { - self.bibiman.open_connected_res(cfg)?; + self.bibiman.open_connected_res(cfg, tui)?; } else if let Some(PopupKind::AppendToFile) = self.bibiman.popup_area.popup_kind { self.bibiman.append_entry_to_file(cfg)? @@ -423,19 +424,6 @@ pub fn open_connected_file(cfg: &BibiConfig, file: &OsStr) -> Result<()> { Ok(()) } -pub fn open_connected_note(cfg: &BibiConfig, file: &OsStr) -> Result<()> { - // let cmd = cfg.general.editor.as_ref().unwrap(); - - let _ = Command::new("xdg-open") - .arg(file) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .wrap_err("Opening file not possible"); - - Ok(()) -} - pub fn open_connected_link(cfg: &BibiConfig, link: &str) -> Result<()> { // Build command to execute pdf-reader. 'xdg-open' is Linux standard let cmd = &cfg.general.url_opener; diff --git a/src/bibiman.rs b/src/bibiman.rs index 1f19b24..e1e97ed 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -30,11 +30,12 @@ use crossterm::event::KeyCode; use editor_command::EditorBuilder; use ratatui::widgets::ScrollbarState; use regex::Regex; +use std::ffi::OsStr; use std::fs::{self, read_to_string}; use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::PathBuf; -use std::process::Command; +use std::process::{Command, Stdio}; use std::result::Result::Ok; use tui_input::Input; @@ -512,6 +513,46 @@ impl Bibiman { Ok(()) } + pub fn open_connected_note( + &mut self, + cfg: &BibiConfig, + tui: &mut Tui, + file: &OsStr, + ) -> Result<()> { + // get filecontent and citekey for calculating line number + let citekey: &str = &self.entry_table.entry_table_items + [self.entry_table.entry_table_state.selected().unwrap()] + .citekey + .clone(); + + // Exit TUI to enter editor + tui.exit()?; + // Use VISUAL or EDITOR. Set "vi" as last fallback + let mut cmd: Command = EditorBuilder::new() + .source(cfg.general.editor.as_ref()) + .environment() + .source(Some("vi")) + .build() + .unwrap(); + // Prepare arguments to open file at specific line + let status = cmd.arg(file).status()?; + if !status.success() { + eprintln!("Spawning editor failed with status {}", status); + } + + // Enter TUI again + tui.enter()?; + tui.terminal.clear()?; + + // Update the database and the lists to show changes + // Self::update_lists(self, cfg); + + // Select entry which was selected before entering editor + self.select_entry_by_citekey(citekey); + + Ok(()) + } + pub fn add_entry(&mut self) { if let CurrentArea::EntryArea = self.current_area { self.former_area = Some(FormerArea::EntryArea); @@ -662,7 +703,7 @@ impl Bibiman { Ok(()) } - pub fn open_connected_res(&mut self, cfg: &BibiConfig) -> Result<()> { + pub fn open_connected_res(&mut self, cfg: &BibiConfig, tui: &mut Tui) -> Result<()> { // Index of selected entry let entry_idx = self.entry_table.entry_table_state.selected().unwrap(); @@ -696,7 +737,7 @@ impl Bibiman { let file = expand_home(&PathBuf::from(popup_entry.clone())); // let object: OsString = popup_entry.into(); if file.is_file() { - app::open_connected_note(cfg, &file.into_os_string())?; + self.open_connected_note(cfg, tui, &file.into_os_string())?; self.close_popup(); } else { self.open_popup( diff --git a/tests/biblatex-test.bib b/tests/biblatex-test.bib index 4071dcb..692375e 100644 --- a/tests/biblatex-test.bib +++ b/tests/biblatex-test.bib @@ -9,7 +9,7 @@ model of particle physics.}, } -@collection{matuz:doody, +@collection{matuz_doody, title = {Contemporary Literary Criticism}, year = {1990}, location = {Detroit}, @@ -54,7 +54,7 @@ field}, } -@book{aristotle:anima, +@book{aristotle_anima, title = {De Anima}, author = {Aristotle}, location = {Cambridge}, @@ -68,7 +68,7 @@ editor}}, } -@book{aristotle:physics, +@book{aristotle_physics, title = {Physics}, shorttitle = {Physics}, author = {Aristotle}, @@ -84,7 +84,7 @@ annotation = {A \texttt{book} entry with a \texttt{translator} field}, } -@book{aristotle:poetics, +@book{aristotle_poetics, title = {Poetics}, shorttitle = {Poetics}, author = {Aristotle}, @@ -100,7 +100,7 @@ editor} as well as a \texttt{series} field}, } -@mvbook{aristotle:rhetoric, +@mvbook{aristotle_rhetoric, title = {The Rhetoric of {Aristotle} with a commentary by the late {Edward Meredith Cope}}, shorttitle = {Rhetoric}, @@ -441,7 +441,7 @@ @string{pup = {Princeton University Press}} -@incollection{westfahl:space, +@incollection{westfahl_space, title = {The True Frontier}, author = {Westfahl, Gary}, pages = {55--65}, diff --git a/tests/note-files/aristotle:poetics.txt b/tests/note-files/aristotle:poetics.txt deleted file mode 100644 index e69de29..0000000 diff --git a/tests/note-files/aristotle:rhetoric.md b/tests/note-files/aristotle:rhetoric.md deleted file mode 100644 index e69de29..0000000 diff --git a/tests/note-files/aristotle_poetics.txt b/tests/note-files/aristotle_poetics.txt new file mode 100644 index 0000000..a156c76 --- /dev/null +++ b/tests/note-files/aristotle_poetics.txt @@ -0,0 +1 @@ +Here some very boring information regarding Aristotle diff --git a/tests/note-files/aristotle_rhetoric.md b/tests/note-files/aristotle_rhetoric.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/pdf-files/annotated-pdfs/ARIStotle:rheTORIC.PDF b/tests/pdf-files/annotated-pdfs/ARIStotle:rheTORIC.PDF deleted file mode 100644 index 6aaba88..0000000 Binary files a/tests/pdf-files/annotated-pdfs/ARIStotle:rheTORIC.PDF and /dev/null differ diff --git a/tests/pdf-files/annotated-pdfs/ARIStotle_rheTORIC.PDF b/tests/pdf-files/annotated-pdfs/ARIStotle_rheTORIC.PDF new file mode 100644 index 0000000..6aaba88 Binary files /dev/null and b/tests/pdf-files/annotated-pdfs/ARIStotle_rheTORIC.PDF differ diff --git a/tests/pdf-files/aristotle:physics.pdf b/tests/pdf-files/aristotle:physics.pdf deleted file mode 100644 index 6aaba88..0000000 Binary files a/tests/pdf-files/aristotle:physics.pdf and /dev/null differ diff --git a/tests/pdf-files/aristotle:rhetoric.pdf b/tests/pdf-files/aristotle:rhetoric.pdf deleted file mode 100644 index 6aaba88..0000000 Binary files a/tests/pdf-files/aristotle:rhetoric.pdf and /dev/null differ diff --git a/tests/pdf-files/aristotle:rhetoric.txt b/tests/pdf-files/aristotle:rhetoric.txt deleted file mode 100644 index e69de29..0000000 diff --git a/tests/pdf-files/aristotle_physics.pdf b/tests/pdf-files/aristotle_physics.pdf new file mode 100644 index 0000000..6aaba88 Binary files /dev/null and b/tests/pdf-files/aristotle_physics.pdf differ diff --git a/tests/pdf-files/aristotle_rhetoric.pdf b/tests/pdf-files/aristotle_rhetoric.pdf new file mode 100644 index 0000000..6aaba88 Binary files /dev/null and b/tests/pdf-files/aristotle_rhetoric.pdf differ diff --git a/tests/pdf-files/aristotle_rhetoric.txt b/tests/pdf-files/aristotle_rhetoric.txt new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From 4ca8417812db530cd157fe5b1de70f6e74e9c400 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 30 Jun 2025 12:54:01 +0200 Subject: UI implementation of notes --- src/bibiman/bibisetup.rs | 1 + src/config.rs | 18 ++++++++++++++++++ src/tui/ui.rs | 28 +++++++++++++++++++--------- 3 files changed, 38 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index e79960c..61144a1 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -76,6 +76,7 @@ impl BibiData { }; vec![ + "", { if self.short_author.is_empty() { self.authors() diff --git a/src/config.rs b/src/config.rs index 6abcfd1..c30d8d1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -113,6 +113,12 @@ pub struct Colors { pub bar_bg_color: Color, pub popup_bg_color: Color, pub selected_row_bg_color: Color, + pub note_color: Color, + pub file_color: Color, + pub link_color: Color, + pub author_color: Color, + pub title_color: Color, + pub year_color: Color, } impl Default for BibiConfig { @@ -187,6 +193,12 @@ impl BibiConfig { bar_bg_color: Color::Indexed(235), popup_bg_color: Color::Indexed(234), selected_row_bg_color: Color::Indexed(237), + note_color: Color::Indexed(123), + file_color: Color::Indexed(209), + link_color: Color::Indexed(33), + author_color: Color::Indexed(38), + title_color: Color::Indexed(37), + year_color: Color::Indexed(135), } } @@ -203,6 +215,12 @@ impl BibiConfig { confirm_color: Color::Indexed(22), warn_color: Color::Indexed(124), selected_row_bg_color: Color::Indexed(107), + note_color: Color::Indexed(123), + file_color: Color::Indexed(209), + link_color: Color::Indexed(27), + author_color: Color::Indexed(38), + title_color: Color::Indexed(37), + year_color: Color::Indexed(135), } } diff --git a/src/tui/ui.rs b/src/tui/ui.rs index ebebe4c..95b9f2c 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -580,6 +580,7 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec .bg(cfg.colors.bar_bg_color); let header = Row::new(vec![ + Cell::from(Line::from("")).bg(cfg.colors.bar_bg_color), Cell::from( Line::from(vec![{ Span::raw("Author") }, { if let Some(EntryTableColumn::Authors) = @@ -725,6 +726,7 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec let entry_table = Table::new( rows, [ + Constraint::Length(4), Constraint::Percentage(20), Constraint::Fill(1), Constraint::Length( @@ -793,7 +795,10 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, lines.push(Line::from(vec![ Span::styled("Authors: ", style_value), // Span::styled(cur_entry.authors.clone(), Style::new().green()), - Span::styled(cur_entry.authors(), Style::new().fg(cfg.colors.info_color)), + Span::styled( + cur_entry.authors(), + Style::new().fg(cfg.colors.author_color), + ), ])); if cur_entry.subtitle.is_some() { lines.push(Line::from(vec![ @@ -801,19 +806,19 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, Span::styled( cur_entry.title(), Style::new() - .fg(cfg.colors.entry_color) + .fg(cfg.colors.title_color) .add_modifier(Modifier::ITALIC), ), Span::styled( ": ", Style::new() - .fg(cfg.colors.entry_color) + .fg(cfg.colors.title_color) .add_modifier(Modifier::ITALIC), ), Span::styled( cur_entry.subtitle(), Style::new() - .fg(cfg.colors.entry_color) + .fg(cfg.colors.title_color) .add_modifier(Modifier::ITALIC), ), ])); @@ -823,14 +828,14 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, Span::styled( cur_entry.title(), Style::new() - .fg(cfg.colors.entry_color) + .fg(cfg.colors.title_color) .add_modifier(Modifier::ITALIC), ), ])); } lines.push(Line::from(vec![ Span::styled("Year: ", style_value), - Span::styled(cur_entry.year(), Style::new().fg(cfg.colors.keyword_color)), + Span::styled(cur_entry.year(), Style::new().fg(cfg.colors.year_color)), ])); // Render keywords in info box in Markdown code style if !cur_entry.keywords.is_empty() { @@ -873,7 +878,7 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, Span::styled("DOI/URL: ", style_value), Span::styled( cur_entry.doi_url(), - Style::new().fg(cfg.colors.main_text_color).underlined(), + Style::new().fg(cfg.colors.link_color).underlined(), ), ])); } @@ -882,7 +887,7 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, Span::styled("File: ", style_value), Span::styled( p.iter().map(|f| f.to_str().unwrap()).join("; "), - Style::new().fg(cfg.colors.main_text_color), + Style::new().fg(cfg.colors.file_color), ), ])); } @@ -913,7 +918,12 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, // We show the list item's info under the list in this paragraph let block = Block::bordered() - .title(Line::raw(" Entry Information ").centered().bold()) + .title( + Line::raw(" Entry Information ") + .centered() + .bold() + .fg(cfg.colors.info_color), + ) .border_set(symbols::border::PLAIN) .border_style(Style::new().fg(cfg.colors.main_text_color)) .padding(Padding::horizontal(1)); -- cgit v1.2.3 From 1b79ba0fb9f4a1d96c0de1fbf6ab42d8e1b0873c Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 30 Jun 2025 14:59:39 +0200 Subject: colored symbols for attachements, basic implementation --- src/bibiman/bibisetup.rs | 67 ++++++++++++++++++++++++++++++++++++++++-------- src/config.rs | 9 +++++++ src/tui/ui.rs | 63 +++++++++++++++++++++++++++------------------ 3 files changed, 104 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index 61144a1..61d1fcf 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -56,12 +56,22 @@ pub struct BibiData { pub file_field: bool, pub subtitle: Option, pub notes: Option>, + pub symbols: [String; 3], +} + +#[derive(Debug, Clone, PartialEq)] +pub struct BibiRow { + pub authors: String, + pub title: String, + pub year: String, + pub pubtype: String, + pub symbols: [String; 3], } impl BibiData { // 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(&mut self) -> Vec<&str> { + pub fn ref_vec(&mut self, cfg: &BibiConfig) -> BibiRow { self.short_author = match self.authors.split_once(",") { Some((first, _rest)) => { if self.authors().contains("(ed.)") { @@ -75,19 +85,35 @@ impl BibiData { None => String::from(""), }; - vec![ - "", - { + self.symbols = self.create_symbols(cfg); + + // vec![ + // { + // if self.short_author.is_empty() { + // self.authors() + // } else { + // &self.short_author + // } + // }, + // self.title(), + // self.year(), + // self.pubtype(), + // &self.symbols, + // ] + + BibiRow { + authors: { if self.short_author.is_empty() { - self.authors() + self.authors().to_string() } else { - &self.short_author + self.short_author.clone() } }, - self.title(), - self.year(), - self.pubtype(), - ] + title: self.title().to_string(), + year: self.year().to_string(), + pubtype: self.pubtype().to_string(), + symbols: self.symbols.clone(), + } } pub fn entry_id(&self) -> &u32 { @@ -133,6 +159,26 @@ impl BibiData { pub fn subtitle(&self) -> &str { self.subtitle.as_ref().unwrap() } + + fn create_symbols(&self, cfg: &BibiConfig) -> [String; 3] { + [ + if self.file_field || self.filepath.is_some() { + cfg.general.file_symbol.clone() + } else { + " ".to_string() + }, + if self.doi_url.is_some() { + cfg.general.link_symbol.clone() + } else { + " ".to_string() + }, + if self.notes.is_some() { + cfg.general.note_symbol.clone() + } else { + " ".to_string() + }, + ] + } } impl BibiSetup { @@ -248,6 +294,7 @@ impl BibiSetup { } else { None }, + symbols: [String::new(), String::new(), String::new()], } }) .collect() diff --git a/src/config.rs b/src/config.rs index c30d8d1..6723ce0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -98,6 +98,9 @@ pub struct General { pub pdf_path: Option, pub note_path: Option, pub note_extensions: Option>, + pub note_symbol: String, + pub file_symbol: String, + pub link_symbol: String, } /// Substruct [colors] in config.toml @@ -133,6 +136,9 @@ impl Default for BibiConfig { pdf_path: None, note_path: None, note_extensions: None, + note_symbol: String::from("N"), + file_symbol: String::from("F"), + link_symbol: String::from("L"), }, colors: Self::dark_colors(), } @@ -151,6 +157,9 @@ impl BibiConfig { pdf_path: None, note_path: None, note_extensions: None, + note_symbol: String::from("N"), + file_symbol: String::from("F"), + link_symbol: String::from("L"), }, colors: if args.light_theme { Self::light_colors() diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 95b9f2c..970a71d 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -580,7 +580,7 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec .bg(cfg.colors.bar_bg_color); let header = Row::new(vec![ - Cell::from(Line::from("")).bg(cfg.colors.bar_bg_color), + // Cell::from(Line::from("")).bg(cfg.colors.bar_bg_color), Cell::from( Line::from(vec![{ Span::raw("Author") }, { if let Some(EntryTableColumn::Authors) = @@ -699,34 +699,46 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec .iter_mut() .enumerate() .map(|(_i, data)| { - let item = data.ref_vec(); - item.into_iter() - .map(|content| Cell::from(Text::from(content.to_string()))) - .collect::() - .style( - // Style::new().fg(color_list( - // args, - // i as i32, - // app.bibiman - // .entry_table - // .entry_table_state - // .selected() - // .unwrap_or(0) as i32, - // args.colors.highlight_text_color, - // 20, - // )), - Style::new().fg(if let CurrentArea::EntryArea = app.bibiman.current_area { - cfg.colors.highlight_text_color - } else { - cfg.colors.main_text_color - }), - ) - .height(1) + let item = data.ref_vec(cfg); + + let row = Row::new(vec![ + Cell::from(Line::from(item.authors)), + Cell::from(Line::from(item.title)), + Cell::from(Line::from(item.year)), + Cell::from(Line::from(item.pubtype)), + Cell::from(Line::from(vec![ + Span::styled( + item.symbols[0].clone(), + Style::new().fg(cfg.colors.file_color), + ), + Span::styled( + item.symbols[1].clone(), + Style::new().fg(cfg.colors.link_color), + ), + Span::styled( + item.symbols[2].clone(), + Style::new().fg(cfg.colors.note_color), + ), + ])), + ]); + + // let row = item + // .into_iter() + // .map(|content| Cell::from(Text::from(content.to_string()))) + // .collect::(); + + row.style( + Style::new().fg(if let CurrentArea::EntryArea = app.bibiman.current_area { + cfg.colors.highlight_text_color + } else { + cfg.colors.main_text_color + }), + ) + .height(1) }); let entry_table = Table::new( rows, [ - Constraint::Length(4), Constraint::Percentage(20), Constraint::Fill(1), Constraint::Length( @@ -739,6 +751,7 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec }, ), Constraint::Percentage(10), + Constraint::Length(4), ], ) .block(block) -- cgit v1.2.3 From 614a20a12138f6691f8472b8c8657cf62b6730fb Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 30 Jun 2025 15:45:14 +0200 Subject: implemented symbols for attachements --- src/bibiman/bibisetup.rs | 42 +++++++++++++++++------------------ src/config.rs | 11 +++++++++ src/tui/ui.rs | 34 +++++++++++++++------------- tests/note-files/aristotle_physics.md | 0 tests/test-config.toml | 11 +++++++++ 5 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 tests/note-files/aristotle_physics.md (limited to 'src') diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index 61d1fcf..1f8a912 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -56,16 +56,16 @@ pub struct BibiData { pub file_field: bool, pub subtitle: Option, pub notes: Option>, - pub symbols: [String; 3], + pub symbols: [Option; 3], } #[derive(Debug, Clone, PartialEq)] -pub struct BibiRow { - pub authors: String, - pub title: String, - pub year: String, - pub pubtype: String, - pub symbols: [String; 3], +pub struct BibiRow<'a> { + pub authors: &'a str, + pub title: &'a str, + pub year: &'a str, + pub pubtype: &'a str, + pub symbols: &'a [Option; 3], } impl BibiData { @@ -104,15 +104,15 @@ impl BibiData { BibiRow { authors: { if self.short_author.is_empty() { - self.authors().to_string() + self.authors() } else { - self.short_author.clone() + &self.short_author } }, - title: self.title().to_string(), - year: self.year().to_string(), - pubtype: self.pubtype().to_string(), - symbols: self.symbols.clone(), + title: self.title(), + year: self.year(), + pubtype: self.pubtype(), + symbols: &self.symbols, } } @@ -160,22 +160,22 @@ impl BibiData { self.subtitle.as_ref().unwrap() } - fn create_symbols(&self, cfg: &BibiConfig) -> [String; 3] { + fn create_symbols(&self, cfg: &BibiConfig) -> [Option; 3] { [ if self.file_field || self.filepath.is_some() { - cfg.general.file_symbol.clone() + Some(cfg.general.file_symbol.clone()) } else { - " ".to_string() + None }, if self.doi_url.is_some() { - cfg.general.link_symbol.clone() + Some(cfg.general.link_symbol.clone()) } else { - " ".to_string() + None }, if self.notes.is_some() { - cfg.general.note_symbol.clone() + Some(cfg.general.note_symbol.clone()) } else { - " ".to_string() + None }, ] } @@ -294,7 +294,7 @@ impl BibiSetup { } else { None }, - symbols: [String::new(), String::new(), String::new()], + symbols: [None, None, None], } }) .collect() diff --git a/src/config.rs b/src/config.rs index 6723ce0..f67cb9d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -62,6 +62,11 @@ const DEFAULT_CONFIG: &str = r##" # note_path = "/path/to/notes/folder" # note_extensions = [ "md", "txt", "org" ] +## Symbols/chars to show if not has specific attachement +# note_symbol = "N" +# file_symbol = "F" +# link_symbol = "L" + # [colors] ## Default values for dark-themed terminal ## Possible values are: @@ -78,6 +83,12 @@ const DEFAULT_CONFIG: &str = r##" # bar_bg_color = "234" # popup_bg_color = "234" # selected_row_bg_color = "237" +# note_color = "123" +# file_color = "209" +# link_color = "27" +# author_color = "38" +# title_color = "37" +# year_color = "135" "##; /// Main struct of the config file. Contains substructs/headings in toml diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 970a71d..5904f88 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -701,25 +701,24 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec .map(|(_i, data)| { let item = data.ref_vec(cfg); + let mut symbol_vec = vec![]; + + if let Some(f) = &item.symbols[0] { + symbol_vec.push(Span::styled(f, Style::new().fg(cfg.colors.file_color))); + } + if let Some(l) = &item.symbols[1] { + symbol_vec.push(Span::styled(l, Style::new().fg(cfg.colors.link_color))); + } + if let Some(n) = &item.symbols[2] { + symbol_vec.push(Span::styled(n, Style::new().fg(cfg.colors.note_color))) + } + let row = Row::new(vec![ Cell::from(Line::from(item.authors)), Cell::from(Line::from(item.title)), Cell::from(Line::from(item.year)), Cell::from(Line::from(item.pubtype)), - Cell::from(Line::from(vec![ - Span::styled( - item.symbols[0].clone(), - Style::new().fg(cfg.colors.file_color), - ), - Span::styled( - item.symbols[1].clone(), - Style::new().fg(cfg.colors.link_color), - ), - Span::styled( - item.symbols[2].clone(), - Style::new().fg(cfg.colors.note_color), - ), - ])), + Cell::from(Line::from(symbol_vec)), ]); // let row = item @@ -751,7 +750,12 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec }, ), Constraint::Percentage(10), - Constraint::Length(4), + Constraint::Length( + (cfg.general.file_symbol.chars().count() + + cfg.general.link_symbol.chars().count() + + cfg.general.note_symbol.chars().count() + + 1) as u16, + ), ], ) .block(block) diff --git a/tests/note-files/aristotle_physics.md b/tests/note-files/aristotle_physics.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/test-config.toml b/tests/test-config.toml index 99d1d00..51bd4e6 100644 --- a/tests/test-config.toml +++ b/tests/test-config.toml @@ -27,6 +27,11 @@ pdf_path = "tests/pdf-files" note_path = "tests/note-files" note_extensions = [ "md", "txt" ] +## Symbols/chars to show if not has specific attachement + file_symbol = " " + link_symbol = "󰌹 " + note_symbol = "󰧮" + # [colors] ## Default values for dark-themed terminal ## Possible values are: @@ -43,3 +48,9 @@ note_extensions = [ "md", "txt" ] # bar_bg_color = "234" # popup_bg_color = "234" # selected_row_bg_color = "237" +# note_color = "123" +# file_color = "209" +# link_color = "27" +# author_color = "38" +# title_color = "37" +# year_color = "135" -- cgit v1.2.3 From 8ea28b55f0c6ba210ddcd6a964c8f56d3e6e25ff Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 30 Jun 2025 15:50:10 +0200 Subject: show notes in file info --- src/tui/ui.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src') diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 5904f88..ac44d09 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -908,6 +908,15 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, ), ])); } + if let Some(n) = &cur_entry.notes { + lines.push(Line::from(vec![ + Span::styled("Note: ", style_value), + Span::styled( + n.iter().map(|n| n.to_str().unwrap()).join("; "), + Style::new().fg(cfg.colors.note_color), + ), + ])); + } // if cur_entry.filepath.is_some() { // lines.push(Line::from(vec![ // Span::styled("File: ", style_value), -- cgit v1.2.3 From 3a40bbb367a79dc3660c12aa7f62e3efc378ea22 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 30 Jun 2025 16:33:01 +0200 Subject: update README: note feature --- README.md | 33 +++++++++++++++++++++++++-------- src/app.rs | 2 +- src/bibiman.rs | 32 ++++++++++++++++++++++++++++---- src/bibiman/entries.rs | 5 +++-- src/config.rs | 4 ++-- src/tui/ui.rs | 39 +++++++++++++++++++-------------------- 6 files changed, 78 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/README.md b/README.md index 5bc40b3..4f0e505 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,16 @@ file_prefix = "/some/path/prefix" # of the format "citekey.pdf". Other PDF basenames are not accepted. # Use absolute paths (~ for HOME works). Otherwise, loading might not work. pdf_path = "/path/to/pdf-folder" +## Path to folder (with subfolders) containing note files with the basename of +## the format "citekey.extension". Other basenames are not accepted. The possible +## extensions can be set through the "note_extensions" array. +note_path = "path/to/note-files" +note_extensions = [ "md", "txt" ] + +## Symbols/chars to show if not has specific attachement + file_symbol = " " + link_symbol = "󰌹 " + note_symbol = "󰧮" ``` `bibfiles` @@ -246,13 +256,14 @@ pdf_path = "/path/to/pdf-folder" created through the `pdf_path` variable. Thus, it is safe to mix both approaches if wanted! -`pdf_path` +`pdf_path` and `note_path` -: The `pdf_path` is used as path wich is recursivley searched for files which - basename consists of the an entrys `citekey` plus a `.pdf` ending - (case-insensitive). Every file which matches this pattern for an existing - `citekey` is associated with the particular entry for the current `bibiman` - session and can be opened from within. +: The `pdf_path`/`note_path` is used as path wich is recursivley searched for + files which basename consists of the an entrys `citekey` plus a `.pdf` ending + or one of the specified note endinfs (case-insensitive). Every file which + matches this pattern for an existing `citekey` is associated with the + particular entry for the current `bibiman` session and can be opened from + within. ### Color Configuration @@ -272,6 +283,12 @@ warn_color = "124" bar_bg_color = "234" popup_bg_color = "234" selected_row_bg_color = "237" +note_color = "123" +file_color = "209" +link_color = "27" +author_color = "38" +title_color = "37" +year_color = "135" ``` Colors can be set through three different methods: @@ -309,7 +326,7 @@ These are the current features, the list will be updated: - [x] **Add Entry via DOI**. - [x] **Implement config file** for setting some default values like main bibfile, PDF-opener, or editor -- [ ] **Open related notes file** for specific entry. +- [x] **Open related notes file** for specific entry. - [ ] **Support Hayagriva(`.yaml`)** format as input (_on hold for now_, because the Hayagriva Yaml style doesn't offer keywords; s. issue in [Hayagriva repo](https://github.com/typst/hayagriva/issues/240)). @@ -346,7 +363,7 @@ Use the following keybindings to manage the TUI: There are some shortcuts to select an item from the opening/yanking popup without navigating the list: -- `o-o`|`o-l`: directly opens the first file/link for the selected entry. +- `o-o`|`o-l`|`o-n`: directly opens the first file|link|note for the selected entry. - `y-y`: directly yanks the citekey of the selected entry to the clipboard. ## Search diff --git a/src/app.rs b/src/app.rs index 496896a..18a97e6 100644 --- a/src/app.rs +++ b/src/app.rs @@ -83,7 +83,7 @@ impl App { } else if let Some(PopupKind::YankItem) | Some(PopupKind::OpenRes) = self.bibiman.popup_area.popup_kind { - self.bibiman.fast_selection(cfg, key_event.code)?; + self.bibiman.fast_selection(cfg, &mut tui, key_event.code)?; } let command = if self.input_mode { CmdAction::Input(InputCmdAction::parse(key_event, &self.input)) diff --git a/src/bibiman.rs b/src/bibiman.rs index e1e97ed..9cc9280 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -367,9 +367,9 @@ impl Bibiman { .entry_table_state .selected_column() .unwrap() - == 3 + == 4 { - self.entry_table.entry_table_state.select_first_column(); + self.entry_table.entry_table_state.select_column(Some(1)); } else { self.entry_table.entry_table_state.select_next_column(); } @@ -396,7 +396,7 @@ impl Bibiman { .entry_table_state .selected_column() .unwrap() - == 0 + == 1 { self.entry_table.entry_table_state.select_last_column(); } else { @@ -788,11 +788,17 @@ impl Bibiman { /// /// `o` -> opens the first file of the `filepath` `Vec` for the current entry /// `l` -> opens the link of the current entry + /// `n` -> opens the first note /// /// **Yanking popup** /// /// `y` -> yanks the citekey for the current entry - pub fn fast_selection(&mut self, cfg: &BibiConfig, key_code: KeyCode) -> Result<()> { + pub fn fast_selection( + &mut self, + cfg: &BibiConfig, + tui: &mut Tui, + key_code: KeyCode, + ) -> Result<()> { if let CurrentArea::PopupArea = self.current_area { let entry_idx = self.entry_table.entry_table_state.selected().unwrap(); match self.popup_area.popup_kind { @@ -817,6 +823,24 @@ impl Bibiman { } } } + KeyCode::Char('n') => { + let file = self.entry_table.entry_table_items[entry_idx].notes.clone(); + if file.is_some() { + let file = expand_home(&PathBuf::from(file.unwrap()[0].clone())); + // let object: OsString = popup_entry.into(); + if file.is_file() { + self.open_connected_note(cfg, tui, &file.into_os_string())?; + self.close_popup(); + } else { + self.open_popup( + PopupKind::MessageError, + Some("No valid file path: "), + Some(file.to_str().unwrap()), + None, + )?; + } + } + } KeyCode::Char('l') => { if self.entry_table.entry_table_items[entry_idx] .doi_url diff --git a/src/bibiman/entries.rs b/src/bibiman/entries.rs index e0c230b..9b536fd 100644 --- a/src/bibiman/entries.rs +++ b/src/bibiman/entries.rs @@ -47,8 +47,9 @@ impl EntryTable { // entry_table let entry_table_state = TableState::default() .with_selected(0) - .with_selected_column(0) - .with_selected_cell(Some((0, 0))); + .with_selected_column(1) + // other two values above are ignored, if selected cell isn't fitting + .with_selected_cell(Some((0, 1))); let entry_scroll_state = ScrollbarState::new(entry_table_items.len()); let entry_info_scroll_state = ScrollbarState::default(); Self { diff --git a/src/config.rs b/src/config.rs index f67cb9d..278f4b1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -85,7 +85,7 @@ const DEFAULT_CONFIG: &str = r##" # selected_row_bg_color = "237" # note_color = "123" # file_color = "209" -# link_color = "27" +# link_color = "39" # author_color = "38" # title_color = "37" # year_color = "135" @@ -215,7 +215,7 @@ impl BibiConfig { selected_row_bg_color: Color::Indexed(237), note_color: Color::Indexed(123), file_color: Color::Indexed(209), - link_color: Color::Indexed(33), + link_color: Color::Indexed(39), author_color: Color::Indexed(38), title_color: Color::Indexed(37), year_color: Color::Indexed(135), diff --git a/src/tui/ui.rs b/src/tui/ui.rs index ac44d09..3c83935 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -580,7 +580,7 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec .bg(cfg.colors.bar_bg_color); let header = Row::new(vec![ - // Cell::from(Line::from("")).bg(cfg.colors.bar_bg_color), + Cell::from(Line::from("")).bg(cfg.colors.bar_bg_color), Cell::from( Line::from(vec![{ Span::raw("Author") }, { if let Some(EntryTableColumn::Authors) = @@ -704,21 +704,30 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec let mut symbol_vec = vec![]; if let Some(f) = &item.symbols[0] { - symbol_vec.push(Span::styled(f, Style::new().fg(cfg.colors.file_color))); + symbol_vec.push(Span::styled( + f, + Style::new().fg(cfg.colors.file_color).bold(), + )); } if let Some(l) = &item.symbols[1] { - symbol_vec.push(Span::styled(l, Style::new().fg(cfg.colors.link_color))); + symbol_vec.push(Span::styled( + l, + Style::new().fg(cfg.colors.link_color).bold(), + )); } if let Some(n) = &item.symbols[2] { - symbol_vec.push(Span::styled(n, Style::new().fg(cfg.colors.note_color))) + symbol_vec.push(Span::styled( + n, + Style::new().fg(cfg.colors.note_color).bold(), + )) } let row = Row::new(vec![ + Cell::from(Line::from(symbol_vec)), Cell::from(Line::from(item.authors)), Cell::from(Line::from(item.title)), Cell::from(Line::from(item.year)), Cell::from(Line::from(item.pubtype)), - Cell::from(Line::from(symbol_vec)), ]); // let row = item @@ -738,6 +747,11 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec let entry_table = Table::new( rows, [ + Constraint::Length( + (cfg.general.file_symbol.chars().count() + + cfg.general.link_symbol.chars().count() + + cfg.general.note_symbol.chars().count()) as u16, + ), Constraint::Percentage(20), Constraint::Fill(1), Constraint::Length( @@ -750,12 +764,6 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec }, ), Constraint::Percentage(10), - Constraint::Length( - (cfg.general.file_symbol.chars().count() - + cfg.general.link_symbol.chars().count() - + cfg.general.note_symbol.chars().count() - + 1) as u16, - ), ], ) .block(block) @@ -917,15 +925,6 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, ), ])); } - // if cur_entry.filepath.is_some() { - // lines.push(Line::from(vec![ - // Span::styled("File: ", style_value), - // Span::styled( - // cur_entry.filepath().to_string_lossy(), - // Style::new().fg(cfg.colors.main_text_color), - // ), - // ])); - // } lines.push(Line::from("")); lines.push(Line::from(vec![Span::styled( cur_entry.abstract_text.clone(), -- cgit v1.2.3 From 9bd2f6fef0d835ffb97e18993161e6639c98d2d1 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 30 Jun 2025 21:49:53 +0200 Subject: align resource symbols, update README --- README.md | 2 ++ src/app.rs | 13 +++++++++---- src/bibiman.rs | 2 +- src/tui/ui.rs | 19 +++++++++++++++++-- tests/test-config.toml | 6 +++--- 5 files changed, 32 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/README.md b/README.md index 4f0e505..ed620e8 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ Here's a small impression how it looks and works: [![bibiman.gif](https://i.postimg.cc/Y0mCNDMg/bibiman.gif)](https://postimg.cc/ct0W0mK4) +![screenshot with new note feature](https://codeberg.org/attachments/69d35f36-cff3-43e5-8bfd-361064ba8ab2) + ## Installation ### Crates.io diff --git a/src/app.rs b/src/app.rs index 18a97e6..f912614 100644 --- a/src/app.rs +++ b/src/app.rs @@ -16,15 +16,14 @@ ///// use crate::bibiman::CurrentArea; -use crate::config::BibiConfig; -use color_eyre::eyre::{Context, Ok, Result}; -use editor_command::EditorBuilder; -// use super::Event; use crate::cliargs::CLIArgs; +use crate::config::BibiConfig; use crate::tui::commands::InputCmdAction; use crate::tui::popup::PopupKind; use crate::tui::{self, Tui}; use crate::{bibiman::Bibiman, tui::commands::CmdAction}; +use color_eyre::eyre::{Context, Ok, Result}; +use crossterm::event::KeyCode; use std::ffi::OsStr; use std::path::PathBuf; use std::process::{Command, Stdio}; @@ -84,6 +83,12 @@ impl App { self.bibiman.popup_area.popup_kind { self.bibiman.fast_selection(cfg, &mut tui, key_event.code)?; + // if a fast match char was used, restart event-loop. + // otherwise, the fast match char will be executed as command + match key_event.code { + KeyCode::Char('o' | 'l' | 'n' | 'y') => continue, + _ => {} + } } let command = if self.input_mode { CmdAction::Input(InputCmdAction::parse(key_event, &self.input)) diff --git a/src/bibiman.rs b/src/bibiman.rs index 9cc9280..f95bbc0 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -35,7 +35,7 @@ use std::fs::{self, read_to_string}; use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::PathBuf; -use std::process::{Command, Stdio}; +use std::process::Command; use std::result::Result::Ok; use tui_input::Input; diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 3c83935..be53f61 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -300,7 +300,7 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { }; let bottom_info = if let Some(PopupKind::OpenRes) = app.bibiman.popup_area.popup_kind { - " (j,k|↓,↑) ━ (o,l) ━ (ENTER) ━ (ESC) ".bold() + " (j,k|↓,↑) ━ (o,l,n) ━ (ENTER) ━ (ESC) ".bold() } else if let Some(PopupKind::YankItem) = app.bibiman.popup_area.popup_kind { " (j,k|↓,↑) ━ (y) ━ (ENTER) ━ (ESC) ".bold() } else { @@ -580,7 +580,7 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec .bg(cfg.colors.bar_bg_color); let header = Row::new(vec![ - Cell::from(Line::from("")).bg(cfg.colors.bar_bg_color), + Cell::from(Line::from("Res.")).bg(cfg.colors.bar_bg_color), Cell::from( Line::from(vec![{ Span::raw("Author") }, { if let Some(EntryTableColumn::Authors) = @@ -703,23 +703,38 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec let mut symbol_vec = vec![]; + // use default or custom symbols for resources + // if an entry has no, replace it with the correct number + // of whitespace to align the symbols correct if let Some(f) = &item.symbols[0] { symbol_vec.push(Span::styled( f, Style::new().fg(cfg.colors.file_color).bold(), )); + } else { + symbol_vec.push(Span::raw( + " ".repeat(cfg.general.file_symbol.chars().count()), + )); } if let Some(l) = &item.symbols[1] { symbol_vec.push(Span::styled( l, Style::new().fg(cfg.colors.link_color).bold(), )); + } else { + symbol_vec.push(Span::raw( + " ".repeat(cfg.general.link_symbol.chars().count()), + )); } if let Some(n) = &item.symbols[2] { symbol_vec.push(Span::styled( n, Style::new().fg(cfg.colors.note_color).bold(), )) + } else { + symbol_vec.push(Span::raw( + " ".repeat(cfg.general.note_symbol.chars().count()), + )); } let row = Row::new(vec![ diff --git a/tests/test-config.toml b/tests/test-config.toml index 51bd4e6..1d29043 100644 --- a/tests/test-config.toml +++ b/tests/test-config.toml @@ -28,9 +28,9 @@ note_path = "tests/note-files" note_extensions = [ "md", "txt" ] ## Symbols/chars to show if not has specific attachement - file_symbol = " " - link_symbol = "󰌹 " - note_symbol = "󰧮" +file_symbol = " " +link_symbol = "󰌹 " +note_symbol = "󰧮" # [colors] ## Default values for dark-themed terminal -- cgit v1.2.3 From 61b22382e0979f756538215047bafd30866ccf1e Mon Sep 17 00:00:00 2001 From: lukeflo Date: Thu, 3 Jul 2025 15:06:19 +0200 Subject: detach opened note from terminal window running bibiman --- src/bibiman.rs | 96 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/bibiman.rs b/src/bibiman.rs index f95bbc0..200db96 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -25,7 +25,7 @@ use crate::tui::Tui; use crate::{app, cliargs}; use crate::{bibiman::entries::EntryTable, bibiman::keywords::TagList}; use arboard::Clipboard; -use color_eyre::eyre::{Error, Result}; +use color_eyre::eyre::{Context, Error, Result}; use crossterm::event::KeyCode; use editor_command::EditorBuilder; use ratatui::widgets::ScrollbarState; @@ -35,7 +35,7 @@ use std::fs::{self, read_to_string}; use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::PathBuf; -use std::process::Command; +use std::process::{Command, Stdio}; use std::result::Result::Ok; use tui_input::Input; @@ -520,35 +520,63 @@ impl Bibiman { file: &OsStr, ) -> Result<()> { // get filecontent and citekey for calculating line number - let citekey: &str = &self.entry_table.entry_table_items - [self.entry_table.entry_table_state.selected().unwrap()] - .citekey - .clone(); - // Exit TUI to enter editor - tui.exit()?; - // Use VISUAL or EDITOR. Set "vi" as last fallback - let mut cmd: Command = EditorBuilder::new() - .source(cfg.general.editor.as_ref()) - .environment() - .source(Some("vi")) - .build() - .unwrap(); - // Prepare arguments to open file at specific line - let status = cmd.arg(file).status()?; - if !status.success() { - eprintln!("Spawning editor failed with status {}", status); - } + match std::env::var("TERM") { + Ok(sh) => { + let editor = if let Some(e) = cfg.general.editor.clone() { + e + } else if let Ok(e) = std::env::var("VISUAL") { + e + } else if let Ok(e) = std::env::var("EDITOR") { + e + } else { + String::from("vi") + }; + let _ = Command::new(sh) + .arg("-e") + .arg(editor) + .arg(file) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .wrap_err("Couldn't run editor"); + // Prepare arguments to open file at specific line + // let status = note_cmd.status()?; + // if !status.success() { + // eprintln!("Spawning editor failed with status {}", status); + // } + } + Err(_e) => { + let citekey: &str = &self.entry_table.entry_table_items + [self.entry_table.entry_table_state.selected().unwrap()] + .citekey + .clone(); + // Exit TUI to enter editor + tui.exit()?; + // Use VISUAL or EDITOR. Set "vi" as last fallback + let mut note_cmd: Command = EditorBuilder::new() + .source(cfg.general.editor.clone()) + .environment() + .source(Some("vi")) + .build() + .unwrap(); + // Prepare arguments to open file at specific line + let status = note_cmd.arg(file).status()?; + if !status.success() { + eprintln!("Spawning editor failed with status {}", status); + } - // Enter TUI again - tui.enter()?; - tui.terminal.clear()?; + // Enter TUI again + tui.enter()?; + tui.terminal.clear()?; - // Update the database and the lists to show changes - // Self::update_lists(self, cfg); + // Update the database and the lists to show changes + // Self::update_lists(self, cfg); - // Select entry which was selected before entering editor - self.select_entry_by_citekey(citekey); + // Select entry which was selected before entering editor + self.select_entry_by_citekey(citekey); + } + } Ok(()) } @@ -808,7 +836,19 @@ impl Bibiman { .filepath .clone(); if file.is_some() { - let file = expand_home(&PathBuf::from(file.unwrap()[0].clone())); + let file = if self.entry_table.entry_table_items[entry_idx].file_field + && cfg.general.file_prefix.is_some() + { + cfg.general + .file_prefix + .clone() + .unwrap() + .join(&file.unwrap()[0]) + .into_os_string() + } else { + file.unwrap()[0].clone() + }; + let file = expand_home(&PathBuf::from(file)); // let object: OsString = popup_entry.into(); if file.is_file() { app::open_connected_file(cfg, &file.into_os_string())?; -- cgit v1.2.3 From 2594cf34dcf2f04f398dab7b6ecae364eb4c7d17 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Fri, 4 Jul 2025 13:56:55 +0200 Subject: impl `PopupItem` enum, adapt ui: include colors --- src/app.rs | 30 ++++++++++++++++++++++++------ src/bibiman.rs | 17 +++++++++++++---- src/tui/popup.rs | 16 +++++++++++++--- src/tui/ui.rs | 16 ++++++++++++---- 4 files changed, 62 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index f912614..d645dbe 100644 --- a/src/app.rs +++ b/src/app.rs @@ -19,7 +19,7 @@ use crate::bibiman::CurrentArea; use crate::cliargs::CLIArgs; use crate::config::BibiConfig; use crate::tui::commands::InputCmdAction; -use crate::tui::popup::PopupKind; +use crate::tui::popup::{PopupItem, PopupKind}; use crate::tui::{self, Tui}; use crate::{bibiman::Bibiman, tui::commands::CmdAction}; use color_eyre::eyre::{Context, Ok, Result}; @@ -311,13 +311,25 @@ impl App { .selected() .unwrap(); let entry = self.bibiman.entry_table.entry_table_items[idx].clone(); - let mut items = vec![("Citekey: ".to_string(), entry.citekey.clone())]; + let mut items = vec![( + "Citekey: ".to_string(), + entry.citekey.clone(), + PopupItem::Default, + )]; if entry.doi_url.is_some() { - items.push(("Weblink: ".into(), entry.doi_url.unwrap().clone())) + items.push(( + "Weblink: ".into(), + entry.doi_url.unwrap().clone(), + PopupItem::Link, + )) } if entry.filepath.is_some() { entry.filepath.unwrap().iter().for_each(|p| { - items.push(("Filepath: ".into(), p.clone().into_string().unwrap())) + items.push(( + "Filepath: ".into(), + p.clone().into_string().unwrap(), + PopupItem::Entryfile, + )) }); // items.push(( // "Filepath: ".into(), @@ -348,13 +360,14 @@ impl App { .selected() .unwrap(); let entry = self.bibiman.entry_table.entry_table_items[idx].clone(); - let mut items: Vec<(String, String)> = vec![]; + let mut items: Vec<(String, String, PopupItem)> = vec![]; if entry.filepath.is_some() || entry.doi_url.is_some() || entry.notes.is_some() { if entry.doi_url.is_some() { items.push(( "Weblink (DOI/URL): ".into(), entry.doi_url.unwrap().clone(), + PopupItem::Link, )) } if entry.filepath.is_some() { @@ -374,12 +387,17 @@ impl App { } else { p.clone().into_string().unwrap() }, + PopupItem::Entryfile, )) }); } if entry.notes.is_some() { entry.notes.unwrap().iter().for_each(|n| { - items.push(("Note: ".into(), n.clone().into_string().unwrap())); + items.push(( + "Note: ".into(), + n.clone().into_string().unwrap(), + PopupItem::Notefile, + )); }); } diff --git a/src/bibiman.rs b/src/bibiman.rs index 200db96..fb72e93 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -20,7 +20,7 @@ use crate::bibiman::entries::EntryTableColumn; use crate::bibiman::{bibisetup::*, search::BibiSearch}; use crate::cliargs::CLIArgs; use crate::config::BibiConfig; -use crate::tui::popup::{PopupArea, PopupKind}; +use crate::tui::popup::{PopupArea, PopupItem, PopupKind}; use crate::tui::Tui; use crate::{app, cliargs}; use crate::{bibiman::entries::EntryTable, bibiman::keywords::TagList}; @@ -161,7 +161,7 @@ impl Bibiman { popup_kind: PopupKind, message: Option<&str>, object: Option<&str>, - items: Option>, + items: Option>, ) -> Result<()> { if let CurrentArea::EntryArea = self.current_area { self.former_area = Some(FormerArea::EntryArea); @@ -631,10 +631,18 @@ impl Bibiman { } pub fn append_to_file(&mut self) { - let mut items = vec![("Create new file".to_owned(), "".to_string())]; + let mut items = vec![( + "Create new file".to_owned(), + "".to_string(), + PopupItem::Default, + )]; if self.main_bibfiles.len() > 1 { for f in self.main_bibfiles.clone() { - items.push(("File: ".into(), f.to_str().unwrap().to_owned())); + items.push(( + "File: ".into(), + f.to_str().unwrap().to_owned(), + PopupItem::Bibfile, + )); } } else { items.push(( @@ -645,6 +653,7 @@ impl Bibiman { .to_str() .unwrap() .to_owned(), + PopupItem::Bibfile, )); } self.popup_area.popup_selection(items); diff --git a/src/tui/popup.rs b/src/tui/popup.rs index 93b01c3..da44744 100644 --- a/src/tui/popup.rs +++ b/src/tui/popup.rs @@ -40,13 +40,23 @@ pub enum PopupKind { YankItem, } +#[derive(Debug)] +pub enum PopupItem { + Bibfile, + Entryfile, + Notefile, + Link, + Default, + None, +} + #[derive(Debug, Default)] pub struct PopupArea { pub is_popup: bool, pub popup_kind: Option, pub popup_message: String, pub popup_scroll_pos: u16, - pub popup_list: Vec<(String, String)>, + pub popup_list: Vec<(String, String, PopupItem)>, pub popup_state: ListState, pub popup_sel_item: String, // pub add_entry_input: String, @@ -116,8 +126,8 @@ impl PopupArea { /// Opens a popup with a selectable list /// - /// The list items are passed as argument of the kind `Vec<(String, String)>`. - pub fn popup_selection(&mut self, items: Vec<(String, String)>) { + /// The list items are passed as argument of the kind `Vec<(String, String, PopupItem)>`. + pub fn popup_selection(&mut self, items: Vec<(String, String, PopupItem)>) { self.popup_list = items; // self.popup_kind = Some(PopupKind::SelectRes); self.is_popup = true; diff --git a/src/tui/ui.rs b/src/tui/ui.rs index be53f61..2c30154 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -17,7 +17,7 @@ use std::path::PathBuf; -use super::popup::PopupArea; +use super::popup::{PopupArea, PopupItem}; use crate::bibiman::entries::EntryTableColumn; use crate::bibiman::{CurrentArea, FormerArea}; use crate::cliargs::CLIArgs; @@ -280,9 +280,17 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { .popup_list .iter() .map( - |(mes, obj)| { + |(mes, obj, i)| { + let style = match i { + PopupItem::Bibfile => Style::new().fg(cfg.colors.entry_color), + PopupItem::Entryfile => Style::new().fg(cfg.colors.file_color), + PopupItem::Notefile => Style::new().fg(cfg.colors.note_color), + PopupItem::Link => Style::new().fg(cfg.colors.link_color), + PopupItem::Default => Style::new(), + PopupItem::None => Style::new(), + }; ListItem::from(Line::from(vec![ - Span::styled(mes, Style::new().bold()), + Span::styled(mes, style.bold()), Span::raw(obj), ])) }, // ListItem::from(mes.to_owned() + obj) @@ -333,7 +341,7 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { .popup_area .popup_list .iter() - .max_by(|(mes, obj), (m, o)| { + .max_by(|(mes, obj, _ik), (m, o, _i)| { let x = mes.chars().count() + obj.chars().count(); let y = m.chars().count() + o.chars().count(); x.cmp(&y) -- cgit v1.2.3 From 5ebecf2ea9d0fb9c57b264d7cd1c6e92b36d30e8 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Fri, 4 Jul 2025 14:41:47 +0200 Subject: `PopupItem::Citekey` impl --- src/app.rs | 8 ++++---- src/config.rs | 4 ++++ src/tui/popup.rs | 1 + src/tui/ui.rs | 28 ++++++++++++++++------------ 4 files changed, 25 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index d645dbe..d475328 100644 --- a/src/app.rs +++ b/src/app.rs @@ -314,7 +314,7 @@ impl App { let mut items = vec![( "Citekey: ".to_string(), entry.citekey.clone(), - PopupItem::Default, + PopupItem::Citekey, )]; if entry.doi_url.is_some() { items.push(( @@ -365,7 +365,7 @@ impl App { { if entry.doi_url.is_some() { items.push(( - "Weblink (DOI/URL): ".into(), + format!("{} ", cfg.general.link_symbol.clone().trim()), entry.doi_url.unwrap().clone(), PopupItem::Link, )) @@ -373,7 +373,7 @@ impl App { if entry.filepath.is_some() { entry.filepath.unwrap().iter().for_each(|p| { items.push(( - "File (PDF/EPUB): ".into(), + format!("{} ", cfg.general.file_symbol.clone().trim()), // p.clone().into_string().unwrap(), if entry.file_field && cfg.general.file_prefix.is_some() { cfg.general @@ -394,7 +394,7 @@ impl App { if entry.notes.is_some() { entry.notes.unwrap().iter().for_each(|n| { items.push(( - "Note: ".into(), + format!("{} ", cfg.general.note_symbol.clone().trim()), n.clone().into_string().unwrap(), PopupItem::Notefile, )); diff --git a/src/config.rs b/src/config.rs index 278f4b1..1f6c619 100644 --- a/src/config.rs +++ b/src/config.rs @@ -81,6 +81,7 @@ const DEFAULT_CONFIG: &str = r##" # confirm_color = "47" # warn_color = "124" # bar_bg_color = "234" +# popup_fg_color = "43" # popup_bg_color = "234" # selected_row_bg_color = "237" # note_color = "123" @@ -125,6 +126,7 @@ pub struct Colors { pub confirm_color: Color, pub warn_color: Color, pub bar_bg_color: Color, + pub popup_fg_color: Color, pub popup_bg_color: Color, pub selected_row_bg_color: Color, pub note_color: Color, @@ -211,6 +213,7 @@ impl BibiConfig { confirm_color: Color::Indexed(47), warn_color: Color::Indexed(124), bar_bg_color: Color::Indexed(235), + popup_fg_color: Color::Indexed(43), popup_bg_color: Color::Indexed(234), selected_row_bg_color: Color::Indexed(237), note_color: Color::Indexed(123), @@ -231,6 +234,7 @@ impl BibiConfig { keyword_color: Color::Indexed(58), info_color: Color::Indexed(57), bar_bg_color: Color::Indexed(144), + popup_fg_color: Color::Indexed(43), popup_bg_color: Color::Indexed(187), confirm_color: Color::Indexed(22), warn_color: Color::Indexed(124), diff --git a/src/tui/popup.rs b/src/tui/popup.rs index da44744..4aaa2c1 100644 --- a/src/tui/popup.rs +++ b/src/tui/popup.rs @@ -45,6 +45,7 @@ pub enum PopupItem { Bibfile, Entryfile, Notefile, + Citekey, Link, Default, None, diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 2c30154..0a34e51 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -281,18 +281,22 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { .iter() .map( |(mes, obj, i)| { - let style = match i { - PopupItem::Bibfile => Style::new().fg(cfg.colors.entry_color), - PopupItem::Entryfile => Style::new().fg(cfg.colors.file_color), - PopupItem::Notefile => Style::new().fg(cfg.colors.note_color), - PopupItem::Link => Style::new().fg(cfg.colors.link_color), - PopupItem::Default => Style::new(), - PopupItem::None => Style::new(), + let style: Color = match i { + PopupItem::Bibfile => cfg.colors.entry_color, + PopupItem::Citekey => cfg.colors.entry_color, + PopupItem::Entryfile => cfg.colors.file_color, + PopupItem::Notefile => cfg.colors.note_color, + PopupItem::Link => cfg.colors.link_color, + PopupItem::Default => cfg.colors.main_text_color, + PopupItem::None => cfg.colors.main_text_color, }; - ListItem::from(Line::from(vec![ - Span::styled(mes, style.bold()), - Span::raw(obj), - ])) + ListItem::from( + Line::from(vec![ + Span::styled(mes, Style::new().bold()), + Span::raw(obj), + ]) + .fg(style), + ) }, // ListItem::from(mes.to_owned() + obj) ) .collect(); @@ -325,7 +329,7 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { .bg(cfg.colors.popup_bg_color), ) .border_set(symbols::border::THICK) - .border_style(Style::new().fg(cfg.colors.keyword_color)); + .border_style(Style::new().fg(cfg.colors.popup_fg_color)); let list = List::new(list_items).block(block).highlight_style( Style::new() -- cgit v1.2.3 From 52079ee745831b06a6c4060f38ee49e42d689dcd Mon Sep 17 00:00:00 2001 From: lukeflo Date: Fri, 4 Jul 2025 15:05:08 +0200 Subject: use `PopupItem` to determine resource type --- src/bibiman.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/bibiman.rs b/src/bibiman.rs index fb72e93..6aec1fb 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -749,12 +749,12 @@ impl Bibiman { 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") { + if let PopupItem::Link = self.popup_area.popup_list[popup_idx].2 { let object = self.entry_table.entry_table_items[entry_idx].doi_url(); let url = app::prepare_weblink(object); app::open_connected_link(cfg, &url)?; self.close_popup(); - } else if self.popup_area.popup_list[popup_idx].0.contains("File") { + } else if let PopupItem::Entryfile = self.popup_area.popup_list[popup_idx].2 { // TODO: Selection for multiple files // let object = self.entry_table.entry_table_items[entry_idx].filepath()[0]; let file = expand_home(&PathBuf::from(popup_entry.clone())); @@ -770,7 +770,7 @@ impl Bibiman { None, )?; } - } else if self.popup_area.popup_list[popup_idx].0.contains("Note") { + } else if let PopupItem::Notefile = self.popup_area.popup_list[popup_idx].2 { let file = expand_home(&PathBuf::from(popup_entry.clone())); // let object: OsString = popup_entry.into(); if file.is_file() { -- cgit v1.2.3 From 80c04702012cb1a43711d559e8ffbe9e250b1a57 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sat, 5 Jul 2025 20:21:52 +0200 Subject: first steps for create new note --- src/app.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/bibiman.rs | 4 ++++ src/tui/commands.rs | 2 ++ src/tui/popup.rs | 2 ++ 4 files changed, 67 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index d475328..14cc864 100644 --- a/src/app.rs +++ b/src/app.rs @@ -365,7 +365,7 @@ impl App { { if entry.doi_url.is_some() { items.push(( - format!("{} ", cfg.general.link_symbol.clone().trim()), + "Link: ".into(), entry.doi_url.unwrap().clone(), PopupItem::Link, )) @@ -373,7 +373,7 @@ impl App { if entry.filepath.is_some() { entry.filepath.unwrap().iter().for_each(|p| { items.push(( - format!("{} ", cfg.general.file_symbol.clone().trim()), + "File: ".into(), // p.clone().into_string().unwrap(), if entry.file_field && cfg.general.file_prefix.is_some() { cfg.general @@ -394,7 +394,7 @@ impl App { if entry.notes.is_some() { entry.notes.unwrap().iter().for_each(|n| { items.push(( - format!("{} ", cfg.general.note_symbol.clone().trim()), + "Note: ".into(), n.clone().into_string().unwrap(), PopupItem::Notefile, )); @@ -419,6 +419,62 @@ impl App { self.bibiman.add_entry(); } } + CmdAction::CreateNote => { + if let CurrentArea::EntryArea = self.bibiman.current_area { + if cfg.general.note_path.is_some() + && cfg.general.note_extensions.is_some() + && self.bibiman.entry_table.entry_table_items[self + .bibiman + .entry_table + .entry_table_state + .selected() + .unwrap()] + .notes + .is_none() + { + let mut items = vec![]; + for ex in cfg.general.note_extensions.as_ref().unwrap() { + items.push(( + self.bibiman.entry_table.entry_table_items[self + .bibiman + .entry_table + .entry_table_state + .selected() + .unwrap()] + .citekey() + .to_string(), + ex.clone(), + PopupItem::Notefile, + )); + } + self.bibiman + .open_popup(PopupKind::CreateNote, None, None, Some(items)); + } else if cfg.general.note_path.is_some() + && self.bibiman.entry_table.entry_table_items[self + .bibiman + .entry_table + .entry_table_state + .selected() + .unwrap()] + .notes + .is_some() + { + self.bibiman.open_popup( + PopupKind::MessageError, + Some("Selected entry already has a connected note"), + None, + None, + )?; + } else { + self.bibiman.open_popup( + PopupKind::MessageError, + Some("No note path found. Set it in config file."), + None, + None, + )?; + } + } + } CmdAction::ShowHelp => { self.bibiman.open_popup(PopupKind::Help, None, None, None)?; } diff --git a/src/bibiman.rs b/src/bibiman.rs index 6aec1fb..c92b869 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -740,6 +740,10 @@ impl Bibiman { Ok(()) } + pub fn create_note(&mut self, cfg: &BibiConfig) -> Result<()> { + Ok(()) + } + pub fn open_connected_res(&mut self, cfg: &BibiConfig, tui: &mut Tui) -> Result<()> { // Index of selected entry let entry_idx = self.entry_table.entry_table_state.selected().unwrap(); diff --git a/src/tui/commands.rs b/src/tui/commands.rs index 08ee677..47d2802 100644 --- a/src/tui/commands.rs +++ b/src/tui/commands.rs @@ -73,6 +73,8 @@ pub enum CmdAction { ShowHelp, // Add new entry AddEntry, + // Create note + CreateNote, // Do nothing. Nothing, } diff --git a/src/tui/popup.rs b/src/tui/popup.rs index 4aaa2c1..46e4792 100644 --- a/src/tui/popup.rs +++ b/src/tui/popup.rs @@ -38,6 +38,8 @@ pub enum PopupKind { AddEntry, /// select an item of the current entry to yank to clipboard YankItem, + /// Create a new note, select extension + CreateNote, } #[derive(Debug)] -- cgit v1.2.3 From 2990df627ff54f01bafdcab767c0a73198e9e6cc Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sat, 5 Jul 2025 22:29:18 +0200 Subject: create note function impl --- src/app.rs | 10 ++++- src/bibiman.rs | 33 +++++++++++++++++ src/tui/commands.rs | 2 + src/tui/ui.rs | 104 ++++++++++++++++++++++++++++++++++------------------ 4 files changed, 111 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 14cc864..01424bc 100644 --- a/src/app.rs +++ b/src/app.rs @@ -192,7 +192,8 @@ impl App { } Some(PopupKind::OpenRes) | Some(PopupKind::AppendToFile) - | Some(PopupKind::YankItem) => { + | Some(PopupKind::YankItem) + | Some(PopupKind::CreateNote) => { self.bibiman.popup_area.popup_state.scroll_down_by(1) } _ => {} @@ -213,7 +214,8 @@ impl App { } Some(PopupKind::OpenRes) | Some(PopupKind::AppendToFile) - | Some(PopupKind::YankItem) => { + | Some(PopupKind::YankItem) + | Some(PopupKind::CreateNote) => { self.bibiman.popup_area.popup_state.scroll_up_by(1) } _ => {} @@ -273,6 +275,8 @@ impl App { self.bibiman.close_popup(); } else if let Some(PopupKind::YankItem) = self.bibiman.popup_area.popup_kind { self.bibiman.close_popup(); + } else if let Some(PopupKind::CreateNote) = self.bibiman.popup_area.popup_kind { + self.bibiman.close_popup(); } } else { self.bibiman.reset_current_list(); @@ -291,6 +295,8 @@ impl App { self.bibiman.append_entry_to_file(cfg)? } else if let Some(PopupKind::YankItem) = self.bibiman.popup_area.popup_kind { self.bibiman.yank_entry_field()? + } else if let Some(PopupKind::CreateNote) = self.bibiman.popup_area.popup_kind { + self.bibiman.create_note(cfg)? } } } diff --git a/src/bibiman.rs b/src/bibiman.rs index c92b869..71ac831 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -238,6 +238,18 @@ impl Bibiman { )) } } + PopupKind::CreateNote => { + if items.is_some() { + self.popup_area.popup_kind = Some(PopupKind::CreateNote); + self.popup_area.popup_selection(items.unwrap()); + self.popup_area.popup_state.select(Some(0)); + Ok(()) + } else { + Err(Error::msg( + "No Vec<(String, String)> passed as argument to generate the items list", + )) + } + } } } @@ -741,6 +753,27 @@ impl Bibiman { } pub fn create_note(&mut self, cfg: &BibiConfig) -> Result<()> { + // Index of selected entry + let entry_idx = self.entry_table.entry_table_state.selected().unwrap(); + let citekey = self.entry_table.entry_table_items[entry_idx].citekey(); + + // Index of selected popup field + let popup_idx = self.popup_area.popup_state.selected().unwrap(); + let ext = self.popup_area.popup_list[popup_idx].1.clone(); + + let basename = PathBuf::from(citekey).with_extension(ext); + let path = cfg.general.note_path.as_ref().unwrap(); + + let new_file = path.join(basename); + + let new_file = if new_file.starts_with("~") { + expand_home(&new_file) + } else { + new_file + }; + + File::create_new(new_file).unwrap(); + self.close_popup(); Ok(()) } diff --git a/src/tui/commands.rs b/src/tui/commands.rs index 47d2802..89fcf44 100644 --- a/src/tui/commands.rs +++ b/src/tui/commands.rs @@ -151,6 +151,8 @@ impl From for CmdAction { // Open linked ressource KeyCode::Char('o') => Self::Open, // KeyCode::Char('u') => Self::Open(OpenRessource::WebLink), + // Create note file + KeyCode::Char('n') => Self::CreateNote, // Edit currently selected entry KeyCode::Char('e') => Self::EditFile, // Yank selected item/value diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 0a34e51..69ca058 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -273,33 +273,49 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { frame.render_widget(Clear, popup_area); frame.render_widget(&content, popup_area) } - Some(PopupKind::OpenRes) | Some(PopupKind::AppendToFile) | Some(PopupKind::YankItem) => { - let list_items: Vec = app - .bibiman - .popup_area - .popup_list - .iter() - .map( - |(mes, obj, i)| { - let style: Color = match i { - PopupItem::Bibfile => cfg.colors.entry_color, - PopupItem::Citekey => cfg.colors.entry_color, - PopupItem::Entryfile => cfg.colors.file_color, - PopupItem::Notefile => cfg.colors.note_color, - PopupItem::Link => cfg.colors.link_color, - PopupItem::Default => cfg.colors.main_text_color, - PopupItem::None => cfg.colors.main_text_color, - }; - ListItem::from( - Line::from(vec![ - Span::styled(mes, Style::new().bold()), - Span::raw(obj), - ]) - .fg(style), - ) - }, // ListItem::from(mes.to_owned() + obj) - ) - .collect(); + Some(PopupKind::OpenRes) + | Some(PopupKind::AppendToFile) + | Some(PopupKind::YankItem) + | Some(PopupKind::CreateNote) => { + let list_items: Vec = if let Some(PopupKind::CreateNote) = + app.bibiman.popup_area.popup_kind + { + app.bibiman + .popup_area + .popup_list + .iter() + .map(|(m, o, _i)| { + ListItem::from(Line::from(vec![Span::raw(m), Span::raw("."), Span::raw(o)])) + .fg(cfg.colors.note_color) + }) + .collect() + } else { + app.bibiman + .popup_area + .popup_list + .iter() + .map( + |(mes, obj, i)| { + let style: Color = match i { + PopupItem::Bibfile => cfg.colors.entry_color, + PopupItem::Citekey => cfg.colors.entry_color, + PopupItem::Entryfile => cfg.colors.file_color, + PopupItem::Notefile => cfg.colors.note_color, + PopupItem::Link => cfg.colors.link_color, + PopupItem::Default => cfg.colors.main_text_color, + PopupItem::None => cfg.colors.main_text_color, + }; + ListItem::from( + Line::from(vec![ + Span::styled(mes, Style::new().bold()), + Span::raw(obj), + ]) + .fg(style), + ) + }, // ListItem::from(mes.to_owned() + obj) + ) + .collect() + }; let title = if let Some(PopupKind::OpenRes) = app.bibiman.popup_area.popup_kind { " Open " @@ -307,21 +323,23 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { " Select file to append entry " } else if let Some(PopupKind::YankItem) = app.bibiman.popup_area.popup_kind { " Yank to clipboard " + } else if let Some(PopupKind::CreateNote) = app.bibiman.popup_area.popup_kind { + " Create Note with extension " } else { " Select " }; let bottom_info = if let Some(PopupKind::OpenRes) = app.bibiman.popup_area.popup_kind { - " (j,k|↓,↑) ━ (o,l,n) ━ (ENTER) ━ (ESC) ".bold() + " (j,k|↓,↑) ━ (o,l,n) ━ (ENTER) ━ (ESC) " } else if let Some(PopupKind::YankItem) = app.bibiman.popup_area.popup_kind { - " (j,k|↓,↑) ━ (y) ━ (ENTER) ━ (ESC) ".bold() + " (j,k|↓,↑) ━ (y) ━ (ENTER) ━ (ESC) " } else { - " (j,k|↓,↑) ━ (ENTER) ━ (ESC) ".bold() + " (j,k|↓,↑) ━ (ENTER) ━ (ESC) " }; let block = Block::bordered() .title_top(title.bold()) - .title_bottom(bottom_info) + .title_bottom(bottom_info.bold()) .title_alignment(Alignment::Center) .style( Style::new() @@ -356,15 +374,29 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { // Now take the max number for the width of the popup // let max_item = list_widths.iter().max().unwrap().to_owned(); - let max_item = - list_widths.0.chars().count() as u16 + list_widths.1.chars().count() as u16; - + let max_item = list_widths.0.clone() + &list_widths.1; + // list_widths.0.chars().count() as u16 + list_widths.1.chars().count() as u16; + + let fitting_width: u16 = { + let lines = vec![title, bottom_info, &max_item]; + let lline = lines + .iter() + .max_by(|a, b| a.chars().count().cmp(&b.chars().count())) + .unwrap(); + // lines.first().unwrap().chars().count() as u16 + lline.chars().count() as u16 + }; // Check if the popup would exceed the terminal frame width - let popup_width = if max_item + 2 > frame.area().width - 2 { + let popup_width = if fitting_width + 2 > frame.area().width - 2 { frame.area().width - 2 } else { - max_item + 2 + fitting_width + 2 }; + // } else if title.chars().count() as u16 > max_item { + // (title.chars().count() + 2) as u16 + // } else { + // max_item + 2 + // }; let popup_heigth = list.len() + 2; let popup_area = popup_area(frame.area(), popup_width, popup_heigth as u16); -- cgit v1.2.3 From e27069540a0bb22640974d0bd1a1bdf153b1b40d Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sat, 5 Jul 2025 22:32:41 +0200 Subject: succesfully tested note creation --- src/bibiman.rs | 1 + tests/note-files/doody.md | 0 2 files changed, 1 insertion(+) create mode 100644 tests/note-files/doody.md (limited to 'src') diff --git a/src/bibiman.rs b/src/bibiman.rs index 71ac831..87963dc 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -774,6 +774,7 @@ impl Bibiman { File::create_new(new_file).unwrap(); self.close_popup(); + self.update_lists(cfg); Ok(()) } diff --git a/tests/note-files/doody.md b/tests/note-files/doody.md new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From e2b4e12cf1ce15a26172ac8f2166c5e02ca89351 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sat, 5 Jul 2025 22:55:46 +0200 Subject: quit creating note function if citekey contains special char --- src/app.rs | 24 ++++++++++++++++++++++-- src/bibiman.rs | 7 +++++-- 2 files changed, 27 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 01424bc..708ec37 100644 --- a/src/app.rs +++ b/src/app.rs @@ -427,7 +427,27 @@ impl App { } CmdAction::CreateNote => { if let CurrentArea::EntryArea = self.bibiman.current_area { - if cfg.general.note_path.is_some() + let citekey = self.bibiman.entry_table.entry_table_items[self + .bibiman + .entry_table + .entry_table_state + .selected() + .unwrap()] + .citekey + .clone(); + if citekey.contains("/") + | citekey.contains("|") + | citekey.contains("#") + | citekey.contains("\\") + | citekey.contains("*") + { + self.bibiman.open_popup( + PopupKind::MessageError, + Some("Selected entrys citekey contains special char: "), + Some(&citekey), + None, + )?; + } else if cfg.general.note_path.is_some() && cfg.general.note_extensions.is_some() && self.bibiman.entry_table.entry_table_items[self .bibiman @@ -454,7 +474,7 @@ impl App { )); } self.bibiman - .open_popup(PopupKind::CreateNote, None, None, Some(items)); + .open_popup(PopupKind::CreateNote, None, None, Some(items))?; } else if cfg.general.note_path.is_some() && self.bibiman.entry_table.entry_table_items[self .bibiman diff --git a/src/bibiman.rs b/src/bibiman.rs index 87963dc..6d21f8c 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -755,13 +755,15 @@ impl Bibiman { pub fn create_note(&mut self, cfg: &BibiConfig) -> Result<()> { // Index of selected entry let entry_idx = self.entry_table.entry_table_state.selected().unwrap(); - let citekey = self.entry_table.entry_table_items[entry_idx].citekey(); + let citekey = self.entry_table.entry_table_items[entry_idx] + .citekey + .clone(); // Index of selected popup field let popup_idx = self.popup_area.popup_state.selected().unwrap(); let ext = self.popup_area.popup_list[popup_idx].1.clone(); - let basename = PathBuf::from(citekey).with_extension(ext); + let basename = PathBuf::from(&citekey).with_extension(ext); let path = cfg.general.note_path.as_ref().unwrap(); let new_file = path.join(basename); @@ -775,6 +777,7 @@ impl Bibiman { File::create_new(new_file).unwrap(); self.close_popup(); self.update_lists(cfg); + self.select_entry_by_citekey(&citekey); Ok(()) } -- cgit v1.2.3 From 785c832d6a797103cf872b5ea6562e8dc59f24be Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 6 Jul 2025 12:36:53 +0200 Subject: add forbidden chars to README --- README.md | 6 ++++++ src/app.rs | 5 +++++ 2 files changed, 11 insertions(+) (limited to 'src') diff --git a/README.md b/README.md index 947c1d6..c00d288 100644 --- a/README.md +++ b/README.md @@ -440,6 +440,12 @@ directory is set to the value of the `note_path` variable. The extension can be choosen from one of the file format extension set in the `note_extensions` array. +**Be aware**: The operation of creating new notes is not permitted if the +citekey contains some special chars which could cause problems with Unixish +shell commands and file operations. Currently the following chars are not +allowed as part of the citekey: `/` | `|` | `#` | `*` | `\\` | `"` | `'` | `;` | +`!` + The bibfile itself will *not be edited*. Therefore, you can't break anything in your bibfile with this operation! diff --git a/src/app.rs b/src/app.rs index 708ec37..8b76f17 100644 --- a/src/app.rs +++ b/src/app.rs @@ -435,11 +435,16 @@ impl App { .unwrap()] .citekey .clone(); + // disallow chars which can cause other shell executions if citekey.contains("/") | citekey.contains("|") | citekey.contains("#") | citekey.contains("\\") | citekey.contains("*") + | citekey.contains("\"") + | citekey.contains(";") + | citekey.contains("!") + | citekey.contains("\'") { self.bibiman.open_popup( PopupKind::MessageError, -- cgit v1.2.3 From b6b74ef40480d86d81bf69467aecb4931750d1de Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 6 Jul 2025 13:35:07 +0200 Subject: colored version function --- Cargo.lock | 1 + Cargo.toml | 1 + src/cliargs.rs | 49 +++++++++++++++++++++++++++++++++++-------------- 3 files changed, 37 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/Cargo.lock b/Cargo.lock index 5ecdfd4..696213a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,6 +104,7 @@ dependencies = [ "itertools", "lexopt", "nucleo-matcher", + "owo-colors", "rand", "ratatui", "regex", diff --git a/Cargo.toml b/Cargo.toml index 18db28d..899ac61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,3 +38,4 @@ ureq = "2.12.1" # config = { version = "0.15.8", default-features = false, features = ["async", "async-trait", "convert-case", "convert_case", "toml"] } serde = { version = "1.0.217", features = ["serde_derive"] } figment = { version = "0.10.19", features = [ "toml", "test" ]} +owo-colors = "4.2.2" diff --git a/src/cliargs.rs b/src/cliargs.rs index 114b15a..46c3012 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -16,9 +16,11 @@ ///// use color_eyre::eyre::Result; -use color_eyre::owo_colors::OwoColorize; use dirs::{config_dir, home_dir}; use lexopt::prelude::*; +use owo_colors::colors::css::LightGreen; +use owo_colors::colors::*; +use owo_colors::OwoColorize; use std::env; use std::path::PathBuf; use walkdir::WalkDir; @@ -142,19 +144,38 @@ FLAGS: help } -pub fn version_func() -> String { - let version = format!( - "\ -{} {} -{} -{} +// pub fn version_func() -> String { +// let version = format!( +// "\ +// {} {} +// {} +// {} -Target Triple: {}", - env!("CARGO_PKG_NAME"), - env!("CARGO_PKG_VERSION"), - env!("CARGO_PKG_AUTHORS"), - env!("CARGO_PKG_LICENSE"), - env!("TARGET") - ); +// Target Triple: {}", +// env!("CARGO_PKG_NAME"), +// env!("CARGO_PKG_VERSION"), +// env!("CARGO_PKG_AUTHORS"), +// env!("CARGO_PKG_LICENSE"), +// env!("TARGET") +// ); +// version +// } +pub fn version_func() -> String { + let version: Vec = vec![ + format!( + "{} {}", + env!("CARGO_PKG_NAME").fg::().bold(), + env!("CARGO_PKG_VERSION").fg::() + ), + format!("{}", env!("CARGO_PKG_AUTHORS").bold()), + format!("{}", env!("CARGO_PKG_LICENSE")), + format!("\n"), + format!( + "{} {}", + "Target Triple:".bold(), + env!("TARGET").fg::() + ), + ]; + let version = version.join("\n"); version } -- cgit v1.2.3 From c0970da999e222cadbcc2242fb67686ed5b00ca4 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 6 Jul 2025 14:41:36 +0200 Subject: colored help and version --- src/cliargs.rs | 100 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/cliargs.rs b/src/cliargs.rs index 46c3012..082ecda 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -114,52 +114,66 @@ pub fn parse_files(args: Vec) -> Vec { } pub fn help_func() -> String { - let help = format!( - "\ -{} {} - -USAGE: - bibiman [FLAGS] [files/dirs] - -POSITIONAL ARGS: - Path to .bib file - Path to directory containing .bib files - - Both can be passed multiple times - -FLAGS: - -h, --help Show this help and exit - -v, --version Show the version and exit - -c, --config-file= Path to config file used for current session. - Takes precedence over standard config file. - --light-terminal Enable color mode for light terminal background - --pdf-path= Use PDF files named by citekey at the given path and its - subdirs as value for the `file` field of the entry matching - the citekey for the current session. - Does not overwrite or change the original file. - (might not work with citekeys containing special chars)", - env!("CARGO_PKG_NAME"), - env!("CARGO_PKG_VERSION"), - ); + let help = vec![ + format!( + "{} {}\n", + env!("CARGO_PKG_NAME").fg::().bold(), + env!("CARGO_PKG_VERSION").fg::(), + ), + format!( + "{}:\n\t{} [Flags] [files/dirs]\n", + "USAGE".bold(), + "bibiman".bold() + ), + format!( + "{}:\n\t{}\t\tPath to {} file", + "POSITIONAL ARGUMENTS".bold(), + "".fg::().bold(), + ".bib".fg::().bold() + ), + format!( + "\t{}\tPath to directory containing {} files", + "".fg::().bold(), + ".bib".fg::().bold() + ), + format!("\n\t{}", "Both can be passed multiple times".italic()), + format!("\n{}:", "FLAGS".bold()), + format!("\t{}", "-h, --help".bold().fg::()), + format!("\t\t{}", "Show this help and exit"), + format!("\t{}", "-v, --version".bold().fg::()), + format!("\t\t{}", "Show the version and exit"), + format!("\t{}", "--light-terminal".bold().fg::()), + format!( + "\t\t{}", + "Enable default colors for light terminal background" + ), + format!( + "\t{}{}", + "-c, --config-file=".bold().fg::(), + "".bold().italic().fg::() + ), + format!("\t\t{}", "Path to config file used for current session."), + format!("\t\t{}", "Takes precedence over standard config file."), + format!( + "\t{}{}", + "--pdf-path=".bold().fg::(), + "".bold().italic().fg::() + ), + format!("\t\t{}", "Path to directory containing PDF files."), + format!( + "\t\t{}", + "If the pdf files basename matches an entrys citekey," + ), + format!( + "\t\t{}", + "its attached as connected PDF file for the current session." + ), + format!("\t\t{}", "Does not edit the bibfile itself!"), + ]; + let help = help.join("\n"); help } -// pub fn version_func() -> String { -// let version = format!( -// "\ -// {} {} -// {} -// {} - -// Target Triple: {}", -// env!("CARGO_PKG_NAME"), -// env!("CARGO_PKG_VERSION"), -// env!("CARGO_PKG_AUTHORS"), -// env!("CARGO_PKG_LICENSE"), -// env!("TARGET") -// ); -// version -// } pub fn version_func() -> String { let version: Vec = vec![ format!( -- cgit v1.2.3