mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-07-31 04:02:00 -07:00
libripgrep is not any one library, but rather, a collection of libraries that roughly separate the following key distinct phases in a grep implementation: 1. Pattern matching (e.g., by a regex engine). 2. Searching a file using a pattern matcher. 3. Printing results. Ultimately, both (1) and (3) are defined by de-coupled interfaces, of which there may be multiple implementations. Namely, (1) is satisfied by the `Matcher` trait in the `grep-matcher` crate and (3) is satisfied by the `Sink` trait in the `grep2` crate. The searcher (2) ties everything together and finds results using a matcher and reports those results using a `Sink` implementation. Closes #162
91 lines
2.1 KiB
Rust
91 lines
2.1 KiB
Rust
use std::io::{self, Write};
|
|
|
|
use termcolor::{ColorSpec, WriteColor};
|
|
|
|
/// A writer that counts the number of bytes that have been successfully
|
|
/// written.
|
|
#[derive(Clone, Debug)]
|
|
pub struct CounterWriter<W> {
|
|
wtr: W,
|
|
count: u64,
|
|
total_count: u64,
|
|
}
|
|
|
|
impl<W: Write> CounterWriter<W> {
|
|
pub fn new(wtr: W) -> CounterWriter<W> {
|
|
CounterWriter { wtr: wtr, count: 0, total_count: 0 }
|
|
}
|
|
}
|
|
|
|
impl<W> CounterWriter<W> {
|
|
/// Returns the total number of bytes written since construction or the
|
|
/// last time `reset` was called.
|
|
pub fn count(&self) -> u64 {
|
|
self.count
|
|
}
|
|
|
|
/// Returns the total number of bytes written since construction.
|
|
pub fn total_count(&self) -> u64 {
|
|
self.total_count + self.count
|
|
}
|
|
|
|
/// Resets the number of bytes written to `0`.
|
|
pub fn reset_count(&mut self) {
|
|
self.total_count += self.count;
|
|
self.count = 0;
|
|
}
|
|
|
|
/// Clear resets all counting related state for this writer.
|
|
///
|
|
/// After this call, the total count of bytes written to the underlying
|
|
/// writer is erased and reset.
|
|
#[allow(dead_code)]
|
|
pub fn clear(&mut self) {
|
|
self.count = 0;
|
|
self.total_count = 0;
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn get_ref(&self) -> &W {
|
|
&self.wtr
|
|
}
|
|
|
|
pub fn get_mut(&mut self) -> &mut W {
|
|
&mut self.wtr
|
|
}
|
|
|
|
pub fn into_inner(self) -> W {
|
|
self.wtr
|
|
}
|
|
}
|
|
|
|
impl<W: Write> Write for CounterWriter<W> {
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
|
|
let n = self.wtr.write(buf)?;
|
|
self.count += n as u64;
|
|
Ok(n)
|
|
}
|
|
|
|
fn flush(&mut self) -> Result<(), io::Error> {
|
|
self.wtr.flush()
|
|
}
|
|
}
|
|
|
|
impl<W: WriteColor> WriteColor for CounterWriter<W> {
|
|
fn supports_color(&self) -> bool {
|
|
self.wtr.supports_color()
|
|
}
|
|
|
|
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
|
|
self.wtr.set_color(spec)
|
|
}
|
|
|
|
fn reset(&mut self) -> io::Result<()> {
|
|
self.wtr.reset()
|
|
}
|
|
|
|
fn is_synchronous(&self) -> bool {
|
|
self.wtr.is_synchronous()
|
|
}
|
|
}
|