mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-22 15:53:29 +01:00
Initial verison of PrettyPrinter builder
This commit is contained in:
parent
319ab779ee
commit
27974616bf
@ -1,22 +1,12 @@
|
|||||||
/// A simple program that prints its own source code using the bat library
|
/// A simple program that prints its own source code using the bat library
|
||||||
use bat::{
|
use bat::PrettyPrinter;
|
||||||
config::{Config, InputFile, OrdinaryFile},
|
|
||||||
Controller, HighlightingAssets,
|
|
||||||
};
|
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let path_to_this_file = OsStr::new(file!());
|
let path_to_this_file = OsStr::new(file!());
|
||||||
|
|
||||||
let config = Config {
|
PrettyPrinter::new()
|
||||||
files: vec![InputFile::Ordinary(OrdinaryFile::from_path(
|
.file(path_to_this_file)
|
||||||
path_to_this_file,
|
.run()
|
||||||
))],
|
.expect("no errors");
|
||||||
colored_output: true,
|
|
||||||
true_color: true,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let assets = HighlightingAssets::from_binary();
|
|
||||||
|
|
||||||
Controller::new(&config, &assets).run().expect("no errors");
|
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,8 @@ pub fn generate_config_file() -> bat::errors::Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let default_config = r#"# This is `bat`s configuration file. Each line either contains a comment or
|
let default_config =
|
||||||
|
r#"# This is `bat`s configuration file. Each line either contains a comment or
|
||||||
# a command-line option that you want to pass to `bat` by default. You can
|
# a command-line option that you want to pass to `bat` by default. You can
|
||||||
# run `bat --help` to get a list of all possible configuration options.
|
# run `bat --help` to get a list of all possible configuration options.
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ mod less;
|
|||||||
pub(crate) mod line_range;
|
pub(crate) mod line_range;
|
||||||
mod output;
|
mod output;
|
||||||
mod preprocessor;
|
mod preprocessor;
|
||||||
|
pub mod pretty_printer;
|
||||||
pub(crate) mod printer;
|
pub(crate) mod printer;
|
||||||
pub(crate) mod style;
|
pub(crate) mod style;
|
||||||
pub(crate) mod syntax_mapping;
|
pub(crate) mod syntax_mapping;
|
||||||
@ -22,4 +23,5 @@ pub(crate) mod wrap;
|
|||||||
pub use assets::HighlightingAssets;
|
pub use assets::HighlightingAssets;
|
||||||
pub use assets_metadata::AssetsMetadata;
|
pub use assets_metadata::AssetsMetadata;
|
||||||
pub use controller::Controller;
|
pub use controller::Controller;
|
||||||
|
pub use pretty_printer::PrettyPrinter;
|
||||||
pub use printer::{InteractivePrinter, Printer, SimplePrinter};
|
pub use printer::{InteractivePrinter, Printer, SimplePrinter};
|
||||||
|
44
src/pretty_printer.rs
Normal file
44
src/pretty_printer.rs
Normal file
@ -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
Block a user