on master help sign in
Add an even simple example
dfe5eb3sharkdp
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+
1use bat::{
3use bat::{
2− config::{Config, InputFile, StyleComponent, StyleComponents},
4+ config::{Config, InputFile},
3 Controller, HighlightingAssets,
5 Controller, HighlightingAssets,
4};
6};
5−use console::Term;
7+use std::ffi::OsStr;
6−use std::process;
7
8
8fn main() {
9fn main() {
9− let files = std::env::args_os().skip(1).collect::<Vec<_>>();
10+ let path_to_this_file = OsStr::new(file!());
10−
11− if files.is_empty() {
12− eprintln!("No input files specified");
13− process::exit(1);
14− }
15
11
16 let config = Config {
12 let config = Config {
17− term_width: Term::stdout().size().1 as usize,
13+ files: vec![InputFile::Ordinary(path_to_this_file)],
18 colored_output: true,
14 colored_output: true,
19 true_color: true,
15 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(),
27 ..Default::default()
16 ..Default::default()
28 };
17 };
29 let assets = HighlightingAssets::from_binary();
18 let assets = HighlightingAssets::from_binary();
src/assets.rs+8−6
@@ -160,12 +160,14 @@ impl HighlightingAssets {
160 match self.theme_set.themes.get(theme) {
160 match self.theme_set.themes.get(theme) {
161 Some(theme) => theme,
161 Some(theme) => theme,
162 None => {
162 None => {
163+ if theme != "" {
163− use ansi_term::Colour::Yellow;
164+ use ansi_term::Colour::Yellow;
164− eprintln!(
165+ eprintln!(
165− "{}: Unknown theme '{}', using default.",
166+ "{}: Unknown theme '{}', using default.",
166− Yellow.paint("[bat warning]"),
167+ Yellow.paint("[bat warning]"),
167− theme
168+ theme
168− );
169+ );
170+ }
169 &self.theme_set.themes[self.fallback_theme.unwrap_or(Self::default_theme())]
171 &self.theme_set.themes[self.fallback_theme.unwrap_or(Self::default_theme())]
170 }
172 }
171 }
173 }