Improve searching and search hints

This commit is contained in:
2022-04-19 15:10:24 +02:00
parent 06e12c186a
commit 666f87a5f3
3 changed files with 31 additions and 15 deletions

View File

@ -183,8 +183,16 @@ pub fn view(model: &Model) -> Vec<Node<Msg>> {
], ],
div![ div![
C![C.song_item_info], C![C.song_item_info],
div![C![C.song_item_title], song.title.to_string()], div![C![C.song_item_title], &song.title],
div![C![C.song_item_artist], song.artist.to_string()], div![
C![C.song_item_artist],
span![&song.artist],
if let Some(year) = song.year.as_ref() {
span![" (", year, ")"]
} else {
empty![]
}
],
], ],
div![ div![
C![C.song_gizmos], C![C.song_gizmos],

View File

@ -11,10 +11,10 @@ pub struct ParsedQuery<'a> {
pub plain: Option<Cow<'a, str>>, pub plain: Option<Cow<'a, str>>,
/// Query a specific title /// Query a specific title
pub title: Option<&'a str>, pub title: Option<Cow<'a, str>>,
/// Query a specific artist /// Query a specific artist
pub artist: Option<&'a str>, pub artist: Option<Cow<'a, str>>,
/// Whether the song is a duet /// Whether the song is a duet
pub duet: Option<bool>, pub duet: Option<bool>,
@ -43,8 +43,8 @@ impl<'a> ParsedQuery<'a> {
for (k, v) in kvs { for (k, v) in kvs {
match k { match k {
"title" => parsed.title = Some(v), "title" => parsed.title = Some(Cow::Borrowed(v)),
"artist" => parsed.artist = Some(v), "artist" => parsed.artist = Some(Cow::Borrowed(v)),
"duet" => parsed.duet = parse_bool(v), "duet" => parsed.duet = parse_bool(v),
"video" => parsed.video = parse_bool(v), "video" => parsed.video = parse_bool(v),
"lang" => parsed.language = Some(v), "lang" => parsed.language = Some(v),
@ -62,6 +62,15 @@ impl<'a> ParsedQuery<'a> {
let until_space = let until_space =
|s: &'a str| -> &'a str { s.trim().split_whitespace().next().unwrap_or("") }; |s: &'a str| -> &'a str { s.trim().split_whitespace().next().unwrap_or("") };
let join_spaces = |s: &'a str| -> Cow<'a, str> {
let s = s.trim();
if s.contains(char::is_whitespace) {
s.replace(char::is_whitespace, "").into()
} else {
Cow::Borrowed(s)
}
};
let mut primary_fields: [&dyn Fn(Self) -> Self; 4] = [ let mut primary_fields: [&dyn Fn(Self) -> Self; 4] = [
&|query| Self { &|query| Self {
plain: Some(Cow::Borrowed(&song.title)), plain: Some(Cow::Borrowed(&song.title)),
@ -72,11 +81,11 @@ impl<'a> ParsedQuery<'a> {
..query ..query
}, },
&|query| Self { &|query| Self {
title: Some(until_space(&song.title)), title: Some(join_spaces(&song.title)),
..query ..query
}, },
&|query| Self { &|query| Self {
artist: Some(until_space(&song.artist)), artist: Some(join_spaces(&song.artist)),
..query ..query
}, },
]; ];

View File

@ -35,14 +35,13 @@ impl Song {
match item { match item {
Some(item) => { Some(item) => {
let score = fuzzy::compare(item.chars(), query.chars()); let score = fuzzy::compare(item.chars(), query.chars());
if score < fuzzy::max_score(query) / 2 { score == fuzzy::max_score(query)
return false;
}
} }
None => return false, None => false,
} }
} else {
true
} }
true
}; };
let filter_bool = let filter_bool =
@ -67,12 +66,12 @@ impl Song {
score = max(title_score, artist_score); score = max(title_score, artist_score);
} }
if let Some(title) = query.title { if let Some(title) = &query.title {
let new_score = fuzzy::compare(self.title.chars(), title.chars()); let new_score = fuzzy::compare(self.title.chars(), title.chars());
score = max(score, new_score); score = max(score, new_score);
} }
if let Some(artist) = query.artist { if let Some(artist) = &query.artist {
let new_score = fuzzy::compare(self.artist.chars(), artist.chars()); let new_score = fuzzy::compare(self.artist.chars(), artist.chars());
score = max(score, new_score); score = max(score, new_score);
} }