1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-06-25 14:22:54 +02:00

printer: add support for line number alignment

Closes #544
This commit is contained in:
Balaji Sivaraman
2018-01-01 19:30:31 +05:30
committed by Andrew Gallant
parent 5e73075ef5
commit ba1023e1e4
7 changed files with 69 additions and 3 deletions

View File

@ -102,6 +102,9 @@ pub fn app() -> App<'static, 'static> {
.value_name("GLOB"))
.arg(flag("ignore-case").short("i"))
.arg(flag("line-number").short("n"))
.arg(flag("line-number-width")
.value_name("NUM").takes_value(true)
.validator(validate_line_number_width))
.arg(flag("no-line-number").short("N").overrides_with("line-number"))
.arg(flag("quiet").short("q"))
.arg(flag("type").short("t")
@ -318,6 +321,11 @@ lazy_static! {
"Show line numbers.",
"Show line numbers (1-based). This is enabled by default when \
searching in a tty.");
doc!(h, "line-number-width",
"Left pad line numbers upto NUM width.",
"Left pad line numbers upto NUM width. Space is used as \
the default padding character. This has no effect if \
--no-line-number is enabled.");
doc!(h, "no-line-number",
"Suppress line numbers.",
"Suppress line numbers. This is enabled by default when NOT \
@ -575,6 +583,15 @@ lazy_static! {
};
}
fn validate_line_number_width(s: String) -> Result<(), String> {
if s.starts_with("0") {
Err(String::from("Custom padding characters are currently not supported. \
Please enter only a numeric value."))
} else {
validate_number(s)
}
}
fn validate_number(s: String) -> Result<(), String> {
s.parse::<usize>().map(|_|()).map_err(|err| err.to_string())
}

View File

@ -54,6 +54,7 @@ pub struct Args {
invert_match: bool,
line_number: bool,
line_per_match: bool,
line_number_width: Option<usize>,
max_columns: Option<usize>,
max_count: Option<u64>,
max_filesize: Option<u64>,
@ -144,7 +145,8 @@ impl Args {
.only_matching(self.only_matching)
.path_separator(self.path_separator)
.with_filename(self.with_filename)
.max_columns(self.max_columns);
.max_columns(self.max_columns)
.line_number_width(self.line_number_width);
if let Some(ref rep) = self.replace {
p = p.replace(rep.clone());
}
@ -336,6 +338,7 @@ impl<'a> ArgMatches<'a> {
ignore_files: self.ignore_files(),
invert_match: self.is_present("invert-match"),
line_number: line_number,
line_number_width: try!(self.usize_of("line-number-width")),
line_per_match: self.is_present("vimgrep"),
max_columns: try!(self.usize_of("max-columns")),
max_count: try!(self.usize_of("max-count")).map(|max| max as u64),

View File

@ -98,7 +98,11 @@ pub struct Printer<W> {
/// The separator to use for file paths. If empty, this is ignored.
path_separator: Option<u8>,
/// Restrict lines to this many columns.
max_columns: Option<usize>
max_columns: Option<usize>,
/// Width of line number displayed. If the number of digits in the
/// line number is less than this, it is left padded with
/// spaces.
line_number_width: Option<usize>
}
impl<W: WriteColor> Printer<W> {
@ -120,6 +124,7 @@ impl<W: WriteColor> Printer<W> {
colors: ColorSpecs::default(),
path_separator: None,
max_columns: None,
line_number_width: None
}
}
@ -208,6 +213,12 @@ impl<W: WriteColor> Printer<W> {
self
}
/// Configure the width of the displayed line number
pub fn line_number_width(mut self, line_number_width: Option<usize>) -> Printer<W> {
self.line_number_width = line_number_width;
self
}
/// Returns true if and only if something has been printed.
pub fn has_printed(&self) -> bool {
self.has_printed
@ -457,7 +468,11 @@ impl<W: WriteColor> Printer<W> {
}
fn line_number(&mut self, n: u64, sep: u8) {
self.write_colored(n.to_string().as_bytes(), |colors| colors.line());
let mut line_number = n.to_string();
if let Some(width) = self.line_number_width {
line_number = format!("{:>width$}", line_number, width = width);
}
self.write_colored(line_number.as_bytes(), |colors| colors.line());
self.separator(&[sep]);
}