#[link(name = "ncurses")]
extern "C" {
fn setupterm(
term: *const libc::c_char,
filedes: libc::c_int,
errret: *mut libc::c_int,
) -> libc::c_int;
fn tigetstr(capname: *const libc::c_char) -> *const libc::c_char;
fn tigetnum(capname: *const libc::c_char) -> libc::c_int;
fn tigetflag(capname: *const libc::c_char) -> libc::c_int;
}
fn ensure_initialized() -> bool {
use std::sync::OnceLock;
static INITIALIZED: OnceLock<bool> = OnceLock::new();
*INITIALIZED.get_or_init(|| {
let mut errret: libc::c_int = 0;
unsafe { setupterm(std::ptr::null(), 1, &mut errret) == 0 }
})
}
pub fn lookup(name: &str) -> Option<String> {
if !ensure_initialized() {
return None;
}
let cname = std::ffi::CString::new(name).ok()?;
unsafe {
let s = tigetstr(cname.as_ptr());
let s_addr = s as isize;
if !s.is_null() && s_addr != -1 {
let bytes = std::ffi::CStr::from_ptr(s).to_bytes();
return Some(String::from_utf8_lossy(bytes).into_owned());
}
let n = tigetnum(cname.as_ptr());
if n >= 0 {
return Some(n.to_string());
}
let b = tigetflag(cname.as_ptr());
if b == 0 || b == 1 {
return Some(if b == 1 { "yes".to_string() } else { "no".to_string() });
}
}
None
}
pub const COMMON_STRING_CAPS: &[&str] = &[
"kf1", "kf2", "kf3", "kf4", "kf5", "kf6", "kf7", "kf8", "kf9", "kf10",
"kf11", "kf12", "kf13", "kf14", "kf15", "kf16", "kf17", "kf18", "kf19",
"kf20",
"kcuu1", "kcud1", "kcuf1", "kcub1",
"khome", "kend", "kpp", "knp",
"kbs", "kich1", "kdch1",
"clear", "ed", "el", "home", "civis", "cnorm",
"smso", "rmso", "smul", "rmul", "bold", "rev", "sgr0",
"smkx", "rmkx", "smcup", "rmcup", "setaf", "setab",
"cup", "ich1", "dch1", "il1", "dl1",
];