mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-18 20:11:03 +00:00
Simplify 'using_controller' example
This commit is contained in:
parent
310654d49f
commit
b9ce3c248c
@ -8,25 +8,25 @@ use bat::{
|
|||||||
style::{OutputComponent, OutputComponents},
|
style::{OutputComponent, OutputComponents},
|
||||||
Config,
|
Config,
|
||||||
};
|
};
|
||||||
|
use console::Term;
|
||||||
use std::{collections::HashSet, io};
|
use std::{collections::HashSet, io};
|
||||||
|
|
||||||
fn main() -> bat::errors::Result<()> {
|
fn main() -> bat::errors::Result<()> {
|
||||||
let assets = HighlightingAssets::new();
|
let config = Config {
|
||||||
let mut config = Config {
|
term_width: Term::stdout().size().1 as usize,
|
||||||
term_width: 14, // must be greater than 13 to enable style=numbers
|
|
||||||
colored_output: true,
|
colored_output: true,
|
||||||
true_color: true,
|
true_color: true,
|
||||||
|
theme: "1337".into(),
|
||||||
line_ranges: LineRanges::from(vec![
|
line_ranges: LineRanges::from(vec![
|
||||||
LineRange::from("5:7")?,
|
LineRange::from("5:7")?,
|
||||||
LineRange::from("92:97")?,
|
LineRange::from("92:97")?,
|
||||||
LineRange::from("15:17")?,
|
LineRange::from("15:17")?,
|
||||||
]),
|
]),
|
||||||
output_components: OutputComponents(with_full_decorations()),
|
output_components: OutputComponents(with_full_decorations()),
|
||||||
|
files: vec![InputFile::Ordinary("build.rs")],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let mut add_file = |file: &'static str| config.files.push(InputFile::Ordinary(file));
|
let assets = HighlightingAssets::new();
|
||||||
|
|
||||||
add_file("build.rs");
|
|
||||||
|
|
||||||
let mut output_type = OutputType::from_mode(config.paging_mode, config.pager)?;
|
let mut output_type = OutputType::from_mode(config.paging_mode, config.pager)?;
|
||||||
let writer = output_type.handle()?;
|
let writer = output_type.handle()?;
|
35
examples/simple.rs
Normal file
35
examples/simple.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use bat::{
|
||||||
|
assets::HighlightingAssets,
|
||||||
|
controller::Controller,
|
||||||
|
inputfile::InputFile,
|
||||||
|
style::{OutputComponent, OutputComponents},
|
||||||
|
Config,
|
||||||
|
};
|
||||||
|
use console::Term;
|
||||||
|
use std::process;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let files = std::env::args().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,
|
||||||
|
output_components: OutputComponents::new(&[
|
||||||
|
OutputComponent::Header,
|
||||||
|
OutputComponent::Grid,
|
||||||
|
OutputComponent::Numbers,
|
||||||
|
]),
|
||||||
|
files: files.iter().map(|file| InputFile::Ordinary(file)).collect(),
|
||||||
|
theme: "1337".into(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let assets = HighlightingAssets::new();
|
||||||
|
|
||||||
|
Controller::new(&config, &assets).run().expect("no errors");
|
||||||
|
}
|
@ -1,32 +0,0 @@
|
|||||||
use bat::{
|
|
||||||
assets::HighlightingAssets,
|
|
||||||
controller::Controller,
|
|
||||||
inputfile::InputFile,
|
|
||||||
style::{OutputComponent, OutputComponents},
|
|
||||||
Config,
|
|
||||||
};
|
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let assets = HighlightingAssets::new();
|
|
||||||
let mut config = Config {
|
|
||||||
term_width: 100, // must be greater than 13 to enable style=numbers
|
|
||||||
colored_output: true,
|
|
||||||
true_color: true,
|
|
||||||
output_components: OutputComponents(with_header()),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let mut add_file = |file: &'static str| config.files.push(InputFile::Ordinary(file));
|
|
||||||
|
|
||||||
add_file("Cargo.toml");
|
|
||||||
|
|
||||||
let print = || Controller::new(&config, &assets).run();
|
|
||||||
print().expect("no error");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_header() -> HashSet<OutputComponent> {
|
|
||||||
[OutputComponent::Header, OutputComponent::Grid]
|
|
||||||
.iter()
|
|
||||||
.cloned()
|
|
||||||
.collect()
|
|
||||||
}
|
|
@ -76,6 +76,10 @@ impl FromStr for OutputComponent {
|
|||||||
pub struct OutputComponents(pub HashSet<OutputComponent>);
|
pub struct OutputComponents(pub HashSet<OutputComponent>);
|
||||||
|
|
||||||
impl OutputComponents {
|
impl OutputComponents {
|
||||||
|
pub fn new(components: &[OutputComponent]) -> OutputComponents {
|
||||||
|
OutputComponents(components.iter().cloned().collect())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn changes(&self) -> bool {
|
pub fn changes(&self) -> bool {
|
||||||
self.0.contains(&OutputComponent::Changes)
|
self.0.contains(&OutputComponent::Changes)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user