Refactored filtering and searching logic to be more clean and added home/end support in tables.

This commit is contained in:
2023-08-08 10:50:04 -06:00
parent 864dd4e331
commit 24a36443e9
7 changed files with 223 additions and 96 deletions
+10
View File
@@ -16,6 +16,8 @@ generate_keybindings! {
backspace, backspace,
search, search,
filter, filter,
home,
end,
submit, submit,
quit, quit,
esc esc
@@ -55,6 +57,14 @@ pub const DEFAULT_KEYBINDINGS: KeyBindings = KeyBindings {
key: Key::Char('f'), key: Key::Char('f'),
desc: "Filter", desc: "Filter",
}, },
home: KeyBinding {
key: Key::Home,
desc: "Home",
},
end: KeyBinding {
key: Key::End,
desc: "End",
},
submit: KeyBinding { submit: KeyBinding {
key: Key::Enter, key: Key::Enter,
desc: "Select", desc: "Select",
+10
View File
@@ -12,6 +12,8 @@ pub enum Key {
Enter, Enter,
Esc, Esc,
Backspace, Backspace,
Home,
End,
Char(char), Char(char),
Unknown, Unknown,
} }
@@ -47,6 +49,14 @@ impl From<KeyEvent> for Key {
code: KeyCode::Backspace, code: KeyCode::Backspace,
.. ..
} => Key::Backspace, } => Key::Backspace,
KeyEvent {
code: KeyCode::Home,
..
} => Key::Home,
KeyEvent {
code: KeyCode::End,
..
} => Key::End,
KeyEvent { KeyEvent {
code: KeyCode::Enter, code: KeyCode::Enter,
.. ..
+4
View File
@@ -13,6 +13,8 @@ pub trait KeyEventHandler<'a, T: Into<Route>> {
match key { match key {
_ if *key == DEFAULT_KEYBINDINGS.up.key => self.handle_scroll_up(), _ if *key == DEFAULT_KEYBINDINGS.up.key => self.handle_scroll_up(),
_ if *key == DEFAULT_KEYBINDINGS.down.key => self.handle_scroll_down(), _ if *key == DEFAULT_KEYBINDINGS.down.key => self.handle_scroll_down(),
_ if *key == DEFAULT_KEYBINDINGS.home.key => self.handle_home(),
_ if *key == DEFAULT_KEYBINDINGS.end.key => self.handle_end(),
_ if *key == DEFAULT_KEYBINDINGS.left.key || *key == DEFAULT_KEYBINDINGS.right.key => { _ if *key == DEFAULT_KEYBINDINGS.left.key || *key == DEFAULT_KEYBINDINGS.right.key => {
self.handle_tab_action() self.handle_tab_action()
} }
@@ -30,6 +32,8 @@ pub trait KeyEventHandler<'a, T: Into<Route>> {
fn get_key(&self) -> &Key; fn get_key(&self) -> &Key;
fn handle_scroll_up(&mut self); fn handle_scroll_up(&mut self);
fn handle_scroll_down(&mut self); fn handle_scroll_down(&mut self);
fn handle_home(&mut self);
fn handle_end(&mut self);
fn handle_tab_action(&mut self); fn handle_tab_action(&mut self);
fn handle_submit(&mut self); fn handle_submit(&mut self);
fn handle_esc(&mut self); fn handle_esc(&mut self);
@@ -39,6 +39,23 @@ impl<'a> KeyEventHandler<'a, ActiveRadarrBlock> for CollectionDetailsHandler<'a>
} }
} }
fn handle_home(&mut self) {
if ActiveRadarrBlock::CollectionDetails == *self.active_radarr_block {
self.app.data.radarr_data.collection_movies.scroll_to_top();
}
}
fn handle_end(&mut self) {
if ActiveRadarrBlock::CollectionDetails == *self.active_radarr_block {
self
.app
.data
.radarr_data
.collection_movies
.scroll_to_bottom();
}
}
fn handle_tab_action(&mut self) {} fn handle_tab_action(&mut self) {}
fn handle_submit(&mut self) { fn handle_submit(&mut self) {
+144 -95
View File
@@ -3,7 +3,6 @@ use crate::app::radarr::ActiveRadarrBlock;
use crate::handlers::radarr_handlers::collection_details_handler::CollectionDetailsHandler; use crate::handlers::radarr_handlers::collection_details_handler::CollectionDetailsHandler;
use crate::handlers::radarr_handlers::movie_details_handler::MovieDetailsHandler; use crate::handlers::radarr_handlers::movie_details_handler::MovieDetailsHandler;
use crate::handlers::{handle_clear_errors, KeyEventHandler}; use crate::handlers::{handle_clear_errors, KeyEventHandler};
use crate::models::radarr_models::{Collection, Movie};
use crate::models::Scrollable; use crate::models::Scrollable;
use crate::utils::strip_non_alphanumeric_characters; use crate::utils::strip_non_alphanumeric_characters;
use crate::{App, Key}; use crate::{App, Key};
@@ -66,9 +65,6 @@ impl<'a> KeyEventHandler<'a, ActiveRadarrBlock> for RadarrHandler<'a> {
self.app.data.radarr_data.collections.scroll_up() self.app.data.radarr_data.collections.scroll_up()
} }
} }
ActiveRadarrBlock::CollectionDetails => {
self.app.data.radarr_data.collection_movies.scroll_up()
}
ActiveRadarrBlock::Movies => { ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() { if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_up(); self.app.data.radarr_data.filtered_movies.scroll_up();
@@ -97,9 +93,6 @@ impl<'a> KeyEventHandler<'a, ActiveRadarrBlock> for RadarrHandler<'a> {
self.app.data.radarr_data.collections.scroll_down() self.app.data.radarr_data.collections.scroll_down()
} }
} }
ActiveRadarrBlock::CollectionDetails => {
self.app.data.radarr_data.collection_movies.scroll_down()
}
ActiveRadarrBlock::Movies => { ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() { if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_down(); self.app.data.radarr_data.filtered_movies.scroll_down();
@@ -112,6 +105,72 @@ impl<'a> KeyEventHandler<'a, ActiveRadarrBlock> for RadarrHandler<'a> {
} }
} }
fn handle_home(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self
.app
.data
.radarr_data
.filtered_collections
.scroll_to_top();
} else {
self.app.data.radarr_data.collections.scroll_to_top()
}
}
ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_to_top();
} else {
self.app.data.radarr_data.movies.scroll_to_top()
}
}
ActiveRadarrBlock::Downloads => self.app.data.radarr_data.downloads.scroll_to_top(),
_ => (),
}
}
fn handle_end(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self
.app
.data
.radarr_data
.filtered_collections
.scroll_to_bottom();
} else {
self.app.data.radarr_data.collections.scroll_to_bottom()
}
}
ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_to_bottom();
} else {
self.app.data.radarr_data.movies.scroll_to_bottom()
}
}
ActiveRadarrBlock::Downloads => self.app.data.radarr_data.downloads.scroll_to_bottom(),
_ => (),
}
}
fn handle_tab_action(&mut self) { fn handle_tab_action(&mut self) {
match self.active_radarr_block { match self.active_radarr_block {
ActiveRadarrBlock::Movies | ActiveRadarrBlock::Downloads | ActiveRadarrBlock::Collections => { ActiveRadarrBlock::Movies | ActiveRadarrBlock::Downloads | ActiveRadarrBlock::Collections => {
@@ -156,123 +215,57 @@ impl<'a> KeyEventHandler<'a, ActiveRadarrBlock> for RadarrHandler<'a> {
.app .app
.push_navigation_stack(ActiveRadarrBlock::CollectionDetails.into()), .push_navigation_stack(ActiveRadarrBlock::CollectionDetails.into()),
ActiveRadarrBlock::SearchMovie => { ActiveRadarrBlock::SearchMovie => {
let search_string = self let selected_index = self
.app .search_table(&self.app.data.radarr_data.movies.items.clone(), |movie| {
.data &movie.title
.radarr_data });
.search self
.drain(..)
.collect::<String>()
.to_lowercase();
let movie_index = self
.app .app
.data .data
.radarr_data .radarr_data
.movies .movies
.items .select_index(selected_index);
.iter()
.position(|movie| {
strip_non_alphanumeric_characters(&movie.title).contains(&search_string)
});
self.app.data.radarr_data.is_searching = false;
self.app.data.radarr_data.movies.select_index(movie_index);
if movie_index.is_some() {
self.app.pop_navigation_stack();
}
} }
ActiveRadarrBlock::SearchCollection => { ActiveRadarrBlock::SearchCollection => {
let search_string = self let selected_index = self.search_table(
.app &self.app.data.radarr_data.collections.items.clone(),
.data |movie| &movie.title,
.radarr_data );
.search
.drain(..)
.collect::<String>()
.to_lowercase();
let collection_index =
self
.app
.data
.radarr_data
.collections
.items
.iter()
.position(|collection| {
strip_non_alphanumeric_characters(&collection.title).contains(&search_string)
});
self.app.data.radarr_data.is_searching = false;
self self
.app .app
.data .data
.radarr_data .radarr_data
.collections .collections
.select_index(collection_index); .select_index(selected_index);
if collection_index.is_some() {
self.app.pop_navigation_stack();
}
} }
ActiveRadarrBlock::FilterMovies => { ActiveRadarrBlock::FilterMovies => {
let filter = strip_non_alphanumeric_characters( let filtered_movies = self
&self .filter_table(&self.app.data.radarr_data.movies.items.clone(), |movie| {
.app &movie.title
.data });
.radarr_data
.filter
.drain(..)
.collect::<String>(),
);
let movie_list = self.app.data.radarr_data.movies.items.clone();
let filter_matches = movie_list
.iter()
.filter(|&movie| strip_non_alphanumeric_characters(&movie.title).contains(&filter))
.map(|movie| movie.to_owned())
.collect::<Vec<Movie>>();
self.app.data.radarr_data.is_searching = false; if !filtered_movies.is_empty() {
if !filter_matches.is_empty() {
self.app.pop_navigation_stack();
self self
.app .app
.data .data
.radarr_data .radarr_data
.filtered_movies .filtered_movies
.set_items(filter_matches); .set_items(filtered_movies);
} }
} }
ActiveRadarrBlock::FilterCollections => { ActiveRadarrBlock::FilterCollections => {
let filter = strip_non_alphanumeric_characters( let filtered_collections = self.filter_table(
&self &self.app.data.radarr_data.collections.items.clone(),
.app |collection| &collection.title,
.data
.radarr_data
.filter
.drain(..)
.collect::<String>(),
); );
let collection_list = self.app.data.radarr_data.collections.items.clone();
let filter_matches = collection_list
.iter()
.filter(|&collection| {
strip_non_alphanumeric_characters(&collection.title).contains(&filter)
})
.map(|collection| collection.to_owned())
.collect::<Vec<Collection>>();
self.app.data.radarr_data.is_searching = false; if !filtered_collections.is_empty() {
if !filter_matches.is_empty() {
self.app.pop_navigation_stack();
self self
.app .app
.data .data
.radarr_data .radarr_data
.filtered_collections .filtered_collections
.set_items(filter_matches); .set_items(filtered_collections);
} }
} }
_ => (), _ => (),
@@ -350,3 +343,59 @@ impl<'a> KeyEventHandler<'a, ActiveRadarrBlock> for RadarrHandler<'a> {
} }
} }
} }
impl RadarrHandler<'_> {
fn search_table<T, F>(&mut self, rows: &[T], field_selection_fn: F) -> Option<usize>
where
F: Fn(&T) -> &str,
{
let search_string = self
.app
.data
.radarr_data
.search
.drain(..)
.collect::<String>()
.to_lowercase();
let collection_index = rows.iter().position(|item| {
strip_non_alphanumeric_characters(field_selection_fn(item)).contains(&search_string)
});
self.app.data.radarr_data.is_searching = false;
if collection_index.is_some() {
self.app.pop_navigation_stack();
}
collection_index
}
fn filter_table<T, F>(&mut self, rows: &[T], field_selection_fn: F) -> Vec<T>
where
F: Fn(&T) -> &str,
T: Clone,
{
let filter = strip_non_alphanumeric_characters(
&self
.app
.data
.radarr_data
.filter
.drain(..)
.collect::<String>(),
);
let filter_matches: Vec<T> = rows
.iter()
.filter(|&item| strip_non_alphanumeric_characters(field_selection_fn(item)).contains(&filter))
.cloned()
.collect();
self.app.data.radarr_data.is_searching = false;
if !filter_matches.is_empty() {
self.app.pop_navigation_stack();
}
filter_matches
}
}
@@ -48,6 +48,26 @@ impl<'a> KeyEventHandler<'a, ActiveRadarrBlock> for MovieDetailsHandler<'a> {
} }
} }
fn handle_home(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::MovieDetails => self.app.data.radarr_data.movie_details.scroll_to_top(),
ActiveRadarrBlock::MovieHistory => self.app.data.radarr_data.movie_history.scroll_to_top(),
ActiveRadarrBlock::Cast => self.app.data.radarr_data.movie_cast.scroll_to_top(),
ActiveRadarrBlock::Crew => self.app.data.radarr_data.movie_crew.scroll_to_top(),
_ => (),
}
}
fn handle_end(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::MovieDetails => self.app.data.radarr_data.movie_details.scroll_to_bottom(),
ActiveRadarrBlock::MovieHistory => self.app.data.radarr_data.movie_history.scroll_to_bottom(),
ActiveRadarrBlock::Cast => self.app.data.radarr_data.movie_cast.scroll_to_bottom(),
ActiveRadarrBlock::Crew => self.app.data.radarr_data.movie_crew.scroll_to_bottom(),
_ => (),
}
}
fn handle_tab_action(&mut self) { fn handle_tab_action(&mut self) {
match self.active_radarr_block { match self.active_radarr_block {
ActiveRadarrBlock::MovieDetails ActiveRadarrBlock::MovieDetails
+18 -1
View File
@@ -1,7 +1,6 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter}; use std::fmt::{Debug, Display, Formatter};
use log::debug;
use serde::Deserialize; use serde::Deserialize;
use tui::widgets::TableState; use tui::widgets::TableState;
@@ -25,6 +24,8 @@ pub enum Route {
pub trait Scrollable { pub trait Scrollable {
fn scroll_down(&mut self); fn scroll_down(&mut self);
fn scroll_up(&mut self); fn scroll_up(&mut self);
fn scroll_to_top(&mut self);
fn scroll_to_bottom(&mut self);
} }
pub struct StatefulTable<T> { pub struct StatefulTable<T> {
@@ -102,6 +103,14 @@ impl<T> Scrollable for StatefulTable<T> {
self.state.select(Some(selected_row)); self.state.select(Some(selected_row));
} }
fn scroll_to_top(&mut self) {
self.state.select(Some(0));
}
fn scroll_to_bottom(&mut self) {
self.state.select(Some(self.items.len() - 1));
}
} }
#[derive(Default)] #[derive(Default)]
@@ -134,6 +143,14 @@ impl Scrollable for ScrollableText {
self.offset -= 1; self.offset -= 1;
} }
} }
fn scroll_to_top(&mut self) {
self.offset = 0;
}
fn scroll_to_bottom(&mut self) {
self.offset = (self.items.len() - 1) as u16;
}
} }
#[derive(Default, Deserialize, Debug, Clone, PartialEq, Eq)] #[derive(Default, Deserialize, Debug, Clone, PartialEq, Eq)]