1use crate::core::security_events::SecurityCounts;
6use crate::core::wrapped::format_tokens;
7
8use super::snapshot::ValueSnapshot;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct Style {
15 pub color: bool,
16 pub unicode: bool,
17}
18
19impl Style {
20 pub const PLAIN: Self = Self {
21 color: false,
22 unicode: true,
23 };
24
25 pub fn from_env() -> Self {
28 Self {
29 color: std::env::var_os("NO_COLOR").is_none(),
30 unicode: std::env::var_os("LEAN_CTX_ASCII").is_none(),
31 }
32 }
33
34 pub(crate) fn mark(self) -> &'static str {
35 if self.unicode { "◆" } else { "*" }
36 }
37
38 pub(crate) fn shield(self) -> &'static str {
39 if self.unicode { "⛨" } else { "sec" }
40 }
41
42 pub(crate) fn minus(self) -> &'static str {
43 if self.unicode { "−" } else { "-" }
44 }
45
46 pub(crate) fn sep(self) -> &'static str {
47 if self.unicode { " · " } else { " | " }
48 }
49}
50
51pub fn one_line(snap: &ValueSnapshot, style: Style) -> Option<String> {
54 if snap.is_empty() {
55 return None;
56 }
57 let mut parts = Vec::new();
58 if snap.tokens_saved > 0 {
59 parts.push(format!(
60 "{}{} tok",
61 style.minus(),
62 format_tokens(snap.tokens_saved)
63 ));
64 }
65 if snap.cache_hits > 0 {
66 parts.push(format!("{} cached", snap.cache_hits));
67 }
68 let security = snap.security.total();
69 if security > 0 {
70 parts.push(format!("{} {security}", style.shield()));
71 }
72 let body = format!("{} lean-ctx {}", style.mark(), parts.join(style.sep()));
73 Some(if style.color {
74 format!("\x1b[2m{body}\x1b[0m")
75 } else {
76 body
77 })
78}
79
80pub fn compact(snap: &ValueSnapshot, style: Style) -> Option<String> {
83 if snap.is_empty() {
84 return None;
85 }
86 let mut out = style.mark().to_string();
87 if snap.tokens_saved > 0 {
88 out.push_str(&format!(
89 " {}{} tok",
90 style.minus(),
91 format_tokens(snap.tokens_saved)
92 ));
93 }
94 let security = snap.security.total();
95 if security > 0 {
96 out.push_str(&format!(" {} {security}", style.shield()));
97 }
98 (out.len() > style.mark().len()).then_some(out)
100}
101
102fn plural(n: u64, one: &str, many: &str) -> String {
103 format!("{n} {}", if n == 1 { one } else { many })
104}
105
106pub fn security_phrases(counts: &SecurityCounts) -> Vec<String> {
109 let mut out = Vec::new();
110 if counts.secrets_redacted > 0 {
111 out.push(format!(
112 "{} kept out of context",
113 plural(counts.secrets_redacted, "secret", "secrets")
114 ));
115 }
116 if counts.shell_blocked > 0 {
117 out.push(format!(
118 "{} blocked",
119 plural(counts.shell_blocked, "risky command", "risky commands")
120 ));
121 }
122 if counts.path_blocked > 0 {
123 out.push(format!(
124 "{} outside the project blocked",
125 plural(counts.path_blocked, "path", "paths")
126 ));
127 }
128 if counts.injection_flagged > 0 {
129 out.push(format!(
130 "{} flagged",
131 plural(
132 counts.injection_flagged,
133 "prompt-injection pattern",
134 "prompt-injection patterns"
135 )
136 ));
137 }
138 if counts.pii_redacted > 0 {
139 out.push(format!(
140 "{} kept out of context",
141 plural(
142 counts.pii_redacted,
143 "personal data value",
144 "personal data values"
145 )
146 ));
147 }
148 if counts.content_withheld > 0 {
149 out.push(format!(
150 "{} withheld",
151 plural(counts.content_withheld, "source", "sources")
152 ));
153 }
154 if counts.coverage_incomplete > 0 {
155 out.push(format!(
156 "{} not fully inspected",
157 plural(counts.coverage_incomplete, "source", "sources")
158 ));
159 }
160 out
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 fn snap(saved: u64, cache: u64, secrets: u64) -> ValueSnapshot {
168 let mut s = ValueSnapshot {
169 tokens_saved: saved,
170 tokens_input: saved * 2,
171 cache_hits: cache,
172 ..ValueSnapshot::default()
173 };
174 s.security.secrets_redacted = secrets;
175 s
176 }
177
178 #[test]
179 fn nothing_measured_renders_nothing() {
180 assert_eq!(one_line(&ValueSnapshot::default(), Style::PLAIN), None);
181 }
182
183 #[test]
184 fn plain_unicode_line() {
185 assert_eq!(
186 one_line(&snap(1_200_000, 41, 3), Style::PLAIN).unwrap(),
187 "◆ lean-ctx −1.2M tok · 41 cached · ⛨ 3"
188 );
189 }
190
191 #[test]
192 fn ascii_fallback_has_no_multibyte_glyphs() {
193 let style = Style {
194 color: false,
195 unicode: false,
196 };
197 let line = one_line(&snap(312_000, 0, 1), style).unwrap();
198 assert!(line.is_ascii(), "{line}");
199 assert_eq!(line, "* lean-ctx -312.0K tok | sec 1");
200 }
201
202 #[test]
203 fn color_is_dim_and_reset() {
204 let style = Style {
205 color: true,
206 unicode: true,
207 };
208 let line = one_line(&snap(5_000, 0, 0), style).unwrap();
209 assert!(line.starts_with("\x1b[2m") && line.ends_with("\x1b[0m"));
210 }
211
212 #[test]
213 fn compact_is_short_and_uncoloured() {
214 let style = Style {
215 color: true,
216 unicode: true,
217 };
218 assert_eq!(
219 compact(&snap(1_200_000, 41, 3), style).unwrap(),
220 "◆ −1.2M tok ⛨ 3"
221 );
222 let ascii = Style {
223 color: false,
224 unicode: false,
225 };
226 assert_eq!(compact(&snap(0, 0, 2), ascii).unwrap(), "* sec 2");
227 assert_eq!(compact(&snap(0, 9, 0), Style::PLAIN), None);
228 assert_eq!(compact(&ValueSnapshot::default(), Style::PLAIN), None);
229 }
230
231 #[test]
232 fn security_only_session_still_shows() {
233 let line = one_line(&snap(0, 0, 2), Style::PLAIN).unwrap();
234 assert_eq!(line, "◆ lean-ctx ⛨ 2");
235 }
236
237 #[test]
238 fn phrases_never_claim_neutralization() {
239 let counts = SecurityCounts {
240 secrets_redacted: 1,
241 shell_blocked: 2,
242 path_blocked: 1,
243 injection_flagged: 3,
244 ..SecurityCounts::ZERO
245 };
246 let text = security_phrases(&counts).join(", ");
247 assert_eq!(
248 text,
249 "1 secret kept out of context, 2 risky commands blocked, \
250 1 path outside the project blocked, 3 prompt-injection patterns flagged"
251 );
252 assert!(!text.contains("neutraliz"));
253 }
254
255 #[test]
256 fn gateway_phrases_say_what_happened_without_overclaiming() {
257 let counts = SecurityCounts {
258 pii_redacted: 2,
259 content_withheld: 1,
260 coverage_incomplete: 1,
261 ..SecurityCounts::ZERO
262 };
263 assert_eq!(
264 security_phrases(&counts).join(", "),
265 "2 personal data values kept out of context, 1 source withheld, \
266 1 source not fully inspected"
267 );
268 }
269}