@@ -14,7 +14,6 @@ extern crate ansi_term;
1414extern crate atty;
1515extern crate console;
1616extern crate directories;
17−extern crate either;
1817extern crate git2;
1918extern crate syntect;
2019
@@ -37,7 +36,6 @@ use ansi_term::Style;
3736use atty::Stream;
3837use clap::{App, AppSettings, Arg, ArgGroup, SubCommand};
3938use directories::ProjectDirs;
40−use either::Either;
4139use git2::{DiffOptions, IntoCString, Repository};
4240
4341use syntect::dumps::{dump_to_file, from_binary, from_reader};
@@ -54,6 +52,7 @@ lazy_static! {
5452mod errors {
5553 error_chain! {
5654 foreign_links {
55+ Clap(::clap::Error);
5756 Io(::std::io::Error);
5857 }
5958
@@ -72,55 +71,45 @@ mod errors {
7271
7372use errors::*;
7473
75−#[derive(Debug, Eq, PartialEq)]
74+#[derive(Debug, Eq, PartialEq, Copy, Clone)]
7675pub enum OutputComponent {
7776 Changes,
7877 Grid,
7978 Header,
8079 Numbers,
81−}
82−
83−impl FromStr for OutputComponent {
84− type Err = Error;
85−
86− fn from_str(s: &str) -> Result<Self> {
87− match s {
88− "changes" => Ok(OutputComponent::Changes),
89− "grid" => Ok(OutputComponent::Grid),
90− "header" => Ok(OutputComponent::Header),
91− "numbers" => Ok(OutputComponent::Numbers),
92− _ => Err(ErrorKind::UnknownStyleName(s.to_owned()).into()),
93− }
94− }
95−}
96−
97−#[derive(Debug, Eq, PartialEq)]
98−enum PredefinedStyle {
9980 Full,
10081 Plain,
10182}
10283
103−impl PredefinedStyle {
84+impl OutputComponent {
10485 fn components(&self) -> &'static [OutputComponent] {
10586 match *self {
106− PredefinedStyle::Full => &[
87+ OutputComponent::Changes => &[OutputComponent::Changes],
88+ OutputComponent::Grid => &[OutputComponent::Grid],
89+ OutputComponent::Header => &[OutputComponent::Header],
90+ OutputComponent::Numbers => &[OutputComponent::Numbers],
91+ OutputComponent::Full => &[
10792 OutputComponent::Changes,
10893 OutputComponent::Grid,
10994 OutputComponent::Header,
11095 OutputComponent::Numbers,
11196 ],
112− PredefinedStyle::Plain => &[],
97+ OutputComponent::Plain => &[],
11398 }
11499 }
115100}
116101
117−impl FromStr for PredefinedStyle {
102+impl FromStr for OutputComponent {
118103 type Err = Error;
119104
120105 fn from_str(s: &str) -> Result<Self> {
121106 match s {
122− "full" => Ok(PredefinedStyle::Full),
123− "plain" => Ok(PredefinedStyle::Plain),
107+ "changes" => Ok(OutputComponent::Changes),
108+ "grid" => Ok(OutputComponent::Grid),
109+ "header" => Ok(OutputComponent::Header),
110+ "numbers" => Ok(OutputComponent::Numbers),
111+ "full" => Ok(OutputComponent::Full),
112+ "plain" => Ok(OutputComponent::Plain),
124113 _ => Err(ErrorKind::UnknownStyleName(s.to_owned()).into()),
125114 }
126115 }
@@ -609,63 +598,17 @@ fn run() -> Result<()> {
609598 })
610599 .unwrap_or_else(|| vec![None]); // read from stdin (None) if no args are given
611600
612− // Split the user-supplied parameter on commas (','), then go over each part and map it
613− // to either a predefined style or a simple output style. Once this mapping is done,
614− // split it up into two lists: one for the matched predefined styles and one for the
615− // simple output styles.
616− let (_predefined_styles, _output_components): (Vec<_>, Vec<_>) = app_matches
617− .values_of("style")
618− .unwrap()
619− .map(|s| {
620− let predefined_style = PredefinedStyle::from_str(s);
621− if let Ok(style) = predefined_style {
622− Some(Either::Left(style))
623− } else {
624− match OutputComponent::from_str(s) {
625− Ok(style) => Some(Either::Right(style)),
626− Err(error) => {
627− eprintln!("{}: {}", Red.paint("[bat error]"), error);
628− None
629− }
630− }
631− }
632− })
633− .filter_map(|s| s)
634− .partition(|style| match *style {
635− Either::Left(_) => true,
636− Either::Right(_) => false,
637− });
638−
639− // Once we have the two lists of predefined styles and simple output styles, we take the
640− // first matched predefined style and use that as our style, discarding every other
641− // predefined or simple output style. If we have no predefined styles though, we take
642− // all matched simple output styles and pass those to the `Options` struct.
643− let output_components: Vec<OutputComponent>;
644− let output_components = if let Some(predefined_style) = _predefined_styles
601+ let output_components = values_t!(app_matches.values_of("style"), OutputComponent)?
645602 .into_iter()
646− .map(|e| e.left().unwrap())
647− .next()
648− {
649− predefined_style.components()
650− } else {
651− output_components = _output_components
652− .into_iter()
653− .map(|e| e.right().unwrap())
654− .collect::<Vec<_>>();
655−
656− // If we are in this else-block, `output_components` is empty and the requested style
657− // wasn't the empty string, the user supplied only wrong style arguments. In this
658− // case we inform them about this.
659− if output_components.is_empty() && app_matches.value_of("style").unwrap() != "" {
660− return Err(ErrorKind::NoCorrectStylesSpecified.into());
661− }
662−
663− &output_components
664− };
603+ .map(|style| style.components())
604+ .fold(vec![], |mut acc, x| {
605+ acc.extend_from_slice(x);
606+ acc
607+ });
665608
666609 let options = Options {
667610 true_color: is_truecolor_terminal(),
668− output_components,
611+ output_components: &output_components,
669612 language: app_matches.value_of("language"),
670613 colored_output: match app_matches.value_of("color") {
671614 Some("always") => true,