1
//! Which graphics protocol a terminal can draw charts with.
2

            
3
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4
pub enum ChartBackend {
5
    Kitty,
6
    Halfblocks,
7
}
8

            
9
#[must_use]
10
38579
pub fn term_speaks_kitty(term: &str) -> bool {
11
38579
    let term = term.to_ascii_lowercase();
12
38579
    term.contains("kitty") || term.contains("ghostty")
13
38579
}
14

            
15
/// Terminal emulators announce themselves through env vars; `env` looks one up.
16
#[must_use]
17
17
pub fn env_speaks_kitty(env: impl Fn(&str) -> Option<String>) -> bool {
18
17
    env("KITTY_WINDOW_ID").is_some()
19
14
        || ["TERM", "TERM_PROGRAM"]
20
14
            .iter()
21
25
            .filter_map(|name| env(name))
22
14
            .any(|value| term_speaks_kitty(&value))
23
8
        || env("TERM_PROGRAM").is_some_and(|value| value.eq_ignore_ascii_case("WezTerm"))
24
17
}
25

            
26
/// `override_` is `kitty` or `halfblocks`; any other value is ignored.
27
#[must_use]
28
62411
pub fn chart_backend(term: Option<&str>, override_: Option<&str>) -> ChartBackend {
29
62411
    match override_.map(str::to_ascii_lowercase).as_deref() {
30
1762
        Some("kitty") => ChartBackend::Kitty,
31
1601
        Some("halfblocks") => ChartBackend::Halfblocks,
32
59048
        _ if term.is_some_and(term_speaks_kitty) => ChartBackend::Kitty,
33
21123
        _ => ChartBackend::Halfblocks,
34
    }
35
62411
}