on master help sign in
Initial verison of PrettyPrinter builder
commit27974616bfe7ec0c132fc259fdce1b471c99c93e authorsharkdp <davidpeter@web.de> dateTue 21 Apr 2020, 19:08 (7 years ago) parent319ab77
4 files changed+53−16
examples/simple.rs+5−15 src/bin/bat/config.rs+2−1 src/lib.rs+2 src/pretty_printer.rs+44
examples/simple.rs+5−15
@@ -1,22 +1,12 @@
11/// A simple program that prints its own source code using the bat library
2−use bat::{
3− config::{Config, InputFile, OrdinaryFile},
4− Controller, HighlightingAssets,
5−};
2+use bat::PrettyPrinter;
63use std::ffi::OsStr;
74
85fn main() {
96 let path_to_this_file = OsStr::new(file!());
107
11− let config = Config {
12− files: vec![InputFile::Ordinary(OrdinaryFile::from_path(
13− path_to_this_file,
14− ))],
15− colored_output: true,
16− true_color: true,
17− ..Default::default()
18− };
19− let assets = HighlightingAssets::from_binary();
20−
21− Controller::new(&config, &assets).run().expect("no errors");
8+ PrettyPrinter::new()
9+ .file(path_to_this_file)
10+ .run()
11+ .expect("no errors");
2212}
src/bin/bat/config.rs+2−1
@@ -43,7 +43,8 @@ pub fn generate_config_file() -> bat::errors::Result<()> {
4343 }
4444 }
4545
46− let default_config = r#"# This is `bat`s configuration file. Each line either contains a comment or
46+ let default_config =
47+ r#"# This is `bat`s configuration file. Each line either contains a comment or
4748# a command-line option that you want to pass to `bat` by default. You can
4849# run `bat --help` to get a list of all possible configuration options.
4950
src/lib.rs+2
@@ -13,6 +13,7 @@ mod less;
1313pub(crate) mod line_range;
1414mod output;
1515mod preprocessor;
16+pub mod pretty_printer;
1617pub(crate) mod printer;
1718pub(crate) mod style;
1819pub(crate) mod syntax_mapping;
@@ -22,4 +23,5 @@ pub(crate) mod wrap;
2223pub use assets::HighlightingAssets;
2324pub use assets_metadata::AssetsMetadata;
2425pub use controller::Controller;
26+pub use pretty_printer::PrettyPrinter;
2527pub use printer::{InteractivePrinter, Printer, SimplePrinter};
src/pretty_printer.rsnew file+44
@@ -0,0 +1,44 @@
1+use std::ffi::OsStr;
2+
3+use crate::{
4+ config::{Config, InputFile, OrdinaryFile},
5+ errors::Result,
6+ Controller, HighlightingAssets,
7+};
8+
9+pub struct PrettyPrinter<'a> {
10+ config: Config<'a>,
11+ assets: HighlightingAssets,
12+}
13+
14+impl<'a> PrettyPrinter<'a> {
15+ pub fn new() -> Self {
16+ let mut config = Config::default();
17+
18+ config.colored_output = true;
19+ config.true_color = true;
20+
21+ PrettyPrinter {
22+ config,
23+ assets: HighlightingAssets::from_binary(),
24+ }
25+ }
26+
27+ pub fn file(&'a mut self, path: &'a OsStr) -> &'a mut Self {
28+ self.config
29+ .files
30+ .push(InputFile::Ordinary(OrdinaryFile::from_path(path)));
31+ self
32+ }
33+
34+ /// Whether or not the output should be colorized (default: true)
35+ pub fn colored_output(&mut self, yes: bool) -> &mut Self {
36+ self.config.colored_output = yes;
37+ self
38+ }
39+
40+ pub fn run(&'a self) -> Result<bool> {
41+ let controller = Controller::new(&self.config, &self.assets);
42+ controller.run()
43+ }
44+}