style: add component 'rule' for horizontal file delimiter

This commit is contained in:
Tom Milligan
2020-10-06 16:57:47 +01:00
parent 33128d75f2
commit aa4000cb0d
23 changed files with 441 additions and 9 deletions

View File

@ -367,7 +367,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.validator(|val| {
let mut invalid_vals = val.split(',').filter(|style| {
!&[
"auto", "full", "plain", "header", "grid", "numbers", "snip",
"auto", "full", "plain", "header", "grid", "rule", "numbers", "snip",
#[cfg(feature = "git")]
"changes",
]
@ -382,7 +382,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
})
.help(
"Comma-separated list of style elements to display \
(*auto*, full, plain, changes, header, grid, numbers, snip).",
(*auto*, full, plain, changes, header, grid, rule, numbers, snip).",
)
.long_help(
"Configure which elements (line numbers, file headers, grid \
@ -400,6 +400,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
* header: show filenames before the content.\n \
* grid: vertical/horizontal lines to separate side bar\n \
and the header from the content.\n \
* rule: horizontal lines to delimit files.\n \
* numbers: show line numbers in the side bar.\n \
* snip: draw separation lines between distinct line ranges.",
),

View File

@ -23,6 +23,7 @@ struct ActiveStyleComponents {
header: bool,
vcs_modification_markers: bool,
grid: bool,
rule: bool,
line_numbers: bool,
snip: bool,
}
@ -179,6 +180,12 @@ impl<'a> PrettyPrinter<'a> {
self
}
/// Whether to paint a horizontal rule to delimit files
pub fn rule(&mut self, yes: bool) -> &mut Self {
self.active_style_components.rule = yes;
self
}
/// Whether to show modification markers for VCS changes. This has no effect if
/// the `git` feature is not activated.
#[cfg_attr(
@ -285,6 +292,9 @@ impl<'a> PrettyPrinter<'a> {
if self.active_style_components.grid {
style_components.push(StyleComponent::Grid);
}
if self.active_style_components.rule {
style_components.push(StyleComponent::Rule);
}
if self.active_style_components.header {
style_components.push(StyleComponent::Header);
}

View File

@ -200,13 +200,18 @@ impl<'a> InteractivePrinter<'a> {
})
}
fn print_horizontal_line_term(&mut self, handle: &mut dyn Write, style: Style) -> Result<()> {
writeln!(
handle,
"{}",
style.paint("".repeat(self.config.term_width))
)?;
Ok(())
}
fn print_horizontal_line(&mut self, handle: &mut dyn Write, grid_char: char) -> Result<()> {
if self.panel_width == 0 {
writeln!(
handle,
"{}",
self.colors.grid.paint("".repeat(self.config.term_width))
)?;
self.print_horizontal_line_term(handle, self.colors.grid)?;
} else {
let hline = "".repeat(self.config.term_width - (self.panel_width + 1));
let hline = format!("{}{}{}", "".repeat(self.panel_width), grid_char, hline);
@ -251,6 +256,10 @@ impl<'a> Printer for InteractivePrinter<'a> {
input: &OpenedInput,
add_header_padding: bool,
) -> Result<()> {
if add_header_padding && self.config.style_components.rule() {
self.print_horizontal_line_term(handle, self.colors.rule)?;
}
if !self.config.style_components.header() {
if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
writeln!(
@ -279,7 +288,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
.paint(if self.panel_width > 0 { "" } else { "" }),
)?;
} else {
if add_header_padding {
// Only pad space between files, if we haven't already drawn a horizontal rule
if add_header_padding && !self.config.style_components.rule() {
writeln!(handle)?;
}
write!(handle, "{}", " ".repeat(self.panel_width))?;
@ -603,6 +613,7 @@ const DEFAULT_GUTTER_COLOR: u8 = 238;
#[derive(Debug, Default)]
pub struct Colors {
pub grid: Style,
pub rule: Style,
pub filename: Style,
pub git_added: Style,
pub git_removed: Style,
@ -624,6 +635,7 @@ impl Colors {
Colors {
grid: gutter_color.normal(),
rule: gutter_color.normal(),
filename: Style::new().bold(),
git_added: Green.normal(),
git_removed: Red.normal(),

View File

@ -9,6 +9,7 @@ pub enum StyleComponent {
#[cfg(feature = "git")]
Changes,
Grid,
Rule,
Header,
LineNumbers,
Snip,
@ -29,6 +30,7 @@ impl StyleComponent {
#[cfg(feature = "git")]
StyleComponent::Changes => &[StyleComponent::Changes],
StyleComponent::Grid => &[StyleComponent::Grid],
StyleComponent::Rule => &[StyleComponent::Rule],
StyleComponent::Header => &[StyleComponent::Header],
StyleComponent::LineNumbers => &[StyleComponent::LineNumbers],
StyleComponent::Snip => &[StyleComponent::Snip],
@ -54,6 +56,7 @@ impl FromStr for StyleComponent {
#[cfg(feature = "git")]
"changes" => Ok(StyleComponent::Changes),
"grid" => Ok(StyleComponent::Grid),
"rule" => Ok(StyleComponent::Rule),
"header" => Ok(StyleComponent::Header),
"numbers" => Ok(StyleComponent::LineNumbers),
"snip" => Ok(StyleComponent::Snip),
@ -81,6 +84,10 @@ impl StyleComponents {
self.0.contains(&StyleComponent::Grid)
}
pub fn rule(&self) -> bool {
self.0.contains(&StyleComponent::Rule)
}
pub fn header(&self) -> bool {
self.0.contains(&StyleComponent::Header)
}