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/tui/ui.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src/tui/ui.rs') 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/tui/ui.rs') 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/tui/ui.rs') 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/tui/ui.rs') 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/tui/ui.rs') 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/tui/ui.rs') 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 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/tui/ui.rs') 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/tui/ui.rs') 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 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/tui/ui.rs') 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