Improve searching and search hints
This commit is contained in:
12
src/app.rs
12
src/app.rs
@ -183,8 +183,16 @@ pub fn view(model: &Model) -> Vec<Node<Msg>> {
|
||||
],
|
||||
div![
|
||||
C![C.song_item_info],
|
||||
div![C![C.song_item_title], song.title.to_string()],
|
||||
div![C![C.song_item_artist], song.artist.to_string()],
|
||||
div![C![C.song_item_title], &song.title],
|
||||
div![
|
||||
C![C.song_item_artist],
|
||||
span![&song.artist],
|
||||
if let Some(year) = song.year.as_ref() {
|
||||
span![" (", year, ")"]
|
||||
} else {
|
||||
empty![]
|
||||
}
|
||||
],
|
||||
],
|
||||
div![
|
||||
C![C.song_gizmos],
|
||||
|
||||
21
src/query.rs
21
src/query.rs
@ -11,10 +11,10 @@ pub struct ParsedQuery<'a> {
|
||||
pub plain: Option<Cow<'a, str>>,
|
||||
|
||||
/// Query a specific title
|
||||
pub title: Option<&'a str>,
|
||||
pub title: Option<Cow<'a, str>>,
|
||||
|
||||
/// Query a specific artist
|
||||
pub artist: Option<&'a str>,
|
||||
pub artist: Option<Cow<'a, str>>,
|
||||
|
||||
/// Whether the song is a duet
|
||||
pub duet: Option<bool>,
|
||||
@ -43,8 +43,8 @@ impl<'a> ParsedQuery<'a> {
|
||||
|
||||
for (k, v) in kvs {
|
||||
match k {
|
||||
"title" => parsed.title = Some(v),
|
||||
"artist" => parsed.artist = Some(v),
|
||||
"title" => parsed.title = Some(Cow::Borrowed(v)),
|
||||
"artist" => parsed.artist = Some(Cow::Borrowed(v)),
|
||||
"duet" => parsed.duet = parse_bool(v),
|
||||
"video" => parsed.video = parse_bool(v),
|
||||
"lang" => parsed.language = Some(v),
|
||||
@ -62,6 +62,15 @@ impl<'a> ParsedQuery<'a> {
|
||||
let until_space =
|
||||
|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] = [
|
||||
&|query| Self {
|
||||
plain: Some(Cow::Borrowed(&song.title)),
|
||||
@ -72,11 +81,11 @@ impl<'a> ParsedQuery<'a> {
|
||||
..query
|
||||
},
|
||||
&|query| Self {
|
||||
title: Some(until_space(&song.title)),
|
||||
title: Some(join_spaces(&song.title)),
|
||||
..query
|
||||
},
|
||||
&|query| Self {
|
||||
artist: Some(until_space(&song.artist)),
|
||||
artist: Some(join_spaces(&song.artist)),
|
||||
..query
|
||||
},
|
||||
];
|
||||
|
||||
13
src/song.rs
13
src/song.rs
@ -35,14 +35,13 @@ impl Song {
|
||||
match item {
|
||||
Some(item) => {
|
||||
let score = fuzzy::compare(item.chars(), query.chars());
|
||||
if score < fuzzy::max_score(query) / 2 {
|
||||
return false;
|
||||
}
|
||||
score == fuzzy::max_score(query)
|
||||
}
|
||||
None => return false,
|
||||
None => false,
|
||||
}
|
||||
} else {
|
||||
true
|
||||
}
|
||||
true
|
||||
};
|
||||
|
||||
let filter_bool =
|
||||
@ -67,12 +66,12 @@ impl Song {
|
||||
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());
|
||||
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());
|
||||
score = max(score, new_score);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user