vtcode_core/utils/tty.rs
1//! TTY detection and capability utilities using Rust's `IsTerminal` trait.
2//!
3//! The canonical [`TtyExt`] trait lives in [`vtcode_commons::tty`] and is
4//! re-exported here so existing `vtcode_core::utils::tty::*` import paths
5//! keep resolving. [`TtyCapabilities`], [`is_interactive_session`], and
6//! [`terminal_size()`] remain core-side helpers built on that trait.
7//!
8//! # Example
9//!
10//! ```rust
11//! use vtcode_core::utils::tty::TtyExt;
12//! use std::io;
13//!
14//! // Check if stdout is a TTY
15//! if io::stdout().is_tty_ext() {
16//! // Apply terminal-specific features
17//! }
18//! ```
19
20pub use vtcode_commons::tty::TtyExt;
21
22use std::io;
23use std::io::IsTerminal;
24
25/// TTY capabilities that can be queried for feature detection.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub struct TtyCapabilities {
28 /// Whether the terminal supports ANSI color codes.
29 pub color: bool,
30 /// Whether the terminal supports cursor movement and manipulation.
31 pub cursor: bool,
32 /// Whether the terminal supports bracketed paste mode.
33 pub bracketed_paste: bool,
34 /// Whether the terminal supports focus change events.
35 pub focus_events: bool,
36 /// Whether the terminal supports mouse input.
37 pub mouse: bool,
38 /// Whether the terminal supports keyboard enhancement flags.
39 pub keyboard_enhancement: bool,
40}
41
42impl TtyCapabilities {
43 /// Detect the capabilities of the current terminal.
44 ///
45 /// This function queries the terminal to determine which features
46 /// are available. It should be called once at application startup
47 /// and the results cached for later use.
48 ///
49 /// # Returns
50 ///
51 /// Returns `Some(TtyCapabilities)` if stderr is a TTY, otherwise `None`.
52 pub fn detect() -> Option<Self> {
53 let stderr = io::stderr();
54 if !stderr.is_terminal() {
55 return None;
56 }
57
58 Some(Self {
59 color: stderr.supports_color(),
60 cursor: true, // All TTYs support basic cursor movement
61 bracketed_paste: true, // Assume support, will fail gracefully if not
62 focus_events: true, // Assume support, will fail gracefully if not
63 mouse: true, // Assume support, will fail gracefully if not
64 keyboard_enhancement: true, // Assume support, will fail gracefully if not
65 })
66 }
67
68 /// Returns `true` if the terminal supports all advanced features.
69 pub fn is_fully_featured(&self) -> bool {
70 self.color
71 && self.cursor
72 && self.bracketed_paste
73 && self.focus_events
74 && self.mouse
75 && self.keyboard_enhancement
76 }
77
78 /// Returns `true` if the terminal supports basic TUI features.
79 pub fn is_basic_tui(&self) -> bool {
80 self.color && self.cursor
81 }
82}
83
84/// Check if the application is running in an interactive TTY context.
85///
86/// This is useful for deciding whether to use rich terminal features
87/// or fall back to plain text output.
88pub fn is_interactive_session() -> bool {
89 io::stderr().is_terminal() && io::stdin().is_terminal()
90}
91
92/// Get the current terminal dimensions.
93///
94/// Returns `Some((width, height))` if the terminal size can be determined,
95/// otherwise `None`.
96pub fn terminal_size() -> Option<(u16, u16)> {
97 terminal_size::terminal_size().map(|(terminal_size::Width(w), terminal_size::Height(h))| (w, h))
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 #[test]
105 fn test_tty_detection() {
106 // These tests verify the TTY detection logic works
107 // Note: In actual test environments, these may vary
108 let stdout = io::stdout();
109 let stderr = io::stderr();
110 let stdin = io::stdin();
111
112 // Just verify the methods don't panic
113 let _ = stdout.is_terminal();
114 let _ = stderr.is_terminal();
115 let _ = stdin.is_terminal();
116 }
117
118 #[test]
119 fn test_capabilities_detection() {
120 // Test that capability detection doesn't panic
121 let caps = TtyCapabilities::detect();
122 // In a test environment, this might be None
123 // Just verify the method works
124 let _ = caps.is_some() || caps.is_none();
125 }
126
127 #[test]
128 fn test_interactive_session() {
129 // Test interactive session detection
130 let _ = is_interactive_session();
131 }
132}