Simplify 'using_controller' example

This commit is contained in:
sharkdp 2019-10-20 21:53:34 +02:00 committed by David Peter
parent 310654d49f
commit b9ce3c248c
4 changed files with 45 additions and 38 deletions

View File

@ -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()?;

35
examples/simple.rs Normal file
View File

@ -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::<Vec<_>>();
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");
}

View File

@ -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> {
[OutputComponent::Header, OutputComponent::Grid]
.iter()
.cloned()
.collect()
}

View File

@ -76,6 +76,10 @@ impl FromStr for OutputComponent {
pub struct OutputComponents(pub HashSet<OutputComponent>);
impl OutputComponents {
pub fn new(components: &[OutputComponent]) -> OutputComponents {
OutputComponents(components.iter().cloned().collect())
}
pub fn changes(&self) -> bool {
self.0.contains(&OutputComponent::Changes)
}