Allow fluent style

This commit is contained in:
sharkdp 2020-04-21 21:29:47 +02:00 committed by David Peter
parent f8d0956893
commit 3bacfc5184
3 changed files with 20 additions and 16 deletions

View File

@ -4,16 +4,14 @@ use bat::{PrettyPrinter, StyleComponent, StyleComponents};
use console::Term;
fn main() {
let mut printer = PrettyPrinter::new();
printer
PrettyPrinter::new()
.term_width(Term::stdout().size().1 as usize)
.style_components(StyleComponents::new(&[
StyleComponent::Header,
StyleComponent::Grid,
StyleComponent::Numbers,
]))
.files(std::env::args_os().skip(1));
printer.run().expect("no errors");
.input_files(std::env::args_os().skip(1))
.run()
.expect("no errors");
}

View File

@ -5,9 +5,8 @@ use std::ffi::OsStr;
fn main() {
let path_to_this_file = OsStr::new(file!());
let mut printer = PrettyPrinter::new();
printer.file(path_to_this_file);
printer.run().expect("no errors");
PrettyPrinter::new()
.input_file(path_to_this_file)
.run()
.expect("no errors");
}

View File

@ -33,14 +33,14 @@ impl<'a> PrettyPrinter<'a> {
}
/// Add a file which should be pretty-printed
pub fn file(&mut self, path: &OsStr) -> &mut Self {
pub fn input_file(&mut self, path: &OsStr) -> &mut Self {
self.inputs
.push(Input::Ordinary(OrdinaryFile::from_path(path)));
self
}
/// Add multiple files which should be pretty-printed
pub fn files<I, P>(&mut self, paths: I) -> &mut Self
pub fn input_files<I, P>(&mut self, paths: I) -> &mut Self
where
I: IntoIterator<Item = P>,
P: AsRef<OsStr>,
@ -52,6 +52,7 @@ impl<'a> PrettyPrinter<'a> {
self
}
/// Specify the syntax file which should be used (default: auto-detect)
pub fn language(&mut self, language: &'a str) -> &mut Self {
self.config.language = Some(language);
self
@ -81,7 +82,7 @@ impl<'a> PrettyPrinter<'a> {
self
}
/// Configure style elements (grid, line numbers, ...)
/// Configure style elements like grid or line numbers (default: "full" style)
pub fn style_components(&mut self, components: StyleComponents) -> &mut Self {
self.config.style_components = components;
self
@ -137,8 +138,14 @@ impl<'a> PrettyPrinter<'a> {
self
}
pub fn run(self) -> Result<bool> {
/// Pretty-print all specified inputs. This method will drain all stored inputs.
/// If you want to call 'run' multiple times, you have to call the appropriate
/// input_* methods again.
pub fn run(&mut self) -> Result<bool> {
let mut inputs: Vec<Input> = vec![];
std::mem::swap(&mut inputs, &mut self.inputs);
let controller = Controller::new(&self.config, &self.assets);
controller.run(self.inputs)
controller.run(inputs)
}
}