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
#![allow(dead_code)]
use crate::config::ColorMode;
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()
}
/// 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 {
// 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;
}
// Check FORCE_COLOR for CI environments that support colors
if std::env::var("FORCE_COLOR").is_ok() {
return true;
}
// Default: use colors for TTY
true
}
/// Get terminal width for word-wrapping, with fallback to default width
pub fn get_terminal_width() -> usize {
if let Some((terminal_size::Width(width), _)) = terminal_size::terminal_size() {
width as usize
} else {
100 // Default fallback width as requested
}
}