diff options
| author | lukeflo | 2025-07-05 22:29:18 +0200 |
|---|---|---|
| committer | lukeflo | 2025-07-05 22:29:18 +0200 |
| commit | 2990df627ff54f01bafdcab767c0a73198e9e6cc (patch) | |
| tree | 8bdb05d9d4502bff9c3240958b8915df6026acda | |
| parent | 80c04702012cb1a43711d559e8ffbe9e250b1a57 (diff) | |
| download | bibiman-2990df627ff54f01bafdcab767c0a73198e9e6cc.tar.gz bibiman-2990df627ff54f01bafdcab767c0a73198e9e6cc.zip | |
create note function impl
| -rw-r--r-- | src/app.rs | 10 | ||||
| -rw-r--r-- | src/bibiman.rs | 33 | ||||
| -rw-r--r-- | src/tui/commands.rs | 2 | ||||
| -rw-r--r-- | src/tui/ui.rs | 104 |
4 files changed, 111 insertions, 38 deletions
@@ -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<KeyEvent> 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<ListItem> = 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<ListItem> = 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); |
