summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorFahmi Akbar Wildana <f.a.wildana@gmail.com>2019-10-15 08:31:02 +0700
committerDavid Peter <sharkdp@users.noreply.github.com>2019-10-20 21:43:51 +0200
commit1e194047360fa023a220edad3d6823be27291f0a (patch)
tree6eff60d4e6c0c7ece8e066cc5b320dc6da5a66a5 /examples
parent2253d07341936c0671dee999f03779db72a1c712 (diff)
Add 2 examples
Diffstat (limited to 'examples')
-rw-r--r--examples/using_controller.rs33
-rw-r--r--examples/using_printer.rs84
2 files changed, 117 insertions, 0 deletions
diff --git a/examples/using_controller.rs b/examples/using_controller.rs
new file mode 100644
index 00000000..d49477e2
--- /dev/null
+++ b/examples/using_controller.rs
@@ -0,0 +1,33 @@
+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");
+ // add_file("build.rs");
+
+ 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/examples/using_printer.rs b/examples/using_printer.rs
new file mode 100644
index 00000000..e8936c97
--- /dev/null
+++ b/examples/using_printer.rs
@@ -0,0 +1,84 @@
+use bat::{
+ self,
+ assets::HighlightingAssets,
+ inputfile::InputFile,
+ line_range::{LineRange, LineRanges, RangeCheckResult},
+ output::OutputType,
+ printer::{InteractivePrinter, Printer},
+ style::{OutputComponent, OutputComponents},
+ Config,
+};
+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
+ colored_output: true,
+ true_color: true,
+ line_ranges: LineRanges::from(vec![
+ LineRange::from("5:7")?,
+ LineRange::from("92:97")?,
+ LineRange::from("15:17")?,
+ ]),
+ output_components: OutputComponents(with_full_decorations()),
+ ..Default::default()
+ };
+ let mut add_file = |file: &'static str| config.files.push(InputFile::Ordinary(file));
+
+ // add_file("Cargo.toml");
+ add_file("build.rs");
+
+ let mut output_type = OutputType::from_mode(config.paging_mode, config.pager)?;
+ let writer = output_type.handle()?;
+ let stdin = io::stdin();
+
+ for input_file in &config.files {
+ let mut reader = input_file.get_reader(&stdin)?;
+ let mut printer = InteractivePrinter::new(&config, &assets, *input_file, &mut reader);
+
+ printer.print_header(writer, *input_file)?;
+
+ if !reader.first_line.is_empty() {
+ let mut line_buffer = Vec::new();
+ let mut line_number = 1;
+ let mut has_snip = true; // skip first line snip
+
+ while reader.read_line(&mut line_buffer)? {
+ match config.line_ranges.check(line_number) {
+ RangeCheckResult::OutsideRange => {
+ if !has_snip {
+ printer.print_snip(writer)?;
+ has_snip = true;
+ }
+ }
+ RangeCheckResult::InRange => {
+ printer.print_line(
+ /*out_of_range = */ false,
+ writer,
+ line_number,
+ &line_buffer,
+ )?;
+ has_snip = false;
+ }
+ RangeCheckResult::AfterLastRange => break,
+ }
+
+ line_number += 1;
+ line_buffer.clear();
+ }
+ }
+
+ printer.print_footer(writer)?;
+ }
+
+ Ok(())
+}
+
+fn with_full_decorations() -> HashSet<OutputComponent> {
+ OutputComponent::Full
+ .components(Default::default())
+ .iter()
+ .cloned()
+ .collect()
+}