2018-05-07 01:32:00 +02:00
|
|
|
use ansi_term::Style;
|
2018-05-10 23:39:13 +02:00
|
|
|
use app::Config;
|
2018-05-10 12:36:09 +02:00
|
|
|
use diff::{LineChange, LineChanges};
|
2018-05-07 01:32:00 +02:00
|
|
|
use errors::*;
|
|
|
|
use std::io::Write;
|
|
|
|
use syntect::highlighting;
|
|
|
|
use terminal::as_terminal_escaped;
|
2018-05-12 06:59:26 +02:00
|
|
|
use style::OutputWrap;
|
2018-05-10 23:39:13 +02:00
|
|
|
use Colors;
|
2018-05-07 01:32:00 +02:00
|
|
|
|
2018-05-12 06:59:26 +02:00
|
|
|
const LINE_NUMBER_WIDTH: usize = 4;
|
|
|
|
|
|
|
|
struct PrintSegment {
|
|
|
|
size: usize,
|
2018-05-12 15:32:23 +02:00
|
|
|
text: String,
|
2018-05-12 06:59:26 +02:00
|
|
|
}
|
2018-05-07 01:32:00 +02:00
|
|
|
|
|
|
|
pub struct Printer<'a> {
|
|
|
|
handle: &'a mut Write,
|
|
|
|
colors: Colors,
|
2018-05-10 23:39:13 +02:00
|
|
|
config: &'a Config<'a>,
|
2018-05-12 06:59:26 +02:00
|
|
|
panel_width: 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-12 06:59:26 +02:00
|
|
|
// Create the instance.
|
|
|
|
let mut instance = Printer {
|
2018-05-07 01:32:00 +02:00
|
|
|
handle,
|
|
|
|
colors,
|
2018-05-10 23:39:13 +02:00
|
|
|
config,
|
2018-05-12 06:59:26 +02:00
|
|
|
panel_width: 0,
|
2018-05-07 01:32:00 +02:00
|
|
|
line_changes: None,
|
2018-05-12 06:59:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Generate the panel (gutter) width.
|
|
|
|
let decorations = instance.gen_decorations(0);
|
2018-05-12 15:32:23 +02:00
|
|
|
instance.panel_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size);
|
2018-05-12 06:59:26 +02:00
|
|
|
|
|
|
|
// Return the instance.
|
|
|
|
return instance;
|
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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_line(
|
|
|
|
&mut self,
|
|
|
|
line_number: usize,
|
|
|
|
regions: &[(highlighting::Style, &str)],
|
|
|
|
) -> Result<()> {
|
2018-05-12 15:32:23 +02:00
|
|
|
let mut cursor: usize = 0;
|
|
|
|
let mut cursor_max: usize = self.config.term_width - 2;
|
2018-05-07 01:32:00 +02:00
|
|
|
|
2018-05-12 06:59:26 +02:00
|
|
|
// Line decoration.
|
|
|
|
let decorations = self.gen_decorations(line_number);
|
|
|
|
let gutter_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size);
|
2018-05-07 01:32:00 +02:00
|
|
|
|
2018-05-12 06:59:26 +02:00
|
|
|
if gutter_width > 0 {
|
|
|
|
cursor_max -= gutter_width;
|
2018-05-12 15:32:23 +02:00
|
|
|
write!(
|
|
|
|
self.handle,
|
|
|
|
"{} ",
|
|
|
|
decorations
|
|
|
|
.iter()
|
|
|
|
.map(|seg| seg.text.to_owned())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(" ")
|
|
|
|
)?;
|
2018-05-12 06:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grid border.
|
|
|
|
let border = if gutter_width > 0 && self.config.output_components.grid() {
|
|
|
|
self.gen_border()
|
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-12 22:23:33 +02:00
|
|
|
for &(style, text) in regions.iter() {
|
|
|
|
let mut chars = text.chars().filter(|c| *c != '\n');
|
|
|
|
let mut remaining = text.chars().filter(|c| *c != '\n').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,
|
|
|
|
&*text,
|
|
|
|
self.config.true_color,
|
|
|
|
self.config.colored_output
|
|
|
|
)
|
|
|
|
)?;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It wraps.
|
|
|
|
if self.config.output_wrap == OutputWrap::Character {
|
|
|
|
let text = chars.by_ref().take(available).collect::<String>();
|
|
|
|
cursor = 0;
|
|
|
|
remaining -= available;
|
|
|
|
|
|
|
|
write!(
|
|
|
|
self.handle,
|
|
|
|
"{}\n{}{}",
|
|
|
|
as_terminal_escaped(
|
|
|
|
style,
|
|
|
|
&*text,
|
|
|
|
self.config.true_color,
|
|
|
|
self.config.colored_output
|
|
|
|
),
|
|
|
|
" ".repeat(gutter_width),
|
|
|
|
border.text.to_owned()
|
|
|
|
)?;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// Finished.
|
2018-05-12 15:32:23 +02:00
|
|
|
Ok(())
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 15:32:23 +02:00
|
|
|
/// Generates all the line decorations.
|
2018-05-12 06:59:26 +02:00
|
|
|
fn gen_decorations(&self, line_number: usize) -> Vec<PrintSegment> {
|
|
|
|
let mut decorations = Vec::new();
|
|
|
|
|
|
|
|
if self.config.output_components.numbers() {
|
|
|
|
decorations.push(self.gen_deco_line_number(line_number));
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:39:13 +02:00
|
|
|
if self.config.output_components.changes() {
|
2018-05-12 06:59:26 +02:00
|
|
|
decorations.push(self.gen_deco_line_changes(line_number));
|
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-12 06:59:26 +02:00
|
|
|
|
|
|
|
return decorations;
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 15:32:23 +02:00
|
|
|
/// Generates the decoration for displaying the line number.
|
2018-05-12 06:59:26 +02:00
|
|
|
fn gen_deco_line_number(&self, line_number: usize) -> PrintSegment {
|
2018-05-12 15:32:23 +02:00
|
|
|
let plain: String = format!("{:width$}", line_number, width = LINE_NUMBER_WIDTH);
|
2018-05-12 06:59:26 +02:00
|
|
|
let color = self.colors.line_number.paint(plain.to_owned());
|
|
|
|
|
|
|
|
return PrintSegment {
|
|
|
|
text: color.to_string(),
|
2018-05-12 15:32:23 +02:00
|
|
|
size: plain.len(),
|
|
|
|
};
|
2018-05-12 06:59:26 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 15:32:23 +02:00
|
|
|
/// Generates the decoration for displaying the git changes.
|
2018-05-12 06:59:26 +02:00
|
|
|
fn gen_deco_line_changes(&self, line_number: usize) -> PrintSegment {
|
|
|
|
let color = 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(" "),
|
|
|
|
}
|
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-12 06:59:26 +02:00
|
|
|
Style::default().paint(" ")
|
|
|
|
};
|
|
|
|
|
|
|
|
return PrintSegment {
|
|
|
|
text: color.to_string(),
|
2018-05-12 15:32:23 +02:00
|
|
|
size: 1,
|
|
|
|
};
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 15:32:23 +02:00
|
|
|
/// Generates the vertical grid border.
|
2018-05-12 06:59:26 +02:00
|
|
|
fn gen_border(&self) -> PrintSegment {
|
|
|
|
return PrintSegment {
|
2018-05-12 22:23:33 +02:00
|
|
|
text: self.colors.grid.paint("│ ").to_string(),
|
|
|
|
size: 2,
|
2018-05-12 15:32:23 +02:00
|
|
|
};
|
2018-05-12 06:59:26 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|