Output ASCII art of cat pictures.

This commit is contained in:
Lyle Mantooth 2023-05-14 10:48:43 -04:00
parent 95db82c430
commit f2581aab84
Signed by: IslandUsurper
GPG key ID: 6DB52EAE123A5789
3 changed files with 571 additions and 13 deletions

View file

@ -1,15 +1,12 @@
use pretty_hex::PrettyHex;
use serde::Deserialize;
#[tokio::main]
async fn main() {
let image_bytes = get_cat_image_bytes().await.unwrap();
// only dump the first 200 bytes so our terminal survives the onslaught. This will panic if the
// image has fewer than 200 bytes.
println!("{:?}", &image_bytes[..200].hex_dump());
let art = get_cat_ascii_art().await.unwrap();
println!("{art}");
}
async fn get_cat_image_bytes() -> color_eyre::Result<Vec<u8>> {
async fn get_cat_ascii_art() -> color_eyre::Result<String> {
#[derive(Deserialize)]
struct CatImage {
url: String,
@ -28,12 +25,16 @@ async fn get_cat_image_bytes() -> color_eyre::Result<Vec<u8>> {
.pop()
.ok_or_else(|| color_eyre::eyre::eyre!("The Cat API returned no images"))?;
Ok(client
let image_bytes = client
.get(image.url)
.send()
.await?
.error_for_status()?
.bytes()
.await?
.to_vec())
.await?;
let image = image::load_from_memory(&image_bytes)?;
let ascii_art = artem::convert(image, artem::options::OptionBuilder::new().build());
Ok(ascii_art)
}