add filter options to list command
This commit is contained in:
parent
83cb590727
commit
0a2d449b08
3 changed files with 39 additions and 27 deletions
|
@ -3,10 +3,16 @@ use reqwest::Client;
|
|||
|
||||
use crate::util;
|
||||
|
||||
pub async fn run(http: &Client) -> Result<()> {
|
||||
pub async fn run(http: &Client, cat: Option<String>, user: Option<String>) -> Result<()> {
|
||||
let memes = util::memes(http).await?;
|
||||
|
||||
let memes = memes
|
||||
.iter()
|
||||
.filter(|&m| cat.as_ref().map(|q| &m.category == q).unwrap_or(true))
|
||||
.filter(|&m| user.as_ref().map(|q| &m.user == q).unwrap_or(true));
|
||||
|
||||
let mut table = util::list_table();
|
||||
|
||||
for m in memes {
|
||||
table.add_row(m.into());
|
||||
}
|
||||
|
|
|
@ -46,7 +46,13 @@ enum Cmd {
|
|||
Search { query: String },
|
||||
|
||||
#[structopt(about = "lists all memes")]
|
||||
List,
|
||||
List {
|
||||
#[structopt(long, short, help = "filter by category")]
|
||||
category: Option<String>,
|
||||
|
||||
#[structopt(long, short, help = "filter by user")]
|
||||
user: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
@ -72,7 +78,7 @@ async fn main() -> Result<()> {
|
|||
.iter()
|
||||
.for_each(|c| println!("{}", c)),
|
||||
Cmd::Search { query } => commands::search::run(&http, query).await?,
|
||||
Cmd::List => commands::list::run(&http).await?,
|
||||
Cmd::List { category, user } => commands::list::run(&http, category, user).await?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use std::unreachable;
|
||||
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
use once_cell::sync::OnceCell;
|
||||
|
@ -25,33 +23,35 @@ pub async fn open_link(url: &str) -> anyhow::Result<()> {
|
|||
}
|
||||
|
||||
pub async fn cats(http: &Client) -> Result<&Vec<String>> {
|
||||
if CATS.get().is_none() {
|
||||
info!("Requesting categories from server");
|
||||
let res = http
|
||||
.get("https://data.tilera.xyz/api/jensmemes/categories")
|
||||
.send()
|
||||
.await?;
|
||||
let cats = serde_json::from_slice::<CatsResp>(&res.bytes().await?)?;
|
||||
Ok(match CATS.get() {
|
||||
None => {
|
||||
info!("Requesting categories from server");
|
||||
let res = http
|
||||
.get("https://data.tilera.xyz/api/jensmemes/categories")
|
||||
.send()
|
||||
.await?;
|
||||
let cats = serde_json::from_slice::<CatsResp>(&res.bytes().await?)?;
|
||||
|
||||
Ok(CATS.get_or_init(|| cats.categories.into_iter().map(|c| c.id).collect()))
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
CATS.get_or_init(|| cats.categories.into_iter().map(|c| c.id).collect())
|
||||
},
|
||||
Some(x) => x,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn memes(http: &Client) -> Result<&Vec<Meme>> {
|
||||
if MEMES.get().is_none() {
|
||||
info!("Requesting memes from server");
|
||||
let res = http
|
||||
.get("https://data.tilera.xyz/api/jensmemes/memes")
|
||||
.send()
|
||||
.await?;
|
||||
let memes = serde_json::from_slice::<MemesResp>(&res.bytes().await?)?;
|
||||
Ok(match MEMES.get() {
|
||||
None => {
|
||||
info!("Requesting memes from server");
|
||||
let res = http
|
||||
.get("https://data.tilera.xyz/api/jensmemes/memes")
|
||||
.send()
|
||||
.await?;
|
||||
let memes = serde_json::from_slice::<MemesResp>(&res.bytes().await?)?;
|
||||
|
||||
Ok(MEMES.get_or_init(|| memes.memes))
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
MEMES.get_or_init(|| memes.memes)
|
||||
},
|
||||
Some(x) => x,
|
||||
})
|
||||
}
|
||||
|
||||
/// returns an empty table with the correct format settings for lists
|
||||
|
|
Loading…
Reference in a new issue