mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-08 22:59:06 +01:00
23813cc08b
The `--style` parameter now accepts a comma-separated list of strings, where every element defines either a single output component (`changes`, `grid`, `header`, `numbers`) or a predefined style (`full`, `line-numbers`, `plain`). If available, bat picks the first predefined style in the user-supplied style-list and ignores everything else. If no predefined style was requested, the other parameters that are simple output components will be used. Examples: --style changes,full,numbers Will internally be reduced to only the predefined style `full`. --style plain,full Will internally be reduced to only the predefined style `plain`. --style changes,numbers Will not be reduced, because the list does not contain any predefined styles. (Note: if `grid` is requested but no other parameters, bat still creates the left-most column with a width of `PANEL_WIDTH`. I didn't want to introduce further logic in this PR that drops or adapts the width of the left column.)
192 lines
5.3 KiB
Rust
192 lines
5.3 KiB
Rust
use ansi_term::Style;
|
|
use errors::*;
|
|
use std::borrow::Cow;
|
|
use std::io::Write;
|
|
use syntect::highlighting;
|
|
use terminal::as_terminal_escaped;
|
|
use {Colors, LineChange, LineChanges, Options, OutputComponent};
|
|
|
|
const PANEL_WIDTH: usize = 7;
|
|
|
|
pub struct Printer<'a> {
|
|
handle: &'a mut Write,
|
|
colors: Colors,
|
|
options: &'a Options<'a>,
|
|
pub line_changes: Option<LineChanges>,
|
|
}
|
|
|
|
impl<'a> Printer<'a> {
|
|
pub fn new(handle: &'a mut Write, options: &'a Options) -> Self {
|
|
let colors = if options.colored_output {
|
|
Colors::colored()
|
|
} else {
|
|
Colors::plain()
|
|
};
|
|
|
|
Printer {
|
|
handle,
|
|
colors,
|
|
options,
|
|
line_changes: None,
|
|
}
|
|
}
|
|
|
|
pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
|
|
if !self.options
|
|
.output_components
|
|
.contains(&OutputComponent::Header)
|
|
{
|
|
return Ok(());
|
|
}
|
|
|
|
if self.options
|
|
.output_components
|
|
.contains(&OutputComponent::Grid)
|
|
{
|
|
self.print_horizontal_line('┬')?;
|
|
|
|
write!(
|
|
self.handle,
|
|
"{}{} ",
|
|
" ".repeat(PANEL_WIDTH),
|
|
self.colors.grid.paint("│"),
|
|
)?;
|
|
|
|
writeln!(
|
|
self.handle,
|
|
"{}{}",
|
|
filename.map_or("", |_| "File: "),
|
|
self.colors.filename.paint(filename.unwrap_or("STDIN"))
|
|
)?;
|
|
|
|
self.print_horizontal_line('┼')
|
|
} else {
|
|
writeln!(
|
|
self.handle,
|
|
"File {}",
|
|
self.colors.filename.paint(filename.unwrap_or("STDIN"))
|
|
).map_err(Into::into)
|
|
}
|
|
}
|
|
|
|
pub fn print_footer(&mut self) -> Result<()> {
|
|
if self.options
|
|
.output_components
|
|
.contains(&OutputComponent::Grid)
|
|
{
|
|
self.print_horizontal_line('┴')
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub fn print_line(
|
|
&mut self,
|
|
line_number: usize,
|
|
regions: &[(highlighting::Style, &str)],
|
|
) -> Result<()> {
|
|
let decorations = vec![
|
|
self.print_line_number(line_number),
|
|
self.print_git_marker(line_number),
|
|
self.print_line_border(),
|
|
Some(
|
|
as_terminal_escaped(
|
|
®ions,
|
|
self.options.true_color,
|
|
self.options.colored_output,
|
|
).into(),
|
|
),
|
|
];
|
|
|
|
let grid_requested = self.options
|
|
.output_components
|
|
.contains(&OutputComponent::Grid);
|
|
write!(
|
|
self.handle,
|
|
"{}",
|
|
decorations
|
|
.into_iter()
|
|
.filter_map(|dec| if grid_requested {
|
|
Some(dec.unwrap_or(" ".into()))
|
|
} else {
|
|
dec
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join(" ")
|
|
)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn print_line_number<'s>(&self, line_number: usize) -> Option<Cow<'s, str>> {
|
|
if self.options
|
|
.output_components
|
|
.contains(&OutputComponent::Numbers)
|
|
{
|
|
Some(
|
|
self.colors
|
|
.line_number
|
|
.paint(format!("{:4}", line_number))
|
|
.to_string()
|
|
.into(),
|
|
)
|
|
} else if self.options
|
|
.output_components
|
|
.contains(&OutputComponent::Grid)
|
|
{
|
|
Some(" ".into())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn print_git_marker<'s>(&self, line_number: usize) -> Option<Cow<'s, str>> {
|
|
if self.options
|
|
.output_components
|
|
.contains(&OutputComponent::Changes)
|
|
{
|
|
Some(
|
|
if let Some(ref changes) = self.line_changes {
|
|
match changes.get(&(line_number as u32)) {
|
|
Some(&LineChange::Added) => self.colors.git_added.paint("+"),
|
|
Some(&LineChange::RemovedAbove) => self.colors.git_removed.paint("‾"),
|
|
Some(&LineChange::RemovedBelow) => self.colors.git_removed.paint("_"),
|
|
Some(&LineChange::Modified) => self.colors.git_modified.paint("~"),
|
|
_ => Style::default().paint(" "),
|
|
}
|
|
} else {
|
|
Style::default().paint(" ")
|
|
}.to_string()
|
|
.into(),
|
|
)
|
|
} else if self.options
|
|
.output_components
|
|
.contains(&OutputComponent::Grid)
|
|
{
|
|
Some(" ".into())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn print_line_border<'s>(&self) -> Option<Cow<'s, str>> {
|
|
if self.options
|
|
.output_components
|
|
.contains(&OutputComponent::Grid)
|
|
{
|
|
Some(self.colors.grid.paint("│").to_string().into())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
|
|
let hline = "─".repeat(self.options.term_width - (PANEL_WIDTH + 1));
|
|
let hline = format!("{}{}{}", "─".repeat(PANEL_WIDTH), grid_char, hline);
|
|
|
|
writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|