use super::osc::write_osc;
use crate::magic;
use crate::resources::read_url;
use failure::Error;
use std::ffi::OsStr;
use std::io::{self, Write};
use url::Url;
use super::super::svg;
pub fn is_iterm2() -> bool {
cfg!(unix)
&& std::env::var("TERM_PROGRAM")
.map(|value| value.contains("iTerm.app"))
.unwrap_or(false)
}
pub struct ITerm2Marks;
impl ITerm2Marks {
pub fn set_mark<W: Write>(&self, writer: &mut W) -> io::Result<()> {
write_osc(writer, "1337;SetMark")
}
}
pub struct ITerm2Images;
impl ITerm2Images {
#[cfg(unix)]
pub fn write_inline_image<W: Write, S: AsRef<OsStr>>(
&self,
writer: &mut W,
name: S,
contents: &[u8],
) -> io::Result<()> {
use std::os::unix::ffi::OsStrExt;
write_osc(
writer,
&format!(
"1337;File=name={};inline=1:{}",
base64::encode(name.as_ref().as_bytes()),
base64::encode(contents)
),
)
}
#[cfg(windows)]
pub fn write_inline_image<W: Write, S: AsRef<OsStr>>(
&self,
_writer: &mut W,
_name: S,
_contents: &[u8],
) -> io::Result<()> {
unimplemented!()
}
pub fn read_and_render(&self, url: &Url) -> Result<Vec<u8>, Error> {
let contents = read_url(&url)?;
if magic::is_svg(&magic::detect_mime_type(&contents)?) {
svg::render_svg(&contents).map_err(Into::into)
} else {
Ok(contents)
}
}
}