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
//! Progress suppression decision logic for `apr pull` (CRUX-A-19).
//!
//! Contract: `contracts/crux-A-19-v1.yaml`.
//!
//! Pure decision function — takes the observed TTY-ness of stderr, the
//! parsed `--quiet` flag, and a slice of environment variables, and
//! returns whether a progress bar should be emitted. No I/O.
//!
//! The actual progress-bar rendering, the live TTY check against
//! `stderr`, and the tqdm-parity regex match on emitted lines are all
//! discharged by a separate TTY-gated harness (follow-up).
/// Environment variable that force-disables progress output (HF-parity
/// to `HF_HUB_DISABLE_PROGRESS_BARS`). aprender uses `APR_PROGRESS=0`
/// per the CRUX-A-19 contract body.
pub const PROGRESS_OFF_ENV: &str = "APR_PROGRESS";
/// Return true iff the raw env value is a "falsy / disable" signal.
/// Accept the same taxonomy as CRUX-A-20: "0", "false", "no", "off"
/// disable progress; "1", "true", "yes", "on" (or absent) leave it on.
fn env_disables_progress(value: &str) -> bool {
matches!(
value.trim().to_ascii_lowercase().as_str(),
"0" | "false" | "no" | "off"
)
}
/// Decide whether a progress bar should be emitted.
///
/// Suppress progress iff ANY of:
/// - stderr is not a TTY (piped / redirected), OR
/// - `--quiet` flag is set, OR
/// - `APR_PROGRESS=0` (or equivalent falsy env value) is set.
///
/// Otherwise emit progress. This is the CRUX-A-19 `progress_suppression`
/// equation stated as a pure function.
///
/// The environment is passed in explicitly (rather than reading
/// `std::env`) so the decision is deterministic and unit-testable
/// without mutating process-global state.
pub fn progress_enabled<'a, I>(stderr_is_tty: bool, quiet_flag: bool, env: I) -> bool
where
I: IntoIterator<Item = (&'a str, &'a str)>,
{
if !stderr_is_tty {
return false;
}
if quiet_flag {
return false;
}
for (k, v) in env {
if k == PROGRESS_OFF_ENV && env_disables_progress(v) {
return false;
}
}
true
}
/// Convenience wrapper reading the real process env. Thin layer so
/// callers don't sprinkle `std::env::var` across the codebase.
pub fn read_progress_env() -> Vec<(String, String)> {
std::env::var(PROGRESS_OFF_ENV)
.ok()
.map(|v| vec![(PROGRESS_OFF_ENV.to_string(), v)])
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tty_no_quiet_no_env_emits_progress() {
assert!(progress_enabled(
true,
false,
std::iter::empty::<(&str, &str)>()
));
}
#[test]
fn non_tty_always_suppresses() {
assert!(!progress_enabled(
false,
false,
std::iter::empty::<(&str, &str)>()
));
assert!(!progress_enabled(
false,
true,
std::iter::empty::<(&str, &str)>()
));
assert!(!progress_enabled(false, false, [("APR_PROGRESS", "1")]));
}
#[test]
fn quiet_flag_suppresses_on_tty() {
assert!(!progress_enabled(
true,
true,
std::iter::empty::<(&str, &str)>()
));
}
#[test]
fn apr_progress_zero_suppresses_on_tty() {
assert!(!progress_enabled(true, false, [("APR_PROGRESS", "0")]));
}
#[test]
fn apr_progress_one_leaves_progress_on() {
assert!(progress_enabled(true, false, [("APR_PROGRESS", "1")]));
}
#[test]
fn apr_progress_missing_leaves_progress_on() {
assert!(progress_enabled(true, false, [("SOME_OTHER", "0")]));
}
#[test]
fn falsy_env_variants_all_suppress() {
for v in ["0", "false", "FALSE", "no", "off", " off "] {
assert!(
!progress_enabled(true, false, [("APR_PROGRESS", v)]),
"APR_PROGRESS={v:?} must suppress",
);
}
}
#[test]
fn truthy_env_variants_leave_progress_on() {
for v in ["1", "true", "yes", "on", ""] {
assert!(
progress_enabled(true, false, [("APR_PROGRESS", v)]),
"APR_PROGRESS={v:?} must leave progress on",
);
}
}
#[test]
fn three_suppression_signals_observationally_equivalent() {
// Signal 1: non-TTY
let a = progress_enabled(false, false, std::iter::empty::<(&str, &str)>());
// Signal 2: --quiet
let b = progress_enabled(true, true, std::iter::empty::<(&str, &str)>());
// Signal 3: APR_PROGRESS=0
let c = progress_enabled(true, false, [("APR_PROGRESS", "0")]);
assert_eq!(a, b);
assert_eq!(b, c);
assert!(!a, "all three signals must produce false");
}
#[test]
fn unrelated_env_var_ignored() {
assert!(progress_enabled(
true,
false,
[("HF_HUB_DISABLE_PROGRESS_BARS", "1")]
));
}
#[test]
fn quiet_overrides_truthy_env() {
assert!(!progress_enabled(true, true, [("APR_PROGRESS", "1")]));
}
#[test]
fn is_deterministic() {
let a = progress_enabled(true, false, [("APR_PROGRESS", "0")]);
let b = progress_enabled(true, false, [("APR_PROGRESS", "0")]);
assert_eq!(a, b);
}
}