jensmemesclient/cli/src/commands/search.rs
2020-12-06 14:57:14 +01:00

33 lines
754 B
Rust

use anyhow::Result;
use log::info;
use reqwest::Client;
use crate::util;
pub async fn run(http: &Client, query: String) -> Result<()> {
let memes = util::memes(http).await?;
let mut matches = vec![];
info!("Starting search with query '{}'", query);
for meme in memes {
let file_name = meme.file_name()?;
if let Some(score) = fuzzy_matcher::clangd::fuzzy_match(file_name, &query) {
info!("Found matching meme '{}' with score {}", file_name, score);
matches.push((meme, score));
}
}
matches.sort_by(|a, b| b.1.cmp(&a.1));
let mut table = util::list_table();
for m in matches {
table.add_row(m.0.into());
}
println!("{}", table.render());
Ok(())
}