on master help sign in
Rename output component to 'style compinent', move wrap to separate module
commit84ba323b1c3c2fec36ea3999e8675379dc64f50d authorsharkdp <davidpeter@web.de> dateSat 21 Mar 2020, 20:54 (7 years ago) parenta8851e1
9 files changed+88−86
examples/simple.rs+5−5 src/bin/bat/app.rs+15−14 src/bin/bat/main.rs+3−3 src/config.rs+3−2 src/controller.rs+2−2 src/lib.rs+1 src/printer.rs+11−11 src/style.rs+37−49 src/wrap.rs+11
examples/simple.rs+5−5
@@ -3,7 +3,7 @@ use bat::{
33 config::Config,
44 controller::Controller,
55 inputfile::InputFile,
6− style::{OutputComponent, OutputComponents},
6+ style::{StyleComponent, StyleComponents},
77};
88use console::Term;
99use std::process;
@@ -20,10 +20,10 @@ fn main() {
2020 term_width: Term::stdout().size().1 as usize,
2121 colored_output: true,
2222 true_color: true,
23− output_components: OutputComponents::new(&[
24− OutputComponent::Header,
25− OutputComponent::Grid,
26− OutputComponent::Numbers,
23+ style_components: StyleComponents::new(&[
24+ StyleComponent::Header,
25+ StyleComponent::Grid,
26+ StyleComponent::Numbers,
2727 ]),
2828 files: files.iter().map(|file| InputFile::Ordinary(file)).collect(),
2929 theme: "1337".into(),
src/bin/bat/app.rs+15−14
@@ -22,8 +22,9 @@ use bat::{
2222 errors::*,
2323 inputfile::InputFile,
2424 line_range::{HighlightedLineRanges, LineRange, LineRanges},
25− style::{OutputComponent, OutputComponents, OutputWrap},
25+ style::{StyleComponent, StyleComponents},
2626 syntax_mapping::SyntaxMapping,
27+ wrap::OutputWrap,
2728};
2829
2930fn is_truecolor_terminal() -> bool {
@@ -79,7 +80,7 @@ impl App {
7980
8081 pub fn config(&self) -> Result<Config> {
8182 let files = self.files();
82− let output_components = self.output_components()?;
83+ let style_components = self.style_components()?;
8384
8485 let paging_mode = match self.matches.value_of("paging") {
8586 Some("always") => PagingMode::Always,
@@ -152,7 +153,7 @@ impl App {
152153 Some("character") => OutputWrap::Character,
153154 Some("never") => OutputWrap::None,
154155 Some("auto") | _ => {
155− if output_components.plain() {
156+ if style_components.plain() {
156157 OutputWrap::None
157158 } else {
158159 OutputWrap::Character
@@ -182,7 +183,7 @@ impl App {
182183 .or_else(|| env::var("BAT_TABS").ok())
183184 .and_then(|t| t.parse().ok())
184185 .unwrap_or(
185− if output_components.plain() && paging_mode == PagingMode::Never {
186+ if style_components.plain() && paging_mode == PagingMode::Never {
186187 0
187188 } else {
188189 4
@@ -208,7 +209,7 @@ impl App {
208209 .transpose()?
209210 .map(LineRanges::from)
210211 .unwrap_or_default(),
211− output_components,
212+ style_components,
212213 syntax_mapping,
213214 pager: self.matches.value_of("pager"),
214215 use_italic_text: match self.matches.value_of("italic-text") {
@@ -243,23 +244,23 @@ impl App {
243244 .unwrap_or_else(|| vec![InputFile::StdIn])
244245 }
245246
246− fn output_components(&self) -> Result<OutputComponents> {
247+ fn style_components(&self) -> Result<StyleComponents> {
247248 let matches = &self.matches;
248− Ok(OutputComponents(
249+ Ok(StyleComponents(
249250 if matches.value_of("decorations") == Some("never") {
250251 HashSet::new()
251252 } else if matches.is_present("number") {
252− [OutputComponent::Numbers].iter().cloned().collect()
253+ [StyleComponent::Numbers].iter().cloned().collect()
253254 } else if matches.is_present("plain") {
254− [OutputComponent::Plain].iter().cloned().collect()
255+ [StyleComponent::Plain].iter().cloned().collect()
255256 } else {
256− let env_style_components: Option<Vec<OutputComponent>> = env::var("BAT_STYLE")
257+ let env_style_components: Option<Vec<StyleComponent>> = env::var("BAT_STYLE")
257258 .ok()
258259 .map(|style_str| {
259260 style_str
260261 .split(',')
261− .map(|x| OutputComponent::from_str(&x))
262− .collect::<Result<Vec<OutputComponent>>>()
262+ .map(|x| StyleComponent::from_str(&x))
263+ .collect::<Result<Vec<StyleComponent>>>()
263264 })
264265 .transpose()?;
265266
@@ -268,12 +269,12 @@ impl App {
268269 .map(|styles| {
269270 styles
270271 .split(',')
271− .map(|style| style.parse::<OutputComponent>())
272+ .map(|style| style.parse::<StyleComponent>())
272273 .filter_map(|style| style.ok())
273274 .collect::<Vec<_>>()
274275 })
275276 .or(env_style_components)
276− .unwrap_or_else(|| vec![OutputComponent::Full])
277+ .unwrap_or_else(|| vec![StyleComponent::Full])
277278 .into_iter()
278279 .map(|style| style.components(self.interactive_output))
279280 .fold(HashSet::new(), |mut acc, components| {
src/bin/bat/main.rs+3−3
@@ -32,7 +32,7 @@ use bat::{
3232 config::Config,
3333 errors::*,
3434 inputfile::InputFile,
35− style::{OutputComponent, OutputComponents},
35+ style::{StyleComponent, StyleComponents},
3636};
3737
3838fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
@@ -123,9 +123,9 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
123123 let assets = assets_from_cache_or_binary();
124124 let mut config = cfg.clone();
125125 let mut style = HashSet::new();
126− style.insert(OutputComponent::Plain);
126+ style.insert(StyleComponent::Plain);
127127 config.files = vec![InputFile::ThemePreviewFile];
128− config.output_components = OutputComponents(style);
128+ config.style_components = StyleComponents(style);
129129
130130 let stdout = io::stdout();
131131 let mut stdout = stdout.lock();
src/config.rs+3−2
@@ -1,7 +1,8 @@
11use crate::inputfile::InputFile;
22use crate::line_range::{HighlightedLineRanges, LineRanges};
3−use crate::style::{OutputComponents, OutputWrap};
3+use crate::style::StyleComponents;
44use crate::syntax_mapping::SyntaxMapping;
5+use crate::wrap::OutputWrap;
56
67#[derive(Debug, Clone, Copy, PartialEq)]
78pub enum PagingMode {
@@ -44,7 +45,7 @@ pub struct Config<'a> {
4445 pub true_color: bool,
4546
4647 /// Style elements (grid, line numbers, ...)
47− pub output_components: OutputComponents,
48+ pub style_components: StyleComponents,
4849
4950 /// Text wrapping mode
5051 pub output_wrap: OutputWrap,
src/controller.rs+2−2
@@ -83,7 +83,7 @@ impl<'b> Controller<'b> {
8383 writer: &mut dyn Write,
8484 input_file: InputFile<'a>,
8585 ) -> Result<()> {
86− if !reader.first_line.is_empty() || self.config.output_components.header() {
86+ if !reader.first_line.is_empty() || self.config.style_components.header() {
8787 printer.print_header(writer, input_file)?;
8888 }
8989
@@ -118,7 +118,7 @@ impl<'b> Controller<'b> {
118118 }
119119
120120 RangeCheckResult::InRange => {
121− if self.config.output_components.snip() {
121+ if self.config.style_components.snip() {
122122 if first_range {
123123 first_range = false;
124124 mid_range = true;
src/lib.rs+1
@@ -27,3 +27,4 @@ mod printer;
2727pub mod style;
2828pub mod syntax_mapping;
2929mod terminal;
30+pub mod wrap;
src/printer.rs+11−11
@@ -30,8 +30,8 @@ use crate::errors::*;
3030use crate::inputfile::{InputFile, InputFileReader};
3131use crate::line_range::RangeCheckResult;
3232use crate::preprocessor::{expand_tabs, replace_nonprintable};
33−use crate::style::OutputWrap;
3433use crate::terminal::{as_terminal_escaped, to_ansi_color};
34+use crate::wrap::OutputWrap;
3535
3636pub trait Printer {
3737 fn print_header(&mut self, handle: &mut dyn Write, file: InputFile) -> Result<()>;
@@ -116,11 +116,11 @@ impl<'a> InteractivePrinter<'a> {
116116 // Create decorations.
117117 let mut decorations: Vec<Box<dyn Decoration>> = Vec::new();
118118
119− if config.output_components.numbers() {
119+ if config.style_components.numbers() {
120120 decorations.push(Box::new(LineNumberDecoration::new(&colors)));
121121 }
122122
123− if config.output_components.changes() {
123+ if config.style_components.changes() {
124124 decorations.push(Box::new(LineChangesDecoration::new(&colors)));
125125 }
126126
@@ -130,7 +130,7 @@ impl<'a> InteractivePrinter<'a> {
130130 // The grid border decoration isn't added until after the panel_width calculation, since the
131131 // print_horizontal_line, print_header, and print_footer functions all assume the panel
132132 // width is without the grid border.
133− if config.output_components.grid() && !decorations.is_empty() {
133+ if config.style_components.grid() && !decorations.is_empty() {
134134 decorations.push(Box::new(GridBorderDecoration::new(&colors)));
135135 }
136136
@@ -152,7 +152,7 @@ impl<'a> InteractivePrinter<'a> {
152152 None
153153 } else {
154154 // Get the Git modifications
155− line_changes = if config.output_components.changes() {
155+ line_changes = if config.style_components.changes() {
156156 match file {
157157 InputFile::Ordinary(filename) => get_git_diff(filename),
158158 _ => None,
@@ -206,7 +206,7 @@ impl<'a> InteractivePrinter<'a> {
206206 text_truncated,
207207 " ".repeat(self.panel_width - 1 - text_truncated.len())
208208 );
209− if self.config.output_components.grid() {
209+ if self.config.style_components.grid() {
210210 format!("{} │ ", text_filled)
211211 } else {
212212 format!("{}", text_filled)
@@ -225,7 +225,7 @@ impl<'a> InteractivePrinter<'a> {
225225
226226impl<'a> Printer for InteractivePrinter<'a> {
227227 fn print_header(&mut self, handle: &mut dyn Write, file: InputFile) -> Result<()> {
228− if !self.config.output_components.header() {
228+ if !self.config.style_components.header() {
229229 if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
230230 let input = match file {
231231 InputFile::Ordinary(filename) => {
@@ -243,14 +243,14 @@ impl<'a> Printer for InteractivePrinter<'a> {
243243 input
244244 )?;
245245 } else {
246− if self.config.output_components.grid() {
246+ if self.config.style_components.grid() {
247247 self.print_horizontal_line(handle, '┬')?;
248248 }
249249 }
250250 return Ok(());
251251 }
252252
253− if self.config.output_components.grid() {
253+ if self.config.style_components.grid() {
254254 self.print_horizontal_line(handle, '┬')?;
255255
256256 write!(
@@ -286,7 +286,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
286286 mode
287287 )?;
288288
289− if self.config.output_components.grid() {
289+ if self.config.style_components.grid() {
290290 if self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable {
291291 self.print_horizontal_line(handle, '┼')?;
292292 } else {
@@ -298,7 +298,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
298298 }
299299
300300 fn print_footer(&mut self, handle: &mut dyn Write) -> Result<()> {
301− if self.config.output_components.grid()
301+ if self.config.style_components.grid()
302302 && (self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable)
303303 {
304304 self.print_horizontal_line(handle, '┴')
src/style.rs+37−49
@@ -4,7 +4,7 @@ use std::str::FromStr;
44use crate::errors::*;
55
66#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
7−pub enum OutputComponent {
7+pub enum StyleComponent {
88 Auto,
99 Changes,
1010 Grid,
@@ -15,92 +15,80 @@ pub enum OutputComponent {
1515 Plain,
1616}
1717
18−#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
19−pub enum OutputWrap {
20− Character,
21− None,
22−}
23−
24−impl Default for OutputWrap {
25− fn default() -> Self {
26− OutputWrap::None
27− }
28−}
29−
30−impl OutputComponent {
31− pub fn components(self, interactive_terminal: bool) -> &'static [OutputComponent] {
18+impl StyleComponent {
19+ pub fn components(self, interactive_terminal: bool) -> &'static [StyleComponent] {
3220 match self {
33− OutputComponent::Auto => {
21+ StyleComponent::Auto => {
3422 if interactive_terminal {
35− OutputComponent::Full.components(interactive_terminal)
23+ StyleComponent::Full.components(interactive_terminal)
3624 } else {
37− OutputComponent::Plain.components(interactive_terminal)
25+ StyleComponent::Plain.components(interactive_terminal)
3826 }
3927 }
40− OutputComponent::Changes => &[OutputComponent::Changes],
41− OutputComponent::Grid => &[OutputComponent::Grid],
42− OutputComponent::Header => &[OutputComponent::Header],
43− OutputComponent::Numbers => &[OutputComponent::Numbers],
44− OutputComponent::Snip => &[OutputComponent::Snip],
45− OutputComponent::Full => &[
46− OutputComponent::Changes,
47− OutputComponent::Grid,
48− OutputComponent::Header,
49− OutputComponent::Numbers,
50− OutputComponent::Snip,
28+ StyleComponent::Changes => &[StyleComponent::Changes],
29+ StyleComponent::Grid => &[StyleComponent::Grid],
30+ StyleComponent::Header => &[StyleComponent::Header],
31+ StyleComponent::Numbers => &[StyleComponent::Numbers],
32+ StyleComponent::Snip => &[StyleComponent::Snip],
33+ StyleComponent::Full => &[
34+ StyleComponent::Changes,
35+ StyleComponent::Grid,
36+ StyleComponent::Header,
37+ StyleComponent::Numbers,
38+ StyleComponent::Snip,
5139 ],
52− OutputComponent::Plain => &[],
40+ StyleComponent::Plain => &[],
5341 }
5442 }
5543}
5644
57−impl FromStr for OutputComponent {
45+impl FromStr for StyleComponent {
5846 type Err = Error;
5947
6048 fn from_str(s: &str) -> Result<Self> {
6149 match s {
62− "auto" => Ok(OutputComponent::Auto),
63− "changes" => Ok(OutputComponent::Changes),
64− "grid" => Ok(OutputComponent::Grid),
65− "header" => Ok(OutputComponent::Header),
66− "numbers" => Ok(OutputComponent::Numbers),
67− "snip" => Ok(OutputComponent::Snip),
68− "full" => Ok(OutputComponent::Full),
69− "plain" => Ok(OutputComponent::Plain),
50+ "auto" => Ok(StyleComponent::Auto),
51+ "changes" => Ok(StyleComponent::Changes),
52+ "grid" => Ok(StyleComponent::Grid),
53+ "header" => Ok(StyleComponent::Header),
54+ "numbers" => Ok(StyleComponent::Numbers),
55+ "snip" => Ok(StyleComponent::Snip),
56+ "full" => Ok(StyleComponent::Full),
57+ "plain" => Ok(StyleComponent::Plain),
7058 _ => Err(format!("Unknown style '{}'", s).into()),
7159 }
7260 }
7361}
7462
7563#[derive(Debug, Clone, Default)]
76−pub struct OutputComponents(pub HashSet<OutputComponent>);
64+pub struct StyleComponents(pub HashSet<StyleComponent>);
7765
78−impl OutputComponents {
79− pub fn new(components: &[OutputComponent]) -> OutputComponents {
80− OutputComponents(components.iter().cloned().collect())
66+impl StyleComponents {
67+ pub fn new(components: &[StyleComponent]) -> StyleComponents {
68+ StyleComponents(components.iter().cloned().collect())
8169 }
8270
8371 pub fn changes(&self) -> bool {
84− self.0.contains(&OutputComponent::Changes)
72+ self.0.contains(&StyleComponent::Changes)
8573 }
8674
8775 pub fn grid(&self) -> bool {
88− self.0.contains(&OutputComponent::Grid)
76+ self.0.contains(&StyleComponent::Grid)
8977 }
9078
9179 pub fn header(&self) -> bool {
92− self.0.contains(&OutputComponent::Header)
80+ self.0.contains(&StyleComponent::Header)
9381 }
9482
9583 pub fn numbers(&self) -> bool {
96− self.0.contains(&OutputComponent::Numbers)
84+ self.0.contains(&StyleComponent::Numbers)
9785 }
9886
9987 pub fn snip(&self) -> bool {
100− self.0.contains(&OutputComponent::Snip)
88+ self.0.contains(&StyleComponent::Snip)
10189 }
10290
10391 pub fn plain(&self) -> bool {
104− self.0.iter().all(|c| c == &OutputComponent::Plain)
92+ self.0.iter().all(|c| c == &StyleComponent::Plain)
10593 }
10694}
src/wrap.rsnew file+11
@@ -0,0 +1,11 @@
1+#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
2+pub enum OutputWrap {
3+ Character,
4+ None,
5+}
6+
7+impl Default for OutputWrap {
8+ fn default() -> Self {
9+ OutputWrap::None
10+ }
11+}