on master help sign in
Easier configuration of style components
commit6a124591df487d0872a0d4963907a69f77fe68ee authorsharkdp <davidpeter@web.de> dateWed 22 Apr 2020, 21:16 (7 years ago) parent7a87315
4 files changed+67−9
src/bin/bat/app.rs+2−1 src/bin/bat/main.rs+6−2 src/lib.rs+1−2 src/pretty_printer.rs+58−4
src/bin/bat/app.rs+2−1
@@ -19,7 +19,8 @@ use bat::{
1919 errors::*,
2020 input::Input,
2121 line_range::{HighlightedLineRanges, LineRange, LineRanges},
22− MappingTarget, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
22+ style::{StyleComponent, StyleComponents},
23+ MappingTarget, SyntaxMapping, WrappingMode,
2324};
2425
2526fn is_truecolor_terminal() -> bool {
src/bin/bat/main.rs+6−2
@@ -26,8 +26,12 @@ use clap::crate_version;
2626use directories::PROJECT_DIRS;
2727
2828use bat::{
29− assets::HighlightingAssets, config::Config, controller::Controller, errors::*, input::Input,
30− StyleComponent, StyleComponents,
29+ assets::HighlightingAssets,
30+ config::Config,
31+ controller::Controller,
32+ errors::*,
33+ input::Input,
34+ style::{StyleComponent, StyleComponents},
3135};
3236
3337fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
src/lib.rs+1−2
@@ -32,13 +32,12 @@ mod output;
3232mod preprocessor;
3333mod pretty_printer;
3434pub(crate) mod printer;
35−pub(crate) mod style;
35+pub mod style;
3636pub(crate) mod syntax_mapping;
3737mod terminal;
3838pub(crate) mod wrapping;
3939
4040pub use line_range::LineRange;
4141pub use pretty_printer::PrettyPrinter;
42−pub use style::{StyleComponent, StyleComponents};
4342pub use syntax_mapping::{MappingTarget, SyntaxMapping};
4443pub use wrapping::WrappingMode;
src/pretty_printer.rs+58−4
@@ -10,12 +10,22 @@ use crate::{
1010 errors::Result,
1111 input::Input,
1212 line_range::{HighlightedLineRanges, LineRanges},
13− LineRange, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
13+ style::{StyleComponent, StyleComponents},
14+ LineRange, SyntaxMapping, WrappingMode,
1415};
1516
1617#[cfg(feature = "paging")]
1718use crate::config::PagingMode;
1819
20+#[derive(Default)]
21+struct ActiveStyleComponents {
22+ header: bool,
23+ vcs_modification_markers: bool,
24+ grid: bool,
25+ line_numbers: bool,
26+ snip: bool,
27+}
28+
1929pub struct PrettyPrinter<'a> {
2030 inputs: Vec<Input<'a>>,
2131 config: Config<'a>,
@@ -23,6 +33,7 @@ pub struct PrettyPrinter<'a> {
2333
2434 highlighted_lines: Vec<LineRange>,
2535 term_width: Option<usize>,
36+ active_style_components: ActiveStyleComponents,
2637}
2738
2839impl<'a> PrettyPrinter<'a> {
@@ -39,6 +50,7 @@ impl<'a> PrettyPrinter<'a> {
3950
4051 highlighted_lines: vec![],
4152 term_width: None,
53+ active_style_components: ActiveStyleComponents::default(),
4254 }
4355 }
4456
@@ -107,9 +119,33 @@ impl<'a> PrettyPrinter<'a> {
107119 self
108120 }
109121
110− /// Configure style elements like grid or line numbers (default: "full" style)
111− pub fn style_components(&mut self, components: &[StyleComponent]) -> &mut Self {
112− self.config.style_components = StyleComponents::new(components);
122+ /// Whether to show a header with the file name
123+ pub fn header(&mut self, yes: bool) -> &mut Self {
124+ self.active_style_components.header = yes;
125+ self
126+ }
127+
128+ /// Whether to show line numbers
129+ pub fn line_numbers(&mut self, yes: bool) -> &mut Self {
130+ self.active_style_components.line_numbers = yes;
131+ self
132+ }
133+
134+ /// Whether to paint a grid, separating line numbers, git changes and the code
135+ pub fn grid(&mut self, yes: bool) -> &mut Self {
136+ self.active_style_components.grid = yes;
137+ self
138+ }
139+
140+ /// Whether to show modification markers for VCS changes
141+ pub fn vcs_modification_markers(&mut self, yes: bool) -> &mut Self {
142+ self.active_style_components.vcs_modification_markers = yes;
143+ self
144+ }
145+
146+ /// Whether to show "snip" markers between visible line ranges (default: no)
147+ pub fn snip(&mut self, yes: bool) -> &mut Self {
148+ self.active_style_components.snip = yes;
113149 self
114150 }
115151
@@ -175,6 +211,24 @@ impl<'a> PrettyPrinter<'a> {
175211 .term_width
176212 .unwrap_or_else(|| Term::stdout().size().1 as usize);
177213
214+ let mut style_components = vec![];
215+ if self.active_style_components.grid {
216+ style_components.push(StyleComponent::Grid);
217+ }
218+ if self.active_style_components.header {
219+ style_components.push(StyleComponent::Header);
220+ }
221+ if self.active_style_components.line_numbers {
222+ style_components.push(StyleComponent::LineNumbers);
223+ }
224+ if self.active_style_components.snip {
225+ style_components.push(StyleComponent::Snip);
226+ }
227+ if self.active_style_components.vcs_modification_markers {
228+ style_components.push(StyleComponent::Changes);
229+ }
230+ self.config.style_components = StyleComponents::new(&style_components);
231+
178232 let mut inputs: Vec<Input> = vec![];
179233 std::mem::swap(&mut inputs, &mut self.inputs);
180234