2018-05-16 23:19:36 +02:00
|
|
|
use Colors;
|
2018-05-10 23:39:13 +02:00
|
|
|
use app::Config;
|
2018-05-16 23:19:36 +02:00
|
|
|
use console::AnsiCodeIterator;
|
2018-05-16 02:45:58 +02:00
|
|
|
use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
|
2018-05-14 03:44:07 +02:00
|
|
|
use diff::LineChanges;
|
2018-05-07 01:32:00 +02:00
|
|
|
use errors::*;
|
2018-05-16 02:45:58 +02:00
|
|
|
use std::boxed::Box;
|
2018-05-07 01:32:00 +02:00
|
|
|
use std::io::Write;
|
2018-05-14 03:44:07 +02:00
|
|
|
use std::vec::Vec;
|
2018-05-16 02:45:58 +02:00
|
|
|
use style::OutputWrap;
|
2018-05-07 01:32:00 +02:00
|
|
|
use syntect::highlighting;
|
|
|
|
use terminal::as_terminal_escaped;
|
|
|
|
|
|
|
|
pub struct Printer<'a> {
|
|
|
|
handle: &'a mut Write,
|
|
|
|
colors: Colors,
|
2018-05-10 23:39:13 +02:00
|
|
|
config: &'a Config<'a>,
|
2018-05-14 03:44:07 +02:00
|
|
|
decorations: Vec<Box<Decoration>>,
|
2018-05-12 06:59:26 +02:00
|
|
|
panel_width: usize,
|
2018-05-18 11:56:25 +02:00
|
|
|
line_number: usize,
|
2018-05-07 01:32:00 +02:00
|
|
|
pub line_changes: Option<LineChanges>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Printer<'a> {
|
2018-05-10 23:39:13 +02:00
|
|
|
pub fn new(handle: &'a mut Write, config: &'a Config) -> Self {
|
|
|
|
let colors = if config.colored_output {
|
2018-05-07 01:32:00 +02:00
|
|
|
Colors::colored()
|
|
|
|
} else {
|
|
|
|
Colors::plain()
|
|
|
|
};
|
|
|
|
|
2018-05-14 03:44:07 +02:00
|
|
|
// Create decorations.
|
|
|
|
let mut decorations: Vec<Box<Decoration>> = Vec::new();
|
|
|
|
|
|
|
|
if config.output_components.numbers() {
|
|
|
|
decorations.push(Box::new(LineNumberDecoration::new(&colors)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.output_components.changes() {
|
|
|
|
decorations.push(Box::new(LineChangesDecoration::new(&colors)));
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:09:51 +02:00
|
|
|
let mut panel_width: usize =
|
2018-05-14 03:44:07 +02:00
|
|
|
decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
|
|
|
|
|
|
|
|
// The grid border decoration isn't added until after the panel_width calculation, since the
|
|
|
|
// print_horizontal_line, print_header, and print_footer functions all assume the panel
|
|
|
|
// width is without the grid border.
|
|
|
|
if config.output_components.grid() && decorations.len() > 0 {
|
|
|
|
decorations.push(Box::new(GridBorderDecoration::new(&colors)));
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:09:51 +02:00
|
|
|
// Disable the panel if the terminal is too small (i.e. can't fit 5 characters with the
|
|
|
|
// panel showing).
|
2018-05-16 02:45:58 +02:00
|
|
|
if config.term_width
|
|
|
|
< (decorations.len() + decorations.iter().fold(0, |a, x| a + x.width())) + 5
|
|
|
|
{
|
2018-05-15 23:09:51 +02:00
|
|
|
decorations.clear();
|
|
|
|
panel_width = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-14 03:44:07 +02:00
|
|
|
// Create printer.
|
|
|
|
Printer {
|
|
|
|
panel_width,
|
2018-05-07 01:32:00 +02:00
|
|
|
handle,
|
|
|
|
colors,
|
2018-05-10 23:39:13 +02:00
|
|
|
config,
|
2018-05-14 03:44:07 +02:00
|
|
|
decorations,
|
2018-05-18 11:56:25 +02:00
|
|
|
line_number: 0,
|
2018-05-07 01:32:00 +02:00
|
|
|
line_changes: None,
|
2018-05-14 03:44:07 +02:00
|
|
|
}
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 19:59:40 +02:00
|
|
|
pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
|
2018-05-10 23:39:13 +02:00
|
|
|
if !self.config.output_components.header() {
|
Make `--style` parameter more flexible
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.)
2018-05-06 20:15:46 +02:00
|
|
|
return Ok(());
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 23:39:13 +02:00
|
|
|
if self.config.output_components.grid() {
|
Make `--style` parameter more flexible
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.)
2018-05-06 20:15:46 +02:00
|
|
|
self.print_horizontal_line('┬')?;
|
|
|
|
|
|
|
|
write!(
|
|
|
|
self.handle,
|
|
|
|
"{}{} ",
|
2018-05-12 06:59:26 +02:00
|
|
|
" ".repeat(self.panel_width),
|
2018-05-12 15:32:23 +02:00
|
|
|
self.colors
|
|
|
|
.grid
|
|
|
|
.paint(if self.panel_width > 0 { "│" } else { "" }),
|
Make `--style` parameter more flexible
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.)
2018-05-06 20:15:46 +02:00
|
|
|
)?;
|
2018-05-09 22:34:03 +02:00
|
|
|
}
|
Make `--style` parameter more flexible
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.)
2018-05-06 20:15:46 +02:00
|
|
|
|
2018-05-09 22:34:03 +02:00
|
|
|
writeln!(
|
|
|
|
self.handle,
|
|
|
|
"{}{}",
|
|
|
|
filename.map_or("", |_| "File: "),
|
|
|
|
self.colors.filename.paint(filename.unwrap_or("STDIN"))
|
|
|
|
)?;
|
Make `--style` parameter more flexible
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.)
2018-05-06 20:15:46 +02:00
|
|
|
|
2018-05-10 23:39:13 +02:00
|
|
|
if self.config.output_components.grid() {
|
2018-05-09 22:34:03 +02:00
|
|
|
self.print_horizontal_line('┼')?;
|
Make `--style` parameter more flexible
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.)
2018-05-06 20:15:46 +02:00
|
|
|
}
|
2018-05-09 22:34:03 +02:00
|
|
|
|
|
|
|
Ok(())
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_footer(&mut self) -> Result<()> {
|
2018-05-10 23:39:13 +02:00
|
|
|
if self.config.output_components.grid() {
|
2018-05-07 01:32:00 +02:00
|
|
|
self.print_horizontal_line('┴')
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:56:25 +02:00
|
|
|
pub fn print_line(&mut self, regions: &[(highlighting::Style, &str)]) -> Result<()> {
|
|
|
|
self.line_number += 1;
|
2018-05-12 15:32:23 +02:00
|
|
|
let mut cursor: usize = 0;
|
2018-05-12 22:44:10 +02:00
|
|
|
let mut cursor_max: usize = self.config.term_width;
|
2018-05-14 03:44:07 +02:00
|
|
|
let mut panel_wrap: Option<String> = None;
|
|
|
|
|
|
|
|
// Line decorations.
|
|
|
|
if self.panel_width > 0 {
|
2018-05-16 23:19:36 +02:00
|
|
|
let decorations = self.decorations
|
2018-05-14 03:44:07 +02:00
|
|
|
.iter()
|
2018-05-18 11:56:25 +02:00
|
|
|
.map(|ref d| d.generate(self.line_number, false, self))
|
2018-05-14 03:44:07 +02:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
for deco in decorations {
|
|
|
|
write!(self.handle, "{} ", deco.text)?;
|
|
|
|
cursor_max -= deco.width + 1;
|
2018-05-12 22:44:10 +02:00
|
|
|
}
|
2018-05-14 03:44:07 +02:00
|
|
|
}
|
2018-05-12 22:44:10 +02:00
|
|
|
|
|
|
|
// Line contents.
|
|
|
|
if self.config.output_wrap == OutputWrap::None {
|
|
|
|
let true_color = self.config.true_color;
|
|
|
|
let colored_output = self.config.colored_output;
|
|
|
|
|
2018-05-13 12:26:23 +02:00
|
|
|
write!(
|
|
|
|
self.handle,
|
|
|
|
"{}",
|
|
|
|
regions
|
|
|
|
.iter()
|
|
|
|
.map(|&(style, text)| as_terminal_escaped(
|
|
|
|
style,
|
|
|
|
text,
|
|
|
|
true_color,
|
2018-05-14 03:44:07 +02:00
|
|
|
colored_output,
|
2018-05-13 12:26:23 +02:00
|
|
|
))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("")
|
2018-05-12 22:44:10 +02:00
|
|
|
)?;
|
Make `--style` parameter more flexible
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.)
2018-05-06 20:15:46 +02:00
|
|
|
} else {
|
2018-05-16 23:19:36 +02:00
|
|
|
for &(style, region) in regions.iter() {
|
|
|
|
let mut ansi_iterator = AnsiCodeIterator::new(region);
|
|
|
|
let mut ansi_prefix = "";
|
|
|
|
while let Some(chunk) = ansi_iterator.next() {
|
|
|
|
match chunk {
|
|
|
|
// ANSI escape passthrough.
|
|
|
|
(text, true) => {
|
|
|
|
ansi_prefix = text;
|
|
|
|
}
|
2018-05-12 22:23:33 +02:00
|
|
|
|
2018-05-16 23:19:36 +02:00
|
|
|
// Regular text.
|
|
|
|
(text, false) => {
|
|
|
|
let text = text.trim_right_matches(|c| c == '\r' || c == '\n');
|
|
|
|
let mut chars = text.chars();
|
|
|
|
let mut remaining = text.chars().count();
|
|
|
|
|
|
|
|
while remaining > 0 {
|
|
|
|
let available = cursor_max - cursor;
|
|
|
|
|
|
|
|
// It fits.
|
|
|
|
if remaining <= available {
|
|
|
|
let text = chars.by_ref().take(remaining).collect::<String>();
|
|
|
|
cursor += remaining;
|
|
|
|
|
|
|
|
write!(
|
|
|
|
self.handle,
|
|
|
|
"{}",
|
|
|
|
as_terminal_escaped(
|
|
|
|
style,
|
|
|
|
&*format!("{}{}", ansi_prefix, text),
|
|
|
|
self.config.true_color,
|
|
|
|
self.config.colored_output,
|
|
|
|
)
|
|
|
|
)?;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate wrap padding if not already generated.
|
|
|
|
if panel_wrap.is_none() {
|
|
|
|
panel_wrap = if self.panel_width > 0 {
|
|
|
|
Some(format!(
|
|
|
|
"{} ",
|
|
|
|
self.decorations
|
|
|
|
.iter()
|
|
|
|
.map(|ref d| d.generate(self.line_number, true, self)
|
|
|
|
.text)
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(" ")
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Some("".to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It wraps.
|
|
|
|
let text = chars.by_ref().take(available).collect::<String>();
|
|
|
|
cursor = 0;
|
|
|
|
remaining -= available;
|
|
|
|
|
|
|
|
write!(
|
|
|
|
self.handle,
|
|
|
|
"{}\n{}",
|
|
|
|
as_terminal_escaped(
|
|
|
|
style,
|
|
|
|
&*format!("{}{}", ansi_prefix, text),
|
|
|
|
self.config.true_color,
|
|
|
|
self.config.colored_output,
|
|
|
|
),
|
|
|
|
panel_wrap.clone().unwrap()
|
|
|
|
)?;
|
|
|
|
}
|
2018-05-14 03:44:07 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 06:59:26 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 22:23:33 +02:00
|
|
|
|
|
|
|
write!(self.handle, "\n")?;
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
2018-05-12 06:59:26 +02:00
|
|
|
|
2018-05-12 15:32:23 +02:00
|
|
|
Ok(())
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 06:59:26 +02:00
|
|
|
fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
|
|
|
|
if self.panel_width == 0 {
|
2018-05-12 15:32:23 +02:00
|
|
|
writeln!(
|
|
|
|
self.handle,
|
|
|
|
"{}",
|
|
|
|
self.colors.grid.paint("─".repeat(self.config.term_width))
|
|
|
|
)?;
|
2018-05-12 06:59:26 +02:00
|
|
|
} else {
|
|
|
|
let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
|
|
|
|
let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
|
|
|
|
writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
|
|
|
|
}
|
2018-05-07 01:32:00 +02:00
|
|
|
|
2018-05-12 15:32:23 +02:00
|
|
|
Ok(())
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
}
|