Skip to main content

cargo_plugin_utils/
tty.rs

1//! TTY detection utilities for respecting cargo's progress settings.
2
3/// Check if progress should be shown based on cargo's term.progress.when
4/// setting (respects CARGO_TERM_PROGRESS_WHEN environment variable).
5///
6/// Returns `true` if progress should be shown, `false` otherwise.
7///
8/// # Values
9///
10/// - `"never"` - Never show progress
11/// - `"always"` - Always show progress
12/// - `"auto"` (default) - Show if stdout is a TTY (interactive terminal)
13///
14/// # Examples
15///
16/// ```no_run
17/// use cargo_plugin_utils::should_show_progress;
18///
19/// if should_show_progress() {
20///     // Show progress bar
21/// }
22/// ```
23#[allow(clippy::disallowed_methods)] // CLI tool needs direct env access
24pub fn should_show_progress() -> bool {
25    // Respect cargo's term.progress.when setting
26    // Values: "auto" (default), "always", "never"
27    match std::env::var("CARGO_TERM_PROGRESS_WHEN")
28        .as_deref()
29        .unwrap_or("auto")
30    {
31        "never" => false,
32        "always" => true,
33        "auto" => {
34            // Auto: show if stdout is a TTY (interactive terminal)
35            atty::is(atty::Stream::Stdout)
36        }
37        _ => {
38            // Default to auto behavior for unknown values
39            atty::is(atty::Stream::Stdout)
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_should_show_progress() {
50        // Should not panic
51        let _ = should_show_progress();
52    }
53}