summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2019-10-20 21:53:34 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2019-10-30 19:47:51 +0100
commitb9ce3c248c2fb18e427677ff122e1447120af3f3 (patch)
treef74dd563b4200336ea5adde509196b8aa87413bf
parent310654d49f1cff9fcb30084539aed2b01cf157c5 (diff)
Simplify 'using_controller' example
-rw-r--r--examples/advanced.rs (renamed from examples/using_printer.rs)12
-rw-r--r--examples/simple.rs35
-rw-r--r--examples/using_controller.rs32
-rw-r--r--src/style.rs4
4 files changed, 45 insertions, 38 deletions
diff --git a/examples/using_printer.rs b/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::<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");
+}
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> {
- [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<OutputComponent>);
impl OutputComponents {
+ pub fn new(components: &[OutputComponent]) -> OutputComponents {
+ OutputComponents(components.iter().cloned().collect())
+ }
+
pub fn changes(&self) -> bool {
self.0.contains(&OutputComponent::Changes)
}