1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-01-18 20:11:03 +00:00

Simplify style_components

This commit is contained in:
sharkdp 2020-04-22 20:35:05 +02:00 committed by David Peter
parent a8f759c080
commit 36dde9275a
2 changed files with 16 additions and 8 deletions

View File

@ -1,16 +1,16 @@
/// A very simple colorized `cat` clone, using `bat` as a library. /// A very simple colorized `cat` clone, using `bat` as a library.
/// See `src/bin/bat` for the full `bat` application. /// See `src/bin/bat` for the full `bat` application.
use bat::{PrettyPrinter, StyleComponent, StyleComponents}; use bat::{PrettyPrinter, StyleComponent};
use console::Term; use console::Term;
fn main() { fn main() {
PrettyPrinter::new() PrettyPrinter::new()
.term_width(Term::stdout().size().1 as usize) .term_width(Term::stdout().size().1 as usize)
.style_components(StyleComponents::new(&[ .style_components(&[
StyleComponent::Header, StyleComponent::Header,
StyleComponent::Grid, StyleComponent::Grid,
StyleComponent::Numbers, StyleComponent::Numbers,
])) ])
.input_files(std::env::args_os().skip(1)) .input_files(std::env::args_os().skip(1))
.print() .print()
.expect("no errors"); .expect("no errors");

View File

@ -1,6 +1,8 @@
use std::ffi::OsStr; use std::ffi::OsStr;
use std::io::Read; use std::io::Read;
use console::Term;
use crate::{ use crate::{
assets::HighlightingAssets, assets::HighlightingAssets,
config::Config, config::Config,
@ -8,7 +10,7 @@ use crate::{
errors::Result, errors::Result,
input::Input, input::Input,
line_range::{HighlightedLineRanges, LineRanges}, line_range::{HighlightedLineRanges, LineRanges},
LineRange, StyleComponents, SyntaxMapping, WrappingMode, LineRange, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
}; };
#[cfg(feature = "paging")] #[cfg(feature = "paging")]
@ -20,6 +22,7 @@ pub struct PrettyPrinter<'a> {
assets: HighlightingAssets, assets: HighlightingAssets,
highlighted_lines: Vec<LineRange>, highlighted_lines: Vec<LineRange>,
term_width: Option<usize>,
} }
impl<'a> PrettyPrinter<'a> { impl<'a> PrettyPrinter<'a> {
@ -33,7 +36,9 @@ impl<'a> PrettyPrinter<'a> {
inputs: vec![], inputs: vec![],
config, config,
assets: HighlightingAssets::from_binary(), assets: HighlightingAssets::from_binary(),
highlighted_lines: vec![], highlighted_lines: vec![],
term_width: None,
} }
} }
@ -78,9 +83,9 @@ impl<'a> PrettyPrinter<'a> {
self self
} }
/// The character width of the terminal (default: unlimited) /// The character width of the terminal (default: autodetect)
pub fn term_width(&mut self, width: usize) -> &mut Self { pub fn term_width(&mut self, width: usize) -> &mut Self {
self.config.term_width = width; self.term_width = Some(width);
self self
} }
@ -103,8 +108,8 @@ impl<'a> PrettyPrinter<'a> {
} }
/// Configure style elements like grid or line numbers (default: "full" style) /// Configure style elements like grid or line numbers (default: "full" style)
pub fn style_components(&mut self, components: StyleComponents) -> &mut Self { pub fn style_components(&mut self, components: &[StyleComponent]) -> &mut Self {
self.config.style_components = components; self.config.style_components = StyleComponents::new(components);
self self
} }
@ -166,6 +171,9 @@ impl<'a> PrettyPrinter<'a> {
pub fn print(&mut self) -> Result<bool> { pub fn print(&mut self) -> Result<bool> {
self.config.highlighted_lines = self.config.highlighted_lines =
HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone())); HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone()));
self.config.term_width = self
.term_width
.unwrap_or_else(|| Term::stdout().size().1 as usize);
let mut inputs: Vec<Input> = vec![]; let mut inputs: Vec<Input> = vec![];
std::mem::swap(&mut inputs, &mut self.inputs); std::mem::swap(&mut inputs, &mut self.inputs);