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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use crate::config::{ColorMode, EmojiMode};
use std::io::IsTerminal;
/// Check if stdout is connected to a TTY
pub fn is_stdout_tty() -> bool {
std::io::stdout().is_terminal()
}
/// Check if stdin is connected to a TTY
pub fn is_stdin_tty() -> bool {
std::io::stdin().is_terminal()
}
/// Check if stderr is connected to a TTY
pub fn is_stderr_tty() -> bool {
std::io::stderr().is_terminal()
}
/// Determine if colors should be used based on CLI color mode and environment
pub fn should_use_colors_with_mode(color_mode: &ColorMode) -> bool {
match color_mode {
ColorMode::Never => false,
ColorMode::Always => {
// --force-color should override NO_COLOR environment variable
true
}
ColorMode::Auto => should_use_colors_auto(),
}
}
/// Auto color detection logic
fn should_use_colors_auto() -> bool {
// Check FORCE_COLOR first for CI environments that support colors
if std::env::var("FORCE_COLOR").is_ok() {
return true;
}
// Don't use colors if not on TTY
if !is_stdout_tty() {
return false;
}
// Respect NO_COLOR environment variable (https://no-color.org/)
if std::env::var("NO_COLOR").is_ok() {
return false;
}
// Default: use colors for TTY
true
}
/// Auto color detection for stderr messages (errors/warnings)
pub fn should_use_colors_for_stderr() -> bool {
// Check FORCE_COLOR first for CI environments that support colors
if std::env::var("FORCE_COLOR").is_ok() {
return true;
}
// Don't use colors if stderr not on TTY
if !is_stderr_tty() {
return false;
}
// Respect NO_COLOR environment variable (https://no-color.org/)
if std::env::var("NO_COLOR").is_ok() {
return false;
}
// Default: use colors for TTY
true
}
/// Determine if emoji should be used based on emoji mode, color settings, and environment
pub fn should_use_emoji_with_mode(emoji_mode: &EmojiMode, color_mode: &ColorMode) -> bool {
match emoji_mode {
EmojiMode::Never => false,
EmojiMode::Always => true,
EmojiMode::Auto => {
// Emoji requires colors to be enabled
let use_colors = should_use_colors_with_mode(color_mode);
if !use_colors {
return false;
}
// Check NO_EMOJI environment variable
if std::env::var("NO_EMOJI").is_ok() {
return false;
}
// Default: use emoji when colors are enabled
true
}
}
}
/// Auto emoji detection for stderr messages (based on color detection)
pub fn should_use_emoji_for_stderr() -> bool {
// Emoji requires colors to be enabled
let use_colors = should_use_colors_for_stderr();
if !use_colors {
return false;
}
// Check NO_EMOJI environment variable
if std::env::var("NO_EMOJI").is_ok() {
return false;
}
// Default: use emoji when colors are enabled
true
}
/// Get terminal width for word-wrapping, with fallback to default width
/// Checks in order: COLUMNS environment variable, terminal size detection, default (100)
pub fn get_terminal_width() -> usize {
// Priority 1: Check COLUMNS environment variable
if let Ok(columns_str) = std::env::var("COLUMNS") {
if let Ok(columns) = columns_str.parse::<usize>() {
if columns > 0 {
return columns;
}
}
}
// Priority 2: Detect terminal size
if let Some((terminal_size::Width(width), _)) = terminal_size::terminal_size() {
width as usize
} else {
// Priority 3: Default fallback width
100
}
}
#[cfg(test)]
mod tests {
use super::*;
use once_cell::sync::Lazy;
use std::sync::Mutex;
static ENV_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
struct EnvGuard {
vars: Vec<(&'static str, Option<String>)>,
}
impl EnvGuard {
fn new(keys: &[&'static str]) -> Self {
let vars = keys
.iter()
.map(|key| (*key, std::env::var(key).ok()))
.collect();
Self { vars }
}
}
impl Drop for EnvGuard {
fn drop(&mut self) {
for (key, value) in &self.vars {
if let Some(v) = value {
std::env::set_var(key, v);
} else {
std::env::remove_var(key);
}
}
}
}
fn with_env_lock<F: FnOnce()>(keys: &[&'static str], f: F) {
let _lock = ENV_LOCK.lock().unwrap();
let _guard = EnvGuard::new(keys);
f();
}
#[test]
fn color_mode_never_disables_colors() {
assert!(!should_use_colors_with_mode(&ColorMode::Never));
}
#[test]
fn color_mode_always_overrides_no_color_environment() {
with_env_lock(&["NO_COLOR"], || {
std::env::set_var("NO_COLOR", "1");
assert!(should_use_colors_with_mode(&ColorMode::Always));
});
}
#[test]
fn terminal_width_uses_columns_env_var() {
with_env_lock(&["COLUMNS"], || {
std::env::set_var("COLUMNS", "120");
assert_eq!(get_terminal_width(), 120);
});
}
#[test]
fn terminal_width_ignores_invalid_columns() {
with_env_lock(&["COLUMNS"], || {
// Non-numeric value should be ignored
std::env::set_var("COLUMNS", "invalid");
let width = get_terminal_width();
// Should fall back to terminal size detection or default (100)
// We can't assert the exact value as it depends on actual terminal size
assert!(width > 0);
});
}
#[test]
fn terminal_width_ignores_zero_columns() {
with_env_lock(&["COLUMNS"], || {
std::env::set_var("COLUMNS", "0");
let width = get_terminal_width();
// Should fall back to terminal size detection or default (100)
assert!(width > 0);
});
}
#[test]
fn terminal_width_fallback_when_columns_unset() {
with_env_lock(&["COLUMNS"], || {
std::env::remove_var("COLUMNS");
let width = get_terminal_width();
// Should use terminal size detection or default fallback
assert!(width > 0);
});
}
}