mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-01-19 05:49:14 +02:00
Add support for printing column numbers.
This commit is contained in:
parent
feff1849c8
commit
5938bed339
@ -75,6 +75,11 @@ Less common options:
|
|||||||
-C, --context NUM
|
-C, --context NUM
|
||||||
Show NUM lines before and after each match.
|
Show NUM lines before and after each match.
|
||||||
|
|
||||||
|
--column
|
||||||
|
Show column numbers (1 based) in output. This only shows the column
|
||||||
|
numbers for the first match on each line. Note that this doesn't try
|
||||||
|
to account for Unicode. One byte is equal to one column.
|
||||||
|
|
||||||
--context-separator ARG
|
--context-separator ARG
|
||||||
The string to use when separating non-continuous context lines. Escape
|
The string to use when separating non-continuous context lines. Escape
|
||||||
sequences may be used. [default: --]
|
sequences may be used. [default: --]
|
||||||
@ -146,6 +151,7 @@ pub struct RawArgs {
|
|||||||
flag_after_context: usize,
|
flag_after_context: usize,
|
||||||
flag_before_context: usize,
|
flag_before_context: usize,
|
||||||
flag_color: String,
|
flag_color: String,
|
||||||
|
flag_column: bool,
|
||||||
flag_context: usize,
|
flag_context: usize,
|
||||||
flag_context_separator: String,
|
flag_context_separator: String,
|
||||||
flag_count: bool,
|
flag_count: bool,
|
||||||
@ -186,6 +192,7 @@ pub struct Args {
|
|||||||
after_context: usize,
|
after_context: usize,
|
||||||
before_context: usize,
|
before_context: usize,
|
||||||
color: bool,
|
color: bool,
|
||||||
|
column: bool,
|
||||||
context_separator: Vec<u8>,
|
context_separator: Vec<u8>,
|
||||||
count: bool,
|
count: bool,
|
||||||
eol: u8,
|
eol: u8,
|
||||||
@ -296,6 +303,7 @@ impl RawArgs {
|
|||||||
after_context: after_context,
|
after_context: after_context,
|
||||||
before_context: before_context,
|
before_context: before_context,
|
||||||
color: color,
|
color: color,
|
||||||
|
column: self.flag_column,
|
||||||
context_separator: unescape(&self.flag_context_separator),
|
context_separator: unescape(&self.flag_context_separator),
|
||||||
count: self.flag_count,
|
count: self.flag_count,
|
||||||
eol: eol,
|
eol: eol,
|
||||||
@ -401,6 +409,7 @@ impl Args {
|
|||||||
/// writer given.
|
/// writer given.
|
||||||
pub fn printer<W: Send + io::Write>(&self, wtr: W) -> Printer<W> {
|
pub fn printer<W: Send + io::Write>(&self, wtr: W) -> Printer<W> {
|
||||||
let mut p = Printer::new(wtr, self.color)
|
let mut p = Printer::new(wtr, self.color)
|
||||||
|
.column(self.column)
|
||||||
.context_separator(self.context_separator.clone())
|
.context_separator(self.context_separator.clone())
|
||||||
.eol(self.eol)
|
.eol(self.eol)
|
||||||
.heading(self.heading)
|
.heading(self.heading)
|
||||||
|
@ -22,6 +22,8 @@ pub struct Printer<W> {
|
|||||||
wtr: Writer<W>,
|
wtr: Writer<W>,
|
||||||
/// Whether anything has been printed to wtr yet.
|
/// Whether anything has been printed to wtr yet.
|
||||||
has_printed: bool,
|
has_printed: bool,
|
||||||
|
/// Whether to show column numbers for the first match or not.
|
||||||
|
column: bool,
|
||||||
/// The string to use to separate non-contiguous runs of context lines.
|
/// The string to use to separate non-contiguous runs of context lines.
|
||||||
context_separator: Vec<u8>,
|
context_separator: Vec<u8>,
|
||||||
/// The end-of-line terminator used by the printer. In general, eols are
|
/// The end-of-line terminator used by the printer. In general, eols are
|
||||||
@ -48,6 +50,7 @@ impl<W: Send + io::Write> Printer<W> {
|
|||||||
Printer {
|
Printer {
|
||||||
wtr: Writer::new(wtr, color),
|
wtr: Writer::new(wtr, color),
|
||||||
has_printed: false,
|
has_printed: false,
|
||||||
|
column: false,
|
||||||
context_separator: "--".to_string().into_bytes(),
|
context_separator: "--".to_string().into_bytes(),
|
||||||
eol: b'\n',
|
eol: b'\n',
|
||||||
heading: false,
|
heading: false,
|
||||||
@ -57,6 +60,13 @@ impl<W: Send + io::Write> Printer<W> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// When set, column numbers will be printed for the first match on each
|
||||||
|
/// line.
|
||||||
|
pub fn column(mut self, yes: bool) -> Printer<W> {
|
||||||
|
self.column = yes;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set the context separator. The default is `--`.
|
/// Set the context separator. The default is `--`.
|
||||||
pub fn context_separator(mut self, sep: Vec<u8>) -> Printer<W> {
|
pub fn context_separator(mut self, sep: Vec<u8>) -> Printer<W> {
|
||||||
self.context_separator = sep;
|
self.context_separator = sep;
|
||||||
@ -173,6 +183,11 @@ impl<W: Send + io::Write> Printer<W> {
|
|||||||
if let Some(line_number) = line_number {
|
if let Some(line_number) = line_number {
|
||||||
self.line_number(line_number, b':');
|
self.line_number(line_number, b':');
|
||||||
}
|
}
|
||||||
|
if self.column {
|
||||||
|
let c = re.find(&buf[start..end]).map(|(s, _)| s + 1).unwrap_or(0);
|
||||||
|
self.write(c.to_string().as_bytes());
|
||||||
|
self.write(b":");
|
||||||
|
}
|
||||||
if self.replace.is_some() {
|
if self.replace.is_some() {
|
||||||
let line = re.replace_all(
|
let line = re.replace_all(
|
||||||
&buf[start..end], &**self.replace.as_ref().unwrap());
|
&buf[start..end], &**self.replace.as_ref().unwrap());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user