1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! # Terminal Integration System
//!
//! This module provides comprehensive terminal interaction capabilities for the boxen library,
//! enabling intelligent terminal size detection, dimension calculations, and adaptive rendering
//! that works seamlessly across different terminal environments and platforms.
//!
//! ## Overview
//!
//! The terminal system handles the complex task of interfacing with various terminal emulators
//! and environments to provide accurate dimension information and optimal rendering parameters.
//! It abstracts away platform-specific differences and provides a consistent API for terminal
//! operations.
//!
//! ## Core Capabilities
//!
//! ### Terminal Size Detection
//! - **Automatic Detection**: Intelligently detects current terminal dimensions
//! - **Fallback Handling**: Provides sensible defaults when detection fails
//! - **Cross-Platform Support**: Works on Windows, macOS, Linux, and other Unix-like systems
//! - **Environment Variable Support**: Respects `COLUMNS` and `LINES` environment variables
//!
//! ### Dimension Calculations
//! - **Border Width Calculation**: Determines space required for different border styles
//! - **Content Area Sizing**: Calculates available space for text content
//! - **Padding and Margin Integration**: Accounts for spacing in dimension calculations
//! - **Constraint Validation**: Ensures dimensions fit within terminal bounds
//!
//! ## Quick Start
//!
//! ```rust
//! use ::boxen::terminal::{get_terminal_width, get_terminal_height, calculate_border_width};
//! use ::boxen::BorderStyle;
//!
//! // Get current terminal dimensions
//! let width = get_terminal_width();
//! let height = get_terminal_height().unwrap_or(24);
//! println!("Terminal: {}x{}", width, height);
//!
//! // Calculate space needed for borders
//! let border_width = calculate_border_width(&BorderStyle::Double);
//! let content_width = width.saturating_sub(border_width);
//! ```
//!
//! ## Terminal Size Detection
//!
//! The terminal size detection system uses multiple strategies to determine dimensions:
//!
//! ### Detection Priority
//! 1. **Direct Terminal Query**: Uses platform-specific APIs to query terminal size
//! 2. **Environment Variables**: Falls back to `COLUMNS` and `LINES` variables
//! 3. **Sensible Defaults**: Uses 80x24 as final fallback for compatibility
//!
//! ### Platform Support
//! - **Unix/Linux**: Uses `ioctl` with `TIOCGWINSZ` for accurate detection
//! - **Windows**: Leverages Windows Console API for dimension queries
//! - **Cross-Platform**: Graceful degradation ensures functionality everywhere
//!
//! ```rust
//! use ::boxen::terminal::{get_terminal_width, get_terminal_height};
//!
//! // Safe terminal size detection
//! let width = get_terminal_width();
//! let height = get_terminal_height().unwrap_or(24);
//!
//! // Use dimensions for layout calculations
//! let max_box_width = width.saturating_sub(4); // Leave margin
//! ```
//!
//! ## Dimension Calculations
//!
//! The dimension calculation system provides utilities for determining space requirements:
//!
//! ### Border Width Calculation
//! Different border styles require different amounts of horizontal space:
//!
//! ```rust
//! use ::boxen::terminal::calculate_border_width;
//! use ::boxen::BorderStyle;
//!
//! // Calculate border overhead for different styles
//! let single_width = calculate_border_width(&BorderStyle::Single); // 2 chars
//! let double_width = calculate_border_width(&BorderStyle::Double); // 2 chars
//! let bold_width = calculate_border_width(&BorderStyle::Bold); // 2 chars
//! ```
//!
//! ### Content Area Calculation
//! Determine available space for text content after accounting for borders and spacing:
//!
//! ```rust
//! use ::boxen::terminal::{get_terminal_width, calculate_border_width};
//! use ::boxen::{BorderStyle, Spacing};
//!
//! let terminal_width = get_terminal_width();
//! let border_width = calculate_border_width(&BorderStyle::Single);
//! let padding = Spacing::from(2); // 2 units on each side
//!
//! // Calculate available content width
//! let content_width = terminal_width
//! .saturating_sub(border_width)
//! .saturating_sub(padding.left + padding.right);
//! ```
//!
//! ## Error Handling and Fallbacks
//!
//! The terminal system is designed to be robust and handle various failure scenarios:
//!
//! ### Detection Failures
//! - **No Terminal**: When running in non-interactive environments
//! - **Permission Issues**: When terminal access is restricted
//! - **Platform Limitations**: When APIs are unavailable
//!
//! ### Graceful Degradation
//! ```rust
//! use ::boxen::terminal::get_terminal_width;
//!
//! // Always provides a usable width
//! let width = get_terminal_width();
//!
//! // Safe for all environments
//! assert!(width > 0);
//! ```
//!
//! ## Integration with Boxen Options
//!
//! The terminal system integrates seamlessly with boxen's configuration system:
//!
//! ### Automatic Sizing
//! When no explicit dimensions are provided, boxen uses terminal detection:
//!
//! ```rust
//! use ::boxen::builder;
//!
//! // Automatically sizes to terminal width
//! let result = builder()
//! .render("Content adapts to terminal size")
//! .unwrap();
//! ```
//!
//! ### Constraint Validation
//! Terminal dimensions are used to validate configuration options:
//!
//! ```rust
//! use ::boxen::BoxenBuilder;
//!
//! // Width is validated against terminal size
//! let result = BoxenBuilder::new()
//! .width(60) // Reasonable width that fits in most terminals
//! .render("Constrained content")
//! .unwrap();
//! ```
//!
//! ## Performance Considerations
//!
//! - **Caching**: Terminal size is cached to avoid repeated system calls
//! - **Lazy Detection**: Size detection only occurs when needed
//! - **Minimal Overhead**: Calculations are optimized for common cases
//! - **No Blocking**: All operations are non-blocking and fast
//!
//! ## Thread Safety
//!
//! All terminal operations are thread-safe and can be used in concurrent environments:
//!
//! ```rust
//! use std::thread;
//! use ::boxen::terminal::get_terminal_width;
//!
//! // Safe to call from multiple threads
//! let handles: Vec<_> = (0..4)
//! .map(|_| thread::spawn(|| get_terminal_width()))
//! .collect();
//! ```
//!
//! ## Testing and Development
//!
//! The terminal system provides utilities for testing and development:
//!
//! ### Environment Variable Override
//! ```bash
//! # Override terminal size for testing
//! COLUMNS=100 LINES=30 cargo test
//! ```
//!
//! ### Predictable Behavior
//! When environment variables are set, the system uses those values consistently,
//! making testing and development predictable across different environments.
/// Terminal size caching for performance
pub use *;
pub use cached_terminal_size;
pub use ;