Add an even simple example

This commit is contained in:
sharkdp 2020-03-21 21:45:03 +01:00 committed by David Peter
parent 2c2861db6a
commit dfe5eb3e98
3 changed files with 48 additions and 23 deletions

34
examples/cat.rs Normal file
View File

@ -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::<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,
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");
}

View File

@ -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::<Vec<_>>();
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();

View File

@ -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())]
}
}