summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2020-04-22 20:35:05 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2020-04-22 23:55:28 +0200
commit36dde9275af3f513b77395a92a0023f7e6134fa6 (patch)
treec78b084b3aafdc9b5748e16acd097c623a6efef9
parenta8f759c0809fab3839c01e83440700740601f246 (diff)
Simplify style_components
-rw-r--r--examples/cat.rs6
-rw-r--r--src/pretty_printer.rs18
2 files changed, 16 insertions, 8 deletions
diff --git a/examples/cat.rs b/examples/cat.rs
index 5111a086..ac653697 100644
--- a/examples/cat.rs
+++ b/examples/cat.rs
@@ -1,16 +1,16 @@
/// A very simple colorized `cat` clone, using `bat` as a library.
/// See `src/bin/bat` for the full `bat` application.
-use bat::{PrettyPrinter, StyleComponent, StyleComponents};
+use bat::{PrettyPrinter, StyleComponent};
use console::Term;
fn main() {
PrettyPrinter::new()
.term_width(Term::stdout().size().1 as usize)
- .style_components(StyleComponents::new(&[
+ .style_components(&[
StyleComponent::Header,
StyleComponent::Grid,
StyleComponent::Numbers,
- ]))
+ ])
.input_files(std::env::args_os().skip(1))
.print()
.expect("no errors");
diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs
index 91d5be54..1dc621ea 100644
--- a/src/pretty_printer.rs
+++ b/src/pretty_printer.rs
@@ -1,6 +1,8 @@
use std::ffi::OsStr;
use std::io::Read;
+use console::Term;
+
use crate::{
assets::HighlightingAssets,
config::Config,
@@ -8,7 +10,7 @@ use crate::{
errors::Result,
input::Input,
line_range::{HighlightedLineRanges, LineRanges},
- LineRange, StyleComponents, SyntaxMapping, WrappingMode,
+ LineRange, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
};
#[cfg(feature = "paging")]
@@ -20,6 +22,7 @@ pub struct PrettyPrinter<'a> {
assets: HighlightingAssets,
highlighted_lines: Vec<LineRange>,
+ term_width: Option<usize>,
}
impl<'a> PrettyPrinter<'a> {
@@ -33,7 +36,9 @@ impl<'a> PrettyPrinter<'a> {
inputs: vec![],
config,
assets: HighlightingAssets::from_binary(),
+
highlighted_lines: vec![],
+ term_width: None,
}
}
@@ -78,9 +83,9 @@ impl<'a> PrettyPrinter<'a> {
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 {
- self.config.term_width = width;
+ self.term_width = Some(width);
self
}
@@ -103,8 +108,8 @@ impl<'a> PrettyPrinter<'a> {
}
/// Configure style elements like grid or line numbers (default: "full" style)
- pub fn style_components(&mut self, components: StyleComponents) -> &mut Self {
- self.config.style_components = components;
+ pub fn style_components(&mut self, components: &[StyleComponent]) -> &mut Self {
+ self.config.style_components = StyleComponents::new(components);
self
}
@@ -166,6 +171,9 @@ impl<'a> PrettyPrinter<'a> {
pub fn print(&mut self) -> Result<bool> {
self.config.highlighted_lines =
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![];
std::mem::swap(&mut inputs, &mut self.inputs);