Provide search functionality through the TUI #134

Merged
wojtek merged 35 commits from 24---provide-search-functionality-through-the-tui into main 2024-02-18 22:12:42 +01:00
2 changed files with 30 additions and 89 deletions
Showing only changes of commit f8815982f4 - Show all commits

View File

@ -7,8 +7,6 @@ use crate::tui::{
lib::IMusicHoard, lib::IMusicHoard,
}; };
use super::selection::IncSearch;
pub enum AppState<BS, IS, RS, SS, ES, CS> { pub enum AppState<BS, IS, RS, SS, ES, CS> {
Browse(BS), Browse(BS),
Info(IS), Info(IS),
@ -298,16 +296,20 @@ impl<MH: IMusicHoard> IAppInteractSearch for App<MH> {
let collection = self.music_hoard.get_collection(); let collection = self.music_hoard.get_collection();
let search = self.state.as_mut().unwrap_search(); let search = self.state.as_mut().unwrap_search();
search.push(ch); search.push(ch);
self.selection self.selection.incremental_artist_search(collection, search);
.incremental_artist_search(IncSearch::Forward, collection, search);
} }
// Removing a character restarts the search from scratch. For now, the performance impact of
// this is not noticeable. If it does become noticeable, some form of memoization should be used
// as an optimisation. The most intuitive behaviour when removing a character is to return to
// the previous search position. However, it is difficult to construct a search predicate in
// selection.rs that will always correctly reverse to the previous position.
fn remove_character(&mut self) { fn remove_character(&mut self) {
let collection = self.music_hoard.get_collection(); let collection = self.music_hoard.get_collection();
let search = self.state.as_mut().unwrap_search(); let search = self.state.as_mut().unwrap_search();
search.pop(); search.pop();
self.selection self.selection.reset_artist(&collection);
.incremental_artist_search(IncSearch::Reverse, collection, search); self.selection.incremental_artist_search(collection, search);
} }
fn finish_search(&mut self) { fn finish_search(&mut self) {

View File

@ -61,11 +61,6 @@ impl Delta {
} }
} }
pub enum IncSearch {
Forward,
Reverse,
}
impl Selection { impl Selection {
pub fn new(artists: &[Artist]) -> Self { pub fn new(artists: &[Artist]) -> Self {
Selection { Selection {
@ -100,14 +95,8 @@ impl Selection {
}; };
} }
pub fn incremental_artist_search( pub fn incremental_artist_search(&mut self, collection: &Collection, artist_name: &str) {
&mut self, self.artist.incremental_search(collection, artist_name);
direction: IncSearch,
collection: &Collection,
artist_name: &str,
) {
self.artist
.incremental_search(direction, collection, artist_name);
} }
pub fn increment_selection(&mut self, collection: &Collection, delta: Delta) { pub fn increment_selection(&mut self, collection: &Collection, delta: Delta) {
@ -204,7 +193,7 @@ impl ArtistSelection {
} }
// FIXME: use aho_corasick for normalization // FIXME: use aho_corasick for normalization
fn normalize_search_string(search: &str, lowercase: bool, asciify: bool) -> String { fn normalize_search(search: &str, lowercase: bool, asciify: bool) -> String {
let normalized = if lowercase { let normalized = if lowercase {
search.to_lowercase() search.to_lowercase()
} else { } else {
@ -243,92 +232,42 @@ impl ArtistSelection {
artist_name.chars().any(|ch| special_chars.contains(&ch)) artist_name.chars().any(|ch| special_chars.contains(&ch))
} }
// FIXME: compare against both sort key and actual name
fn incremental_search_predicate( fn incremental_search_predicate(
case_sensitive: bool, case_sensitive: bool,
char_sensitive: bool, char_sensitive: bool,
search_name: &String, search_name: &String,
probe: &Artist, probe: &Artist,
) -> bool { ) -> bool {
let probe_name = &probe.get_sort_key().name; let name = Self::normalize_search(&probe.id.name, !case_sensitive, !char_sensitive);
match Self::normalize_search_string(probe_name, !case_sensitive, !char_sensitive) let mut result = name.starts_with(search_name);
.cmp(search_name)
{ if let Some(ref probe_sort) = probe.sort {
std::cmp::Ordering::Less => false, let name = Self::normalize_search(&probe_sort.name, !case_sensitive, !char_sensitive);
std::cmp::Ordering::Equal | std::cmp::Ordering::Greater => true, result = result || name.starts_with(search_name);
} }
result
} }
fn incremental_search(&mut self, direction: IncSearch, artists: &[Artist], artist_name: &str) { // FIXME: Use memoization - the only way to have correct behaviour
fn incremental_search(&mut self, artists: &[Artist], artist_name: &str) {
if let Some(index) = self.state.list.selected() { if let Some(index) = self.state.list.selected() {
let case_sensitive = Self::is_case_sensitive(artist_name); let case_sensitive = Self::is_case_sensitive(artist_name);
let char_sensitive = Self::is_char_sensitive(artist_name); let char_sensitive = Self::is_char_sensitive(artist_name);
let search_name = let search = Self::normalize_search(artist_name, !case_sensitive, !char_sensitive);
Self::normalize_search_string(artist_name, !case_sensitive, !char_sensitive);
match direction { let slice = &artists[index..];
IncSearch::Forward => self.forward_incremental_search(
artists, let result = slice.iter().position(|probe| {
index, Self::incremental_search_predicate(case_sensitive, char_sensitive, &search, probe)
case_sensitive, });
char_sensitive,
&search_name, if let Some(slice_index) = result {
), self.select_to(artists, index + slice_index);
IncSearch::Reverse => self.reverse_incremental_search(
artists,
index,
case_sensitive,
char_sensitive,
&search_name,
),
} }
} }
} }
fn forward_incremental_search(
&mut self,
artists: &[Artist],
index: usize,
case_sensitive: bool,
char_sensitive: bool,
search: &String,
) {
let slice = &artists[index..];
let result = slice.iter().position(|probe| {
Self::incremental_search_predicate(case_sensitive, char_sensitive, &search, probe)
});
let new_index = match result {
Some(slice_index) => index + slice_index,
None => artists.len(),
};
self.select_to(artists, new_index);
}
fn reverse_incremental_search(
&mut self,
artists: &[Artist],
index: usize,
case_sensitive: bool,
char_sensitive: bool,
search: &String,
) {
let slice = &artists[..(index + 1)];
// We search using opposite predicate in the reverse direction because what matters is the
// point at which the predicate flips value.
let result = slice.iter().rev().position(|probe| {
!Self::incremental_search_predicate(case_sensitive, char_sensitive, &search, probe)
});
let new_index = match result {
Some(slice_index) => index - slice_index + 1,
None => 0,
};
self.select_to(artists, new_index);
}
fn increment_by(&mut self, artists: &[Artist], by: usize) { fn increment_by(&mut self, artists: &[Artist], by: usize) {
if let Some(index) = self.state.list.selected() { if let Some(index) = self.state.list.selected() {
let result = index.saturating_add(by); let result = index.saturating_add(by);