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

@@ -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]);
}