bat/src/pretty_printer.rs

185 lines
5.5 KiB
Rust
Raw Normal View History

use std::ffi::OsStr;
2020-04-21 20:24:47 +00:00
use std::io::Read;
2020-04-22 18:35:05 +00:00
use console::Term;
use crate::{
2020-04-22 17:15:33 +00:00
assets::HighlightingAssets,
config::Config,
controller::Controller,
errors::Result,
input::Input,
line_range::{HighlightedLineRanges, LineRanges},
2020-04-22 18:35:05 +00:00
LineRange, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
};
2020-04-21 18:21:02 +00:00
#[cfg(feature = "paging")]
use crate::config::PagingMode;
pub struct PrettyPrinter<'a> {
inputs: Vec<Input<'a>>,
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>,
}
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![],
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,
}
}
/// Add a file which should be pretty-printed
2020-04-21 19:29:47 +00:00
pub fn input_file(&mut self, path: &OsStr) -> &mut Self {
self.inputs.push(Input::ordinary_file(path));
self
}
/// 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
where
I: IntoIterator<Item = P>,
P: AsRef<OsStr>,
{
for path in paths {
self.inputs.push(Input::ordinary_file(path.as_ref()));
}
self
}
2020-04-21 20:24:47 +00:00
/// Add STDIN as an input
pub fn input_stdin(&mut self) -> &mut Self {
self.inputs.push(Input::stdin());
2020-04-21 20:24:47 +00:00
self
}
/// Use a string as an input
pub fn input_from_bytes(&mut self, content: &'a [u8]) -> &mut Self {
self.input_from_reader(content)
}
2020-04-22 06:16:08 +00:00
/// Add a custom reader as an input
pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
self.inputs.push(Input::from_reader(Box::new(reader)));
2020-04-21 20:24:47 +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)
pub fn term_width(&mut self, width: usize) -> &mut Self {
2020-04-22 18:35:05 +00:00
self.term_width = Some(width);
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
}
/// 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
}
/// 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-21 19:29:47 +00:00
/// Configure style elements like grid or line numbers (default: "full" style)
2020-04-22 18:35:05 +00:00
pub fn style_components(&mut self, components: &[StyleComponent]) -> &mut Self {
self.config.style_components = StyleComponents::new(components);
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 {
self.config.line_ranges = ranges;
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.
pub fn highlight(&mut self, range: LineRange) -> &mut Self {
self.highlighted_lines.push(range);
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;
self
}
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-21 19:29:47 +00:00
let mut inputs: Vec<Input> = vec![];
std::mem::swap(&mut inputs, &mut self.inputs);
let controller = Controller::new(&self.config, &self.assets);
2020-04-21 19:29:47 +00:00
controller.run(inputs)
}
}