Crate grep_printer

Source
Expand description

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.

use {
    grep_regex::RegexMatcher,
    grep_printer::Standard,
    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.
";

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);

Structs§

ColorSpecs
A merged set of color specifications.
HyperlinkConfig
Hyperlink configuration.
HyperlinkEnvironment
A static environment for hyperlink interpolation.
HyperlinkFormat
A hyperlink format with variables.
HyperlinkFormatError
An error that can occur when parsing a hyperlink format.
JSONserde
The JSON printer, which emits results in a JSON lines format.
JSONBuilderserde
A builder for a JSON lines printer.
JSONSinkserde
An implementation of Sink associated with a matcher and an optional file path for the JSON printer.
PathPrinter
A printer file paths, with optional color and hyperlink support.
PathPrinterBuilder
A builder for a printer that emits file paths.
Standard
The standard printer, which implements grep-like formatting, including color support.
StandardBuilder
A builder for the “standard” grep-like printer.
StandardSink
An implementation of Sink associated with a matcher and an optional file path for the standard printer.
Stats
Summary statistics produced at the end of a search.
Summary
The summary printer, which emits aggregate results from a search.
SummaryBuilder
A builder for summary printer.
SummarySink
An implementation of Sink associated with a matcher and an optional file path for the summary printer.
UserColorSpec
A single color specification provided by the user.

Enums§

ColorError
An error that can occur when parsing color specifications.
SummaryKind
The type of summary output (if any) to print.

Functions§

default_color_specs
Returns a default set of color specifications.