summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2020-04-21 21:29:47 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2020-04-22 23:55:28 +0200
commit3bacfc5184184c4a845d56f9e7226e87eae6d172 (patch)
tree2996b6039e6ecb02e22b29a9be8279cec1b91850
parentf8d0956893a0a6e42f9caddbb8e54a25c74d1166 (diff)
Allow fluent style
-rw-r--r--examples/cat.rs10
-rw-r--r--examples/simple.rs9
-rw-r--r--src/pretty_printer.rs17
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<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)
}
}