use std::io::Write;
use std::vec::Vec;
use ansi_term::Colour::Fixed;
use ansi_term::Style;
use console::AnsiCodeIterator;
use syntect::easy::HighlightLines;
use syntect::highlighting::Theme;
use syntect::parsing::SyntaxSet;
use content_inspector::ContentType;
use encoding::all::{UTF_16BE, UTF_16LE};
use encoding::{DecoderTrap, Encoding};
use crate::assets::HighlightingAssets;
use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration};
use crate::errors::*;
use crate::inputfile::{InputFile, InputFileReader};
use crate::preprocessor::{expand_tabs, replace_nonprintable};
use crate::style::OutputComponents;
use crate::style::OutputWrap;
use crate::syntax_mapping::SyntaxMapping;
use crate::terminal::{as_terminal_escaped, to_ansi_color};
pub trait Printer {
fn print_header(
&mut self,
handle: &mut dyn Write,
file: &InputFile,
header_overwrite: Option<String>,
) -> Result<()>;
fn print_footer(&mut self, handle: &mut dyn Write) -> Result<()>;
fn print_line(
&mut self,
out_of_range: bool,
handle: &mut dyn Write,
line_number: usize,
line_buffer: &[u8],
) -> Result<()>;
}
pub struct InteractivePrinter<'a> {
colors: Colors,
decorations: Vec<Box<dyn Decoration>>,
panel_width: usize,
ansi_prefix_sgr: String,
content_type: ContentType,
highlighter: Option<HighlightLines<'a>>,
syntax_set: &'a SyntaxSet,
output_components: OutputComponents,
colored_output: bool,
true_color: bool,
term_width: usize,
tab_width: usize,
show_nonprintable: bool,
output_wrap: OutputWrap,
use_italic_text: bool,
}
impl<'a> InteractivePrinter<'a> {
pub fn new(
assets: &'a HighlightingAssets,
file: &InputFile,
reader: &mut InputFileReader,
output_components: OutputComponents,
theme: String,
colored_output: bool,
true_color: bool,
term_width: usize,
language: Option<String>,
syntax_mapping: SyntaxMapping,
tab_width: usize,
show_nonprintable: bool,
output_wrap: OutputWrap,
use_italic_text: bool,
) -> Self {
let theme = assets.get_theme(&theme);
let colors = if colored_output {
Colors::colored(theme, true_color)
} else {
Colors::plain()
};
let mut decorations: Vec<Box<dyn Decoration>> = Vec::new();
if output_components.numbers() {
decorations.push(Box::new(LineNumberDecoration::new(&colors)));
}
let mut panel_width: usize =
decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
if output_components.grid() && !decorations.is_empty() {
decorations.push(Box::new(GridBorderDecoration::new(&colors)));
}
if term_width < (decorations.len() + decorations.iter().fold(0, |a, x| a + x.width())) + 5 {
decorations.clear();
panel_width = 0;
}
let highlighter = if reader.content_type.is_binary() {
None
} else {
let syntax = assets.get_syntax(language, file, reader, &syntax_mapping);
Some(HighlightLines::new(syntax, theme))
};
InteractivePrinter {
panel_width,
colors,
decorations,
content_type: reader.content_type,
ansi_prefix_sgr: String::new(),
highlighter,
syntax_set: &assets.syntax_set,
output_components,
colored_output,
true_color,
term_width,
tab_width,
show_nonprintable,
output_wrap,
use_italic_text,
}
}
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.term_width))
)?;
} else {
let hline = "─".repeat(self.term_width - (self.panel_width + 1));
let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
writeln!(handle, "{}", self.colors.grid.paint(hline))?;
}
Ok(())
}
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
if self.tab_width > 0 {
expand_tabs(text, self.tab_width, cursor)
} else {
text.to_string()
}
}
}
impl<'a> Printer for InteractivePrinter<'a> {
fn print_header(
&mut self,
handle: &mut dyn Write,
file: &InputFile,
header_overwrite: Option<String>,
) -> Result<()> {
if !self.output_components.header() {
return Ok(());
}
if self.output_components.grid() {
self.print_horizontal_line(handle, '┬')?;
write!(
handle,
"{}{}",
" ".repeat(self.panel_width),
self.colors
.grid
.paint(if self.panel_width > 0 { "│ " } else { "" }),
)?;
} else {
write!(handle, "{}", " ".repeat(self.panel_width))?;
};
let (prefix, name): (&str, String) = match header_overwrite {
Some(overwrite) => ("", overwrite),
None => match file {
InputFile::Ordinary(filename) => ("File: ", filename.to_string()),
InputFile::String(_) => ("", "".to_string()),
_ => unimplemented!(),
},
};
let mode = match self.content_type {
ContentType::BINARY => " <BINARY>",
ContentType::UTF_16LE => " <UTF-16LE>",
ContentType::UTF_16BE => " <UTF-16BE>",
_ => "",
};
writeln!(
handle,
"{}{}{}",
prefix,
self.colors.filename.paint(name),
mode
)?;
if self.output_components.grid() {
if self.content_type.is_text() {
self.print_horizontal_line(handle, '┼')?;
} else {
self.print_horizontal_line(handle, 'â”´')?;
}
}
Ok(())
}
fn print_footer(&mut self, handle: &mut dyn Write) -> Result<()> {
if self.output_components.grid() && self.content_type.is_text() {
self.print_horizontal_line(handle, 'â”´')
} else {
Ok(())
}
}
fn print_line(
&mut self,
out_of_range: bool,
handle: &mut dyn Write,
line_number: usize,
line_buffer: &[u8],
) -> Result<()> {
let mut line = match self.content_type {
ContentType::BINARY => {
return Ok(());
}
ContentType::UTF_16LE => UTF_16LE
.decode(&line_buffer, DecoderTrap::Strict)
.unwrap_or("Invalid UTF-16LE".into()),
ContentType::UTF_16BE => UTF_16BE
.decode(&line_buffer, DecoderTrap::Strict)
.unwrap_or("Invalid UTF-16BE".into()),
_ => String::from_utf8_lossy(&line_buffer).to_string(),
};
if self.show_nonprintable {
line = replace_nonprintable(&mut line, self.tab_width);
}
let regions = {
let highlighter = match self.highlighter {
Some(ref mut highlighter) => highlighter,
_ => {
return Ok(());
}
};
highlighter.highlight(line.as_ref(), self.syntax_set)
};
if out_of_range {
return Ok(());
}
let mut cursor: usize = 0;
let mut cursor_max: usize = self.term_width;
let mut cursor_total: usize = 0;
let mut panel_wrap: Option<String> = None;
if self.panel_width > 0 {
let decorations = self
.decorations
.iter()
.map(|ref d| d.generate(line_number, false, self))
.collect::<Vec<_>>();
for deco in decorations {
write!(handle, "{} ", deco.text)?;
cursor_max -= deco.width + 1;
}
}
if self.output_wrap == OutputWrap::None {
let true_color = self.true_color;
let colored_output = self.colored_output;
let italics = self.use_italic_text;
for &(style, region) in regions.iter() {
let text = &*self.preprocess(region, &mut cursor_total);
write!(
handle,
"{}",
as_terminal_escaped(style, &*text, true_color, colored_output, italics,)
)?;
}
if line.bytes().next_back() != Some(b'\n') {
write!(handle, "\n")?;
}
} else {
for &(style, region) in regions.iter() {
let ansi_iterator = AnsiCodeIterator::new(region);
let mut ansi_prefix: String = String::new();
for chunk in ansi_iterator {
match chunk {
(text, true) => {
if text.chars().last().map_or(false, |c| c == 'm') {
ansi_prefix.push_str(text);
if text == "\x1B[0m" {
self.ansi_prefix_sgr = "\x1B[0m".to_owned();
} else {
self.ansi_prefix_sgr.push_str(text);
}
} else {
ansi_prefix.push_str(text);
}
}
(text, false) => {
let text = self.preprocess(
text.trim_end_matches(|c| c == '\r' || c == '\n'),
&mut cursor_total,
);
let mut chars = text.chars();
let mut remaining = text.chars().count();
while remaining > 0 {
let available = cursor_max - cursor;
if remaining <= available {
let text = chars.by_ref().take(remaining).collect::<String>();
cursor += remaining;
write!(
handle,
"{}",
as_terminal_escaped(
style,
&*format!(
"{}{}{}",
self.ansi_prefix_sgr, ansi_prefix, text
),
self.true_color,
self.colored_output,
self.use_italic_text
)
)?;
break;
}
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())
}
}
let text = chars.by_ref().take(available).collect::<String>();
cursor = 0;
remaining -= available;
write!(
handle,
"{}\n{}",
as_terminal_escaped(
style,
&*format!(
"{}{}{}",
self.ansi_prefix_sgr, ansi_prefix, text
),
self.true_color,
self.colored_output,
self.use_italic_text
),
panel_wrap.clone().unwrap()
)?;
}
ansi_prefix.clear();
}
}
}
}
write!(handle, "\n")?;
}
Ok(())
}
}
const DEFAULT_GUTTER_COLOR: u8 = 238;
#[derive(Default)]
pub struct Colors {
pub grid: Style,
pub filename: Style,
pub line_number: Style,
}
impl Colors {
fn plain() -> Self {
Colors::default()
}
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));
Colors {
grid: gutter_color.normal(),
filename: Style::new().bold(),
line_number: gutter_color.normal(),
}
}
}