on master help sign in
Allow fluent style
commit3bacfc5184184c4a845d56f9e7226e87eae6d172 authorsharkdp <davidpeter@web.de> dateTue 21 Apr 2020, 21:29 (7 years ago) parentf8d0956
3 files changed+20−16
examples/cat.rs+4−6 examples/simple.rs+4−5 src/pretty_printer.rs+12−5
examples/cat.rs+4−6
@@ -4,16 +4,14 @@ use bat::{PrettyPrinter, StyleComponent, StyleComponents};
44use console::Term;
55
66fn main() {
7− let mut printer = PrettyPrinter::new();
8−
9− printer
7+ PrettyPrinter::new()
108 .term_width(Term::stdout().size().1 as usize)
119 .style_components(StyleComponents::new(&[
1210 StyleComponent::Header,
1311 StyleComponent::Grid,
1412 StyleComponent::Numbers,
1513 ]))
16− .files(std::env::args_os().skip(1));
17−
18− printer.run().expect("no errors");
14+ .input_files(std::env::args_os().skip(1))
15+ .run()
16+ .expect("no errors");
1917}
examples/simple.rs+4−5
@@ -5,9 +5,8 @@ use std::ffi::OsStr;
55fn main() {
66 let path_to_this_file = OsStr::new(file!());
77
8− let mut printer = PrettyPrinter::new();
9−
10− printer.file(path_to_this_file);
11−
12− printer.run().expect("no errors");
8+ PrettyPrinter::new()
9+ .input_file(path_to_this_file)
10+ .run()
11+ .expect("no errors");
1312}
src/pretty_printer.rs+12−5
@@ -33,14 +33,14 @@ impl<'a> PrettyPrinter<'a> {
3333 }
3434
3535 /// Add a file which should be pretty-printed
36− pub fn file(&mut self, path: &OsStr) -> &mut Self {
36+ pub fn input_file(&mut self, path: &OsStr) -> &mut Self {
3737 self.inputs
3838 .push(Input::Ordinary(OrdinaryFile::from_path(path)));
3939 self
4040 }
4141
4242 /// Add multiple files which should be pretty-printed
43− pub fn files<I, P>(&mut self, paths: I) -> &mut Self
43+ pub fn input_files<I, P>(&mut self, paths: I) -> &mut Self
4444 where
4545 I: IntoIterator<Item = P>,
4646 P: AsRef<OsStr>,
@@ -52,6 +52,7 @@ impl<'a> PrettyPrinter<'a> {
5252 self
5353 }
5454
55+ /// Specify the syntax file which should be used (default: auto-detect)
5556 pub fn language(&mut self, language: &'a str) -> &mut Self {
5657 self.config.language = Some(language);
5758 self
@@ -81,7 +82,7 @@ impl<'a> PrettyPrinter<'a> {
8182 self
8283 }
8384
84− /// Configure style elements (grid, line numbers, ...)
85+ /// Configure style elements like grid or line numbers (default: "full" style)
8586 pub fn style_components(&mut self, components: StyleComponents) -> &mut Self {
8687 self.config.style_components = components;
8788 self
@@ -137,8 +138,14 @@ impl<'a> PrettyPrinter<'a> {
137138 self
138139 }
139140
140− pub fn run(self) -> Result<bool> {
141+ /// Pretty-print all specified inputs. This method will drain all stored inputs.
142+ /// If you want to call 'run' multiple times, you have to call the appropriate
143+ /// input_* methods again.
144+ pub fn run(&mut self) -> Result<bool> {
145+ let mut inputs: Vec<Input> = vec![];
146+ std::mem::swap(&mut inputs, &mut self.inputs);
147+
141148 let controller = Controller::new(&self.config, &self.assets);
142− controller.run(self.inputs)
149+ controller.run(inputs)
143150 }
144151}