use ratatui_image::picker::Picker;
use ratatui_image::protocol::StatefulProtocol;
use std::collections::HashMap;
pub enum ImageState {
#[allow(dead_code)]
Pending,
Loading,
Ready(StatefulProtocol),
Failed(String),
}
pub struct ImageCache {
pub picker: Option<Picker>,
pub images: HashMap<String, ImageState>,
}
fn terminal_supports_graphics() -> bool {
if std::env::var("KITTY_WINDOW_ID").is_ok() {
return true;
}
match std::env::var("TERM_PROGRAM").as_deref() {
Ok("Apple_Terminal") => false,
Ok("iTerm.app") | Ok("WezTerm") | Ok("ghostty") => true,
_ => matches!(
std::env::var("COLORTERM").as_deref(),
Ok("truecolor") | Ok("24bit")
),
}
}
impl Default for ImageCache {
fn default() -> Self {
Self::new()
}
}
impl ImageCache {
pub fn new() -> Self {
let picker = if terminal_supports_graphics() {
Picker::from_query_stdio().ok()
} else {
None
};
Self {
picker,
images: HashMap::new(),
}
}
}