diff options
author | Balaji Sivaraman <balaji@balajisivaraman.com> | 2018-01-01 19:30:31 +0530 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2018-01-01 09:00:31 -0500 |
commit | ba1023e1e45d7bd6edffa43d06c9613d3b84de8f (patch) | |
tree | 4fee4b0b3ef368344713e5fcac8e75593337d543 /src | |
parent | 5e73075ef5300fdec03f6c4685750788108b00f4 (diff) |
printer: add support for line number alignment
Closes #544
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 17 | ||||
-rw-r--r-- | src/args.rs | 5 | ||||
-rw-r--r-- | src/printer.rs | 19 |
3 files changed, 38 insertions, 3 deletions
@@ -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()) } diff --git a/src/args.rs b/src/args.rs index 106762c4..f2c9de44 100644 --- a/src/args.rs +++ b/src/args.rs @@ -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), diff --git a/src/printer.rs b/src/printer.rs index 37680f9b..2fb4ed60 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -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]); } |