on master help sign in
Create `OutputComponents` struct, use HashSet
commit61109ece15946f11aecf5508440f101aa1818f4c authorPit Kleyersburg <pitkley@googlemail.com> dateWed 9 May 2018, 22:31 (9 years ago) parent389edd7
2 files changed+36−42
src/main.rs+26−6 src/printer.rs+10−36
src/main.rs+26−6
@@ -20,7 +20,7 @@ extern crate syntect;
2020mod printer;
2121mod terminal;
2222
23−use std::collections::HashMap;
23+use std::collections::{HashMap, HashSet};
2424use std::env;
2525use std::fs::{self, File};
2626use std::io::{self, BufRead, BufReader, Write};
@@ -71,7 +71,7 @@ mod errors {
7171
7272use errors::*;
7373
74−#[derive(Debug, Eq, PartialEq, Copy, Clone)]
74+#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
7575pub enum OutputComponent {
7676 Changes,
7777 Grid,
@@ -115,9 +115,29 @@ impl FromStr for OutputComponent {
115115 }
116116}
117117
118+pub struct OutputComponents(HashSet<OutputComponent>);
119+
120+impl OutputComponents {
121+ fn changes(&self) -> bool {
122+ self.0.contains(&OutputComponent::Changes)
123+ }
124+
125+ fn grid(&self) -> bool {
126+ self.0.contains(&OutputComponent::Grid)
127+ }
128+
129+ fn header(&self) -> bool {
130+ self.0.contains(&OutputComponent::Header)
131+ }
132+
133+ fn numbers(&self) -> bool {
134+ self.0.contains(&OutputComponent::Numbers)
135+ }
136+}
137+
118138pub struct Options<'a> {
119139 pub true_color: bool,
120− pub output_components: &'a [OutputComponent],
140+ pub output_components: OutputComponents,
121141 pub language: Option<&'a str>,
122142 pub colored_output: bool,
123143 pub paging: bool,
@@ -601,14 +621,14 @@ fn run() -> Result<()> {
601621 let output_components = values_t!(app_matches.values_of("style"), OutputComponent)?
602622 .into_iter()
603623 .map(|style| style.components())
604− .fold(vec![], |mut acc, x| {
605− acc.extend_from_slice(x);
624+ .fold(HashSet::new(), |mut acc, components| {
625+ acc.extend(components.iter().cloned());
606626 acc
607627 });
608628
609629 let options = Options {
610630 true_color: is_truecolor_terminal(),
611− output_components: &output_components,
631+ output_components: OutputComponents(output_components),
612632 language: app_matches.value_of("language"),
613633 colored_output: match app_matches.value_of("color") {
614634 Some("always") => true,
src/printer.rs+10−36
@@ -4,7 +4,7 @@ use std::borrow::Cow;
44use std::io::Write;
55use syntect::highlighting;
66use terminal::as_terminal_escaped;
7−use {Colors, LineChange, LineChanges, Options, OutputComponent};
7+use {Colors, LineChange, LineChanges, Options};
88
99const PANEL_WIDTH: usize = 7;
1010
@@ -32,17 +32,11 @@ impl<'a> Printer<'a> {
3232 }
3333
3434 pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
35− if !self.options
36− .output_components
37− .contains(&OutputComponent::Header)
38− {
35+ if !self.options.output_components.header() {
3936 return Ok(());
4037 }
4138
42− if self.options
43− .output_components
44− .contains(&OutputComponent::Grid)
45− {
39+ if self.options.output_components.grid() {
4640 self.print_horizontal_line('┬')?;
4741
4842 write!(
@@ -70,10 +64,7 @@ impl<'a> Printer<'a> {
7064 }
7165
7266 pub fn print_footer(&mut self) -> Result<()> {
73− if self.options
74− .output_components
75− .contains(&OutputComponent::Grid)
76− {
67+ if self.options.output_components.grid() {
7768 self.print_horizontal_line('┴')
7869 } else {
7970 Ok(())
@@ -98,9 +89,7 @@ impl<'a> Printer<'a> {
9889 ),
9990 ];
10091
101− let grid_requested = self.options
102− .output_components
103− .contains(&OutputComponent::Grid);
92+ let grid_requested = self.options.output_components.grid();
10493 write!(
10594 self.handle,
10695 "{}",
@@ -119,10 +108,7 @@ impl<'a> Printer<'a> {
119108 }
120109
121110 fn print_line_number<'s>(&self, line_number: usize) -> Option<Cow<'s, str>> {
122− if self.options
123− .output_components
124− .contains(&OutputComponent::Numbers)
125− {
111+ if self.options.output_components.numbers() {
126112 Some(
127113 self.colors
128114 .line_number
@@ -130,10 +116,7 @@ impl<'a> Printer<'a> {
130116 .to_string()
131117 .into(),
132118 )
133− } else if self.options
134− .output_components
135− .contains(&OutputComponent::Grid)
136− {
119+ } else if self.options.output_components.grid() {
137120 Some(" ".into())
138121 } else {
139122 None
@@ -141,10 +124,7 @@ impl<'a> Printer<'a> {
141124 }
142125
143126 fn print_git_marker<'s>(&self, line_number: usize) -> Option<Cow<'s, str>> {
144− if self.options
145− .output_components
146− .contains(&OutputComponent::Changes)
147− {
127+ if self.options.output_components.changes() {
148128 Some(
149129 if let Some(ref changes) = self.line_changes {
150130 match changes.get(&(line_number as u32)) {
@@ -159,10 +139,7 @@ impl<'a> Printer<'a> {
159139 }.to_string()
160140 .into(),
161141 )
162− } else if self.options
163− .output_components
164− .contains(&OutputComponent::Grid)
165− {
142+ } else if self.options.output_components.grid() {
166143 Some(" ".into())
167144 } else {
168145 None
@@ -170,10 +147,7 @@ impl<'a> Printer<'a> {
170147 }
171148
172149 fn print_line_border<'s>(&self) -> Option<Cow<'s, str>> {
173− if self.options
174− .output_components
175− .contains(&OutputComponent::Grid)
176− {
150+ if self.options.output_components.grid() {
177151 Some(self.colors.grid.paint("│").to_string().into())
178152 } else {
179153 None