nhentairs/src/commands/view.rs

38 lines
1.2 KiB
Rust
Raw Normal View History

2020-09-12 12:26:07 +00:00
use crate::utils;
2020-12-01 15:02:05 +00:00
use crate::structs;
2020-09-12 12:26:07 +00:00
use std::env;
use std::process::exit;
use tokio::task::JoinHandle;
2020-12-01 15:02:05 +00:00
//use std::collections::BTreeMap;
2020-09-12 12:26:07 +00:00
extern crate tokio;
extern crate reqwest;
pub async fn run(args: env::Args) {
let sauces = utils::get_arg_sauces(args).unwrap();
let mut remaining_to_show = sauces.len();
if remaining_to_show < 1 {
eprintln!("Missing sauce(s)");
exit(1);
}
let client = reqwest::Client::new();
let mut handles: Vec<JoinHandle<(structs::GalleryInfo, usize)>> = Vec::with_capacity(sauces.len());
2020-09-12 12:26:07 +00:00
for sauce in sauces {
let cloned_client = client.clone();
handles.push(tokio::spawn(async move {
(utils::get_sauce_info(cloned_client, sauce).await.unwrap(), sauce)
2020-09-12 12:26:07 +00:00
}));
}
for handle in handles {
let (sauce_info, sauce) = handle.await.unwrap();
match sauce_info {
structs::GalleryInfo::Info(sauce_info) => println!("{}", utils::human_sauce_info(&sauce_info)),
structs::GalleryInfo::Error(sauce_error) => eprintln!("Sauce: {}\nError: {}", sauce, sauce_error.error)
};
2020-09-12 12:26:07 +00:00
if remaining_to_show > 1 {
println!("");
remaining_to_show -= 1;
}
}
}