From 61109ece15946f11aecf5508440f101aa1818f4c Mon Sep 17 00:00:00 2001 From: Pit Kleyersburg Date: Wed, 9 May 2018 22:31:10 +0200 Subject: [PATCH] Create `OutputComponents` struct, use HashSet --- src/main.rs | 32 ++++++++++++++++++++++++++------ src/printer.rs | 46 ++++++++++------------------------------------ 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7e4124cb..b3982d13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ extern crate syntect; mod printer; mod terminal; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::env; use std::fs::{self, File}; use std::io::{self, BufRead, BufReader, Write}; @@ -71,7 +71,7 @@ mod errors { use errors::*; -#[derive(Debug, Eq, PartialEq, Copy, Clone)] +#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] pub enum OutputComponent { Changes, Grid, @@ -115,9 +115,29 @@ impl FromStr for OutputComponent { } } +pub struct OutputComponents(HashSet); + +impl OutputComponents { + fn changes(&self) -> bool { + self.0.contains(&OutputComponent::Changes) + } + + fn grid(&self) -> bool { + self.0.contains(&OutputComponent::Grid) + } + + fn header(&self) -> bool { + self.0.contains(&OutputComponent::Header) + } + + fn numbers(&self) -> bool { + self.0.contains(&OutputComponent::Numbers) + } +} + pub struct Options<'a> { pub true_color: bool, - pub output_components: &'a [OutputComponent], + pub output_components: OutputComponents, pub language: Option<&'a str>, pub colored_output: bool, pub paging: bool, @@ -601,14 +621,14 @@ fn run() -> Result<()> { let output_components = values_t!(app_matches.values_of("style"), OutputComponent)? .into_iter() .map(|style| style.components()) - .fold(vec![], |mut acc, x| { - acc.extend_from_slice(x); + .fold(HashSet::new(), |mut acc, components| { + acc.extend(components.iter().cloned()); acc }); let options = Options { true_color: is_truecolor_terminal(), - output_components: &output_components, + output_components: OutputComponents(output_components), language: app_matches.value_of("language"), colored_output: match app_matches.value_of("color") { Some("always") => true, diff --git a/src/printer.rs b/src/printer.rs index c6181ed4..c7fe63c3 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -4,7 +4,7 @@ use std::borrow::Cow; use std::io::Write; use syntect::highlighting; use terminal::as_terminal_escaped; -use {Colors, LineChange, LineChanges, Options, OutputComponent}; +use {Colors, LineChange, LineChanges, Options}; const PANEL_WIDTH: usize = 7; @@ -32,17 +32,11 @@ impl<'a> Printer<'a> { } pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> { - if !self.options - .output_components - .contains(&OutputComponent::Header) - { + if !self.options.output_components.header() { return Ok(()); } - if self.options - .output_components - .contains(&OutputComponent::Grid) - { + if self.options.output_components.grid() { self.print_horizontal_line('┬')?; write!( @@ -70,10 +64,7 @@ impl<'a> Printer<'a> { } pub fn print_footer(&mut self) -> Result<()> { - if self.options - .output_components - .contains(&OutputComponent::Grid) - { + if self.options.output_components.grid() { self.print_horizontal_line('┴') } else { Ok(()) @@ -98,9 +89,7 @@ impl<'a> Printer<'a> { ), ]; - let grid_requested = self.options - .output_components - .contains(&OutputComponent::Grid); + let grid_requested = self.options.output_components.grid(); write!( self.handle, "{}", @@ -119,10 +108,7 @@ impl<'a> Printer<'a> { } fn print_line_number<'s>(&self, line_number: usize) -> Option> { - if self.options - .output_components - .contains(&OutputComponent::Numbers) - { + if self.options.output_components.numbers() { Some( self.colors .line_number @@ -130,10 +116,7 @@ impl<'a> Printer<'a> { .to_string() .into(), ) - } else if self.options - .output_components - .contains(&OutputComponent::Grid) - { + } else if self.options.output_components.grid() { Some(" ".into()) } else { None @@ -141,10 +124,7 @@ impl<'a> Printer<'a> { } fn print_git_marker<'s>(&self, line_number: usize) -> Option> { - if self.options - .output_components - .contains(&OutputComponent::Changes) - { + if self.options.output_components.changes() { Some( if let Some(ref changes) = self.line_changes { match changes.get(&(line_number as u32)) { @@ -159,10 +139,7 @@ impl<'a> Printer<'a> { }.to_string() .into(), ) - } else if self.options - .output_components - .contains(&OutputComponent::Grid) - { + } else if self.options.output_components.grid() { Some(" ".into()) } else { None @@ -170,10 +147,7 @@ impl<'a> Printer<'a> { } fn print_line_border<'s>(&self) -> Option> { - if self.options - .output_components - .contains(&OutputComponent::Grid) - { + if self.options.output_components.grid() { Some(self.colors.grid.paint("│").to_string().into()) } else { None