summaryrefslogtreecommitdiffstats
path: root/src/path_printer.rs
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2018-08-03 17:26:22 -0400
committerAndrew Gallant <jamslam@gmail.com>2018-08-20 07:10:19 -0400
commitbb110c1ebeeda452046830b3991f705f5759da92 (patch)
treecc2b0112a3ca9b8d05cf1e953553907d71564082 /src/path_printer.rs
parentd9ca5293569efb255608d3c601107bcfe7060f15 (diff)
ripgrep: migrate to libripgrep
This commit does the work to delete the old `grep` crate and effectively rewrite most of ripgrep core to use the new libripgrep crates. The new `grep` crate is now a facade that collects the various crates that make up libripgrep. The most complex part of ripgrep core is now arguably the translation between command line parameters and the library options, which is ultimately where we want to be.
Diffstat (limited to 'src/path_printer.rs')
-rw-r--r--src/path_printer.rs101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/path_printer.rs b/src/path_printer.rs
new file mode 100644
index 00000000..324a27c4
--- /dev/null
+++ b/src/path_printer.rs
@@ -0,0 +1,101 @@
+use std::io;
+use std::path::Path;
+
+use grep::printer::{ColorSpecs, PrinterPath};
+use termcolor::WriteColor;
+
+/// A configuration for describing how paths should be written.
+#[derive(Clone, Debug)]
+struct Config {
+ colors: ColorSpecs,
+ separator: Option<u8>,
+ terminator: u8,
+}
+
+impl Default for Config {
+ fn default() -> Config {
+ Config {
+ colors: ColorSpecs::default(),
+ separator: None,
+ terminator: b'\n',
+ }
+ }
+}
+
+/// A builder for constructing things to search over.
+#[derive(Clone, Debug)]
+pub struct PathPrinterBuilder {
+ config: Config,
+}
+
+impl PathPrinterBuilder {
+ /// Return a new subject builder with a default configuration.
+ pub fn new() -> PathPrinterBuilder {
+ PathPrinterBuilder { config: Config::default() }
+ }
+
+ /// Create a new path printer with the current configuration that writes
+ /// paths to the given writer.
+ pub fn build<W: WriteColor>(&self, wtr: W) -> PathPrinter<W> {
+ PathPrinter {
+ config: self.config.clone(),
+ wtr: wtr,
+ }
+ }
+
+ /// Set the color specification for this printer.
+ ///
+ /// Currently, only the `path` component of the given specification is
+ /// used.
+ pub fn color_specs(
+ &mut self,
+ specs: ColorSpecs,
+ ) -> &mut PathPrinterBuilder {
+ self.config.colors = specs;
+ self
+ }
+
+ /// A path separator.
+ ///
+ /// When provided, the path's default separator will be replaced with
+ /// the given separator.
+ ///
+ /// This is not set by default, and the system's default path separator
+ /// will be used.
+ pub fn separator(&mut self, sep: Option<u8>) -> &mut PathPrinterBuilder {
+ self.config.separator = sep;
+ self
+ }
+
+ /// A path terminator.
+ ///
+ /// When printing a path, it will be by terminated by the given byte.
+ ///
+ /// This is set to `\n` by default.
+ pub fn terminator(&mut self, terminator: u8) -> &mut PathPrinterBuilder {
+ self.config.terminator = terminator;
+ self
+ }
+}
+
+/// A printer for emitting paths to a writer, with optional color support.
+#[derive(Debug)]
+pub struct PathPrinter<W> {
+ config: Config,
+ wtr: W,
+}
+
+impl<W: WriteColor> PathPrinter<W> {
+ /// Write the given path to the underlying writer.
+ pub fn write_path(&mut self, path: &Path) -> io::Result<()> {
+ let ppath = PrinterPath::with_separator(path, self.config.separator);
+ if !self.wtr.supports_color() {
+ self.wtr.write_all(ppath.as_bytes())?;
+ } else {
+ self.wtr.set_color(self.config.colors.path())?;
+ self.wtr.write_all(ppath.as_bytes())?;
+ self.wtr.reset()?;
+ }
+ self.wtr.write_all(&[self.config.terminator])
+ }
+}