use crate::core::security_events::SecurityCounts;
use crate::core::wrapped::format_tokens;
use super::snapshot::ValueSnapshot;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Style {
pub color: bool,
pub unicode: bool,
}
impl Style {
pub const PLAIN: Self = Self {
color: false,
unicode: true,
};
pub fn from_env() -> Self {
Self {
color: std::env::var_os("NO_COLOR").is_none(),
unicode: std::env::var_os("LEAN_CTX_ASCII").is_none(),
}
}
pub(crate) fn mark(self) -> &'static str {
if self.unicode { "◆" } else { "*" }
}
pub(crate) fn shield(self) -> &'static str {
if self.unicode { "⛨" } else { "sec" }
}
pub(crate) fn minus(self) -> &'static str {
if self.unicode { "−" } else { "-" }
}
pub(crate) fn sep(self) -> &'static str {
if self.unicode { " · " } else { " | " }
}
}
pub fn one_line(snap: &ValueSnapshot, style: Style) -> Option<String> {
if snap.is_empty() {
return None;
}
let mut parts = Vec::new();
if snap.tokens_saved > 0 {
parts.push(format!(
"{}{} tok",
style.minus(),
format_tokens(snap.tokens_saved)
));
}
if snap.cache_hits > 0 {
parts.push(format!("{} cached", snap.cache_hits));
}
let security = snap.security.total();
if security > 0 {
parts.push(format!("{} {security}", style.shield()));
}
let body = format!("{} lean-ctx {}", style.mark(), parts.join(style.sep()));
Some(if style.color {
format!("\x1b[2m{body}\x1b[0m")
} else {
body
})
}
pub fn compact(snap: &ValueSnapshot, style: Style) -> Option<String> {
if snap.is_empty() {
return None;
}
let mut out = style.mark().to_string();
if snap.tokens_saved > 0 {
out.push_str(&format!(
" {}{} tok",
style.minus(),
format_tokens(snap.tokens_saved)
));
}
let security = snap.security.total();
if security > 0 {
out.push_str(&format!(" {} {security}", style.shield()));
}
(out.len() > style.mark().len()).then_some(out)
}
fn plural(n: u64, one: &str, many: &str) -> String {
format!("{n} {}", if n == 1 { one } else { many })
}
pub fn security_phrases(counts: &SecurityCounts) -> Vec<String> {
let mut out = Vec::new();
if counts.secrets_redacted > 0 {
out.push(format!(
"{} kept out of context",
plural(counts.secrets_redacted, "secret", "secrets")
));
}
if counts.shell_blocked > 0 {
out.push(format!(
"{} blocked",
plural(counts.shell_blocked, "risky command", "risky commands")
));
}
if counts.path_blocked > 0 {
out.push(format!(
"{} outside the project blocked",
plural(counts.path_blocked, "path", "paths")
));
}
if counts.injection_flagged > 0 {
out.push(format!(
"{} flagged",
plural(
counts.injection_flagged,
"prompt-injection pattern",
"prompt-injection patterns"
)
));
}
if counts.pii_redacted > 0 {
out.push(format!(
"{} kept out of context",
plural(
counts.pii_redacted,
"personal data value",
"personal data values"
)
));
}
if counts.content_withheld > 0 {
out.push(format!(
"{} withheld",
plural(counts.content_withheld, "source", "sources")
));
}
if counts.coverage_incomplete > 0 {
out.push(format!(
"{} not fully inspected",
plural(counts.coverage_incomplete, "source", "sources")
));
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn snap(saved: u64, cache: u64, secrets: u64) -> ValueSnapshot {
let mut s = ValueSnapshot {
tokens_saved: saved,
tokens_input: saved * 2,
cache_hits: cache,
..ValueSnapshot::default()
};
s.security.secrets_redacted = secrets;
s
}
#[test]
fn nothing_measured_renders_nothing() {
assert_eq!(one_line(&ValueSnapshot::default(), Style::PLAIN), None);
}
#[test]
fn plain_unicode_line() {
assert_eq!(
one_line(&snap(1_200_000, 41, 3), Style::PLAIN).unwrap(),
"◆ lean-ctx −1.2M tok · 41 cached · ⛨ 3"
);
}
#[test]
fn ascii_fallback_has_no_multibyte_glyphs() {
let style = Style {
color: false,
unicode: false,
};
let line = one_line(&snap(312_000, 0, 1), style).unwrap();
assert!(line.is_ascii(), "{line}");
assert_eq!(line, "* lean-ctx -312.0K tok | sec 1");
}
#[test]
fn color_is_dim_and_reset() {
let style = Style {
color: true,
unicode: true,
};
let line = one_line(&snap(5_000, 0, 0), style).unwrap();
assert!(line.starts_with("\x1b[2m") && line.ends_with("\x1b[0m"));
}
#[test]
fn compact_is_short_and_uncoloured() {
let style = Style {
color: true,
unicode: true,
};
assert_eq!(
compact(&snap(1_200_000, 41, 3), style).unwrap(),
"◆ −1.2M tok ⛨ 3"
);
let ascii = Style {
color: false,
unicode: false,
};
assert_eq!(compact(&snap(0, 0, 2), ascii).unwrap(), "* sec 2");
assert_eq!(compact(&snap(0, 9, 0), Style::PLAIN), None);
assert_eq!(compact(&ValueSnapshot::default(), Style::PLAIN), None);
}
#[test]
fn security_only_session_still_shows() {
let line = one_line(&snap(0, 0, 2), Style::PLAIN).unwrap();
assert_eq!(line, "◆ lean-ctx ⛨ 2");
}
#[test]
fn phrases_never_claim_neutralization() {
let counts = SecurityCounts {
secrets_redacted: 1,
shell_blocked: 2,
path_blocked: 1,
injection_flagged: 3,
..SecurityCounts::ZERO
};
let text = security_phrases(&counts).join(", ");
assert_eq!(
text,
"1 secret kept out of context, 2 risky commands blocked, \
1 path outside the project blocked, 3 prompt-injection patterns flagged"
);
assert!(!text.contains("neutraliz"));
}
#[test]
fn gateway_phrases_say_what_happened_without_overclaiming() {
let counts = SecurityCounts {
pii_redacted: 2,
content_withheld: 1,
coverage_incomplete: 1,
..SecurityCounts::ZERO
};
assert_eq!(
security_phrases(&counts).join(", "),
"2 personal data values kept out of context, 1 source withheld, \
1 source not fully inspected"
);
}
}