on master help sign in
Combine `OutputComponent`s and `PredefinedStyle`s

This commit tries to simply the change. Instead of separating an `OutputComponent` and a `PredefinedStyle`, I have combined the two into just `OutputComponent`. To still have the styles work, I added an impl to `OutputComponent` with a function `components` which returns the components related to the specified component. For a simple output component this is trivial, but for the predefined styles this is a list of other components. The evaluating of the command-line parameter simplified significantly, making use of Claps `values_t!` macro to parse the supplied parameters into a `Vec<OutputComponent>`. After that it is simply a task of combining all supplied output components into one vector. Important: this means that the styles are now additive, rather than one of the predefined styles overruling other parameters supplied.

commit389edd7239304959b50a18b47eda0db1d5a8b4c1 authorPit Kleyersburg <pitkley@googlemail.com> dateWed 9 May 2018, 21:09 (9 years ago) parent23813cc
3 files changed+23−88
Cargo.lock−7 Cargo.toml−1 src/main.rs+23−80
Cargo.lock−7
@@ -68,7 +68,6 @@ dependencies = [
6868 "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
6969 "console 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
7070 "directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
71− "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
7271 "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
7372 "git2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
7473 "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -195,11 +194,6 @@ dependencies = [
195194 "shared_child 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
196195]
197196
198−[[package]]
199−name = "either"
200−version = "1.5.0"
201−source = "registry+https://github.com/rust-lang/crates.io-index"
202−
203197[[package]]
204198name = "error-chain"
205199version = "0.11.0"
@@ -826,7 +820,6 @@ dependencies = [
826820"checksum directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc2561db021b6f1321d0f16b67ed28ce843ef4610dfaa432e3ffa2e8a3050ebf"
827821"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
828822"checksum duct 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "166298c17c5b4fe5997b962c2f22e887c7c5adc44308eb9103ce5b66af45a423"
829−"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
830823"checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3"
831824"checksum flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fac2277e84e5e858483756647a9d0aa8d9a2b7cba517fd84325a0aaa69a0909"
832825"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
Cargo.toml−1
@@ -13,7 +13,6 @@ version = "0.3.0"
1313atty = "0.2.2"
1414ansi_term = "0.10"
1515console = "0.6"
16−either = "1.4"
1716error-chain = "0.11"
1817directories = "0.10"
1918lazy_static = "1.0"
src/main.rs+23−80
@@ -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,