lib.rs
This file
Path
crates/printer/src/lib.rs
103 lines · 3.5 KiB · Rust
Last changed by
printer: use `doc_cfg` instead of `doc_auto_cfg`Andrew Gallantbefore adoption5748f81
History of this fileTake it
1/*!2This crate provides featureful and fast printers that interoperate with the3[`grep-searcher`](https://docs.rs/grep-searcher)4crate.56# Brief overview78The [`Standard`] printer shows results in a human readable format, and is9modeled after the formats used by standard grep-like tools. Features include,10but are not limited to, cross platform terminal coloring, search & replace,11multi-line result handling and reporting summary statistics.1213The [`JSON`] printer shows results in a machine readable format.14To facilitate a stream of search results, the format uses [JSON15Lines](https://jsonlines.org/) by emitting a series of messages as search16results are found.1718The [`Summary`] printer shows *aggregate* results for a single search in a19human readable format, and is modeled after similar formats found in standard20grep-like tools. This printer is useful for showing the total number of matches21and/or printing file paths that either contain or don't contain matches.2223# Example2425This example shows how to create a "standard" printer and execute a search.2627```28use {29 grep_regex::RegexMatcher,30 grep_printer::Standard,31 grep_searcher::Searcher,32};3334const SHERLOCK: &'static [u8] = b"\35For the Doctor Watsons of this world, as opposed to the Sherlock36Holmeses, success in the province of detective work must always37be, to a very large extent, the result of luck. Sherlock Holmes38can extract a clew from a wisp of straw or a flake of cigar ash;39but Doctor Watson has to have it taken out for him and dusted,40and exhibited clearly, with a label attached.41";4243let matcher = RegexMatcher::new(r"Sherlock")?;44let mut printer = Standard::new_no_color(vec![]);45Searcher::new().search_slice(&matcher, SHERLOCK, printer.sink(&matcher))?;4647// into_inner gives us back the underlying writer we provided to48// new_no_color, which is wrapped in a termcolor::NoColor. Thus, a second49// into_inner gives us back the actual buffer.50let output = String::from_utf8(printer.into_inner().into_inner())?;51let expected = "\521:For the Doctor Watsons of this world, as opposed to the Sherlock533:be, to a very large extent, the result of luck. Sherlock Holmes54";55assert_eq!(output, expected);56# Ok::<(), Box<dyn std::error::Error>>(())57```58*/5960#![deny(missing_docs)]61#![cfg_attr(docsrs, feature(doc_cfg))]6263pub use crate::{64 color::{ColorError, ColorSpecs, UserColorSpec, default_color_specs},65 hyperlink::{66 HyperlinkAlias, HyperlinkConfig, HyperlinkEnvironment,67 HyperlinkFormat, HyperlinkFormatError, hyperlink_aliases,68 },69 path::{PathPrinter, PathPrinterBuilder},70 standard::{Standard, StandardBuilder, StandardSink},71 stats::Stats,72 summary::{Summary, SummaryBuilder, SummaryKind, SummarySink},73};7475#[cfg(feature = "serde")]76pub use crate::json::{JSON, JSONBuilder, JSONSink};7778// The maximum number of bytes to execute a search to account for look-ahead.79//80// This is an unfortunate kludge since PCRE2 doesn't provide a way to search81// a substring of some input while accounting for look-ahead. In theory, we82// could refactor the various 'grep' interfaces to account for it, but it would83// be a large change. So for now, we just let PCRE2 go looking a bit for a84// match without searching the entire rest of the contents.85//86// Note that this kludge is only active in multi-line mode.87const MAX_LOOK_AHEAD: usize = 128;8889#[macro_use]90mod macros;9192mod color;93mod counter;94mod hyperlink;95#[cfg(feature = "serde")]96mod json;97#[cfg(feature = "serde")]98mod jsont;99mod path;100mod standard;101mod stats;102mod summary;103mod util;