diff options
| author | lukeflo | 2024-10-24 14:58:50 +0200 |
|---|---|---|
| committer | lukeflo | 2024-10-24 14:58:50 +0200 |
| commit | c01722b7b517ea5fbee942276ca9f6442cae068b (patch) | |
| tree | 6b9ae39c4fae09ba7a5feb5f21a900ae84701fa9 /src | |
| parent | f32b6a19851b8b103ac843503ab008197f0639cd (diff) | |
| download | bibiman-c01722b7b517ea5fbee942276ca9f6442cae068b.tar.gz bibiman-c01722b7b517ea5fbee942276ca9f6442cae068b.zip | |
implementig tui_input for searching
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.rs | 160 | ||||
| -rw-r--r-- | src/bibiman.rs | 15 | ||||
| -rw-r--r-- | src/tui/command.rs | 13 | ||||
| -rw-r--r-- | src/tui/commandnew.rs | 77 |
4 files changed, 225 insertions, 40 deletions
@@ -15,13 +15,16 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. ///// +use crate::bibiman::CurrentArea; // use super::Event; -use crate::bibiman::Bibiman; use crate::cliargs::CLIArgs; -use crate::tui; -use crate::tui::handler::handle_key_events; +use crate::tui::commandnew::{InputCmdAction, OpenRessource}; +use crate::tui::{self, Tui}; +use crate::{bibiman::Bibiman, tui::commandnew::CmdAction}; use color_eyre::eyre::{Ok, Result}; use tui::Event; +use tui_input::backend::crossterm::EventHandler; +use tui_input::Input; // Application. #[derive(Debug)] @@ -30,6 +33,10 @@ pub struct App { pub running: bool, // bibimain pub bibiman: Bibiman, + // Input mode + pub input: Input, + // Input mode bool + pub input_mode: bool, } impl App { @@ -37,8 +44,14 @@ impl App { pub fn new(args: CLIArgs) -> Result<Self> { // Self::default() let running = true; + let input = Input::default(); let bibiman = Bibiman::new(args)?; - Ok(Self { running, bibiman }) + Ok(Self { + running, + bibiman, + input, + input_mode: false, + }) } pub async fn run(&mut self) -> Result<()> { @@ -52,8 +65,20 @@ impl App { // Handle events. match tui.next().await? { Event::Tick => self.tick(), - Event::Key(key_event) => handle_key_events(key_event, self, &mut tui)?, - Event::Mouse(_) => {} + // Event::Key(key_event) => handle_key_events(key_event, self, &mut tui)?, + // Event::Mouse(_) => {} + Event::Key(key_event) => { + let command = if self.input_mode { + CmdAction::Input(InputCmdAction::parse(key_event, &self.input)) + } else { + CmdAction::from(key_event) + }; + self.run_command(command, &mut tui)? + } + Event::Mouse(mouse_event) => { + self.run_command(CmdAction::from(mouse_event), &mut tui)? + } + Event::Resize(_, _) => {} } } @@ -72,4 +97,127 @@ impl App { pub fn quit(&mut self) { self.running = false; } + + pub fn run_command(&mut self, cmd: CmdAction, tui: &mut Tui) -> Result<()> { + match cmd { + CmdAction::Input(cmd) => match cmd { + InputCmdAction::Handle(event) => { + self.input.handle_event(&event); + self.bibiman.search_list_by_pattern(&self.input); + } + InputCmdAction::Enter => { + self.input_mode = true; + // Logic for TABS to be added + self.bibiman.enter_search_area(); + } + InputCmdAction::Confirm => { + self.input_mode = false; + // Logic for TABS to be added + self.bibiman.confirm_search(); + } + InputCmdAction::Exit => { + self.input = Input::default(); + self.input_mode = false; + self.bibiman.break_search(); + } + }, + CmdAction::SelectNextRow(amount) => match self.bibiman.current_area { + // Here add logic to select TAB + CurrentArea::EntryArea => { + self.bibiman.select_next_entry(amount); + } + CurrentArea::TagArea => { + self.bibiman.select_next_tag(amount); + } + _ => {} + }, + CmdAction::SelectPrevRow(amount) => match self.bibiman.current_area { + // Here add logic to select TAB + CurrentArea::EntryArea => { + self.bibiman.select_previous_entry(amount); + } + CurrentArea::TagArea => { + self.bibiman.select_next_tag(amount); + } + _ => {} + }, + CmdAction::SelectNextCol => { + if let CurrentArea::EntryArea = self.bibiman.current_area { + self.bibiman.select_next_column(); + } + } + CmdAction::SelectPrevCol => { + if let CurrentArea::EntryArea = self.bibiman.current_area { + self.bibiman.select_prev_column(); + } + } + CmdAction::ScrollInfoDown => { + self.bibiman.scroll_info_down(); + } + CmdAction::ScrollInfoUp => { + self.bibiman.scroll_info_up(); + } + CmdAction::Bottom => match self.bibiman.current_area { + CurrentArea::EntryArea => { + self.bibiman.select_last_entry(); + } + CurrentArea::TagArea => { + self.bibiman.select_last_tag(); + } + _ => {} + }, + CmdAction::Top => match self.bibiman.current_area { + CurrentArea::EntryArea => { + self.bibiman.select_first_entry(); + } + CurrentArea::TagArea => { + self.bibiman.select_first_tag(); + } + _ => {} + }, + CmdAction::ToggleArea => { + self.bibiman.toggle_area(); + } + CmdAction::SearchList => {} + CmdAction::ResetList => { + self.bibiman.reset_current_list(); + } + CmdAction::Confirm => { + if let CurrentArea::TagArea = self.bibiman.current_area { + self.bibiman.filter_for_tags(); + } + } + CmdAction::SortList => { + if let CurrentArea::EntryArea = self.bibiman.current_area { + self.bibiman.entry_table.sort_entry_table(true); + } + } + CmdAction::YankItem => { + Bibiman::yank_text(&self.bibiman.get_selected_citekey()); + } + CmdAction::EditFile => { + if let CurrentArea::EntryArea = self.bibiman.current_area { + self.bibiman.run_editor(tui)?; + } + } + CmdAction::Open(ressource) => match ressource { + OpenRessource::Pdf => { + if let CurrentArea::EntryArea = self.bibiman.current_area { + self.bibiman.open_connected_file()?; + } + } + OpenRessource::WebLink => { + if let CurrentArea::EntryArea = self.bibiman.current_area { + self.bibiman.open_doi_url()?; + } + } + OpenRessource::Note => {} + }, + CmdAction::Exit => { + self.quit(); + } + CmdAction::Nothing => {} + } + Ok(()) + } } diff --git a/src/bibiman.rs b/src/bibiman.rs index 3bb731b..988bac8 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -15,12 +15,15 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. ///// +use crate::app::App; use crate::bibiman::{bibisetup::*, search::BibiSearch}; use crate::cliargs::CLIArgs; +use crate::tui::commandnew::CmdAction; use crate::{bibiman::entries::EntryTable, bibiman::keywords::TagList}; use arboard::Clipboard; use color_eyre::eyre::{Ok, Result}; use std::path::PathBuf; +use tui_input::Input; pub mod bibisetup; pub mod entries; @@ -94,7 +97,7 @@ impl Bibiman { self.entry_table = EntryTable::new(self.main_biblio.entry_list.clone()); } - // Toggle moveable list between entries and tags + /// Toggle moveable list between entries and tags pub fn toggle_area(&mut self) { if let CurrentArea::EntryArea = self.current_area { self.entry_table.entry_scroll_state = self.entry_table.entry_scroll_state.position(0); @@ -220,4 +223,14 @@ impl Bibiman { self.search_tags(); } } + + pub fn search_list_by_pattern(&mut self, searchpattern: &Input) { + self.search_struct.search_string = searchpattern.value().to_string(); + if let Some(FormerArea::EntryArea) = self.former_area { + self.search_entries(); + self.filter_tags_by_entries(); + } else if let Some(FormerArea::TagArea) = self.former_area { + self.search_tags(); + } + } } diff --git a/src/tui/command.rs b/src/tui/command.rs index 8416a3e..823a1dc 100644 --- a/src/tui/command.rs +++ b/src/tui/command.rs @@ -28,7 +28,10 @@ use std::process::{Command, Stdio}; impl Bibiman { // Entry Table commands - // Movement + /// Select next entry in Table holding the bibliographic entries. + /// + /// Takes u16 value as argument to specify number of entries which + /// should be scrolled pub fn select_next_entry(&mut self, entries: u16) { self.entry_table.entry_info_scroll = 0; self.entry_table.entry_info_scroll_state = @@ -40,6 +43,10 @@ impl Bibiman { .position(self.entry_table.entry_table_state.selected().unwrap()); } + /// Select previous entry in Table holding the bib entries. + /// + /// Takes u16 value as argument to specify number of entries which + /// should be scrolled pub fn select_previous_entry(&mut self, entries: u16) { self.entry_table.entry_info_scroll = 0; self.entry_table.entry_info_scroll_state = @@ -51,6 +58,7 @@ impl Bibiman { .position(self.entry_table.entry_table_state.selected().unwrap()); } + /// Select first entry in bib list pub fn select_first_entry(&mut self) { self.entry_table.entry_info_scroll = 0; self.entry_table.entry_info_scroll_state = @@ -59,6 +67,7 @@ impl Bibiman { self.entry_table.entry_scroll_state = self.entry_table.entry_scroll_state.position(0); } + /// Select last entry in bib list pub fn select_last_entry(&mut self) { self.entry_table.entry_info_scroll = 0; self.entry_table.entry_info_scroll_state = @@ -70,6 +79,7 @@ impl Bibiman { .position(self.entry_table.entry_table_items.len()); } + /// Select next (right) column of entry table pub fn select_next_column(&mut self) { match self.entry_table.entry_table_selected_column { EntryTableColumn::Authors => { @@ -87,6 +97,7 @@ impl Bibiman { } } + /// Select previous (left) column of entry table pub fn select_prev_column(&mut self) { match self.entry_table.entry_table_selected_column { EntryTableColumn::Authors => { diff --git a/src/tui/commandnew.rs b/src/tui/commandnew.rs index 45b2f52..1f70264 100644 --- a/src/tui/commandnew.rs +++ b/src/tui/commandnew.rs @@ -20,31 +20,31 @@ use ratatui::crossterm::event::{ }; use tui_input::Input; -// Possible scroll areas. -#[derive(Debug, PartialEq, Eq)] -pub enum ScrollType { - Rows, - Cols, - InfoArea, -} - // Possible ressources to open #[derive(Debug, PartialEq, Eq)] pub enum OpenRessource { - PDF, + Pdf, WebLink, Note, } /// Application command. #[derive(Debug, PartialEq, Eq)] -pub enum Command { +pub enum CmdAction { // Toggle area ToggleArea, - // Next - Next(ScrollType, usize), - // Previous. - Previous(ScrollType, usize), + // Scroll list/table down + SelectNextRow(u16), + // Scroll list/table up. + SelectPrevRow(u16), + // Select nex table col. + SelectNextCol, + // Select previous table col. + SelectPrevCol, + // Scroll info/preview area down + ScrollInfoDown, + // Scroll info/preview area up + ScrollInfoUp, // Go to top. Top, // Go to bottom. @@ -64,42 +64,55 @@ pub enum Command { // Open linked ressource Open(OpenRessource), // Input command. - Input(InputCommand), + Input(InputCmdAction), // Hexdump command. Exit, // Do nothing. Nothing, } -impl From<KeyEvent> for Command { +impl From<KeyEvent> for CmdAction { fn from(key_event: KeyEvent) -> Self { match key_event.code { // Go to first/last entry of selected list/table KeyCode::Char('g') | KeyCode::Home => Self::Top, KeyCode::Char('G') | KeyCode::End => Self::Bottom, // Scroll columns of EntryTable - KeyCode::Right | KeyCode::Char('l') => Self::Next(ScrollType::Cols, 1), - KeyCode::Left | KeyCode::Char('h') => Self::Previous(ScrollType::Cols, 1), + KeyCode::Right | KeyCode::Char('l') => Self::SelectNextCol, + KeyCode::Left | KeyCode::Char('h') => Self::SelectPrevCol, // Scroll table/list vertically by 1 - KeyCode::Down | KeyCode::Char('j') => Self::Next(ScrollType::Rows, 1), - KeyCode::Up | KeyCode::Char('k') => Self::Previous(ScrollType::Rows, 1), + KeyCode::Down | KeyCode::Char('j') => { + if key_event.modifiers == KeyModifiers::ALT { + Self::ScrollInfoDown + } else { + Self::SelectNextRow(1) + } + } + KeyCode::Up | KeyCode::Char('k') => { + if key_event.modifiers == KeyModifiers::ALT { + Self::ScrollInfoUp + } else { + Self::SelectPrevRow(1) + } + } // Scroll table/list vertically by 5 - KeyCode::PageDown => Self::Next(ScrollType::Rows, 5), - KeyCode::PageUp => Self::Previous(ScrollType::Rows, 5), KeyCode::Char('d') => { if key_event.modifiers == KeyModifiers::CONTROL { - Self::Next(ScrollType::Rows, 5) + Self::SelectNextRow(5) } else { Self::Nothing } } KeyCode::Char('u') => { if key_event.modifiers == KeyModifiers::CONTROL { - Self::Previous(ScrollType::Rows, 5) + Self::SelectPrevRow(5) } else { Self::Open(OpenRessource::WebLink) } } + // Scroll info/preview area + KeyCode::PageDown => Self::ScrollInfoDown, + KeyCode::PageUp => Self::ScrollInfoUp, // Exit App KeyCode::Char('q') => Self::Exit, KeyCode::Char('c') | KeyCode::Char('C') => { @@ -113,10 +126,10 @@ impl From<KeyEvent> for Command { KeyCode::Tab => Self::ToggleArea, KeyCode::BackTab => Self::ToggleArea, // Enter search mode - KeyCode::Char('/') => Self::Input(InputCommand::Enter), + KeyCode::Char('/') => Self::Input(InputCmdAction::Enter), KeyCode::Char('f') => { if key_event.modifiers == KeyModifiers::CONTROL { - Self::Input(InputCommand::Enter) + Self::Input(InputCmdAction::Enter) } else { Self::Nothing } @@ -127,7 +140,7 @@ impl From<KeyEvent> for Command { // Reset lists/tables KeyCode::Esc => Self::ResetList, // Open linked ressource - KeyCode::Char('o') => Self::Open(OpenRessource::PDF), + KeyCode::Char('o') => Self::Open(OpenRessource::Pdf), // KeyCode::Char('u') => Self::Open(OpenRessource::WebLink), // Edit currently selected entry KeyCode::Char('e') => Self::EditFile, @@ -139,11 +152,11 @@ impl From<KeyEvent> for Command { } } -impl From<MouseEvent> for Command { +impl From<MouseEvent> for CmdAction { fn from(mouse_event: MouseEvent) -> Self { match mouse_event.kind { - MouseEventKind::ScrollDown => Self::Next(ScrollType::Rows, 1), - MouseEventKind::ScrollUp => Self::Previous(ScrollType::Rows, 1), + MouseEventKind::ScrollDown => Self::SelectNextRow(1), + MouseEventKind::ScrollUp => Self::SelectPrevRow(1), _ => Self::Nothing, } } @@ -151,7 +164,7 @@ impl From<MouseEvent> for Command { /// Input mode command. #[derive(Debug, PartialEq, Eq)] -pub enum InputCommand { +pub enum InputCmdAction { // Handle input. Handle(Event), // Enter input mode. @@ -162,7 +175,7 @@ pub enum InputCommand { Exit, } -impl InputCommand { +impl InputCmdAction { /// Parses the event. pub fn parse(key_event: KeyEvent, input: &Input) -> Self { if key_event.code == KeyCode::Esc |
