use super::TerminalSize;
use std::io::{Result, Write};
use url::Url;
pub fn is_terminology() -> bool {
std::env::var("TERMINOLOGY")
.map(|value| value.trim() == "1")
.unwrap_or(false)
}
pub struct TerminologyImages;
impl TerminologyImages {
pub fn write_inline_image<W: Write>(
&self,
writer: &mut W,
max_size: TerminalSize,
url: &Url,
) -> Result<()> {
let columns = max_size.width;
let lines = Some(url)
.filter(|url| url.scheme() == "file")
.and_then(|url| url.to_file_path().ok())
.and_then(|path| image::image_dimensions(path).ok())
.map(|(width, height)| {
let (w, h) = (f64::from(width), f64::from(height));
(h * (columns / 2) as f64 / w) as usize
})
.unwrap_or(max_size.height / 2);
let mut command = format!("\x1b}}ic#{};{};{}\x00", columns, lines, url.as_str());
for _ in 0..lines {
command.push_str("\x1b}ib\x00");
for _ in 0..columns {
command.push('#');
}
command.push_str("\x1b}ie\x00\n");
}
writer.write_all(command.as_bytes())?;
Ok(())
}
}