Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions crates/rustern-core/src/render/highlight.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Write as _;
use std::sync::Arc;

use owo_colors::OwoColorize;
Expand All @@ -21,26 +22,27 @@ impl SternHighlightLineFormatter {
impl LineFormatter for SternHighlightLineFormatter {
fn format_into(&self, event: &LogEvent, buf: &mut String) {
self.inner.format_into(event, buf);
if self.re.find(buf).is_none() {
return;
if let Some(highlighted) = highlight_default_line(buf, &self.re) {
*buf = highlighted;
}
let highlighted = highlight_default_line(buf, &self.re);
buf.clear();
buf.push_str(&highlighted);
}
}

fn highlight_default_line(text: &str, re: &Regex) -> String {
fn highlight_default_line(text: &str, re: &Regex) -> Option<String> {
let mut last = 0usize;
let mut out = String::with_capacity(text.len().saturating_add(text.len().min(4096)));
let mut out = None::<String>;
for m in re.find_iter(text) {
let out = out.get_or_insert_with(|| {
String::with_capacity(text.len().saturating_add(text.len().min(4096)))
});
out.push_str(&text[last..m.start()]);
let styled = (&text[m.start()..m.end()]).red().bold().to_string();
out.push_str(&styled);
let _ = write!(out, "{}", (&text[m.start()..m.end()]).red().bold());
last = m.end();
}
out.push_str(&text[last..]);
out
out.map(|mut highlighted| {
highlighted.push_str(&text[last..]);
highlighted
})
}

/// Stern merges `--include` and `--highlight`, sorts alternation segments by descending pattern-string
Expand Down Expand Up @@ -82,7 +84,7 @@ mod tests {
.expect("combined pattern");
assert!(re.is_match("foobar"));

let out = highlight_default_line("- foobar!", &re);
let out = highlight_default_line("- foobar!", &re).expect("match");
assert!(out.contains("foobar"));
assert!(out.starts_with('-'));
assert!(out.contains('\x1b')); // ansi wrap
Expand All @@ -91,4 +93,12 @@ mod tests {
"prefix before match should survive: {out:?}"
);
}

#[test]
fn no_match_returns_none() {
let re = compile_stern_highlight_regex(&["needle".into()], &[])
.unwrap()
.expect("pattern");
assert!(highlight_default_line("no haystack here", &re).is_none());
}
}