mirror of
https://github.com/sharkdp/bat.git
synced 2025-04-18 16:48:25 +02: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::{
|
use bat::{
|
||||||
config::{Config, InputFile, StyleComponent, StyleComponents},
|
config::{Config, InputFile},
|
||||||
Controller, HighlightingAssets,
|
Controller, HighlightingAssets,
|
||||||
};
|
};
|
||||||
use console::Term;
|
use std::ffi::OsStr;
|
||||||
use std::process;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let files = std::env::args_os().skip(1).collect::<Vec<_>>();
|
let path_to_this_file = OsStr::new(file!());
|
||||||
|
|
||||||
if files.is_empty() {
|
|
||||||
eprintln!("No input files specified");
|
|
||||||
process::exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
let config = Config {
|
let config = Config {
|
||||||
term_width: Term::stdout().size().1 as usize,
|
files: vec![InputFile::Ordinary(path_to_this_file)],
|
||||||
colored_output: true,
|
colored_output: true,
|
||||||
true_color: 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()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let assets = HighlightingAssets::from_binary();
|
let assets = HighlightingAssets::from_binary();
|
||||||
|
@ -160,12 +160,14 @@ impl HighlightingAssets {
|
|||||||
match self.theme_set.themes.get(theme) {
|
match self.theme_set.themes.get(theme) {
|
||||||
Some(theme) => theme,
|
Some(theme) => theme,
|
||||||
None => {
|
None => {
|
||||||
use ansi_term::Colour::Yellow;
|
if theme != "" {
|
||||||
eprintln!(
|
use ansi_term::Colour::Yellow;
|
||||||
"{}: Unknown theme '{}', using default.",
|
eprintln!(
|
||||||
Yellow.paint("[bat warning]"),
|
"{}: Unknown theme '{}', using default.",
|
||||||
theme
|
Yellow.paint("[bat warning]"),
|
||||||
);
|
theme
|
||||||
|
);
|
||||||
|
}
|
||||||
&self.theme_set.themes[self.fallback_theme.unwrap_or(Self::default_theme())]
|
&self.theme_set.themes[self.fallback_theme.unwrap_or(Self::default_theme())]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user