diff --git a/examples/cat.rs b/examples/cat.rs new file mode 100644 index 00000000..99bf1add --- /dev/null +++ b/examples/cat.rs @@ -0,0 +1,34 @@ +/// A very simple colorized `cat` clone, using `bat` as a library. +/// See `src/bin/bat` for the full `bat` application. + +use bat::{ + config::{Config, InputFile, StyleComponent, StyleComponents}, + Controller, HighlightingAssets, +}; +use console::Term; +use std::process; + +fn main() { + let files = std::env::args_os().skip(1).collect::>(); + + 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, + style_components: StyleComponents::new(&[ + StyleComponent::Header, + StyleComponent::Grid, + StyleComponent::Numbers, + ]), + files: files.iter().map(|file| InputFile::Ordinary(file)).collect(), + ..Default::default() + }; + let assets = HighlightingAssets::from_binary(); + + Controller::new(&config, &assets).run().expect("no errors"); +} diff --git a/examples/simple.rs b/examples/simple.rs index 3e582eae..5fba7547 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,29 +1,18 @@ +/// A simple program that prints its own source code using the bat library + use bat::{ - config::{Config, InputFile, StyleComponent, StyleComponents}, + config::{Config, InputFile}, Controller, HighlightingAssets, }; -use console::Term; -use std::process; +use std::ffi::OsStr; fn main() { - let files = std::env::args_os().skip(1).collect::>(); - - if files.is_empty() { - eprintln!("No input files specified"); - process::exit(1); - } + let path_to_this_file = OsStr::new(file!()); let config = Config { - term_width: Term::stdout().size().1 as usize, + files: vec![InputFile::Ordinary(path_to_this_file)], colored_output: true, true_color: true, - style_components: StyleComponents::new(&[ - StyleComponent::Header, - StyleComponent::Grid, - StyleComponent::Numbers, - ]), - files: files.iter().map(|file| InputFile::Ordinary(file)).collect(), - theme: "1337".into(), ..Default::default() }; let assets = HighlightingAssets::from_binary(); diff --git a/src/assets.rs b/src/assets.rs index ee4d1da9..79305660 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -160,12 +160,14 @@ impl HighlightingAssets { match self.theme_set.themes.get(theme) { Some(theme) => theme, None => { - use ansi_term::Colour::Yellow; - eprintln!( - "{}: Unknown theme '{}', using default.", - Yellow.paint("[bat warning]"), - theme - ); + if theme != "" { + use ansi_term::Colour::Yellow; + eprintln!( + "{}: Unknown theme '{}', using default.", + Yellow.paint("[bat warning]"), + theme + ); + } &self.theme_set.themes[self.fallback_theme.unwrap_or(Self::default_theme())] } }