Skip to main content

rpc/chart/
backend.rs

1//! Which graphics protocol a terminal can draw charts with.
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum ChartBackend {
5    Kitty,
6    Halfblocks,
7}
8
9#[must_use]
10pub fn term_speaks_kitty(term: &str) -> bool {
11    let term = term.to_ascii_lowercase();
12    term.contains("kitty") || term.contains("ghostty")
13}
14
15/// Terminal emulators announce themselves through env vars; `env` looks one up.
16#[must_use]
17pub fn env_speaks_kitty(env: impl Fn(&str) -> Option<String>) -> bool {
18    env("KITTY_WINDOW_ID").is_some()
19        || ["TERM", "TERM_PROGRAM"]
20            .iter()
21            .filter_map(|name| env(name))
22            .any(|value| term_speaks_kitty(&value))
23        || env("TERM_PROGRAM").is_some_and(|value| value.eq_ignore_ascii_case("WezTerm"))
24}
25
26/// `override_` is `kitty` or `halfblocks`; any other value is ignored.
27#[must_use]
28pub fn chart_backend(term: Option<&str>, override_: Option<&str>) -> ChartBackend {
29    match override_.map(str::to_ascii_lowercase).as_deref() {
30        Some("kitty") => ChartBackend::Kitty,
31        Some("halfblocks") => ChartBackend::Halfblocks,
32        _ if term.is_some_and(term_speaks_kitty) => ChartBackend::Kitty,
33        _ => ChartBackend::Halfblocks,
34    }
35}