use regex::Regex;
use std::path::{Path, PathBuf};
pub const LINE_CAP: usize = 2048;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TriggerAction {
Highlight,
Notify,
Bell,
Capture,
Redact,
}
pub const MASK: char = '\u{2022}';
#[derive(Clone, Debug)]
pub struct Trigger {
pub regex: Regex,
pub action: TriggerAction,
pub fg: Option<(u8, u8, u8)>,
pub bg: Option<(u8, u8, u8)>,
pub message: Option<String>,
pub copy_masked: bool,
pub needs_entropy: bool,
}
#[derive(Clone, Debug, Default)]
pub struct TriggerSet {
pub triggers: Vec<Trigger>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HighlightSpan {
pub start: usize,
pub len: usize,
pub fg: Option<(u8, u8, u8)>,
pub bg: Option<(u8, u8, u8)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TriggerHit {
pub action: TriggerAction,
pub message: String,
pub line: String,
}
pub fn triggers_path() -> PathBuf {
crate::prefs::config_dir().join("triggers.json")
}
#[derive(serde::Deserialize)]
struct TriggerRow {
regex: String,
action: String,
#[serde(default)]
fg: Option<String>,
#[serde(default)]
bg: Option<String>,
#[serde(default)]
message: Option<String>,
#[serde(default)]
copy: Option<String>,
#[serde(default = "enabled_default")]
enabled: bool,
}
fn enabled_default() -> bool {
true
}
fn parse_hex(s: &str) -> Option<(u8, u8, u8)> {
let hex = s.strip_prefix('#')?;
if hex.len() != 6 {
return None;
}
let n = u32::from_str_radix(hex, 16).ok()?;
Some(((n >> 16) as u8, (n >> 8) as u8, n as u8))
}
pub const TEMPLATE: &str = r##"// croft terminal triggers: regexes watched on terminal output.
// "action": "highlight" recolour every visible occurrence ("fg"/"bg" as #rrggbb)
// "notify" status-bar notice ("message" template: \0 whole match, \1-\9 groups)
// "bell" status-bar bell notice
// "capture" collect matched lines into the CAPTURES panel (click jumps to the line)
// "redact" mask the match (group 1 if the regex has one) as •••• on screen;
// click the mask to see the value; "copy": "masked" masks copies too.
// Built-in key/token rules run first (Settings: Terminal: Redact Secrets).
// Highlights repaint live on the visible screen and persist into scrollback.
// notify/bell/capture fire once per completed output line (never inside full-screen apps).
// "enabled": false keeps a rule without running it.
[
{ "regex": "\\b(ERROR|FATAL|panicked)\\b", "action": "highlight", "fg": "#ffffff", "bg": "#c0392b" },
{ "regex": "\\bwarning\\b", "action": "highlight", "fg": "#000000", "bg": "#e5c07b", "enabled": false },
{ "regex": "(BUILD|Compiling|error): (.+)", "action": "notify", "message": "\\1: \\2", "enabled": false },
{ "regex": "(?i)x-api-key: *(\\S+)", "action": "redact", "copy": "masked", "enabled": false }
]
"##;
impl TriggerSet {
pub fn load(path: &Path) -> Self {
let Ok(json) = std::fs::read_to_string(path) else {
return Self::default();
};
Self::from_json(&json)
}
pub fn from_json(json: &str) -> Self {
let rows: Vec<TriggerRow> =
serde_json::from_str(&crate::keymap::strip_line_comments(json)).unwrap_or_default();
let triggers = rows
.into_iter()
.filter(|r| r.enabled)
.filter_map(|r| {
let action = match r.action.as_str() {
"highlight" => TriggerAction::Highlight,
"notify" => TriggerAction::Notify,
"bell" => TriggerAction::Bell,
"capture" => TriggerAction::Capture,
"redact" => TriggerAction::Redact,
_ => return None,
};
Some(Trigger {
regex: Regex::new(&r.regex).ok()?,
action,
fg: r.fg.as_deref().and_then(parse_hex),
bg: r.bg.as_deref().and_then(parse_hex),
message: r.message,
copy_masked: r.copy.as_deref() == Some("masked"),
needs_entropy: false,
})
})
.collect();
Self { triggers }
}
pub fn is_empty(&self) -> bool {
self.triggers.is_empty()
}
pub fn with_builtin_redactions(mut self) -> Self {
let mut all = builtin_redactions();
all.append(&mut self.triggers);
self.triggers = all;
self
}
pub fn has_highlights(&self) -> bool {
self.triggers
.iter()
.any(|t| matches!(t.action, TriggerAction::Highlight | TriggerAction::Redact))
}
pub fn has_redactions(&self) -> bool {
self.triggers
.iter()
.any(|t| t.action == TriggerAction::Redact)
}
pub fn has_events(&self) -> bool {
self.triggers
.iter()
.any(|t| !matches!(t.action, TriggerAction::Highlight | TriggerAction::Redact))
}
}
pub fn builtin_redactions() -> Vec<Trigger> {
static RULES: std::sync::LazyLock<Vec<Trigger>> = std::sync::LazyLock::new(|| {
const PATTERNS: &[(&str, bool)] = &[
(r"\b(?:AKIA|ASIA)[0-9A-Z]{16}\b", false),
(r"\bsk-(?:[A-Za-z0-9]{1,10}-){0,3}[A-Za-z0-9_]{16,}\b", true),
(
r"\b(?:gh[pousr]_[A-Za-z0-9]{36,}|github_pat_[A-Za-z0-9_]{22,})\b",
false,
),
(r"\bxox[abpors]-[A-Za-z0-9-]{10,}\b", false),
(
r"\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b",
false,
),
(
r#"(?i)\bauthorization"? *: *"?bearer +([^\s'",;]{16,})"#,
false,
),
];
PATTERNS
.iter()
.map(|(r, needs_entropy)| Trigger {
regex: Regex::new(r).expect("built-in redaction regex compiles"),
action: TriggerAction::Redact,
fg: None,
bg: None,
message: None,
copy_masked: false,
needs_entropy: *needs_entropy,
})
.collect()
});
RULES.clone()
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RedactSpan {
pub start: usize,
pub len: usize,
pub copy_masked: bool,
}
pub fn redact_spans(line: &str, set: &TriggerSet) -> Vec<RedactSpan> {
let mut out = Vec::new();
for t in &set.triggers {
if t.action != TriggerAction::Redact {
continue;
}
for caps in t.regex.captures_iter(line) {
let m = match caps.get(1) {
Some(g) => g,
None => match caps.get(0) {
Some(m) => m,
None => continue,
},
};
let labelled = m.as_str().matches('-').count() > 1;
let body = m.as_str().rsplit('-').next().unwrap_or(m.as_str());
if m.as_str().is_empty()
|| (t.needs_entropy
&& !labelled
&& !body
.chars()
.any(|c| c.is_ascii_digit() || c.is_ascii_uppercase()))
{
continue;
}
out.push(RedactSpan {
start: line[..m.start()].chars().count(),
len: m.as_str().chars().count(),
copy_masked: t.copy_masked,
});
}
}
out
}
pub fn mask_text(text: &str, set: &TriggerSet, copy_only: bool) -> String {
if !set.has_redactions() {
return text.to_string();
}
let mut out = String::with_capacity(text.len());
for (i, line) in text.split('\n').enumerate() {
if i > 0 {
out.push('\n');
}
let spans: Vec<RedactSpan> = redact_spans(line, set)
.into_iter()
.filter(|s| !copy_only || s.copy_masked)
.collect();
if spans.is_empty() {
out.push_str(line);
continue;
}
for (k, c) in line.chars().enumerate() {
if spans.iter().any(|s| k >= s.start && k < s.start + s.len) {
out.push(MASK);
} else {
out.push(c);
}
}
}
out
}
pub fn highlight_spans(line: &str, set: &TriggerSet) -> Vec<HighlightSpan> {
let mut out = Vec::new();
for t in &set.triggers {
if t.action != TriggerAction::Highlight {
continue;
}
for m in t.regex.find_iter(line) {
out.push(HighlightSpan {
start: line[..m.start()].chars().count(),
len: m.as_str().chars().count(),
fg: t.fg,
bg: t.bg,
});
}
}
out
}
fn interpolate(template: Option<&str>, caps: ®ex::Captures) -> String {
let whole = caps.get(0).map(|m| m.as_str()).unwrap_or_default();
let Some(template) = template else {
return whole.to_string();
};
let mut out = String::new();
let mut chars = template.chars().peekable();
while let Some(c) = chars.next() {
if c == '\\'
&& let Some(d) = chars.peek().and_then(|p| p.to_digit(10))
{
chars.next();
out.push_str(caps.get(d as usize).map(|m| m.as_str()).unwrap_or_default());
} else {
out.push(c);
}
}
out
}
#[derive(Default)]
pub struct TriggerScanner {
line: Vec<u8>,
state: ScanState,
cr_pending: bool,
in_alt: bool,
csi: Vec<u8>,
}
const CSI_CAP: usize = 32;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
enum ScanState {
#[default]
Ground,
Esc,
Csi,
InString,
StringEsc,
}
impl TriggerScanner {
pub fn new() -> Self {
Self::default()
}
pub fn scan(&mut self, bytes: &[u8], set: &TriggerSet, out: &mut Vec<TriggerHit>) {
self.scan_collect(bytes, set, out, None);
}
pub fn scan_collect(
&mut self,
bytes: &[u8],
set: &TriggerSet,
out: &mut Vec<TriggerHit>,
mut lines: Option<&mut Vec<String>>,
) {
for &b in bytes {
if self.cr_pending && self.state == ScanState::Ground {
self.cr_pending = false;
if b == b'\n' {
self.complete_line(set, out, lines.as_deref_mut());
continue;
}
self.reset_line();
}
match self.state {
ScanState::Ground => match b {
0x1b => self.state = ScanState::Esc,
b'\n' => self.complete_line(set, out, lines.as_deref_mut()),
b'\r' => self.cr_pending = true,
0x00..=0x1f | 0x7f => {}
_ => {
if !self.in_alt && self.line.len() < LINE_CAP {
self.line.push(b);
}
}
},
ScanState::Esc => {
self.state = match b {
b'[' => ScanState::Csi,
b']' | b'P' | b'X' | b'^' | b'_' => ScanState::InString,
b'c' => {
self.set_alt(false);
ScanState::Ground
}
_ => ScanState::Ground,
};
}
ScanState::Csi => {
if (0x40..=0x7e).contains(&b) {
if (b == b'h' || b == b'l') && self.csi.first() == Some(&b'?') {
let alt = self.csi[1..]
.split(|&c| c == b';')
.any(|p| p == b"47" || p == b"1047" || p == b"1049");
if alt {
self.set_alt(b == b'h');
}
}
self.csi.clear();
self.state = ScanState::Ground;
} else if b == 0x18 || b == 0x1a {
self.csi.clear();
self.state = ScanState::Ground;
} else if b == 0x1b {
self.csi.clear();
self.state = ScanState::Esc;
} else if self.csi.len() < CSI_CAP {
self.csi.push(b);
}
}
ScanState::InString => match b {
0x07 => self.state = ScanState::Ground,
0x1b => self.state = ScanState::StringEsc,
0x18 | 0x1a => self.state = ScanState::Ground,
_ => {}
},
ScanState::StringEsc => {
self.state = match b {
b'\\' => ScanState::Ground,
0x18 | 0x1a => ScanState::Ground,
_ => ScanState::InString,
};
}
}
}
}
fn set_alt(&mut self, alt: bool) {
if self.in_alt != alt {
self.in_alt = alt;
self.reset_line();
self.cr_pending = false;
}
}
fn reset_line(&mut self) {
self.line.clear();
}
fn complete_line(
&mut self,
set: &TriggerSet,
out: &mut Vec<TriggerHit>,
lines: Option<&mut Vec<String>>,
) {
if self.in_alt {
self.reset_line();
return;
}
if let Some(lines) = lines {
lines.push(String::from_utf8_lossy(&self.line).into_owned());
}
if !self.line.is_empty() {
let line = String::from_utf8_lossy(&self.line);
for t in &set.triggers {
if matches!(t.action, TriggerAction::Highlight | TriggerAction::Redact) {
continue;
}
if let Some(caps) = t.regex.captures(&line) {
out.push(TriggerHit {
action: t.action,
message: interpolate(t.message.as_deref(), &caps),
line: line.to_string(),
});
}
}
}
self.reset_line();
}
}
#[cfg(test)]
mod tests {
use super::*;
fn set(json: &str) -> TriggerSet {
TriggerSet::from_json(json)
}
#[test]
fn config_rows_parse_and_bad_rows_are_skipped() {
let s = set(r##"// comment survives
[
{ "regex": "\\bERROR\\b", "action": "highlight", "fg": "#ffffff", "bg": "#c0392b" },
{ "regex": "((broken", "action": "highlight" },
{ "regex": "done", "action": "notify", "message": "finished: \\0" },
{ "regex": "beep", "action": "bell" },
{ "regex": "off", "action": "notify", "enabled": false },
{ "regex": "what", "action": "unknown-action" }
]"##);
assert_eq!(
s.triggers.len(),
3,
"bad regex, disabled and unknown-action rows must be skipped: {s:?}"
);
assert!(s.has_highlights());
assert!(s.has_events());
assert_eq!(s.triggers[0].fg, Some((0xff, 0xff, 0xff)));
assert_eq!(s.triggers[0].bg, Some((0xc0, 0x39, 0x2b)));
assert!(set("total garbage").is_empty());
assert!(set("").is_empty());
}
#[test]
fn redact_rows_parse_with_their_copy_mode() {
let s = set(r##"[
{ "regex": "secret=(\\S+)", "action": "redact", "copy": "masked" },
{ "regex": "token=\\S+", "action": "redact" }
]"##);
assert_eq!(s.triggers.len(), 2);
assert!(s.has_highlights(), "redact rides the paint pass");
assert!(s.has_redactions());
assert!(
!s.has_events(),
"redact never wakes the reader-thread scanner"
);
assert!(s.triggers[0].copy_masked);
assert!(!s.triggers[1].copy_masked);
}
#[test]
fn redact_spans_mask_group_one_when_present_else_the_whole_match() {
let s = set(r##"[
{ "regex": "secret=(\\S+)", "action": "redact", "copy": "masked" },
{ "regex": "token=\\S+", "action": "redact" }
]"##);
let spans = redact_spans("naïve secret=abc token=xyz", &s);
assert_eq!(
spans,
vec![
RedactSpan {
start: 13,
len: 3,
copy_masked: true
},
RedactSpan {
start: 17,
len: 9,
copy_masked: false
},
]
);
assert_eq!(
mask_text("naïve secret=abc token=xyz", &s, false),
"naïve secret=••• •••••••••",
"the dump masks every rule, width preserved"
);
assert_eq!(
mask_text("naïve secret=abc token=xyz", &s, true),
"naïve secret=••• token=xyz",
"a copy masks only rules marked copy=masked"
);
assert_eq!(
mask_text("line one\nsecret=q\n", &s, false),
"line one\nsecret=•\n",
"masking is per line and keeps the newlines"
);
}
#[test]
fn redact_rules_never_fire_as_hits() {
let s = TriggerSet::default().with_builtin_redactions();
let mut sc = TriggerScanner::new();
let mut out = Vec::new();
sc.scan(b"key AKIAIOSFODNN7EXAMPLE end\n", &s, &mut out);
assert!(out.is_empty(), "{out:?}");
let user = set(r##"[{ "regex": "hunter2", "action": "redact" }]"##);
sc.scan(b"pw hunter2\n", &user, &mut out);
assert!(out.is_empty(), "{out:?}");
assert!(!s.has_events(), "nothing here wakes the scanner");
}
#[test]
fn builtin_rules_mask_the_usual_key_shapes_and_leave_prose_alone() {
let s = TriggerSet::default().with_builtin_redactions();
let masked = |line: &str| mask_text(line, &s, false);
assert_eq!(
masked("export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE"),
"export AWS_ACCESS_KEY_ID=••••••••••••••••••••"
);
let key = "sk-proj-abcdefghijklmnopqrstuvwxyz0123";
assert_eq!(
masked(&format!("OPENAI_API_KEY={key}")),
format!("OPENAI_API_KEY={}", "•".repeat(key.len()))
);
assert_eq!(
masked("token: ghp_abcdefghijklmnopqrstuvwxyz0123456789"),
"token: ••••••••••••••••••••••••••••••••••••••••"
);
assert_eq!(
masked("xoxb-123456789012-abcdefGHIJ"),
"••••••••••••••••••••••••••••"
);
assert_eq!(
masked("ghp_abcdefghijklmnopqrstuvwxyzABCDEFGHIJ"),
"•".repeat(40),
"a real token can be digit-free; the prefix is the anchor"
);
assert_eq!(masked("xoxb-abcdefghijklmnopqrstuvwx"), "•".repeat(29));
assert_eq!(
masked("sk-proj-abcdefghijklmnopqrstuvwxyzABCDEFGH"),
"•".repeat(42),
"one labelled segment is a key shape"
);
let ant = "sk-ant-api03-AbCdEf1234567890AbCdEf1234567890";
assert_eq!(masked(ant), "•".repeat(ant.len()), "two labels: Anthropic");
let or = "sk-or-v1-abcdef1234567890abcdef1234567890";
assert_eq!(masked(or), "•".repeat(or.len()), "two labels: OpenRouter");
let plain_body = "sk-abcdefghijklmnopqrstuvwxyzabcdefgH1";
assert_eq!(
masked(plain_body),
"•".repeat(plain_body.len()),
"no label, mixed body"
);
let lowercase_body = "sk-ant-api03-abcdefghijklmnopqrstuvwxyzabcdef";
assert_eq!(
masked(lowercase_body),
"•".repeat(lowercase_body.len()),
"a labelled key masks whatever its body looks like"
);
let labelled_word = "sk-learn-preprocessingtransformerspipeline";
assert_eq!(
masked(labelled_word),
"•".repeat(labelled_word.len()),
"a labelled lowercase token masks too: a click reveals it, a missed key never comes back"
);
assert_eq!(
masked(r#"{"Authorization": "Bearer tok_abcdef1234567890"}"#),
r#"{"Authorization": "Bearer ••••••••••••••••••••"}"#,
"the JSON header form"
);
assert_eq!(
masked("Authorization: Bearer abcdef0123456789ABCDEF, X-Other: 1"),
"Authorization: Bearer ••••••••••••••••••••••, X-Other: 1",
"a trailing comma stays outside"
);
assert_eq!(
masked("curl -H 'Authorization: Bearer abcdef0123456789ABCDEF' https://x"),
"curl -H 'Authorization: Bearer ••••••••••••••••••••••' https://x",
"the closing quote stays outside the mask"
);
assert_eq!(
masked("Authorization: Bearer abcdef0123456789ABCDEF"),
"Authorization: Bearer ••••••••••••••••••••••",
"a bearer header keeps its words, the token goes"
);
let jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
assert_eq!(masked(jwt), "•".repeat(jwt.chars().count()));
for prose in [
"cargo build --release",
"Bearer of bad news",
"Bearer responsibilities matter",
"run task sk-something-like-this-long",
"sk-preprocessingtransformerspipeline",
"the skeleton key",
"sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
] {
assert_eq!(masked(prose), prose, "prose must not be masked");
}
let both =
set(r##"[{ "regex": "hunter2", "action": "redact" }]"##).with_builtin_redactions();
assert_eq!(mask_text("pw hunter2", &both, false), "pw •••••••");
}
#[test]
fn highlight_spans_are_char_indexed_per_trigger_colours() {
let s = set(r##"[
{ "regex": "ERROR", "action": "highlight", "bg": "#c0392b" },
{ "regex": "ok", "action": "highlight", "fg": "#00ff00" }
]"##);
let spans = highlight_spans("naïve ERROR then ok", &s);
assert_eq!(spans.len(), 2, "{spans:?}");
let err = &spans[0];
assert_eq!((err.start, err.len), (6, 5));
assert_eq!(err.bg, Some((0xc0, 0x39, 0x2b)));
assert_eq!(err.fg, None);
assert_eq!((spans[1].start, spans[1].len), (17, 2));
}
#[test]
fn scanner_fires_on_completed_lines_with_interpolation() {
let s = set(r##"[
{ "regex": "BUILD (\\w+)", "action": "notify", "message": "build: \\1" },
{ "regex": "beep", "action": "bell" }
]"##);
let mut sc = TriggerScanner::new();
let mut out = Vec::new();
sc.scan(b"\x1b[32mBUILD \x1b[1mFAILED\x1b[0m", &s, &mut out);
assert!(out.is_empty(), "no newline yet: {out:?}");
sc.scan(b"\n", &s, &mut out);
assert_eq!(
out,
vec![TriggerHit {
action: TriggerAction::Notify,
message: String::from("build: FAILED"),
line: String::from("BUILD FAILED"),
}]
);
out.clear();
sc.scan(b"beep 10%\rdone 100%\n", &s, &mut out);
assert!(out.is_empty(), "text before \\r must not fire: {out:?}");
sc.scan(b"\x1b]0;beep title", &s, &mut out);
sc.scan(b"\x07real beep\n", &s, &mut out);
assert_eq!(out.len(), 1, "{out:?}");
assert_eq!(out[0].action, TriggerAction::Bell);
assert_eq!(out[0].message, "beep");
out.clear();
sc.scan(b"beep one\r\n", &s, &mut out);
assert_eq!(out.len(), 1, "CRLF must complete the line: {out:?}");
out.clear();
sc.scan(b"beep two\r", &s, &mut out);
sc.scan(b"\n", &s, &mut out);
assert_eq!(out.len(), 1, "chunk-split CRLF must complete: {out:?}");
}
#[test]
fn scanner_tracks_the_alt_screen_positionally() {
let s = set(r##"[
{ "regex": "SECRET", "action": "notify" },
{ "regex": "BUILD FAILED", "action": "notify" },
{ "regex": "ERROR: failed", "action": "notify" }
]"##);
let mut sc = TriggerScanner::new();
let mut out = Vec::new();
sc.scan(b"\x1b[?1049hSECRET\n\x1b[?1049l", &s, &mut out);
assert!(out.is_empty(), "alt-screen content fired: {out:?}");
sc.scan(b"BUILD FAILED\n\x1b[?1049h", &s, &mut out);
assert_eq!(out.len(), 1, "primary text before alt entry: {out:?}");
assert_eq!(out[0].line, "BUILD FAILED");
out.clear();
sc.scan(b"\x1b[?1049l", &s, &mut out);
sc.scan(b"ERR", &s, &mut out);
sc.scan(b"\x1b[?1049halt stuff\x1b[?1049l", &s, &mut out);
sc.scan(b"OR: failed\n", &s, &mut out);
assert!(out.is_empty(), "spliced a line never printed: {out:?}");
sc.scan(b"\x1b[?1002;1049hSECRET\n\x1b[?1049;1002l", &s, &mut out);
assert!(out.is_empty(), "combined DECSET alt entry missed: {out:?}");
sc.scan(b"\x1b[?1049h\x1bcBUILD FAILED\n", &s, &mut out);
assert_eq!(out.len(), 1, "RIS must exit the alt screen: {out:?}");
}
#[test]
fn a_cancelled_csi_does_not_swallow_the_alt_screen_entry() {
let s = set(r##"[ { "regex": "SECRET", "action": "notify" } ]"##);
let mut sc = TriggerScanner::new();
let mut out = Vec::new();
sc.scan(b"\x1b[\x18\x1b[?1049hSECRET\n\x1b[?1049l", &s, &mut out);
assert!(
out.is_empty(),
"CAN-cancelled CSI ate the alt entry: {out:?}"
);
sc.scan(b"\x1b[12\x1a\x1b[?1049hSECRET\n\x1b[?1049l", &s, &mut out);
assert!(
out.is_empty(),
"SUB-cancelled CSI ate the alt entry: {out:?}"
);
sc.scan(b"\x1b[12\x1b[?1049hSECRET\n\x1b[?1049l", &s, &mut out);
assert!(out.is_empty(), "ESC-in-CSI ate the alt entry: {out:?}");
}
#[test]
fn can_and_sub_cancel_an_unterminated_control_string() {
let s = set(
r##"[ { "regex": "PRIMARY OK", "action": "notify" }, { "regex": "SECRET", "action": "notify" } ]"##,
);
for intro in [b"\x1b]".as_slice(), b"\x1bP", b"\x1b_", b"\x1b^", b"\x1bX"] {
for cancel in [0x18u8, 0x1a] {
let mut sc = TriggerScanner::new();
let mut out = Vec::new();
let mut bytes = Vec::new();
bytes.extend_from_slice(intro);
bytes.extend_from_slice(b"unterminated-body");
bytes.push(cancel);
bytes.extend_from_slice(b"PRIMARY OK\n\x1b[?1049hSECRET\n\x1b[?1049l");
sc.scan(&bytes, &s, &mut out);
assert_eq!(
out.len(),
1,
"intro {intro:?} cancel {cancel:#x}: the cancelled string must \
release the scanner: {out:?}"
);
assert_eq!(
out[0].line, "PRIMARY OK",
"intro {intro:?} cancel {cancel:#x}"
);
}
}
}
#[test]
fn scanner_caps_runaway_lines() {
let s = set(r#"[ { "regex": "needle", "action": "notify" } ]"#);
let mut sc = TriggerScanner::new();
let mut out = Vec::new();
let long = vec![b'x'; LINE_CAP + 512];
sc.scan(&long, &s, &mut out);
sc.scan(b"needle\n", &s, &mut out);
assert!(
out.is_empty(),
"bytes past the cap are dropped, the needle never enters the buffer: {out:?}"
);
}
#[test]
fn capture_action_parses_and_scanner_reports_the_full_line() {
let s = set(
r#"[ { "regex": "error\\[(\\w+)\\]", "action": "capture", "message": "rustc \\1" } ]"#,
);
assert!(s.has_events(), "capture is an event action");
let mut sc = TriggerScanner::new();
let mut out = Vec::new();
sc.scan(b"error[E0308]: mismatched types\r\n", &s, &mut out);
assert_eq!(out.len(), 1, "{out:?}");
assert_eq!(out[0].action, TriggerAction::Capture);
assert_eq!(out[0].message, "rustc E0308");
assert_eq!(
out[0].line, "error[E0308]: mismatched types",
"a capture hit carries the whole matched line for the panel"
);
}
#[test]
fn notify_without_a_template_reports_the_whole_match() {
let s = set(r#"[ { "regex": "deploy (ok|failed)", "action": "notify" } ]"#);
let mut sc = TriggerScanner::new();
let mut out = Vec::new();
sc.scan(b"deploy failed\n", &s, &mut out);
assert_eq!(out.len(), 1);
assert_eq!(out[0].message, "deploy failed");
}
}