From a6fca1fcf164142d84d09242b9d95a1da0b2d2d9 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 15 Dec 2024 13:21:42 +0100 Subject: use input struct, place cursor at pos --- src/app.rs | 99 ++++++++++++++++++++++++++++--------------------- src/bibiman.rs | 4 +- src/tui/popup.rs | 4 +- src/tui/ui.rs | 41 ++++++++++---------- tests/biblatex-test.bib | 13 +++++++ 5 files changed, 95 insertions(+), 66 deletions(-) diff --git a/src/app.rs b/src/app.rs index 2834e61..bcdef39 100644 --- a/src/app.rs +++ b/src/app.rs @@ -79,47 +79,49 @@ impl App { } else if let Some(PopupKind::MessageError) = self.bibiman.popup_area.popup_kind { self.bibiman.close_popup() - } else if let Some(PopupKind::AddEntry) = self.bibiman.popup_area.popup_kind { - // Handle key events for AddEntry popup - match key_event.code { - KeyCode::Char(c) => { - let index = self.bibiman.popup_area.add_entry_cursor_position; - self.bibiman.popup_area.add_entry_input.insert(index, c); - self.bibiman.popup_area.add_entry_cursor_position += 1; - } - KeyCode::Backspace => { - if self.bibiman.popup_area.add_entry_cursor_position > 0 { - self.bibiman.popup_area.add_entry_cursor_position -= 1; - let index = self.bibiman.popup_area.add_entry_cursor_position; - self.bibiman.popup_area.add_entry_input.remove(index); - } - } - KeyCode::Left => { - if self.bibiman.popup_area.add_entry_cursor_position > 0 { - self.bibiman.popup_area.add_entry_cursor_position -= 1; - } - } - KeyCode::Right => { - if self.bibiman.popup_area.add_entry_cursor_position - < self.bibiman.popup_area.add_entry_input.len() - { - self.bibiman.popup_area.add_entry_cursor_position += 1; - } - } - KeyCode::Enter => { - // Handle submission of the new entry - self.bibiman.handle_new_entry_submission(args); - self.bibiman.close_popup(); - self.input_mode = false; - } - KeyCode::Esc => { - // Close the popup without saving - self.bibiman.close_popup(); - self.input_mode = false; - } - _ => {} - } - } else { + } + // else if let Some(PopupKind::AddEntry) = self.bibiman.popup_area.popup_kind { + // // Handle key events for AddEntry popup + // match key_event.code { + // KeyCode::Char(c) => { + // let index = self.bibiman.popup_area.add_entry_cursor_position; + // self.bibiman.popup_area.add_entry_input.insert(index, c); + // self.bibiman.popup_area.add_entry_cursor_position += 1; + // } + // KeyCode::Backspace => { + // if self.bibiman.popup_area.add_entry_cursor_position > 0 { + // self.bibiman.popup_area.add_entry_cursor_position -= 1; + // let index = self.bibiman.popup_area.add_entry_cursor_position; + // self.bibiman.popup_area.add_entry_input.remove(index); + // } + // } + // KeyCode::Left => { + // if self.bibiman.popup_area.add_entry_cursor_position > 0 { + // self.bibiman.popup_area.add_entry_cursor_position -= 1; + // } + // } + // KeyCode::Right => { + // if self.bibiman.popup_area.add_entry_cursor_position + // < self.bibiman.popup_area.add_entry_input.len() + // { + // self.bibiman.popup_area.add_entry_cursor_position += 1; + // } + // } + // KeyCode::Enter => { + // // Handle submission of the new entry + // self.bibiman.handle_new_entry_submission(args, &self.input); + // self.bibiman.close_popup(); + // self.input_mode = false; + // } + // KeyCode::Esc => { + // // Close the popup without saving + // self.bibiman.close_popup(); + // self.input_mode = false; + // } + // _ => {} + // } + // } + else { let command = if self.input_mode { CmdAction::Input(InputCmdAction::parse(key_event, &self.input)) } else { @@ -183,18 +185,28 @@ impl App { // self.bibiman.enter_search_area(); } InputCmdAction::Confirm => { - self.input = Input::default(); - self.input_mode = false; // Logic for TABS to be added if let CurrentArea::SearchArea = self.bibiman.current_area { self.bibiman.confirm_search(); + } else if let CurrentArea::PopupArea = self.bibiman.current_area { + match self.bibiman.popup_area.popup_kind { + Some(PopupKind::AddEntry) => { + self.bibiman.handle_new_entry_submission(args, &self.input); + self.bibiman.close_popup(); + } + _ => {} + } } + self.input = Input::default(); + self.input_mode = false; } InputCmdAction::Exit => { self.input = Input::default(); self.input_mode = false; if let CurrentArea::SearchArea = self.bibiman.current_area { self.bibiman.break_search(); + } else if let CurrentArea::PopupArea = self.bibiman.current_area { + self.bibiman.close_popup(); } } }, @@ -380,6 +392,7 @@ impl App { } } CmdAction::AddEntry => { + self.input_mode = true; self.bibiman.add_entry(); } CmdAction::ShowHelp => { diff --git a/src/bibiman.rs b/src/bibiman.rs index 10dab1e..4e6e5e8 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -123,8 +123,8 @@ impl Bibiman { self.popup_area.popup_kind = Some(PopupKind::AddEntry); } - pub fn handle_new_entry_submission(&mut self, args: &CLIArgs) { - let new_entry_title = self.popup_area.add_entry_input.trim(); + pub fn handle_new_entry_submission(&mut self, args: &CLIArgs, doi_string: &Input) { + let new_entry_title = doi_string.value(); let doi2bib = doi2bib::Doi2Bib::new().unwrap(); let new_entry_future = doi2bib.resolve_doi(new_entry_title); let new_entry = block_on(new_entry_future); diff --git a/src/tui/popup.rs b/src/tui/popup.rs index 9b91801..afe0cfc 100644 --- a/src/tui/popup.rs +++ b/src/tui/popup.rs @@ -40,8 +40,8 @@ pub struct PopupArea { pub popup_scroll_pos: u16, pub popup_list: Vec, pub popup_state: ListState, - pub add_entry_input: String, - pub add_entry_cursor_position: usize, + // pub add_entry_input: String, + // pub add_entry_cursor_position: usize, } impl PopupArea { diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 8d7498a..e3fddfd 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -198,37 +198,39 @@ pub fn render_popup(app: &mut App, args: &CLIArgs, frame: &mut Frame) { .border_style(Style::new().fg(Color::Indexed(args.colors.entry_color))); // Prepare the input fields - let content = vec![ - Line::from(vec![Span::styled( + let content = vec![Line::from(vec![ + Span::styled( "DOI: ", Style::new().fg(Color::Indexed(args.colors.entry_color)), - )]), - Line::from(app.bibiman.popup_area.add_entry_input.clone()), - ]; + ), + Span::raw(app.input.value().to_string().clone()), + ])]; let paragraph = Paragraph::new(content) .block(block.clone()) .style(Style::new().fg(Color::Indexed(args.colors.main_text_color))) .wrap(Wrap { trim: false }); + let doi_lines = paragraph.line_count(area.width / 2); // Calculate popup size let popup_width = area.width / 2; - let popup_height = 5; // Adjust as needed + let popup_height = doi_lines as u16; // Adjust as needed let popup_area = popup_area(area, popup_width, popup_height); // Render the popup frame.render_widget(Clear, popup_area); + render_cursor(app, frame, popup_area, 6, 2); frame.render_widget(paragraph, popup_area); - // Set the cursor position - if app.input_mode { - // Calculate cursor x and y - let input_prefix_len = "Title: ".len() as u16 + 1; // +1 for padding - let cursor_x = popup_area.x - + input_prefix_len - + app.bibiman.popup_area.add_entry_cursor_position as u16; - let cursor_y = popup_area.y + 1; // Line after 'Title: ' - frame.set_cursor_position(Position::new(cursor_x, cursor_y)); - } + // // Set the cursor position + // if app.input_mode { + // // Calculate cursor x and y + // let input_prefix_len = "Title: ".len() as u16 + 1; // +1 for padding + // let cursor_x = popup_area.x + // + input_prefix_len + // + app.bibiman.popup_area.add_entry_cursor_position as u16; + // let cursor_y = popup_area.y + 1; // Line after 'Title: ' + // frame.set_cursor_position(Position::new(cursor_x, cursor_y)); + // } } Some(PopupKind::MessageConfirm) => { let area = frame.area(); @@ -376,7 +378,7 @@ pub fn render_footer(app: &mut App, args: &CLIArgs, frame: &mut Frame, rect: Rec ])) .block(block); - render_cursor(app, frame, rect, title_lenght + 1); + render_cursor(app, frame, rect, title_lenght + 1, 1); frame.render_widget(search_string, rect); } @@ -1035,12 +1037,13 @@ pub fn render_taglist(app: &mut App, args: &CLIArgs, frame: &mut Frame, rect: Re } /// Render the cursor when in InputMode -fn render_cursor(app: &mut App, frame: &mut Frame, area: Rect, x_offset: u16) { +fn render_cursor(app: &mut App, frame: &mut Frame, area: Rect, x_offset: u16, y_offset: u16) { let scroll = app.input.visual_scroll(area.width as usize); if app.input_mode { let (x, y) = ( area.x + ((app.input.visual_cursor()).max(scroll) - scroll) as u16 + x_offset, - area.bottom().saturating_sub(1), + // area.bottom().saturating_sub(1) + y_offset, + area.bottom().saturating_sub(y_offset), ); frame.render_widget( Clear, diff --git a/tests/biblatex-test.bib b/tests/biblatex-test.bib index b366fc3..d64b980 100644 --- a/tests/biblatex-test.bib +++ b/tests/biblatex-test.bib @@ -376,3 +376,16 @@ date = {2006}, indextitle = {Palladium pincer complexes}, } + +@article{ + 034780862i9pet, + doi = {10.34780/862I-9PET}, + url = {https://publications.dainst.org/journals/aa/article/view/4444}, + author = {Kammerer-Grothaus, Helke}, + keywords = {Topographie, Grabbauten, Nachleben, Tomba Baccelli}, + language = {de}, + title = {Die ›Tomba Baccelli‹ an der Via Latina in Rom}, + publisher = {Archäologischer Anzeiger}, + year = {2024}, + file = {} +} \ No newline at end of file -- cgit v1.2.3