-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
printer: implement --max-columns-preview-before #2683
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,7 @@ pub(super) const FLAGS: &[&dyn Flag] = &[ | |
&LineRegexp, | ||
&MaxColumns, | ||
&MaxColumnsPreview, | ||
&MaxColumnsPreviewBefore, | ||
&MaxCount, | ||
&MaxDepth, | ||
&MaxFilesize, | ||
|
@@ -118,6 +119,7 @@ pub(super) const FLAGS: &[&dyn Flag] = &[ | |
&Passthru, | ||
&PCRE2, | ||
&PCRE2Version, | ||
&PerMatch, | ||
&Pre, | ||
&PreGlob, | ||
&Pretty, | ||
|
@@ -3782,6 +3784,53 @@ fn test_max_columns_preview() { | |
assert_eq!(false, args.max_columns_preview); | ||
} | ||
|
||
/// --max-columns-preview-before | ||
#[derive(Debug)] | ||
struct MaxColumnsPreviewBefore; | ||
|
||
impl Flag for MaxColumnsPreviewBefore { | ||
fn is_switch(&self) -> bool { | ||
false | ||
} | ||
fn name_long(&self) -> &'static str { | ||
"max-columns-preview-before" | ||
} | ||
fn doc_variable(&self) -> Option<&'static str> { | ||
Some("NUM") | ||
} | ||
fn doc_category(&self) -> Category { | ||
Category::Output | ||
} | ||
fn doc_short(&self) -> &'static str { | ||
r"Columns to be printed before the first match in the preview." | ||
} | ||
fn doc_long(&self) -> &'static str { | ||
r" | ||
Set the maximum amount of columns before the first match should be printed in | ||
the preview. | ||
.sp | ||
When the \flag{max-columns-preview} flag is used, ripgrep will print a preview | ||
start from the beginning of the line. When this flag is combined with | ||
\flag{max-columns-preview}, the preview will start \fINUM\fP columns before the | ||
first match. | ||
.sp | ||
If the \flag{max-columns-preview} flag is not set, then this has no effect. | ||
" | ||
} | ||
|
||
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> { | ||
args.max_columns_preview_before = | ||
Some(convert::u64(&v.unwrap_value())?); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
#[test] | ||
fn test_max_columns_preview_before() { | ||
// TODO | ||
} | ||
|
||
/// -m/--max-count | ||
#[derive(Debug)] | ||
struct MaxCount; | ||
|
@@ -5349,6 +5398,42 @@ fn test_pcre2_version() { | |
assert_eq!(Some(SpecialMode::VersionPCRE2), args.special); | ||
} | ||
|
||
/// --per-match | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expose this to cli. |
||
#[derive(Debug)] | ||
struct PerMatch; | ||
|
||
impl Flag for PerMatch { | ||
fn is_switch(&self) -> bool { | ||
true | ||
} | ||
fn name_long(&self) -> &'static str { | ||
"per-match" | ||
} | ||
fn doc_category(&self) -> Category { | ||
Category::Output | ||
} | ||
fn doc_short(&self) -> &'static str { | ||
r"TODO" | ||
} | ||
fn doc_long(&self) -> &'static str { | ||
r" | ||
TODO | ||
" | ||
} | ||
|
||
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> { | ||
assert!(v.unwrap_switch(), "--per-match does not have a negation"); | ||
args.per_match = true; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
#[test] | ||
fn test_per_match() { | ||
// TODO | ||
} | ||
|
||
/// --pre | ||
#[derive(Debug)] | ||
struct Pre; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ pub(crate) struct HiArgs { | |
line_number: bool, | ||
max_columns: Option<u64>, | ||
max_columns_preview: bool, | ||
max_columns_preview_before: Option<u64>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can combine this to |
||
max_count: Option<u64>, | ||
max_depth: Option<usize>, | ||
max_filesize: Option<u64>, | ||
|
@@ -87,6 +88,7 @@ pub(crate) struct HiArgs { | |
paths: Paths, | ||
path_terminator: Option<u8>, | ||
patterns: Patterns, | ||
per_match: bool, | ||
pre: Option<PathBuf>, | ||
pre_globs: ignore::overrides::Override, | ||
quiet: bool, | ||
|
@@ -281,6 +283,7 @@ impl HiArgs { | |
line_number, | ||
max_columns: low.max_columns, | ||
max_columns_preview: low.max_columns_preview, | ||
max_columns_preview_before: low.max_columns_preview_before, | ||
max_count: low.max_count, | ||
max_depth: low.max_depth, | ||
max_filesize: low.max_filesize, | ||
|
@@ -301,6 +304,7 @@ impl HiArgs { | |
globs, | ||
path_separator: low.path_separator, | ||
path_terminator, | ||
per_match: low.per_match, | ||
pre: low.pre, | ||
pre_globs, | ||
quiet: low.quiet, | ||
|
@@ -606,13 +610,14 @@ impl HiArgs { | |
.heading(self.heading) | ||
.hyperlink(self.hyperlink_config.clone()) | ||
.max_columns_preview(self.max_columns_preview) | ||
.max_columns_preview_before(self.max_columns_preview_before) | ||
.max_columns(self.max_columns) | ||
.max_matches(self.max_count) | ||
.only_matching(self.only_matching) | ||
.path(self.with_filename) | ||
.path_terminator(self.path_terminator.clone()) | ||
.per_match_one_line(true) | ||
.per_match(self.vimgrep) | ||
.per_match(self.per_match || self.vimgrep) | ||
.replacement(self.replace.clone().map(|r| r.into())) | ||
.separator_context(self.context_separator.clone().into_bytes()) | ||
.separator_field_context( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ struct Config { | |
replacement: Arc<Option<Vec<u8>>>, | ||
max_columns: Option<u64>, | ||
max_columns_preview: bool, | ||
max_columns_preview_before: Option<u64>, | ||
max_matches: Option<u64>, | ||
column: bool, | ||
byte_offset: bool, | ||
|
@@ -72,6 +73,7 @@ impl Default for Config { | |
replacement: Arc::new(None), | ||
max_columns: None, | ||
max_columns_preview: false, | ||
max_columns_preview_before: None, | ||
max_matches: None, | ||
column: false, | ||
byte_offset: false, | ||
|
@@ -326,6 +328,19 @@ impl StandardBuilder { | |
self | ||
} | ||
|
||
/// Set the maximum amount of columns before the first match should be | ||
/// printed in the preview. | ||
/// | ||
/// When this is not set (the default), the preview will print from the | ||
/// beginning of the line. | ||
pub fn max_columns_preview_before( | ||
&mut self, | ||
limit: Option<u64>, | ||
) -> &mut StandardBuilder { | ||
self.config.max_columns_preview_before = limit; | ||
self | ||
} | ||
|
||
/// Set the maximum amount of matching lines that are printed. | ||
/// | ||
/// If multi line search is enabled and a match spans multiple lines, then | ||
|
@@ -1360,6 +1375,13 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> { | |
) -> io::Result<()> { | ||
if self.config().max_columns_preview { | ||
let original = line; | ||
if let Some(p) = self.config().max_columns_preview_before { | ||
let start = matches[0].start().saturating_sub(p as usize); | ||
if start != 0 { | ||
self.write(b"[... omitted start of long line] ")?; | ||
line = line.with_start(start); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compare to the full |
||
let end = bytes[line] | ||
.grapheme_indices() | ||
.map(|(_, end, _)| end) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a better name?