use std::env;
use std::process::Command;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Features {
pub sixel: bool,
pub hyperlinks: bool,
}
pub fn features() -> Option<Features> {
let pane = env::var("TMUX_PANE").ok()?;
env::var_os("TMUX")?;
let out = Command::new("tmux")
.args([
"display-message",
"-p",
"-t",
&pane,
"#{client_termfeatures}",
])
.output()
.ok()?;
if !out.status.success() {
return None;
}
Some(parse(&String::from_utf8_lossy(&out.stdout)))
}
pub fn parse(list: &str) -> Features {
let mut features = Features::default();
for name in list.trim().split(',') {
match name.trim() {
"sixel" => features.sixel = true,
"hyperlinks" => features.hyperlinks = true,
_ => {}
}
}
features
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_the_features_we_care_about() {
let f = parse("bpaste,ccolour,clipboard,hyperlinks,cstyle,focus,RGB,sixel,title\n");
assert_eq!(
f,
Features {
sixel: true,
hyperlinks: true
}
);
}
#[test]
fn a_terminal_without_them_is_not_promoted() {
let f = parse("bpaste,ccolour,clipboard,cstyle,focus,RGB,title\n");
assert_eq!(f, Features::default());
}
#[test]
fn nothing_at_all_is_not_a_yes() {
assert_eq!(parse(""), Features::default());
assert_eq!(parse("\n"), Features::default());
}
#[test]
fn a_feature_is_matched_whole() {
assert_eq!(parse("nosixel,hyperlinksextra"), Features::default());
}
}