Output ASCII art of cat pictures.
This commit is contained in:
parent
95db82c430
commit
f2581aab84
3 changed files with 571 additions and 13 deletions
19
src/main.rs
19
src/main.rs
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue