2020-02-28 10:27:06 +01:00
|
|
|
use std::borrow::Cow;
|
2018-08-22 22:29:12 +02:00
|
|
|
use std::io::Write;
|
|
|
|
use std::vec::Vec;
|
|
|
|
|
2018-08-19 12:32:35 +02:00
|
|
|
use ansi_term::Colour::{Fixed, Green, Red, Yellow};
|
2018-05-21 15:00:00 +02:00
|
|
|
use ansi_term::Style;
|
2018-08-22 22:29:12 +02:00
|
|
|
|
2018-05-16 23:19:36 +02:00
|
|
|
use console::AnsiCodeIterator;
|
2018-08-22 22:29:12 +02:00
|
|
|
|
2018-08-23 22:37:27 +02:00
|
|
|
use syntect::easy::HighlightLines;
|
2018-10-10 06:25:33 +02:00
|
|
|
use syntect::highlighting::Color;
|
2018-08-23 22:37:27 +02:00
|
|
|
use syntect::highlighting::Theme;
|
2018-10-09 21:18:40 +02:00
|
|
|
use syntect::parsing::SyntaxSet;
|
2018-08-22 22:29:12 +02:00
|
|
|
|
2018-10-07 16:44:59 +02:00
|
|
|
use content_inspector::ContentType;
|
|
|
|
|
|
|
|
use encoding::all::{UTF_16BE, UTF_16LE};
|
|
|
|
use encoding::{DecoderTrap, Encoding};
|
2018-10-07 14:24:47 +02:00
|
|
|
|
2020-02-01 10:50:34 +01:00
|
|
|
use unicode_width::UnicodeWidthChar;
|
|
|
|
|
2019-10-06 03:44:14 +02:00
|
|
|
use crate::assets::HighlightingAssets;
|
2020-03-21 19:40:43 +01:00
|
|
|
use crate::config::Config;
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
|
|
|
use crate::decorations::LineChangesDecoration;
|
2020-04-11 19:40:04 +02:00
|
|
|
use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration};
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
|
|
|
use crate::diff::{get_git_diff, LineChanges};
|
2019-10-06 03:44:14 +02:00
|
|
|
use crate::errors::*;
|
|
|
|
use crate::inputfile::{InputFile, InputFileReader};
|
2020-01-23 04:26:21 +01:00
|
|
|
use crate::line_range::RangeCheckResult;
|
2019-10-06 03:44:14 +02:00
|
|
|
use crate::preprocessor::{expand_tabs, replace_nonprintable};
|
|
|
|
use crate::terminal::{as_terminal_escaped, to_ansi_color};
|
2020-04-21 20:06:09 +02:00
|
|
|
use crate::wrap::WrappingMode;
|
2018-05-07 01:32:00 +02:00
|
|
|
|
2018-08-23 19:43:10 +02:00
|
|
|
pub trait Printer {
|
2020-04-21 20:06:09 +02:00
|
|
|
fn print_header(&mut self, handle: &mut dyn Write, file: &InputFile) -> Result<()>;
|
2019-08-02 09:14:57 +02:00
|
|
|
fn print_footer(&mut self, handle: &mut dyn Write) -> Result<()>;
|
2019-05-25 03:24:13 +02:00
|
|
|
|
|
|
|
fn print_snip(&mut self, handle: &mut dyn Write) -> Result<()>;
|
|
|
|
|
2018-08-23 23:13:24 +02:00
|
|
|
fn print_line(
|
|
|
|
&mut self,
|
2018-08-23 23:35:57 +02:00
|
|
|
out_of_range: bool,
|
2019-08-02 09:14:57 +02:00
|
|
|
handle: &mut dyn Write,
|
2018-08-23 23:13:24 +02:00
|
|
|
line_number: usize,
|
|
|
|
line_buffer: &[u8],
|
|
|
|
) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SimplePrinter;
|
|
|
|
|
|
|
|
impl SimplePrinter {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
SimplePrinter {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Printer for SimplePrinter {
|
2020-04-21 20:06:09 +02:00
|
|
|
fn print_header(&mut self, _handle: &mut dyn Write, _file: &InputFile) -> Result<()> {
|
2018-08-23 23:13:24 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 09:14:57 +02:00
|
|
|
fn print_footer(&mut self, _handle: &mut dyn Write) -> Result<()> {
|
2018-08-23 23:13:24 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-31 12:24:29 +02:00
|
|
|
fn print_snip(&mut self, _handle: &mut dyn Write) -> Result<()> {
|
2019-05-25 03:24:13 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-23 23:13:24 +02:00
|
|
|
fn print_line(
|
|
|
|
&mut self,
|
2018-08-23 23:35:57 +02:00
|
|
|
out_of_range: bool,
|
2019-08-02 09:14:57 +02:00
|
|
|
handle: &mut dyn Write,
|
2018-08-23 23:13:24 +02:00
|
|
|
_line_number: usize,
|
|
|
|
line_buffer: &[u8],
|
|
|
|
) -> Result<()> {
|
2018-08-23 23:35:57 +02:00
|
|
|
if !out_of_range {
|
2019-03-08 11:46:49 +01:00
|
|
|
handle.write_all(line_buffer)?;
|
2018-08-23 23:35:57 +02:00
|
|
|
}
|
2018-08-23 23:13:24 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-08-23 19:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct InteractivePrinter<'a> {
|
2018-05-07 01:32:00 +02:00
|
|
|
colors: Colors,
|
2018-08-23 22:37:27 +02:00
|
|
|
config: &'a Config<'a>,
|
2018-10-07 11:55:39 +02:00
|
|
|
decorations: Vec<Box<dyn Decoration>>,
|
2018-05-12 06:59:26 +02:00
|
|
|
panel_width: usize,
|
2018-08-23 22:37:27 +02:00
|
|
|
ansi_prefix_sgr: String,
|
2019-05-15 21:53:22 +02:00
|
|
|
content_type: Option<ContentType>,
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
2018-05-07 01:32:00 +02:00
|
|
|
pub line_changes: Option<LineChanges>,
|
2018-10-07 14:24:47 +02:00
|
|
|
highlighter: Option<HighlightLines<'a>>,
|
2018-10-09 21:18:40 +02:00
|
|
|
syntax_set: &'a SyntaxSet,
|
2018-12-16 21:00:18 +01:00
|
|
|
background_color_highlight: Option<Color>,
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 19:43:10 +02:00
|
|
|
impl<'a> InteractivePrinter<'a> {
|
2018-10-07 13:26:50 +02:00
|
|
|
pub fn new(
|
|
|
|
config: &'a Config,
|
|
|
|
assets: &'a HighlightingAssets,
|
2020-04-21 20:06:09 +02:00
|
|
|
file: &InputFile,
|
2018-10-07 13:26:50 +02:00
|
|
|
reader: &mut InputFileReader,
|
|
|
|
) -> Self {
|
2018-08-23 22:37:27 +02:00
|
|
|
let theme = assets.get_theme(&config.theme);
|
|
|
|
|
2018-12-16 21:00:18 +01:00
|
|
|
let background_color_highlight = theme.settings.line_highlight;
|
2018-10-10 06:25:33 +02:00
|
|
|
|
2018-05-10 23:39:13 +02:00
|
|
|
let colors = if config.colored_output {
|
2018-08-19 12:32:35 +02:00
|
|
|
Colors::colored(theme, config.true_color)
|
2018-05-07 01:32:00 +02:00
|
|
|
} else {
|
|
|
|
Colors::plain()
|
|
|
|
};
|
|
|
|
|
2018-05-14 03:44:07 +02:00
|
|
|
// Create decorations.
|
2018-10-07 11:55:39 +02:00
|
|
|
let mut decorations: Vec<Box<dyn Decoration>> = Vec::new();
|
2018-05-14 03:44:07 +02:00
|
|
|
|
2020-03-21 20:54:16 +01:00
|
|
|
if config.style_components.numbers() {
|
2018-05-14 03:44:07 +02:00
|
|
|
decorations.push(Box::new(LineNumberDecoration::new(&colors)));
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
|
|
|
{
|
|
|
|
if config.style_components.changes() {
|
|
|
|
decorations.push(Box::new(LineChangesDecoration::new(&colors)));
|
|
|
|
}
|
2018-05-14 03:44:07 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2020-03-21 20:54:16 +01:00
|
|
|
if config.style_components.grid() && !decorations.is_empty() {
|
2018-05-14 03:44:07 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
2018-10-07 14:24:47 +02:00
|
|
|
let mut line_changes = None;
|
|
|
|
|
2019-08-27 05:05:47 +02:00
|
|
|
let highlighter = if reader
|
|
|
|
.content_type
|
|
|
|
.map_or(false, |c| c.is_binary() && !config.show_nonprintable)
|
|
|
|
{
|
2018-09-11 08:47:49 +02:00
|
|
|
None
|
2018-10-07 14:24:47 +02:00
|
|
|
} else {
|
|
|
|
// Get the Git modifications
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
|
|
|
{
|
|
|
|
if config.style_components.changes() {
|
2020-04-05 02:49:55 +02:00
|
|
|
if let InputFile::Ordinary(ofile) = file {
|
2020-04-21 08:19:24 +02:00
|
|
|
line_changes = get_git_diff(ofile.provided_path());
|
2020-03-30 19:37:29 +02:00
|
|
|
}
|
2018-10-07 14:24:47 +02:00
|
|
|
}
|
2020-03-30 19:37:29 +02:00
|
|
|
}
|
2018-10-07 14:24:47 +02:00
|
|
|
|
|
|
|
// Determine the type of syntax for highlighting
|
2020-04-05 02:49:55 +02:00
|
|
|
let syntax = assets.get_syntax(config.language, file, reader, &config.syntax_mapping);
|
2018-10-07 14:24:47 +02:00
|
|
|
Some(HighlightLines::new(syntax, theme))
|
2018-08-28 20:12:45 +02:00
|
|
|
};
|
2018-08-23 22:37:27 +02:00
|
|
|
|
2018-08-23 19:43:10 +02:00
|
|
|
InteractivePrinter {
|
2018-05-14 03:44:07 +02:00
|
|
|
panel_width,
|
2018-05-07 01:32:00 +02:00
|
|
|
colors,
|
2018-05-10 23:39:13 +02:00
|
|
|
config,
|
2018-05-14 03:44:07 +02:00
|
|
|
decorations,
|
2018-10-07 16:44:59 +02:00
|
|
|
content_type: reader.content_type,
|
2018-07-05 22:24:34 +02:00
|
|
|
ansi_prefix_sgr: String::new(),
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
2018-08-23 22:37:27 +02:00
|
|
|
line_changes,
|
|
|
|
highlighter,
|
2018-10-09 21:18:40 +02:00
|
|
|
syntax_set: &assets.syntax_set,
|
2018-12-16 21:00:18 +01:00
|
|
|
background_color_highlight,
|
2018-05-14 03:44:07 +02:00
|
|
|
}
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 09:14:57 +02:00
|
|
|
fn print_horizontal_line(&mut self, handle: &mut dyn Write, grid_char: char) -> Result<()> {
|
2018-08-23 19:43:10 +02:00
|
|
|
if self.panel_width == 0 {
|
|
|
|
writeln!(
|
2018-08-23 22:37:27 +02:00
|
|
|
handle,
|
2018-08-23 19:43:10 +02:00
|
|
|
"{}",
|
|
|
|
self.colors.grid.paint("─".repeat(self.config.term_width))
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
|
|
|
|
let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
|
2018-08-23 22:37:27 +02:00
|
|
|
writeln!(handle, "{}", self.colors.grid.paint(hline))?;
|
2018-08-23 19:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-09-11 22:02:22 +02:00
|
|
|
|
2019-05-25 03:24:13 +02:00
|
|
|
fn create_fake_panel(&self, text: &str) -> String {
|
|
|
|
if self.panel_width == 0 {
|
|
|
|
"".to_string()
|
|
|
|
} else {
|
|
|
|
let text_truncated: String = text.chars().take(self.panel_width - 1).collect();
|
|
|
|
let text_filled: String = format!(
|
|
|
|
"{}{}",
|
|
|
|
text_truncated,
|
|
|
|
" ".repeat(self.panel_width - 1 - text_truncated.len())
|
|
|
|
);
|
2020-03-21 20:54:16 +01:00
|
|
|
if self.config.style_components.grid() {
|
2019-05-25 03:24:13 +02:00
|
|
|
format!("{} │ ", text_filled)
|
|
|
|
} else {
|
|
|
|
format!("{}", text_filled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 22:45:49 +02:00
|
|
|
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
|
2018-09-11 22:02:22 +02:00
|
|
|
if self.config.tab_width > 0 {
|
2018-11-01 13:02:29 +01:00
|
|
|
expand_tabs(text, self.config.tab_width, cursor)
|
2018-09-11 22:02:22 +02:00
|
|
|
} else {
|
|
|
|
text.to_string()
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 19:43:10 +02:00
|
|
|
}
|
2018-05-19 12:25:07 +02:00
|
|
|
|
2018-08-23 19:43:10 +02:00
|
|
|
impl<'a> Printer for InteractivePrinter<'a> {
|
2020-04-21 20:06:09 +02:00
|
|
|
fn print_header(&mut self, handle: &mut dyn Write, file: &InputFile) -> Result<()> {
|
2020-03-21 20:54:16 +01:00
|
|
|
if !self.config.style_components.header() {
|
2019-08-31 19:35:04 +02:00
|
|
|
if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
|
2019-05-14 23:14:41 +02:00
|
|
|
let input = match file {
|
2020-04-05 02:49:55 +02:00
|
|
|
InputFile::Ordinary(ofile) => {
|
2020-04-21 08:19:24 +02:00
|
|
|
format!("file '{}'", &ofile.provided_path().to_string_lossy())
|
2020-04-05 02:49:55 +02:00
|
|
|
}
|
2020-04-21 08:28:15 +02:00
|
|
|
InputFile::StdIn(Some(name)) => format!(
|
|
|
|
"STDIN (with name '{}')",
|
|
|
|
name.to_string_lossy().into_owned()
|
|
|
|
),
|
2020-04-05 02:49:55 +02:00
|
|
|
InputFile::StdIn(None) => "STDIN".to_owned(),
|
|
|
|
InputFile::ThemePreviewFile => "".to_owned(),
|
2019-05-14 23:14:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
writeln!(
|
|
|
|
handle,
|
|
|
|
"{}: Binary content from {} will not be printed to the terminal \
|
2019-08-31 19:30:24 +02:00
|
|
|
(but will be present if the output of 'bat' is piped). You can use 'bat -A' \
|
|
|
|
to show the binary file contents.",
|
2019-05-14 23:14:41 +02:00
|
|
|
Yellow.paint("[bat warning]"),
|
|
|
|
input
|
|
|
|
)?;
|
2019-06-01 00:12:25 +02:00
|
|
|
} else {
|
2020-03-21 20:54:16 +01:00
|
|
|
if self.config.style_components.grid() {
|
2019-06-01 00:12:25 +02:00
|
|
|
self.print_horizontal_line(handle, '┬')?;
|
|
|
|
}
|
2019-04-24 16:11:31 +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
|
|
|
return Ok(());
|
2018-05-07 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 20:54:16 +01:00
|
|
|
if self.config.style_components.grid() {
|
2018-08-23 22:37:27 +02:00
|
|
|
self.print_horizontal_line(handle, '┬')?;
|
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
|
|
|
|
|
|
|
write!(
|
2018-08-23 22:37:27 +02:00
|
|
|
handle,
|
2018-05-24 12:05:33 +02:00
|
|
|
"{}{}",
|
2018-05-12 06:59:26 +02:00
|
|
|
" ".repeat(self.panel_width),
|
2018-05-12 15:32:23 +02:00
|
|
|
self.colors
|
|
|
|
.grid
|
2018-05-24 12:05:33 +02:00
|
|
|
.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-24 11:47:10 +02:00
|
|
|
} else {
|
2018-08-23 22:37:27 +02:00
|
|
|
write!(handle, "{}", " ".repeat(self.panel_width))?;
|
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-08-28 20:12:45 +02:00
|
|
|
let (prefix, name) = match file {
|
2020-04-05 02:49:55 +02:00
|
|
|
InputFile::Ordinary(ofile) => (
|
2020-03-20 03:46:19 +01:00
|
|
|
"File: ",
|
2020-04-21 08:19:24 +02:00
|
|
|
Cow::from(ofile.provided_path().to_string_lossy().to_owned()),
|
2020-03-18 03:24:48 +01:00
|
|
|
),
|
2020-04-05 02:49:55 +02:00
|
|
|
InputFile::StdIn(Some(name)) => {
|
|
|
|
("File: ", Cow::from(name.to_string_lossy().to_owned()))
|
|
|
|
}
|
|
|
|
InputFile::StdIn(None) => ("File: ", Cow::from("STDIN".to_owned())),
|
|
|
|
InputFile::ThemePreviewFile => ("", Cow::from("")),
|
2018-08-28 20:12:45 +02:00
|
|
|
};
|
|
|
|
|
2018-10-07 16:44:59 +02:00
|
|
|
let mode = match self.content_type {
|
2019-05-15 21:53:22 +02:00
|
|
|
Some(ContentType::BINARY) => " <BINARY>",
|
|
|
|
Some(ContentType::UTF_16LE) => " <UTF-16LE>",
|
|
|
|
Some(ContentType::UTF_16BE) => " <UTF-16BE>",
|
|
|
|
None => " <EMPTY>",
|
2018-10-07 17:01:26 +02:00
|
|
|
_ => "",
|
2018-10-07 14:24:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
writeln!(
|
|
|
|
handle,
|
|
|
|
"{}{}{}",
|
|
|
|
prefix,
|
|
|
|
self.colors.filename.paint(name),
|
|
|
|
mode
|
|
|
|
)?;
|
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
|
|
|
|
2020-03-21 20:54:16 +01:00
|
|
|
if self.config.style_components.grid() {
|
2019-08-31 19:30:24 +02:00
|
|
|
if self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable {
|
2018-10-07 14:24:47 +02:00
|
|
|
self.print_horizontal_line(handle, '┼')?;
|
|
|
|
} else {
|
|
|
|
self.print_horizontal_line(handle, '┴')?;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-08-02 09:14:57 +02:00
|
|
|
fn print_footer(&mut self, handle: &mut dyn Write) -> Result<()> {
|
2020-03-21 20:54:16 +01:00
|
|
|
if self.config.style_components.grid()
|
2019-08-31 19:30:24 +02:00
|
|
|
&& (self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable)
|
2019-05-15 21:53:22 +02:00
|
|
|
{
|
2018-08-23 22:37:27 +02:00
|
|
|
self.print_horizontal_line(handle, '┴')
|
2018-05-07 01:32:00 +02:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 12:24:29 +02:00
|
|
|
fn print_snip(&mut self, handle: &mut dyn Write) -> Result<()> {
|
2019-05-25 03:24:13 +02:00
|
|
|
let panel = self.create_fake_panel(" ...");
|
|
|
|
let panel_count = panel.chars().count();
|
|
|
|
|
|
|
|
let title = "8<";
|
|
|
|
let title_count = title.chars().count();
|
|
|
|
|
2019-08-31 12:46:27 +02:00
|
|
|
let snip_left = "─ ".repeat((self.config.term_width - panel_count - (title_count / 2)) / 4);
|
2019-05-25 03:24:13 +02:00
|
|
|
let snip_left_count = snip_left.chars().count(); // Can't use .len() with Unicode.
|
|
|
|
|
2019-08-31 12:46:27 +02:00
|
|
|
let snip_right =
|
|
|
|
" ─".repeat((self.config.term_width - panel_count - snip_left_count - title_count) / 2);
|
2019-05-25 03:24:13 +02:00
|
|
|
|
|
|
|
write!(
|
|
|
|
handle,
|
|
|
|
"{}\n",
|
|
|
|
self.colors
|
|
|
|
.grid
|
|
|
|
.paint(format!("{}{}{}{}", panel, snip_left, title, snip_right))
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-23 23:13:24 +02:00
|
|
|
fn print_line(
|
|
|
|
&mut self,
|
2018-08-23 23:35:57 +02:00
|
|
|
out_of_range: bool,
|
2019-08-02 09:14:57 +02:00
|
|
|
handle: &mut dyn Write,
|
2018-08-23 23:13:24 +02:00
|
|
|
line_number: usize,
|
|
|
|
line_buffer: &[u8],
|
|
|
|
) -> Result<()> {
|
2019-08-31 19:30:24 +02:00
|
|
|
let line = if self.config.show_nonprintable {
|
|
|
|
replace_nonprintable(&line_buffer, self.config.tab_width)
|
|
|
|
} else {
|
|
|
|
match self.content_type {
|
|
|
|
Some(ContentType::BINARY) | None => {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Some(ContentType::UTF_16LE) => UTF_16LE
|
|
|
|
.decode(&line_buffer, DecoderTrap::Replace)
|
|
|
|
.map_err(|_| "Invalid UTF-16LE")?,
|
|
|
|
Some(ContentType::UTF_16BE) => UTF_16BE
|
|
|
|
.decode(&line_buffer, DecoderTrap::Replace)
|
|
|
|
.map_err(|_| "Invalid UTF-16BE")?,
|
|
|
|
_ => String::from_utf8_lossy(&line_buffer).to_string(),
|
2018-10-07 16:44:59 +02:00
|
|
|
}
|
|
|
|
};
|
2018-11-01 20:29:48 +01:00
|
|
|
|
2018-10-07 14:24:47 +02:00
|
|
|
let regions = {
|
|
|
|
let highlighter = match self.highlighter {
|
|
|
|
Some(ref mut highlighter) => highlighter,
|
|
|
|
_ => {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
};
|
2018-10-09 21:18:40 +02:00
|
|
|
highlighter.highlight(line.as_ref(), self.syntax_set)
|
2018-10-07 14:24:47 +02:00
|
|
|
};
|
2018-08-23 22:37:27 +02:00
|
|
|
|
2018-08-23 23:35:57 +02:00
|
|
|
if out_of_range {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
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-09-11 22:02:22 +02:00
|
|
|
let mut cursor_total: usize = 0;
|
2018-05-14 03:44:07 +02:00
|
|
|
let mut panel_wrap: Option<String> = None;
|
|
|
|
|
2018-12-16 21:00:18 +01:00
|
|
|
// Line highlighting
|
2020-01-23 04:26:21 +01:00
|
|
|
let highlight_this_line =
|
2020-03-21 17:22:17 +01:00
|
|
|
self.config.highlighted_lines.0.check(line_number) == RangeCheckResult::InRange;
|
2018-12-16 21:53:15 +01:00
|
|
|
|
|
|
|
let background_color = self
|
|
|
|
.background_color_highlight
|
|
|
|
.filter(|_| highlight_this_line);
|
2018-12-16 21:00:18 +01:00
|
|
|
|
2018-05-14 03:44:07 +02:00
|
|
|
// Line decorations.
|
|
|
|
if self.panel_width > 0 {
|
2018-07-17 23:38:45 +02:00
|
|
|
let decorations = self
|
|
|
|
.decorations
|
2018-05-14 03:44:07 +02:00
|
|
|
.iter()
|
2018-08-23 19:43:10 +02:00
|
|
|
.map(|ref d| d.generate(line_number, false, self))
|
2018-05-14 03:44:07 +02:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
for deco in decorations {
|
2018-08-23 22:37:27 +02:00
|
|
|
write!(handle, "{} ", deco.text)?;
|
2018-05-14 03:44:07 +02:00
|
|
|
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.
|
2020-04-21 20:06:09 +02:00
|
|
|
if self.config.wrapping_mode == WrappingMode::NoWrapping {
|
2018-05-12 22:44:10 +02:00
|
|
|
let true_color = self.config.true_color;
|
|
|
|
let colored_output = self.config.colored_output;
|
2018-11-04 18:08:33 +01:00
|
|
|
let italics = self.config.use_italic_text;
|
2018-11-02 12:41:56 +01:00
|
|
|
|
2018-09-11 22:45:49 +02:00
|
|
|
for &(style, region) in regions.iter() {
|
|
|
|
let text = &*self.preprocess(region, &mut cursor_total);
|
2018-12-14 21:23:27 +01:00
|
|
|
let text_trimmed = text.trim_end_matches(|c| c == '\r' || c == '\n');
|
2018-09-11 22:47:35 +02:00
|
|
|
write!(
|
|
|
|
handle,
|
|
|
|
"{}",
|
2018-12-16 21:00:18 +01:00
|
|
|
as_terminal_escaped(
|
|
|
|
style,
|
|
|
|
text_trimmed,
|
|
|
|
true_color,
|
|
|
|
colored_output,
|
|
|
|
italics,
|
|
|
|
background_color
|
|
|
|
)
|
2018-09-11 22:47:35 +02:00
|
|
|
)?;
|
2018-12-16 21:53:15 +01:00
|
|
|
|
|
|
|
if text.len() != text_trimmed.len() {
|
|
|
|
if let Some(background_color) = background_color {
|
|
|
|
let mut ansi_style = Style::default();
|
|
|
|
ansi_style.background = Some(to_ansi_color(background_color, true_color));
|
|
|
|
let width = if cursor_total <= cursor_max {
|
|
|
|
cursor_max - cursor_total + 1
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
write!(handle, "{}", ansi_style.paint(" ".repeat(width)))?;
|
|
|
|
}
|
|
|
|
write!(handle, "{}", &text[text_trimmed.len()..])?;
|
|
|
|
}
|
2018-09-11 22:45:49 +02:00
|
|
|
}
|
2018-09-10 21:36:58 +02:00
|
|
|
|
|
|
|
if line.bytes().next_back() != Some(b'\n') {
|
2019-03-08 11:46:49 +01:00
|
|
|
writeln!(handle)?;
|
2018-09-10 21:36:58 +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() {
|
2019-03-08 11:46:49 +01:00
|
|
|
let ansi_iterator = AnsiCodeIterator::new(region);
|
2018-07-05 22:24:34 +02:00
|
|
|
let mut ansi_prefix: String = String::new();
|
2018-05-31 15:27:55 +02:00
|
|
|
for chunk in ansi_iterator {
|
2018-05-16 23:19:36 +02:00
|
|
|
match chunk {
|
|
|
|
// ANSI escape passthrough.
|
|
|
|
(text, true) => {
|
2019-05-10 23:15:47 +02:00
|
|
|
let is_ansi_csi = text.starts_with("\x1B[");
|
2019-05-09 04:30:06 +02:00
|
|
|
|
2019-05-10 23:15:47 +02:00
|
|
|
if is_ansi_csi && text.ends_with('m') {
|
2019-05-09 04:30:06 +02:00
|
|
|
// It's an ANSI SGR sequence.
|
|
|
|
// We should be mostly safe to just append these together.
|
2018-07-05 22:24:34 +02:00
|
|
|
ansi_prefix.push_str(text);
|
2018-07-05 23:30:06 +02:00
|
|
|
if text == "\x1B[0m" {
|
|
|
|
self.ansi_prefix_sgr = "\x1B[0m".to_owned();
|
|
|
|
} else {
|
|
|
|
self.ansi_prefix_sgr.push_str(text);
|
|
|
|
}
|
2019-05-09 04:30:06 +02:00
|
|
|
} else if is_ansi_csi {
|
|
|
|
// It's a regular CSI sequence.
|
|
|
|
// We should be mostly safe to just append these together.
|
2018-07-05 22:24:34 +02:00
|
|
|
ansi_prefix.push_str(text);
|
2019-05-09 04:30:06 +02:00
|
|
|
} else {
|
|
|
|
// It's probably a VT100 code.
|
|
|
|
// Passing it through is the safest bet.
|
|
|
|
write!(handle, "{}", text)?;
|
2018-07-05 22:24:34 +02:00
|
|
|
}
|
2018-05-16 23:19:36 +02:00
|
|
|
}
|
2018-05-12 22:23:33 +02:00
|
|
|
|
2018-05-16 23:19:36 +02:00
|
|
|
// Regular text.
|
|
|
|
(text, false) => {
|
2018-09-11 22:02:22 +02:00
|
|
|
let text = self.preprocess(
|
2018-12-14 21:23:27 +01:00
|
|
|
text.trim_end_matches(|c| c == '\r' || c == '\n'),
|
2018-09-11 22:45:49 +02:00
|
|
|
&mut cursor_total,
|
2018-09-11 22:02:22 +02:00
|
|
|
);
|
2018-09-11 22:45:49 +02:00
|
|
|
|
2020-02-01 12:25:07 +01:00
|
|
|
let mut max_width = cursor_max - cursor;
|
2020-02-01 10:50:34 +01:00
|
|
|
|
|
|
|
// line buffer (avoid calling write! for every character)
|
|
|
|
let mut line_buf = String::with_capacity(max_width * 4);
|
|
|
|
|
|
|
|
// Displayed width of line_buf
|
|
|
|
let mut current_width = 0;
|
|
|
|
|
|
|
|
for c in text.chars() {
|
|
|
|
// calculate the displayed width for next character
|
|
|
|
let cw = c.width().unwrap_or(0);
|
|
|
|
current_width += cw;
|
|
|
|
|
|
|
|
// if next character cannot be printed on this line,
|
|
|
|
// flush the buffer.
|
|
|
|
if current_width > max_width {
|
|
|
|
// 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(line_number, true, self)
|
|
|
|
.text)
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(" ")
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Some("".to_string())
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 23:19:36 +02:00
|
|
|
|
2020-02-01 10:50:34 +01:00
|
|
|
// It wraps.
|
2018-05-16 23:19:36 +02:00
|
|
|
write!(
|
2018-08-23 22:37:27 +02:00
|
|
|
handle,
|
2020-02-01 10:50:34 +01:00
|
|
|
"{}\n{}",
|
2018-05-16 23:19:36 +02:00
|
|
|
as_terminal_escaped(
|
|
|
|
style,
|
2018-07-05 22:24:34 +02:00
|
|
|
&*format!(
|
|
|
|
"{}{}{}",
|
2020-02-01 10:50:34 +01:00
|
|
|
self.ansi_prefix_sgr, ansi_prefix, line_buf
|
2018-07-05 22:24:34 +02:00
|
|
|
),
|
2018-05-16 23:19:36 +02:00
|
|
|
self.config.true_color,
|
|
|
|
self.config.colored_output,
|
2018-12-16 20:43:36 +01:00
|
|
|
self.config.use_italic_text,
|
2018-12-16 21:00:18 +01:00
|
|
|
background_color
|
2020-02-01 10:50:34 +01:00
|
|
|
),
|
|
|
|
panel_wrap.clone().unwrap()
|
2018-05-16 23:19:36 +02:00
|
|
|
)?;
|
|
|
|
|
2020-02-01 12:25:07 +01:00
|
|
|
cursor = 0;
|
|
|
|
max_width = cursor_max;
|
|
|
|
|
2020-02-01 10:50:34 +01:00
|
|
|
line_buf.clear();
|
|
|
|
current_width = cw;
|
2018-05-16 23:19:36 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 10:50:34 +01:00
|
|
|
line_buf.push(c);
|
2018-05-16 23:19:36 +02:00
|
|
|
}
|
2018-07-05 22:24:34 +02:00
|
|
|
|
2020-02-01 10:50:34 +01:00
|
|
|
// flush the buffer
|
|
|
|
cursor += current_width;
|
|
|
|
write!(
|
|
|
|
handle,
|
|
|
|
"{}",
|
|
|
|
as_terminal_escaped(
|
|
|
|
style,
|
|
|
|
&*format!(
|
|
|
|
"{}{}{}",
|
|
|
|
self.ansi_prefix_sgr, ansi_prefix, line_buf
|
|
|
|
),
|
|
|
|
self.config.true_color,
|
|
|
|
self.config.colored_output,
|
|
|
|
self.config.use_italic_text,
|
|
|
|
background_color
|
|
|
|
)
|
|
|
|
)?;
|
|
|
|
|
2018-07-05 22:24:34 +02:00
|
|
|
// Clear the ANSI prefix buffer.
|
|
|
|
ansi_prefix.clear();
|
2018-05-14 03:44:07 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 06:59:26 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 22:23:33 +02:00
|
|
|
|
2018-12-16 21:53:15 +01:00
|
|
|
if let Some(background_color) = background_color {
|
|
|
|
let mut ansi_style = Style::default();
|
|
|
|
ansi_style.background =
|
|
|
|
Some(to_ansi_color(background_color, self.config.true_color));
|
|
|
|
|
|
|
|
write!(
|
|
|
|
handle,
|
|
|
|
"{}",
|
|
|
|
ansi_style.paint(" ".repeat(cursor_max - cursor))
|
|
|
|
)?;
|
|
|
|
}
|
2019-03-08 11:46:49 +01:00
|
|
|
writeln!(handle)?;
|
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-21 15:00:00 +02:00
|
|
|
|
2018-08-19 12:32:35 +02:00
|
|
|
const DEFAULT_GUTTER_COLOR: u8 = 238;
|
2018-05-21 15:00:00 +02:00
|
|
|
|
2019-10-15 03:25:53 +02:00
|
|
|
#[derive(Debug, Default)]
|
2018-05-21 15:00:00 +02:00
|
|
|
pub struct Colors {
|
|
|
|
pub grid: Style,
|
|
|
|
pub filename: Style,
|
|
|
|
pub git_added: Style,
|
|
|
|
pub git_removed: Style,
|
|
|
|
pub git_modified: Style,
|
|
|
|
pub line_number: Style,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Colors {
|
|
|
|
fn plain() -> Self {
|
|
|
|
Colors::default()
|
|
|
|
}
|
|
|
|
|
2018-08-19 12:32:35 +02:00
|
|
|
fn colored(theme: &Theme, true_color: bool) -> Self {
|
|
|
|
let gutter_color = theme
|
|
|
|
.settings
|
|
|
|
.gutter_foreground
|
|
|
|
.map(|c| to_ansi_color(c, true_color))
|
|
|
|
.unwrap_or(Fixed(DEFAULT_GUTTER_COLOR));
|
|
|
|
|
2018-05-21 15:00:00 +02:00
|
|
|
Colors {
|
2018-08-19 12:32:35 +02:00
|
|
|
grid: gutter_color.normal(),
|
|
|
|
filename: Style::new().bold(),
|
2018-05-21 15:00:00 +02:00
|
|
|
git_added: Green.normal(),
|
|
|
|
git_removed: Red.normal(),
|
|
|
|
git_modified: Yellow.normal(),
|
2018-08-19 12:32:35 +02:00
|
|
|
line_number: gutter_color.normal(),
|
2018-05-21 15:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|