Create `OutputComponents` struct, use HashSet

This commit is contained in:
Pit Kleyersburg 2018-05-09 22:31:10 +02:00 committed by David Peter
parent 389edd7239
commit 61109ece15
2 changed files with 36 additions and 42 deletions

View File

@ -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<OutputComponent>);
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,

View File

@ -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<Cow<'s, str>> {
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<Cow<'s, str>> {
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<Cow<'s, str>> {
if self.options
.output_components
.contains(&OutputComponent::Grid)
{
if self.options.output_components.grid() {
Some(self.colors.grid.paint("").to_string().into())
} else {
None