mirror of
https://github.com/sharkdp/bat.git
synced 2024-12-26 00:08:54 +01:00
Add an even simple example
This commit is contained in:
parent
2c2861db6a
commit
dfe5eb3e98
34
examples/cat.rs
Normal file
34
examples/cat.rs
Normal 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");
|
||||
}
|
@ -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();
|
||||
|
@ -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())]
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user