#grep #pattern #print #printer #sink

grep-printer

An implementation of the grep crate’s Sink trait that provides standard printing of search results, similar to grep itself

3 unstable releases

0.1.1 Sep 7, 2018
0.1.0 Aug 20, 2018
0.0.1 Aug 8, 2018

#87 in Filesystem

Download history 17/week @ 2018-08-10 5/week @ 2018-08-17 17/week @ 2018-08-24 5/week @ 2018-08-31 63/week @ 2018-09-07

35 downloads per month
Used in 2 crates (1 directly)

Unlicense/MIT

482KB
10K SLoC

grep-printer

Print results from line oriented searching in a human readable, aggregate or JSON Lines format.

Linux build status Windows build status

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/grep-printer

NOTE: You probably don't want to use this crate directly. Instead, you should prefer the facade defined in the grep crate.

Usage

Add this to your Cargo.toml:

[dependencies]
grep-printer = "0.1"

and this to your crate root:

extern crate grep_printer;

lib.rs:

This crate provides featureful and fast printers that interoperate with the grep-searcher crate.

Brief overview

The Standard printer shows results in a human readable format, and is modeled after the formats used by standard grep-like tools. Features include, but are not limited to, cross platform terminal coloring, search & replace, multi-line result handling and reporting summary statistics.

The JSON printer shows results in a machine readable format. To facilitate a stream of search results, the format uses JSON Lines by emitting a series of messages as search results are found.

The Summary printer shows aggregate results for a single search in a human readable format, and is modeled after similar formats found in standard grep-like tools. This printer is useful for showing the total number of matches and/or printing file paths that either contain or don't contain matches.

Example

This example shows how to create a "standard" printer and execute a search.

extern crate grep_regex;
extern crate grep_printer;
extern crate grep_searcher;

use std::error::Error;

use grep_regex::RegexMatcher;
use grep_printer::Standard;
use grep_searcher::Searcher;

const SHERLOCK: &'static [u8] = b"\
For the Doctor Watsons of this world, as opposed to the Sherlock
Holmeses, success in the province of detective work must always
be, to a very large extent, the result of luck. Sherlock Holmes
can extract a clew from a wisp of straw or a flake of cigar ash;
but Doctor Watson has to have it taken out for him and dusted,
and exhibited clearly, with a label attached.
";

# fn main() { example().unwrap(); }
fn example() -> Result<(), Box<Error>> {
let matcher = RegexMatcher::new(r"Sherlock")?;
let mut printer = Standard::new_no_color(vec![]);
Searcher::new().search_slice(&matcher, SHERLOCK, printer.sink(&matcher))?;

// into_inner gives us back the underlying writer we provided to
// new_no_color, which is wrapped in a termcolor::NoColor. Thus, a second
// into_inner gives us back the actual buffer.
let output = String::from_utf8(printer.into_inner().into_inner())?;
let expected = "\
1:For the Doctor Watsons of this world, as opposed to the Sherlock
3:be, to a very large extent, the result of luck. Sherlock Holmes
";
assert_eq!(output, expected);
Ok(())
}

Dependencies

~3MB
~56K SLoC