From 3bacfc5184184c4a845d56f9e7226e87eae6d172 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Tue, 21 Apr 2020 21:29:47 +0200 Subject: Allow fluent style --- examples/cat.rs | 10 ++++------ examples/simple.rs | 9 ++++----- src/pretty_printer.rs | 17 ++++++++++++----- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/examples/cat.rs b/examples/cat.rs index c529bd6a..c1c9b242 100644 --- a/examples/cat.rs +++ b/examples/cat.rs @@ -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"); } diff --git a/examples/simple.rs b/examples/simple.rs index a035b6c5..c30cbd2a 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -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"); } diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs index a517de0f..a03b0e3b 100644 --- a/src/pretty_printer.rs +++ b/src/pretty_printer.rs @@ -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(&mut self, paths: I) -> &mut Self + pub fn input_files(&mut self, paths: I) -> &mut Self where I: IntoIterator, P: AsRef, @@ -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 { + /// 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 { + let mut inputs: Vec = vec![]; + std::mem::swap(&mut inputs, &mut self.inputs); + let controller = Controller::new(&self.config, &self.assets); - controller.run(self.inputs) + controller.run(inputs) } } -- cgit v1.2.3