Skip to content

Commit 23e2113

Browse files
committed
printer: move PathPrinter into grep-printer
I originally did not put PathPrinter into grep-printer because I considered it somewhat extraneous to what a "grep" program does, and also that its implementation was rather simple. But now with hyperlink support, its implementation has grown a smidge more complicated. And more importantly, its existence required exposing a lot more of the hyperlink guts. Without it, we can keep things like HyperlinkPath and HyperlinkSpan completely private. We can now also keep `PrinterPath` completely private as well. And this is a breaking change.
1 parent 0990556 commit 23e2113

File tree

9 files changed

+222
-159
lines changed

9 files changed

+222
-159
lines changed

crates/core/args.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use grep::pcre2::{
1818
RegexMatcherBuilder as PCRE2RegexMatcherBuilder,
1919
};
2020
use grep::printer::{
21-
default_color_specs, ColorSpecs, HyperlinkPattern, JSONBuilder, Standard,
22-
StandardBuilder, Stats, Summary, SummaryBuilder, SummaryKind, JSON,
21+
default_color_specs, ColorSpecs, HyperlinkPattern, JSONBuilder,
22+
PathPrinter, PathPrinterBuilder, Standard, StandardBuilder, Stats,
23+
Summary, SummaryBuilder, SummaryKind, JSON,
2324
};
2425
use grep::regex::{
2526
RegexMatcher as RustRegexMatcher,
@@ -38,7 +39,6 @@ use crate::app;
3839
use crate::config;
3940
use crate::logger::Logger;
4041
use crate::messages::{set_ignore_messages, set_messages};
41-
use crate::path_printer::{PathPrinter, PathPrinterBuilder};
4242
use crate::search::{
4343
PatternMatcher, Printer, SearchWorker, SearchWorkerBuilder,
4444
};

crates/core/main.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ mod app;
1616
mod args;
1717
mod config;
1818
mod logger;
19-
mod path_printer;
2019
mod search;
2120
mod subject;
2221

@@ -248,7 +247,7 @@ fn files(args: &Args) -> Result<bool> {
248247
if quit_after_match {
249248
break;
250249
}
251-
if let Err(err) = path_printer.write_path(subject.path()) {
250+
if let Err(err) = path_printer.write(subject.path()) {
252251
// A broken pipe means graceful termination.
253252
if err.kind() == io::ErrorKind::BrokenPipe {
254253
break;
@@ -293,7 +292,7 @@ fn files_parallel(args: &Args) -> Result<bool> {
293292

294293
let print_thread = thread::spawn(move || -> io::Result<()> {
295294
for subject in rx.iter() {
296-
path_printer.write_path(subject.path())?;
295+
path_printer.write(subject.path())?;
297296
}
298297
Ok(())
299298
});

crates/core/path_printer.rs

-134
This file was deleted.

crates/printer/src/hyperlink.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub enum HyperlinkPatternError {
5959

6060
/// The values to replace the pattern placeholders with.
6161
#[derive(Clone, Debug)]
62-
pub struct HyperlinkValues<'a> {
62+
pub(crate) struct HyperlinkValues<'a> {
6363
file: &'a HyperlinkPath,
6464
line: u64,
6565
column: u64,
@@ -70,7 +70,7 @@ pub struct HyperlinkValues<'a> {
7070
/// This is the value to use as-is in the hyperlink, converted from an OS file
7171
/// path.
7272
#[derive(Clone, Debug)]
73-
pub struct HyperlinkPath(Vec<u8>);
73+
pub(crate) struct HyperlinkPath(Vec<u8>);
7474

7575
impl HyperlinkPatternBuilder {
7676
/// Creates a new hyperlink pattern builder.
@@ -222,7 +222,7 @@ impl HyperlinkPattern {
222222
}
223223

224224
/// Renders this pattern with the given values to the given output.
225-
pub fn render(
225+
pub(crate) fn render(
226226
&self,
227227
values: &HyperlinkValues,
228228
output: &mut impl Write,
@@ -353,7 +353,7 @@ impl std::error::Error for HyperlinkPatternError {}
353353

354354
impl<'a> HyperlinkValues<'a> {
355355
/// Creates a new set of hyperlink values.
356-
pub fn new(
356+
pub(crate) fn new(
357357
file: &'a HyperlinkPath,
358358
line: Option<u64>,
359359
column: Option<u64>,
@@ -369,7 +369,7 @@ impl<'a> HyperlinkValues<'a> {
369369
impl HyperlinkPath {
370370
/// Returns a hyperlink path from an OS path.
371371
#[cfg(unix)]
372-
pub fn from_path(path: &Path) -> Option<Self> {
372+
pub(crate) fn from_path(path: &Path) -> Option<Self> {
373373
// On Unix, this function returns the absolute file path without the
374374
// leading slash, as it makes for more natural hyperlink patterns, for
375375
// instance:
@@ -506,14 +506,14 @@ impl std::fmt::Display for HyperlinkPath {
506506
/// A simple abstraction over a hyperlink span written to the terminal. This
507507
/// helps tracking whether a hyperlink has been started, and should be ended.
508508
#[derive(Debug, Default)]
509-
pub struct HyperlinkSpan {
509+
pub(crate) struct HyperlinkSpan {
510510
active: bool,
511511
}
512512

513513
impl HyperlinkSpan {
514514
/// Starts a hyperlink and returns a span which tracks whether it is still
515515
/// in effect.
516-
pub fn start(
516+
pub(crate) fn start(
517517
wtr: &mut impl WriteColor,
518518
hyperlink: &HyperlinkSpec,
519519
) -> io::Result<Self> {
@@ -526,7 +526,7 @@ impl HyperlinkSpan {
526526
}
527527

528528
/// Ends the hyperlink span if it is active.
529-
pub fn end(&mut self, wtr: &mut impl WriteColor) -> io::Result<()> {
529+
pub(crate) fn end(&mut self, wtr: &mut impl WriteColor) -> io::Result<()> {
530530
if self.is_active() {
531531
wtr.set_hyperlink(&HyperlinkSpec::close())?;
532532
self.active = false;
@@ -535,7 +535,7 @@ impl HyperlinkSpan {
535535
}
536536

537537
/// Returns true if there is currently an active hyperlink.
538-
pub fn is_active(&self) -> bool {
538+
pub(crate) fn is_active(&self) -> bool {
539539
self.active
540540
}
541541
}

crates/printer/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ assert_eq!(output, expected);
6565
pub use crate::{
6666
color::{default_color_specs, ColorError, ColorSpecs, UserColorSpec},
6767
hyperlink::{
68-
HyperlinkPath, HyperlinkPattern, HyperlinkPatternBuilder,
69-
HyperlinkPatternError, HyperlinkSpan, HyperlinkValues,
68+
HyperlinkPattern, HyperlinkPatternBuilder, HyperlinkPatternError,
7069
},
70+
path::{PathPrinter, PathPrinterBuilder},
7171
standard::{Standard, StandardBuilder, StandardSink},
7272
stats::Stats,
7373
summary::{Summary, SummaryBuilder, SummaryKind, SummarySink},
74-
util::PrinterPath,
7574
};
7675

7776
#[cfg(feature = "serde")]
@@ -99,6 +98,7 @@ mod hyperlink_aliases;
9998
mod json;
10099
#[cfg(feature = "serde")]
101100
mod jsont;
101+
mod path;
102102
mod standard;
103103
mod stats;
104104
mod summary;

0 commit comments

Comments
 (0)