2020-04-21 17:08:19 +00:00
|
|
|
use std::ffi::OsStr;
|
2020-04-21 20:24:47 +00:00
|
|
|
use std::io::Read;
|
2020-04-21 17:08:19 +00:00
|
|
|
|
2020-04-22 18:35:05 +00:00
|
|
|
use console::Term;
|
2020-04-22 20:05:54 +00:00
|
|
|
use syntect::parsing::SyntaxReference;
|
2020-04-22 18:35:05 +00:00
|
|
|
|
2020-04-21 17:08:19 +00:00
|
|
|
use crate::{
|
2020-04-22 17:15:33 +00:00
|
|
|
assets::HighlightingAssets,
|
2020-04-23 21:39:30 +00:00
|
|
|
config::{Config, VisibleLines},
|
2020-04-22 17:15:33 +00:00
|
|
|
controller::Controller,
|
2020-04-22 19:45:47 +00:00
|
|
|
error::Result,
|
2020-05-16 22:06:43 +00:00
|
|
|
input::Input as BatInput,
|
2020-04-22 20:50:57 +00:00
|
|
|
line_range::{HighlightedLineRanges, LineRange, LineRanges},
|
2020-04-22 19:16:30 +00:00
|
|
|
style::{StyleComponent, StyleComponents},
|
2020-04-22 20:50:57 +00:00
|
|
|
SyntaxMapping, WrappingMode,
|
2020-04-21 17:08:19 +00:00
|
|
|
};
|
|
|
|
|
2020-04-21 18:21:02 +00:00
|
|
|
#[cfg(feature = "paging")]
|
2020-04-22 20:54:33 +00:00
|
|
|
use crate::paging::PagingMode;
|
2020-04-21 18:21:02 +00:00
|
|
|
|
2020-04-22 19:16:30 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct ActiveStyleComponents {
|
|
|
|
header: bool,
|
|
|
|
vcs_modification_markers: bool,
|
|
|
|
grid: bool,
|
|
|
|
line_numbers: bool,
|
|
|
|
snip: bool,
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:08:19 +00:00
|
|
|
pub struct PrettyPrinter<'a> {
|
2020-04-22 16:10:26 +00:00
|
|
|
inputs: Vec<Input<'a>>,
|
2020-04-21 17:08:19 +00:00
|
|
|
config: Config<'a>,
|
|
|
|
assets: HighlightingAssets,
|
2020-04-22 17:15:33 +00:00
|
|
|
|
|
|
|
highlighted_lines: Vec<LineRange>,
|
2020-04-22 18:35:05 +00:00
|
|
|
term_width: Option<usize>,
|
2020-04-22 19:16:30 +00:00
|
|
|
active_style_components: ActiveStyleComponents,
|
2020-04-21 17:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PrettyPrinter<'a> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut config = Config::default();
|
|
|
|
|
|
|
|
config.colored_output = true;
|
|
|
|
config.true_color = true;
|
|
|
|
|
|
|
|
PrettyPrinter {
|
2020-04-21 19:14:44 +00:00
|
|
|
inputs: vec![],
|
2020-04-21 17:08:19 +00:00
|
|
|
config,
|
|
|
|
assets: HighlightingAssets::from_binary(),
|
2020-04-22 18:35:05 +00:00
|
|
|
|
2020-04-22 17:15:33 +00:00
|
|
|
highlighted_lines: vec![],
|
2020-04-22 18:35:05 +00:00
|
|
|
term_width: None,
|
2020-04-22 19:16:30 +00:00
|
|
|
active_style_components: ActiveStyleComponents::default(),
|
2020-04-21 17:08:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 23:18:44 +00:00
|
|
|
/// Add an input which should be pretty-printed
|
|
|
|
pub fn input(&mut self, input: Input<'a>) -> &mut Self {
|
|
|
|
self.inputs.push(input);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-05-16 22:06:43 +00:00
|
|
|
/// Adds multiple inputs which should be pretty-printed
|
|
|
|
pub fn inputs(&mut self, inputs: impl IntoIterator<Item = Input<'a>>) -> &mut Self {
|
|
|
|
for input in inputs {
|
|
|
|
self.inputs.push(input);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:06:09 +00:00
|
|
|
/// Add a file which should be pretty-printed
|
2020-04-22 19:21:47 +00:00
|
|
|
pub fn input_file(&mut self, path: impl AsRef<OsStr>) -> &mut Self {
|
2020-05-16 22:06:43 +00:00
|
|
|
self.input(Input::from_file(path).kind("File"))
|
2020-04-21 17:08:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 18:06:09 +00:00
|
|
|
/// Add multiple files which should be pretty-printed
|
2020-04-21 19:29:47 +00:00
|
|
|
pub fn input_files<I, P>(&mut self, paths: I) -> &mut Self
|
2020-04-21 18:06:09 +00:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = P>,
|
|
|
|
P: AsRef<OsStr>,
|
|
|
|
{
|
2020-05-16 22:06:43 +00:00
|
|
|
self.inputs(paths.into_iter().map(Input::from_file))
|
2020-04-21 18:06:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 20:24:47 +00:00
|
|
|
/// Add STDIN as an input
|
|
|
|
pub fn input_stdin(&mut self) -> &mut Self {
|
2020-05-16 22:06:43 +00:00
|
|
|
self.inputs.push(Input::from_stdin());
|
2020-04-21 20:24:47 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:41:25 +00:00
|
|
|
/// Add STDIN as an input (with customized name)
|
2020-05-16 22:06:43 +00:00
|
|
|
#[deprecated]
|
2020-04-22 20:41:25 +00:00
|
|
|
pub fn input_stdin_with_name(&mut self, name: impl AsRef<OsStr>) -> &mut Self {
|
|
|
|
self.inputs
|
2020-05-16 22:06:43 +00:00
|
|
|
.push(Input::from_stdin().name(name).kind("File"));
|
2020-04-22 20:41:25 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a byte string as an input
|
2020-04-22 16:10:26 +00:00
|
|
|
pub fn input_from_bytes(&mut self, content: &'a [u8]) -> &mut Self {
|
|
|
|
self.input_from_reader(content)
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:41:25 +00:00
|
|
|
/// Add a byte string as an input (with customized name)
|
2020-05-16 22:06:43 +00:00
|
|
|
#[deprecated]
|
|
|
|
#[allow(deprecated)]
|
2020-04-22 20:41:25 +00:00
|
|
|
pub fn input_from_bytes_with_name(
|
|
|
|
&mut self,
|
|
|
|
content: &'a [u8],
|
|
|
|
name: impl AsRef<OsStr>,
|
|
|
|
) -> &mut Self {
|
|
|
|
self.input_from_reader_with_name(content, name)
|
|
|
|
}
|
|
|
|
|
2020-04-22 06:16:08 +00:00
|
|
|
/// Add a custom reader as an input
|
2020-04-22 16:10:26 +00:00
|
|
|
pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
|
2020-05-16 22:06:43 +00:00
|
|
|
self.inputs.push(Input::from_reader(reader));
|
2020-04-21 20:24:47 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:41:25 +00:00
|
|
|
/// Add a custom reader as an input (with customized name)
|
2020-05-16 22:06:43 +00:00
|
|
|
#[deprecated]
|
2020-04-22 20:41:25 +00:00
|
|
|
pub fn input_from_reader_with_name<R: Read + 'a>(
|
|
|
|
&mut self,
|
|
|
|
reader: R,
|
|
|
|
name: impl AsRef<OsStr>,
|
|
|
|
) -> &mut Self {
|
|
|
|
self.inputs
|
2020-05-16 22:06:43 +00:00
|
|
|
.push(Input::from_reader(reader).name(name).kind("File"));
|
2020-04-22 20:41:25 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-21 19:29:47 +00:00
|
|
|
/// Specify the syntax file which should be used (default: auto-detect)
|
2020-04-21 18:21:02 +00:00
|
|
|
pub fn language(&mut self, language: &'a str) -> &mut Self {
|
|
|
|
self.config.language = Some(language);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-22 18:35:05 +00:00
|
|
|
/// The character width of the terminal (default: autodetect)
|
2020-04-21 18:06:09 +00:00
|
|
|
pub fn term_width(&mut self, width: usize) -> &mut Self {
|
2020-04-22 18:35:05 +00:00
|
|
|
self.term_width = Some(width);
|
2020-04-21 18:06:09 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The width of tab characters (default: None - do not turn tabs to spaces)
|
|
|
|
pub fn tab_width(&mut self, tab_width: Option<usize>) -> &mut Self {
|
|
|
|
self.config.tab_width = tab_width.unwrap_or(0);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:08:19 +00:00
|
|
|
/// Whether or not the output should be colorized (default: true)
|
|
|
|
pub fn colored_output(&mut self, yes: bool) -> &mut Self {
|
|
|
|
self.config.colored_output = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:06:09 +00:00
|
|
|
/// Whether or not to output 24bit colors (default: true)
|
|
|
|
pub fn true_color(&mut self, yes: bool) -> &mut Self {
|
|
|
|
self.config.true_color = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:16:30 +00:00
|
|
|
/// Whether to show a header with the file name
|
|
|
|
pub fn header(&mut self, yes: bool) -> &mut Self {
|
|
|
|
self.active_style_components.header = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether to show line numbers
|
|
|
|
pub fn line_numbers(&mut self, yes: bool) -> &mut Self {
|
|
|
|
self.active_style_components.line_numbers = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether to paint a grid, separating line numbers, git changes and the code
|
|
|
|
pub fn grid(&mut self, yes: bool) -> &mut Self {
|
|
|
|
self.active_style_components.grid = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-05-24 14:19:07 +00:00
|
|
|
/// Whether to show modification markers for VCS changes. This has no effect if
|
|
|
|
/// the `git` feature is not activated.
|
2020-05-25 07:03:54 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
not(feature = "git"),
|
|
|
|
deprecated(
|
|
|
|
note = "Using vcs_modification_markers without the 'git' feature has no effect. \
|
|
|
|
The function will be removed (for non-'git' use cases) in the future."
|
|
|
|
)
|
|
|
|
)]
|
|
|
|
#[allow(unused_variables)]
|
2020-04-22 19:16:30 +00:00
|
|
|
pub fn vcs_modification_markers(&mut self, yes: bool) -> &mut Self {
|
2020-05-24 14:19:07 +00:00
|
|
|
#[cfg(feature = "git")]
|
|
|
|
{
|
|
|
|
self.active_style_components.vcs_modification_markers = yes;
|
|
|
|
}
|
2020-04-22 19:16:30 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether to show "snip" markers between visible line ranges (default: no)
|
|
|
|
pub fn snip(&mut self, yes: bool) -> &mut Self {
|
|
|
|
self.active_style_components.snip = yes;
|
2020-04-21 18:06:09 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Text wrapping mode (default: do not wrap)
|
2020-04-21 18:21:02 +00:00
|
|
|
pub fn wrapping_mode(&mut self, mode: WrappingMode) -> &mut Self {
|
|
|
|
self.config.wrapping_mode = mode;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether or not to use ANSI italics (default: off)
|
|
|
|
pub fn use_italics(&mut self, yes: bool) -> &mut Self {
|
|
|
|
self.config.use_italic_text = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If and how to use a pager (default: no paging)
|
|
|
|
#[cfg(feature = "paging")]
|
|
|
|
pub fn paging_mode(&mut self, mode: PagingMode) -> &mut Self {
|
|
|
|
self.config.paging_mode = mode;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Specify the command to start the pager (default: use "less")
|
|
|
|
#[cfg(feature = "paging")]
|
|
|
|
pub fn pager(&mut self, cmd: &'a str) -> &mut Self {
|
|
|
|
self.config.pager = Some(cmd);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Specify the lines that should be printed (default: all)
|
|
|
|
pub fn line_ranges(&mut self, ranges: LineRanges) -> &mut Self {
|
2020-04-23 21:39:30 +00:00
|
|
|
self.config.visible_lines = VisibleLines::Ranges(ranges);
|
2020-04-21 18:21:02 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:50:57 +00:00
|
|
|
/// Specify a line that should be highlighted (default: none).
|
|
|
|
/// This can be called multiple times to highlight more than one
|
|
|
|
/// line. See also: highlight_range.
|
|
|
|
pub fn highlight(&mut self, line: usize) -> &mut Self {
|
|
|
|
self.highlighted_lines.push(LineRange::new(line, line));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-22 17:15:33 +00:00
|
|
|
/// Specify a range of lines that should be highlighted (default: none).
|
|
|
|
/// This can be called multiple times to highlight more than one range
|
|
|
|
/// of lines.
|
2020-04-22 20:50:57 +00:00
|
|
|
pub fn highlight_range(&mut self, from: usize, to: usize) -> &mut Self {
|
|
|
|
self.highlighted_lines.push(LineRange::new(from, to));
|
2020-04-21 18:21:02 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Specify the highlighting theme
|
|
|
|
pub fn theme(&mut self, theme: impl AsRef<str>) -> &mut Self {
|
|
|
|
self.config.theme = theme.as_ref().to_owned();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Specify custom file extension / file name to syntax mappings
|
|
|
|
pub fn syntax_mapping(&mut self, mapping: SyntaxMapping<'a>) -> &mut Self {
|
|
|
|
self.config.syntax_mapping = mapping;
|
2020-04-21 18:06:09 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:05:54 +00:00
|
|
|
pub fn themes(&self) -> impl Iterator<Item = &str> {
|
|
|
|
self.assets.themes()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn syntaxes(&self) -> impl Iterator<Item = &SyntaxReference> {
|
|
|
|
self.assets.syntaxes().iter()
|
|
|
|
}
|
|
|
|
|
2020-04-22 18:33:19 +00:00
|
|
|
/// Pretty-print all specified inputs. This method will "use" all stored inputs.
|
|
|
|
/// If you want to call 'print' multiple times, you have to call the appropriate
|
2020-04-21 19:29:47 +00:00
|
|
|
/// input_* methods again.
|
2020-04-22 18:33:19 +00:00
|
|
|
pub fn print(&mut self) -> Result<bool> {
|
2020-04-22 17:15:33 +00:00
|
|
|
self.config.highlighted_lines =
|
|
|
|
HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone()));
|
2020-04-22 18:35:05 +00:00
|
|
|
self.config.term_width = self
|
|
|
|
.term_width
|
|
|
|
.unwrap_or_else(|| Term::stdout().size().1 as usize);
|
2020-04-22 17:15:33 +00:00
|
|
|
|
2020-04-22 19:16:30 +00:00
|
|
|
let mut style_components = vec![];
|
|
|
|
if self.active_style_components.grid {
|
|
|
|
style_components.push(StyleComponent::Grid);
|
|
|
|
}
|
|
|
|
if self.active_style_components.header {
|
|
|
|
style_components.push(StyleComponent::Header);
|
|
|
|
}
|
|
|
|
if self.active_style_components.line_numbers {
|
|
|
|
style_components.push(StyleComponent::LineNumbers);
|
|
|
|
}
|
|
|
|
if self.active_style_components.snip {
|
|
|
|
style_components.push(StyleComponent::Snip);
|
|
|
|
}
|
|
|
|
if self.active_style_components.vcs_modification_markers {
|
2020-05-13 05:24:51 +00:00
|
|
|
#[cfg(feature = "git")]
|
2020-04-22 19:16:30 +00:00
|
|
|
style_components.push(StyleComponent::Changes);
|
|
|
|
}
|
|
|
|
self.config.style_components = StyleComponents::new(&style_components);
|
|
|
|
|
2020-05-16 22:06:43 +00:00
|
|
|
// Collect the inputs to print
|
2020-04-21 19:29:47 +00:00
|
|
|
let mut inputs: Vec<Input> = vec![];
|
|
|
|
std::mem::swap(&mut inputs, &mut self.inputs);
|
|
|
|
|
2020-05-16 22:06:43 +00:00
|
|
|
// Run the controller
|
2020-04-21 17:08:19 +00:00
|
|
|
let controller = Controller::new(&self.config, &self.assets);
|
2020-05-16 22:06:43 +00:00
|
|
|
controller.run(inputs.into_iter().map(|i| i.into()).collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Input<'a> {
|
|
|
|
input: BatInput<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Input<'a> {
|
|
|
|
/// A new input from a reader.
|
|
|
|
pub fn from_reader<R: Read + 'a>(reader: R) -> Self {
|
|
|
|
BatInput::from_reader(Box::new(reader)).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A new input from a file.
|
|
|
|
pub fn from_file(path: impl AsRef<OsStr>) -> Self {
|
|
|
|
BatInput::ordinary_file(path.as_ref()).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A new input from bytes.
|
|
|
|
pub fn from_bytes(bytes: &'a [u8]) -> Self {
|
|
|
|
Input::from_reader(bytes).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A new input from STDIN.
|
|
|
|
pub fn from_stdin() -> Self {
|
|
|
|
BatInput::stdin().into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The filename of the input.
|
|
|
|
/// This affects syntax detection and changes the default header title.
|
|
|
|
pub fn name(mut self, name: impl AsRef<OsStr>) -> Self {
|
|
|
|
self.input = self.input.with_name(Some(name.as_ref()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The description for the type of input (e.g. "File")
|
|
|
|
/// This defaults to "File" for files, and nothing for other inputs.
|
|
|
|
pub fn kind(mut self, kind: impl Into<String>) -> Self {
|
|
|
|
let kind = kind.into();
|
|
|
|
self.input
|
|
|
|
.description_mut()
|
|
|
|
.set_kind(if kind.is_empty() { None } else { Some(kind) });
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The title for the input (e.g. "http://example.com/example.txt")
|
|
|
|
/// This defaults to the file name.
|
|
|
|
pub fn title(mut self, title: impl Into<String>) -> Self {
|
|
|
|
self.input.description_mut().set_title(Some(title.into()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Into<Input<'a>> for BatInput<'a> {
|
|
|
|
fn into(self) -> Input<'a> {
|
|
|
|
Input { input: self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Into<BatInput<'a>> for Input<'a> {
|
|
|
|
fn into(self) -> BatInput<'a> {
|
|
|
|
self.input
|
2020-04-21 17:08:19 +00:00
|
|
|
}
|
|
|
|
}
|