@@ -1,6 +1,8 @@
11use std::ffi::OsStr;
22use std::io::Read;
33
4+use console::Term;
5+
46use crate::{
57 assets::HighlightingAssets,
68 config::Config,
@@ -8,7 +10,7 @@ use crate::{
810 errors::Result,
911 input::Input,
1012 line_range::{HighlightedLineRanges, LineRanges},
11− LineRange, StyleComponents, SyntaxMapping, WrappingMode,
13+ LineRange, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
1214};
1315
1416#[cfg(feature = "paging")]
@@ -20,6 +22,7 @@ pub struct PrettyPrinter<'a> {
2022 assets: HighlightingAssets,
2123
2224 highlighted_lines: Vec<LineRange>,
25+ term_width: Option<usize>,
2326}
2427
2528impl<'a> PrettyPrinter<'a> {
@@ -33,7 +36,9 @@ impl<'a> PrettyPrinter<'a> {
3336 inputs: vec![],
3437 config,
3538 assets: HighlightingAssets::from_binary(),
39+
3640 highlighted_lines: vec![],
41+ term_width: None,
3742 }
3843 }
3944
@@ -78,9 +83,9 @@ impl<'a> PrettyPrinter<'a> {
7883 self
7984 }
8085
81− /// The character width of the terminal (default: unlimited)
86+ /// The character width of the terminal (default: autodetect)
8287 pub fn term_width(&mut self, width: usize) -> &mut Self {
83− self.config.term_width = width;
88+ self.term_width = Some(width);
8489 self
8590 }
8691
@@ -103,8 +108,8 @@ impl<'a> PrettyPrinter<'a> {
103108 }
104109
105110 /// Configure style elements like grid or line numbers (default: "full" style)
106− pub fn style_components(&mut self, components: StyleComponents) -> &mut Self {
107− self.config.style_components = components;
111+ pub fn style_components(&mut self, components: &[StyleComponent]) -> &mut Self {
112+ self.config.style_components = StyleComponents::new(components);
108113 self
109114 }
110115
@@ -166,6 +171,9 @@ impl<'a> PrettyPrinter<'a> {
166171 pub fn print(&mut self) -> Result<bool> {
167172 self.config.highlighted_lines =
168173 HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone()));
174+ self.config.term_width = self
175+ .term_width
176+ .unwrap_or_else(|| Term::stdout().size().1 as usize);
169177
170178 let mut inputs: Vec<Input> = vec![];
171179 std::mem::swap(&mut inputs, &mut self.inputs);