pub const fn is_bsd() -> bool {
cfg!(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))
}
pub const fn supports_raw_console_mouse() -> bool {
cfg!(any(
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))
}
pub fn is_console_environment() -> bool {
if let Ok(term) = std::env::var("TERM") {
if term == "linux" {
return true;
}
if term == "cons25" || term == "xterm" && is_bsd() {
if let Ok(tty) = std::env::var("TTY") {
if tty.starts_with("/dev/ttyv") || tty.starts_with("/dev/ttyC") {
return true;
}
}
}
if term == "wsvt25" || term == "vt220" {
return true;
}
}
let no_display = std::env::var("DISPLAY").is_err();
let no_ssh = std::env::var("SSH_CONNECTION").is_err();
let no_wayland = std::env::var("WAYLAND_DISPLAY").is_err();
if no_display && no_ssh && no_wayland && supports_raw_console_mouse() {
#[cfg(unix)]
{
use std::os::unix::io::AsRawFd;
let stdin = std::io::stdin();
let fd = stdin.as_raw_fd();
unsafe {
let name = libc::ttyname(fd);
if !name.is_null() {
let name_str = std::ffi::CStr::from_ptr(name).to_string_lossy();
if name_str.starts_with("/dev/tty") && !name_str.starts_with("/dev/ttyS") {
if let Some(num_str) = name_str.strip_prefix("/dev/tty") {
if num_str.chars().all(|c| c.is_ascii_digit()) {
return true;
}
}
}
if name_str.starts_with("/dev/ttyv") {
return true;
}
if name_str.starts_with("/dev/ttyC") {
return true;
}
}
}
}
}
false
}