pub use vtcode_commons::tty::TtyExt;
use std::io;
use std::io::IsTerminal;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TtyCapabilities {
color: bool,
cursor: bool,
bracketed_paste: bool,
focus_events: bool,
mouse: bool,
keyboard_enhancement: bool,
}
impl TtyCapabilities {
pub(crate) fn detect() -> Option<Self> {
let stderr = io::stderr();
if !stderr.is_terminal() {
return None;
}
Some(Self {
color: stderr.supports_color(),
cursor: true, bracketed_paste: true, focus_events: true, mouse: true, keyboard_enhancement: true, })
}
pub fn is_fully_featured(&self) -> bool {
self.color
&& self.cursor
&& self.bracketed_paste
&& self.focus_events
&& self.mouse
&& self.keyboard_enhancement
}
pub(crate) fn is_basic_tui(&self) -> bool {
self.color && self.cursor
}
}
fn is_interactive_session() -> bool {
io::stderr().is_terminal() && io::stdin().is_terminal()
}
pub fn terminal_size() -> Option<(u16, u16)> {
crossterm::terminal::size().ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tty_detection() {
let stdout = io::stdout();
let stderr = io::stderr();
let stdin = io::stdin();
let _ = stdout.is_terminal();
let _ = stderr.is_terminal();
let _ = stdin.is_terminal();
}
#[test]
fn test_capabilities_detection() {
let caps = TtyCapabilities::detect();
let _ = caps.is_some() || caps.is_none();
}
#[test]
fn test_interactive_session() {
let _ = is_interactive_session();
}
}