Function suggestion_query -> first_similarity, added more tests to said function

This commit is contained in:
Xithrius 2023-01-03 19:40:28 -08:00
parent d79add9e35
commit 3ccadc8a9a
No known key found for this signature in database
GPG Key ID: A867F27CC80B28C1
3 changed files with 27 additions and 10 deletions

View File

@ -6,7 +6,7 @@ use crate::{
statics::{COMMANDS, TWITCH_MESSAGE_LIMIT},
WindowAttributes,
},
utils::text::suggestion_query,
utils::text::first_similarity,
};
pub fn render_chat_box<T: Backend>(window: WindowAttributes<T>, mention_suggestions: bool) {
@ -27,12 +27,12 @@ pub fn render_chat_box<T: Backend>(window: WindowAttributes<T>, mention_suggesti
.next()
.and_then(|start_character| match start_character {
'/' => {
let possible_suggestion = suggestion_query(
&current_input[1..],
let possible_suggestion = first_similarity(
&COMMANDS
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>(),
&current_input[1..],
);
let default_suggestion = possible_suggestion.clone();
@ -41,7 +41,7 @@ pub fn render_chat_box<T: Backend>(window: WindowAttributes<T>, mention_suggesti
}
'@' => {
let possible_suggestion =
suggestion_query(&current_input[1..], &app.storage.get("mentions"));
first_similarity(&app.storage.get("mentions"), &current_input[1..]);
let default_suggestion = possible_suggestion.clone();

View File

@ -9,7 +9,7 @@ use crate::{
statics::CHANNEL_NAME_REGEX,
WindowAttributes,
},
utils::text::suggestion_query,
utils::text::first_similarity,
};
pub fn render_channel_switcher<T: Backend>(window: WindowAttributes<T>, channel_suggestions: bool) {
@ -25,13 +25,13 @@ pub fn render_channel_switcher<T: Backend>(window: WindowAttributes<T>, channel_
let input_rect = centered_popup(frame.size(), frame.size().height);
let suggestion = if channel_suggestions {
suggestion_query(
input_buffer,
first_similarity(
&app.storage
.get("channels")
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>(),
input_buffer,
)
} else {
None

View File

@ -73,7 +73,8 @@ pub fn title_spans<'a>(contents: &'a [TitleStyle<'a>], style: Style) -> Vec<Span
complete
}
pub fn suggestion_query(search: &str, possibilities: &[String]) -> Option<String> {
/// Within an array of strings, find the first partial or full match, if any.
pub fn first_similarity(possibilities: &[String], search: &str) -> Option<String> {
possibilities
.iter()
.filter(|s| s.starts_with(search))
@ -168,11 +169,27 @@ mod tests {
}
#[test]
fn test_partial_suggestion_output() {
fn test_first_similarity_some_output() {
let v = vec!["Nope".to_string()];
let output = suggestion_query("No", &v);
let output = first_similarity(&v, "No");
assert_eq!(output, Some("Nope".to_string()));
}
#[test]
fn test_first_similarity_no_output() {
let v = vec!["Something".to_string()];
let output = first_similarity(&v, "blah");
assert_eq!(output, None);
}
#[test]
fn test_first_similarity_no_input_no_output() {
let output = first_similarity(&vec![], "asdf");
assert_eq!(output, None);
}
}