Skip to main content

focus_block

Function focus_block 

Source
pub fn focus_block(text: &str, re: &Regex, ctx: usize) -> Option<String>
Expand description

Distil text to the lines matching re, each with ctx lines of context, merging overlapping windows; non-contiguous windows are separated by a -- line and every kept line is prefixed with its 1-based number. Returns None when nothing matches.

ยงExamples

use coding_tools::testrun::focus_block;
use coding_tools::pattern::compile;

let re = compile("ERROR").unwrap();
let log = "ok\nERROR: a\nok\nok\nok\nERROR: b\ntail\n";
// ctx 0 keeps only the matching lines; the two windows are separated by `--`.
assert_eq!(focus_block(log, &re, 0).unwrap(), "2: ERROR: a\n--\n6: ERROR: b");

assert!(focus_block("all clean here", &re, 0).is_none());