on master help sign in
Add an even simple example
commitdfe5eb3e989e87db04a3af900d2c49c49308bc0b authorsharkdp <davidpeter@web.de> dateSat 21 Mar 2020, 21:45 (7 years ago) parent2c2861d
3 files changed+48−23
examples/cat.rs+34 examples/simple.rs+6−17 src/assets.rs+8−6
examples/cat.rsnew file+34
@@ -0,0 +1,34 @@
1+/// A very simple colorized `cat` clone, using `bat` as a library.
2+/// See `src/bin/bat` for the full `bat` application.
3+
4+use bat::{
5+ config::{Config, InputFile, StyleComponent, StyleComponents},
6+ Controller, HighlightingAssets,
7+};
8+use console::Term;
9+use std::process;
10+
11+fn main() {
12+ let files = std::env::args_os().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+ style_components: StyleComponents::new(&[
24+ StyleComponent::Header,
25+ StyleComponent::Grid,
26+ StyleComponent::Numbers,
27+ ]),
28+ files: files.iter().map(|file| InputFile::Ordinary(file)).collect(),
29+ ..Default::default()
30+ };
31+ let assets = HighlightingAssets::from_binary();
32+
33+ Controller::new(&config, &assets).run().expect("no errors");
34+}
examples/simple.rs+6−17
@@ -1,29 +1,18 @@
1+/// A simple program that prints its own source code using the bat library
2+
13use bat::{
2− config::{Config, InputFile, StyleComponent, StyleComponents},
4+ config::{Config, InputFile},
35 Controller, HighlightingAssets,
46};
5−use console::Term;
6−use std::process;
7+use std::ffi::OsStr;
78
89fn main() {
9− let files = std::env::args_os().skip(1).collect::<Vec<_>>();
10−
11− if files.is_empty() {
12− eprintln!("No input files specified");
13− process::exit(1);
14− }
10+ let path_to_this_file = OsStr::new(file!());
1511
1612 let config = Config {
17− term_width: Term::stdout().size().1 as usize,
13+ files: vec![InputFile::Ordinary(path_to_this_file)],
1814 colored_output: true,
1915 true_color: true,
20− style_components: StyleComponents::new(&[
21− StyleComponent::Header,
22− StyleComponent::Grid,
23− StyleComponent::Numbers,
24− ]),
25− files: files.iter().map(|file| InputFile::Ordinary(file)).collect(),
26− theme: "1337".into(),
2716 ..Default::default()
2817 };
2918 let assets = HighlightingAssets::from_binary();
src/assets.rs+8−6
@@ -160,12 +160,14 @@ impl HighlightingAssets {
160160 match self.theme_set.themes.get(theme) {
161161 Some(theme) => theme,
162162 None => {
163− use ansi_term::Colour::Yellow;
164− eprintln!(
165− "{}: Unknown theme '{}', using default.",
166− Yellow.paint("[bat warning]"),
167− theme
168− );
163+ if theme != "" {
164+ use ansi_term::Colour::Yellow;
165+ eprintln!(
166+ "{}: Unknown theme '{}', using default.",
167+ Yellow.paint("[bat warning]"),
168+ theme
169+ );
170+ }
169171 &self.theme_set.themes[self.fallback_theme.unwrap_or(Self::default_theme())]
170172 }
171173 }