diff options
author | Andrew Gallant <jamslam@gmail.com> | 2019-04-13 18:35:24 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2019-04-14 19:29:27 -0400 |
commit | ece1f50cfe49fea324d96f97a2ae00ebbd0cad03 (patch) | |
tree | 8615570232a16f572702619333b446a0a530772c /src | |
parent | a7d26c8f144a4957b75f71087a66692d0b25759a (diff) |
printer: support previews for long lines
This commit adds support for showing a preview of long lines. While the
default still remains as completely suppressing the entire line, this
new functionality will show the first N graphemes of a matching line,
including the number of matches that are suppressed.
This was unfortunately a fairly invasive change to the printer that
required a bit of refactoring. On the bright side, the single line
and multi-line coloring are now more unified than they were before.
Closes #1078
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 25 | ||||
-rw-r--r-- | src/args.rs | 7 |
2 files changed, 32 insertions, 0 deletions
@@ -582,6 +582,7 @@ pub fn all_args_and_flags() -> Vec<RGArg> { flag_line_number(&mut args); flag_line_regexp(&mut args); flag_max_columns(&mut args); + flag_max_column_preview(&mut args); flag_max_count(&mut args); flag_max_depth(&mut args); flag_max_filesize(&mut args); @@ -1443,6 +1444,30 @@ When this flag is omitted or is set to 0, then it has no effect. args.push(arg); } +fn flag_max_column_preview(args: &mut Vec<RGArg>) { + const SHORT: &str = "Print a preview for lines exceeding the limit."; + const LONG: &str = long!("\ +When the '--max-columns' flag is used, ripgrep will by default completely +replace any line that is too long with a message indicating that a matching +line was removed. When this flag is combined with '--max-columns', a preview +of the line (corresponding to the limit size) is shown instead, where the part +of the line exceeding the limit is not shown. + +If the '--max-columns' flag is not set, then this has no effect. + +This flag can be disabled with '--no-max-column-preview'. +"); + let arg = RGArg::switch("max-column-preview") + .help(SHORT).long_help(LONG) + .overrides("no-max-column-preview"); + args.push(arg); + + let arg = RGArg::switch("no-max-column-preview") + .hidden() + .overrides("max-column-preview"); + args.push(arg); +} + fn flag_max_count(args: &mut Vec<RGArg>) { const SHORT: &str = "Limit the number of matches."; const LONG: &str = long!("\ diff --git a/src/args.rs b/src/args.rs index 6a5f09f9..1a5b8a31 100644 --- a/src/args.rs +++ b/src/args.rs @@ -746,6 +746,7 @@ impl ArgMatches { .per_match(self.is_present("vimgrep")) .replacement(self.replacement()) .max_columns(self.max_columns()?) + .max_column_preview(self.max_column_preview()) .max_matches(self.max_count()?) .column(self.column()) .byte_offset(self.is_present("byte-offset")) @@ -1142,6 +1143,12 @@ impl ArgMatches { Ok(self.usize_of_nonzero("max-columns")?.map(|n| n as u64)) } + /// Returns true if and only if a preview should be shown for lines that + /// exceed the maximum column limit. + fn max_column_preview(&self) -> bool { + self.is_present("max-column-preview") + } + /// The maximum number of matches permitted. fn max_count(&self) -> Result<Option<u64>> { Ok(self.usize_of("max-count")?.map(|n| n as u64)) |