on master help sign in
Simplify 'using_controller' example
b9ce3c2sharkdp
examples/using_printer.rs → examples/advanced.rsrenamed+6−6
@@ -8,25 +8,25 @@ use bat::{
8 style::{OutputComponent, OutputComponents},
8 style::{OutputComponent, OutputComponents},
9 Config,
9 Config,
10};
10};
11+use console::Term;
11use std::{collections::HashSet, io};
12use std::{collections::HashSet, io};
12
13
13fn main() -> bat::errors::Result<()> {
14fn main() -> bat::errors::Result<()> {
14− let assets = HighlightingAssets::new();
15− let mut config = Config {
15+ let config = Config {
16− term_width: 14, // must be greater than 13 to enable style=numbers
16+ term_width: Term::stdout().size().1 as usize,
17 colored_output: true,
17 colored_output: true,
18 true_color: true,
18 true_color: true,
19+ theme: "1337".into(),
19 line_ranges: LineRanges::from(vec![
20 line_ranges: LineRanges::from(vec![
20 LineRange::from("5:7")?,
21 LineRange::from("5:7")?,
21 LineRange::from("92:97")?,
22 LineRange::from("92:97")?,
22 LineRange::from("15:17")?,
23 LineRange::from("15:17")?,
23 ]),
24 ]),
24 output_components: OutputComponents(with_full_decorations()),
25 output_components: OutputComponents(with_full_decorations()),
26+ files: vec![InputFile::Ordinary("build.rs")],
25 ..Default::default()
27 ..Default::default()
26 };
28 };
27− let mut add_file = |file: &'static str| config.files.push(InputFile::Ordinary(file));
29+ let assets = HighlightingAssets::new();
28−
29− add_file("build.rs");
30
30
31 let mut output_type = OutputType::from_mode(config.paging_mode, config.pager)?;
31 let mut output_type = OutputType::from_mode(config.paging_mode, config.pager)?;
32 let writer = output_type.handle()?;
32 let writer = output_type.handle()?;
examples/simple.rsnew file+35
@@ -0,0 +1,35 @@
1+use bat::{
2+ assets::HighlightingAssets,
3+ controller::Controller,
4+ inputfile::InputFile,
5+ style::{OutputComponent, OutputComponents},
6+ Config,
7+};
8+use console::Term;
9+use std::process;
10+
11+fn main() {
12+ let files = std::env::args().skip(1).collect::<Vec<_>>();
13+
14+ if files.is_empty() {
15+ eprintln!("No input files specified");
16+ process::exit(1);
17+ }
18+
19+ let config = Config {
20+ term_width: Term::stdout().size().1 as usize,
21+ colored_output: true,
22+ true_color: true,
23+ output_components: OutputComponents::new(&[
24+ OutputComponent::Header,
25+ OutputComponent::Grid,
26+ OutputComponent::Numbers,
27+ ]),
28+ files: files.iter().map(|file| InputFile::Ordinary(file)).collect(),
29+ theme: "1337".into(),
30+ ..Default::default()
31+ };
32+ let assets = HighlightingAssets::new();
33+
34+ Controller::new(&config, &assets).run().expect("no errors");
35+}
examples/using_controller.rsdeleted−32
@@ -1,32 +0,0 @@
1−use bat::{
2− assets::HighlightingAssets,
3− controller::Controller,
4− inputfile::InputFile,
5− style::{OutputComponent, OutputComponents},
6− Config,
7−};
8−use std::collections::HashSet;
9−
10−fn main() {
11− let assets = HighlightingAssets::new();
12− let mut config = Config {
13− term_width: 100, // must be greater than 13 to enable style=numbers
14− colored_output: true,
15− true_color: true,
16− output_components: OutputComponents(with_header()),
17− ..Default::default()
18− };
19− let mut add_file = |file: &'static str| config.files.push(InputFile::Ordinary(file));
20−
21− add_file("Cargo.toml");
22−
23− let print = || Controller::new(&config, &assets).run();
24− print().expect("no error");
25−}
26−
27−fn with_header() -> HashSet<OutputComponent> {
28− [OutputComponent::Header, OutputComponent::Grid]
29− .iter()
30− .cloned()
31− .collect()
32−}
src/style.rs+4
@@ -76,6 +76,10 @@ impl FromStr for OutputComponent {
76pub struct OutputComponents(pub HashSet<OutputComponent>);
76pub struct OutputComponents(pub HashSet<OutputComponent>);
77
77
78impl OutputComponents {
78impl OutputComponents {
79+ pub fn new(components: &[OutputComponent]) -> OutputComponents {
80+ OutputComponents(components.iter().cloned().collect())
81+ }
82+
79 pub fn changes(&self) -> bool {
83 pub fn changes(&self) -> bool {
80 self.0.contains(&OutputComponent::Changes)
84 self.0.contains(&OutputComponent::Changes)
81 }
85 }