From 96a5cc63cce9a2a8f505538268a42fef0891f0b5 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sat, 28 Sep 2024 23:11:15 +0200 Subject: start impl of search mode --- src/frontend/app.rs | 41 ++++++++++++++++++++++++++++++++++++++--- src/frontend/handler.rs | 42 +++++++++++++++++++++++++++++++++++++++++- src/frontend/ui.rs | 37 +++++++++++++++++++++++++++---------- 3 files changed, 106 insertions(+), 14 deletions(-) (limited to 'src/frontend') diff --git a/src/frontend/app.rs b/src/frontend/app.rs index 1a8eab7..75a6ede 100644 --- a/src/frontend/app.rs +++ b/src/frontend/app.rs @@ -30,7 +30,15 @@ pub type AppResult = std::result::Result>; pub enum CurrentArea { EntryArea, TagArea, - // SearchArea, + SearchArea, + HelpArea, +} + +// Check which area was active when popup set active +#[derive(Debug)] +pub enum FormerArea { + EntryArea, + TagArea, } // Application. @@ -50,6 +58,10 @@ pub struct App { pub scroll_info: u16, // area pub current_area: CurrentArea, + // mode for popup window + pub former_area: Option, + // search string + pub search_string: String, } // Define the fundamental List @@ -176,6 +188,8 @@ impl Default for App { entry_table, scroll_info: 0, current_area, + former_area: None, + search_string: String::new(), } } } @@ -199,18 +213,34 @@ impl App { match self.current_area { CurrentArea::EntryArea => self.current_area = CurrentArea::TagArea, CurrentArea::TagArea => self.current_area = CurrentArea::EntryArea, + CurrentArea::SearchArea => { + if let Some(former_area) = &self.former_area { + match former_area { + FormerArea::EntryArea => self.current_area = CurrentArea::EntryArea, + FormerArea::TagArea => self.current_area = CurrentArea::TagArea, + } + } + } + CurrentArea::HelpArea => { + if let Some(former_area) = &self.former_area { + match former_area { + FormerArea::EntryArea => self.current_area = CurrentArea::EntryArea, + FormerArea::TagArea => self.current_area = CurrentArea::TagArea, + } + } + } } } pub fn scroll_info_down(&mut self) { - self.scroll_info = (self.scroll_info + 1); + self.scroll_info = self.scroll_info + 1; } pub fn scroll_info_up(&mut self) { if self.scroll_info == 0 { {} } else { - self.scroll_info = (self.scroll_info - 1); + self.scroll_info = self.scroll_info - 1; } } @@ -218,6 +248,7 @@ impl App { match self.current_area { CurrentArea::EntryArea => self.entry_table.entry_table_state.select(None), CurrentArea::TagArea => self.tag_list.tag_list_state.select(None), + _ => {} } // self.tag_list.tag_list_state.select(None); } @@ -227,6 +258,7 @@ impl App { match self.current_area { CurrentArea::EntryArea => self.entry_table.entry_table_state.select_next(), CurrentArea::TagArea => self.tag_list.tag_list_state.select_next(), + _ => {} } // self.tag_list.tag_list_state.select_next(); } @@ -235,6 +267,7 @@ impl App { match self.current_area { CurrentArea::EntryArea => self.entry_table.entry_table_state.select_previous(), CurrentArea::TagArea => self.tag_list.tag_list_state.select_previous(), + _ => {} } // self.tag_list.tag_list_state.select_previous(); } @@ -244,6 +277,7 @@ impl App { match self.current_area { CurrentArea::EntryArea => self.entry_table.entry_table_state.select_first(), CurrentArea::TagArea => self.tag_list.tag_list_state.select_first(), + _ => {} } // self.tag_list.tag_list_state.select_first(); } @@ -253,6 +287,7 @@ impl App { match self.current_area { CurrentArea::EntryArea => self.entry_table.entry_table_state.select_last(), CurrentArea::TagArea => self.tag_list.tag_list_state.select_last(), + _ => {} } // self.tag_list.tag_list_state.select_last(); } diff --git a/src/frontend/handler.rs b/src/frontend/handler.rs index 73f30ae..27aa8de 100644 --- a/src/frontend/handler.rs +++ b/src/frontend/handler.rs @@ -18,7 +18,7 @@ use crate::frontend::app::{App, AppResult}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; -use super::app::CurrentArea; +use super::app::{CurrentArea, FormerArea}; /// Handles the key events and updates the state of [`App`]. pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { @@ -61,6 +61,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { KeyCode::Char('G') | KeyCode::End => { app.select_last(); } + KeyCode::Char('/') => { + app.former_area = Some(FormerArea::TagArea); + app.current_area = CurrentArea::SearchArea; + } KeyCode::Tab | KeyCode::BackTab => { app.toggle_area(); } @@ -86,11 +90,47 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { KeyCode::Char('y') => { App::yank_text(&app.get_selected_citekey()); } + KeyCode::Char('/') => { + app.former_area = Some(FormerArea::EntryArea); + app.current_area = CurrentArea::SearchArea; + } KeyCode::Tab | KeyCode::BackTab => { app.toggle_area(); } _ => {} }, + // Keycodes for the search area (popup) + CurrentArea::SearchArea => match key_event.code { + KeyCode::Esc => { + app.toggle_area(); + app.former_area = None; + app.search_string.clear(); + } + KeyCode::Enter => { + // TODO: run function for filtering the list + app.toggle_area(); + app.former_area = None; + app.search_string.clear(); + } + KeyCode::Backspace => { + app.search_string.pop(); + } + KeyCode::Char(search_pattern) => { + app.search_string.push(search_pattern); + } + _ => {} + }, + // Keycodes for the help area (popup) + CurrentArea::HelpArea => match key_event.code { + KeyCode::Char('q') => { + app.quit(); + } + KeyCode::Esc => { + app.toggle_area(); + app.former_area = None; + } + _ => {} + }, } Ok(()) } diff --git a/src/frontend/ui.rs b/src/frontend/ui.rs index 5413194..0e44e5c 100644 --- a/src/frontend/ui.rs +++ b/src/frontend/ui.rs @@ -15,7 +15,6 @@ // along with this program. If not, see . ///// -use biblatex::ChunksExt; use ratatui::{ buffer::Buffer, layout::{Constraint, Layout, Rect}, @@ -26,8 +25,8 @@ use ratatui::{ symbols, text::{Line, Span, Text}, widgets::{ - Block, Cell, HighlightSpacing, List, ListItem, Padding, Paragraph, Row, StatefulWidget, - Table, TableState, Widget, Wrap, + Block, Borders, Cell, HighlightSpacing, List, ListItem, Padding, Paragraph, Row, + StatefulWidget, Table, Widget, Wrap, }, }; @@ -36,7 +35,7 @@ use crate::{ frontend::app::{App, TagListItem}, }; -use super::app::EntryTableItem; +use super::app::CurrentArea; const MAIN_BLUE_COLOR: Color = Color::Indexed(39); const MAIN_PURPLE_COLOR: Color = Color::Indexed(129); @@ -75,7 +74,7 @@ impl Widget for &mut App { let [header_area, main_area, footer_area] = Layout::vertical([ Constraint::Length(1), Constraint::Fill(1), - Constraint::Length(1), + Constraint::Length(3), ]) .areas(area); @@ -87,7 +86,7 @@ impl Widget for &mut App { // Render header and footer App::render_header(header_area, buf); - App::render_footer(footer_area, buf); + self.render_footer(footer_area, buf); // Render list area where entry gets selected // self.render_entry_table(list_area, buf); self.render_entrytable(list_area, buf); @@ -107,10 +106,28 @@ impl App { .render(area, buf); } - pub fn render_footer(area: Rect, buf: &mut Buffer) { - Paragraph::new("Use j/k to move, h to unselect, g/G to go top/bottom.") - .centered() - .render(area, buf); + pub fn render_footer(&mut self, area: Rect, buf: &mut Buffer) { + match &self.current_area { + CurrentArea::SearchArea => { + let block = Block::bordered() + .title(" Search Entries ") + .border_set(symbols::border::ROUNDED); + Paragraph::new(self.search_string.clone()) + .block(block) + .render(area, buf); + } + _ => { + let block = Block::bordered() + .title(Line::raw(" Basic Commands ").centered()) + .border_set(symbols::border::ROUNDED); + Paragraph::new( + "Use j/k to move, g/G to go top/bottom, y to yank the current citekey", + ) + .block(block) + .centered() + .render(area, buf); + } + } } pub fn render_entrytable(&mut self, area: Rect, buf: &mut Buffer) { -- cgit v1.2.3