diff --git a/examples/using_printer.rs b/examples/advanced.rs similarity index 91% rename from examples/using_printer.rs rename to examples/advanced.rs index 94222518..37932bb4 100644 --- a/examples/using_printer.rs +++ b/examples/advanced.rs @@ -8,25 +8,25 @@ use bat::{ style::{OutputComponent, OutputComponents}, Config, }; +use console::Term; use std::{collections::HashSet, io}; fn main() -> bat::errors::Result<()> { - let assets = HighlightingAssets::new(); - let mut config = Config { - term_width: 14, // must be greater than 13 to enable style=numbers + let config = Config { + term_width: Term::stdout().size().1 as usize, colored_output: true, true_color: true, + theme: "1337".into(), line_ranges: LineRanges::from(vec![ LineRange::from("5:7")?, LineRange::from("92:97")?, LineRange::from("15:17")?, ]), output_components: OutputComponents(with_full_decorations()), + files: vec![InputFile::Ordinary("build.rs")], ..Default::default() }; - let mut add_file = |file: &'static str| config.files.push(InputFile::Ordinary(file)); - - add_file("build.rs"); + let assets = HighlightingAssets::new(); let mut output_type = OutputType::from_mode(config.paging_mode, config.pager)?; let writer = output_type.handle()?; diff --git a/examples/simple.rs b/examples/simple.rs new file mode 100644 index 00000000..395706f4 --- /dev/null +++ b/examples/simple.rs @@ -0,0 +1,35 @@ +use bat::{ + assets::HighlightingAssets, + controller::Controller, + inputfile::InputFile, + style::{OutputComponent, OutputComponents}, + Config, +}; +use console::Term; +use std::process; + +fn main() { + let files = std::env::args().skip(1).collect::>(); + + if files.is_empty() { + eprintln!("No input files specified"); + process::exit(1); + } + + let config = Config { + term_width: Term::stdout().size().1 as usize, + colored_output: true, + true_color: true, + output_components: OutputComponents::new(&[ + OutputComponent::Header, + OutputComponent::Grid, + OutputComponent::Numbers, + ]), + files: files.iter().map(|file| InputFile::Ordinary(file)).collect(), + theme: "1337".into(), + ..Default::default() + }; + let assets = HighlightingAssets::new(); + + Controller::new(&config, &assets).run().expect("no errors"); +} diff --git a/examples/using_controller.rs b/examples/using_controller.rs deleted file mode 100644 index 6341c5c6..00000000 --- a/examples/using_controller.rs +++ /dev/null @@ -1,32 +0,0 @@ -use bat::{ - assets::HighlightingAssets, - controller::Controller, - inputfile::InputFile, - style::{OutputComponent, OutputComponents}, - Config, -}; -use std::collections::HashSet; - -fn main() { - let assets = HighlightingAssets::new(); - let mut config = Config { - term_width: 100, // must be greater than 13 to enable style=numbers - colored_output: true, - true_color: true, - output_components: OutputComponents(with_header()), - ..Default::default() - }; - let mut add_file = |file: &'static str| config.files.push(InputFile::Ordinary(file)); - - add_file("Cargo.toml"); - - let print = || Controller::new(&config, &assets).run(); - print().expect("no error"); -} - -fn with_header() -> HashSet { - [OutputComponent::Header, OutputComponent::Grid] - .iter() - .cloned() - .collect() -} diff --git a/src/style.rs b/src/style.rs index b0573cd1..e06bba79 100644 --- a/src/style.rs +++ b/src/style.rs @@ -76,6 +76,10 @@ impl FromStr for OutputComponent { pub struct OutputComponents(pub HashSet); impl OutputComponents { + pub fn new(components: &[OutputComponent]) -> OutputComponents { + OutputComponents(components.iter().cloned().collect()) + } + pub fn changes(&self) -> bool { self.0.contains(&OutputComponent::Changes) }