#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Graphics {
Kitty,
Sixel,
HalfBlock,
None,
}
impl Graphics {
pub const ALL: [Self; 4] = [Self::Kitty, Self::Sixel, Self::HalfBlock, Self::None];
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Kitty => "kitty",
Self::Sixel => "sixel",
Self::HalfBlock => "halfblock",
Self::None => "none",
}
}
#[must_use]
pub fn can_draw(self) -> bool {
self != Self::None
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
let name = name.trim();
Self::ALL.into_iter().find(|graphics| graphics.name().eq_ignore_ascii_case(name))
}
}
pub(crate) const VARIABLE: &str = "QUVYTA_GRAPHICS";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct GraphicsFacts {
pub(crate) answer: Graphics,
pub(crate) forced: Option<Graphics>,
pub(crate) multiplexed: bool,
}
impl Default for GraphicsFacts {
fn default() -> Self {
Self { answer: Graphics::HalfBlock, forced: None, multiplexed: false }
}
}
impl GraphicsFacts {
pub(crate) fn detect(lookup: impl Fn(&str) -> Option<String>) -> (Self, Option<String>) {
let set = |name: &str| lookup(name).filter(|value| !value.trim().is_empty());
let multiplexed = set("TMUX").is_some() || set("STY").is_some();
let (forced, unknown) = match set(VARIABLE) {
Some(value) => match Graphics::from_name(&value) {
Some(graphics) => (Some(graphics), None),
None => (None, Some(value)),
},
None => (None, None),
};
(Self { answer: Graphics::HalfBlock, forced, multiplexed }, unknown)
}
pub(crate) fn resolve(self, depth: crate::color::ColorDepth, glyphs: crate::icons::GlyphMode) -> Graphics {
if let Some(forced) = self.forced {
return forced;
}
if depth == crate::color::ColorDepth::Ansi16 || glyphs == crate::icons::GlyphMode::Ascii {
return Graphics::None;
}
match self.answer {
Graphics::Kitty | Graphics::Sixel if self.multiplexed => Graphics::HalfBlock,
answer => answer,
}
}
pub(crate) fn worth_asking(self, depth: crate::color::ColorDepth) -> bool {
self.forced.is_none() && !self.multiplexed && depth != crate::color::ColorDepth::Ansi16
}
}
pub(crate) const QUERY: &str = "\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\x1b[c";
pub(crate) fn classify(replies: &[u8]) -> Graphics {
if kitty_ok(replies) {
Graphics::Kitty
} else if primary_attributes(replies).is_some_and(|attributes| attributes.contains(&"4")) {
Graphics::Sixel
} else {
Graphics::HalfBlock
}
}
pub(crate) fn answered(replies: &[u8]) -> bool {
primary_attributes(replies).is_some()
}
fn kitty_ok(replies: &[u8]) -> bool {
let mut rest = replies;
while let Some(start) = find(rest, b"\x1b_G") {
let after = &rest[start + 3..];
let Some(end) = find(after, b"\x1b\\") else {
return false;
};
let body = &after[..end];
if let Some(split) = body.iter().position(|&byte| byte == b';') {
let (keys, message) = (&body[..split], &body[split + 1..]);
if keys.split(|&byte| byte == b',').any(|key| key == b"i=31") && message == b"OK" {
return true;
}
}
rest = &after[end + 2..];
}
false
}
fn primary_attributes(replies: &[u8]) -> Option<Vec<&str>> {
let mut rest = replies;
while let Some(start) = find(rest, b"\x1b[?") {
let body = &rest[start + 3..];
let length = body.iter().position(|&byte| !(byte.is_ascii_digit() || byte == b';'))?;
if body[length] == b'c' {
let text = std::str::from_utf8(&body[..length]).ok()?;
return Some(text.split(';').collect());
}
rest = &body[length..];
}
None
}
fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack.windows(needle.len()).position(|window| window == needle)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::color::ColorDepth;
use crate::icons::GlyphMode;
const KITTY_OK: &[u8] = b"\x1b_Gi=31;OK\x1b\\";
const DA1_PLAIN: &[u8] = b"\x1b[?62;c";
const DA1_SIXEL: &[u8] = b"\x1b[?62;4;22c";
#[test]
fn a_kitty_ok_before_the_attributes_means_kitty() {
assert_eq!(classify(&[KITTY_OK, DA1_PLAIN].concat()), Graphics::Kitty);
assert_eq!(classify(&[KITTY_OK, DA1_SIXEL].concat()), Graphics::Kitty, "the sharper of the two wins");
}
#[test]
fn attributes_listing_4_mean_sixel() {
assert_eq!(classify(DA1_SIXEL), Graphics::Sixel);
assert_eq!(classify(b"\x1b[?4c"), Graphics::Sixel, "4 alone");
assert_eq!(classify(b"\x1b[?64;1;2;4;6;9;15;18;21;22c"), Graphics::Sixel, "xterm as a VT340");
}
#[test]
fn attributes_without_4_mean_half_blocks() {
assert_eq!(classify(DA1_PLAIN), Graphics::HalfBlock);
assert_eq!(classify(b"\x1b[?1;2c"), Graphics::HalfBlock, "a VT100 with advanced video");
assert_eq!(classify(b"\x1b[?64;14;22c"), Graphics::HalfBlock, "14 is not 4");
}
#[test]
fn a_kitty_error_or_another_image_is_not_kitty() {
let refused = [&b"\x1b_Gi=31;ENOTSUPPORTED:no\x1b\\"[..], DA1_PLAIN].concat();
assert_eq!(classify(&refused), Graphics::HalfBlock);
let other = [&b"\x1b_Gi=7;OK\x1b\\"[..], DA1_PLAIN].concat();
assert_eq!(classify(&other), Graphics::HalfBlock);
}
#[test]
fn garbage_and_silence_mean_half_blocks() {
assert_eq!(classify(b""), Graphics::HalfBlock);
assert_eq!(classify(b"hello \x1b[?62;4"), Graphics::HalfBlock, "an unfinished answer");
assert_eq!(classify(b"\x1b_Gi=31;OK"), Graphics::HalfBlock, "an unterminated kitty answer");
assert_eq!(classify(b"\x1b[?6x4c\x1b\x1b_G;"), Graphics::HalfBlock);
assert_eq!(classify(&[0xff, 0x1b, b'[', b'?', 0xfe]), Graphics::HalfBlock);
}
#[test]
fn the_attributes_mark_the_end_of_the_answers() {
assert!(!answered(b""));
assert!(!answered(KITTY_OK), "the kitty answer comes first; the attributes are still due");
assert!(!answered(b"\x1b[?62;4"));
assert!(answered(&[KITTY_OK, DA1_PLAIN].concat()));
assert!(answered(DA1_SIXEL));
}
#[test]
fn a_multiplexer_turns_kitty_and_sixel_into_half_blocks() {
for variable in ["TMUX", "STY"] {
let lookup = |name: &str| (name == variable).then(|| "/tmp/tmux-1000/default,1234,0".to_owned());
let (mut facts, unknown) = GraphicsFacts::detect(lookup);
assert!(facts.multiplexed && unknown.is_none(), "{variable}");
for answer in [Graphics::Kitty, Graphics::Sixel, Graphics::HalfBlock] {
facts.answer = answer;
let graphics = facts.resolve(ColorDepth::TrueColor, GlyphMode::Unicode);
assert_eq!(graphics, Graphics::HalfBlock, "{variable} with {answer:?}");
}
assert!(!facts.worth_asking(ColorDepth::TrueColor), "a multiplexer needs no question");
}
let empty = |name: &str| (name == "TMUX").then(String::new);
assert!(!GraphicsFacts::detect(empty).0.multiplexed, "an empty variable is unset");
}
#[test]
fn outside_a_multiplexer_the_answer_decides() {
let (mut facts, _) = GraphicsFacts::detect(|_| None);
for answer in [Graphics::Kitty, Graphics::Sixel, Graphics::HalfBlock] {
facts.answer = answer;
assert_eq!(facts.resolve(ColorDepth::TrueColor, GlyphMode::Unicode), answer);
assert_eq!(facts.resolve(ColorDepth::Ansi256, GlyphMode::Nerd), answer);
}
assert!(facts.worth_asking(ColorDepth::Ansi256));
}
#[test]
fn sixteen_colours_and_ascii_show_no_picture() {
let facts = GraphicsFacts { answer: Graphics::Kitty, ..GraphicsFacts::default() };
assert_eq!(facts.resolve(ColorDepth::Ansi16, GlyphMode::Unicode), Graphics::None);
assert_eq!(facts.resolve(ColorDepth::TrueColor, GlyphMode::Ascii), Graphics::None);
assert!(!facts.worth_asking(ColorDepth::Ansi16), "16 colours never change while it runs");
assert!(facts.worth_asking(ColorDepth::TrueColor), "ASCII glyphs can be switched off while it runs");
}
#[test]
fn the_variable_wins_over_everything() {
for forced in Graphics::ALL {
let lookup = |name: &str| match name {
VARIABLE => Some(format!(" {} ", forced.name().to_uppercase())),
"TMUX" => Some("/tmp/tmux".to_owned()),
_ => None,
};
let (mut facts, unknown) = GraphicsFacts::detect(lookup);
assert_eq!((facts.forced, unknown), (Some(forced), None));
facts.answer = Graphics::Sixel;
assert_eq!(facts.resolve(ColorDepth::Ansi16, GlyphMode::Ascii), forced, "{forced:?}");
assert_eq!(facts.resolve(ColorDepth::TrueColor, GlyphMode::Unicode), forced, "{forced:?}");
assert!(!facts.worth_asking(ColorDepth::TrueColor), "the variable already decided");
}
}
#[test]
fn an_unknown_name_is_reported_and_ignored() {
let lookup = |name: &str| (name == VARIABLE).then(|| "pixels".to_owned());
let (facts, unknown) = GraphicsFacts::detect(lookup);
assert_eq!(facts.forced, None);
assert_eq!(unknown.as_deref(), Some("pixels"));
}
#[test]
fn names_read_back() {
for graphics in Graphics::ALL {
assert_eq!(Graphics::from_name(graphics.name()), Some(graphics));
}
assert_eq!(Graphics::from_name("half-block"), None);
}
}