mirror of https://github.com/sharkdp/bat
Initial verison of PrettyPrinter builder
parent
319ab779ee
commit
27974616bf
@ -1,22 +1,12 @@
|
||||
/// A simple program that prints its own source code using the bat library
|
||||
use bat::{
|
||||
config::{Config, InputFile, OrdinaryFile},
|
||||
Controller, HighlightingAssets,
|
||||
};
|
||||
use bat::PrettyPrinter;
|
||||
use std::ffi::OsStr;
|
||||
|
||||
fn main() {
|
||||
let path_to_this_file = OsStr::new(file!());
|
||||
|
||||
let config = Config {
|
||||
files: vec![InputFile::Ordinary(OrdinaryFile::from_path(
|
||||
path_to_this_file,
|
||||
))],
|
||||
colored_output: true,
|
||||
true_color: true,
|
||||
..Default::default()
|
||||
};
|
||||
let assets = HighlightingAssets::from_binary();
|
||||
|
||||
Controller::new(&config, &assets).run().expect("no errors");
|
||||
PrettyPrinter::new()
|
||||
.file(path_to_this_file)
|
||||
.run()
|
||||
.expect("no errors");
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
use std::ffi::OsStr;
|
||||
|
||||
use crate::{
|
||||
config::{Config, InputFile, OrdinaryFile},
|
||||
errors::Result,
|
||||
Controller, HighlightingAssets,
|
||||
};
|
||||
|
||||
pub struct PrettyPrinter<'a> {
|
||||
config: Config<'a>,
|
||||
assets: HighlightingAssets,
|
||||
}
|
||||
|
||||
impl<'a> PrettyPrinter<'a> {
|
||||
pub fn new() -> Self {
|
||||
let mut config = Config::default();
|
||||
|
||||
config.colored_output = true;
|
||||
config.true_color = true;
|
||||
|
||||
PrettyPrinter {
|
||||
config,
|
||||
assets: HighlightingAssets::from_binary(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file(&'a mut self, path: &'a OsStr) -> &'a mut Self {
|
||||
self.config
|
||||
.files
|
||||
.push(InputFile::Ordinary(OrdinaryFile::from_path(path)));
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether or not the output should be colorized (default: true)
|
||||
pub fn colored_output(&mut self, yes: bool) -> &mut Self {
|
||||
self.config.colored_output = yes;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn run(&'a self) -> Result<bool> {
|
||||
let controller = Controller::new(&self.config, &self.assets);
|
||||
controller.run()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue