mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-18 10:41:05 +01:00
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.
This commit is contained in:
parent
23813cc08b
commit
389edd7239
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -68,7 +68,6 @@ dependencies = [
|
|||||||
"clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"console 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"console 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"git2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"git2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
@ -195,11 +194,6 @@ dependencies = [
|
|||||||
"shared_child 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"shared_child 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "either"
|
|
||||||
version = "1.5.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "error-chain"
|
name = "error-chain"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
@ -826,7 +820,6 @@ dependencies = [
|
|||||||
"checksum directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc2561db021b6f1321d0f16b67ed28ce843ef4610dfaa432e3ffa2e8a3050ebf"
|
"checksum directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc2561db021b6f1321d0f16b67ed28ce843ef4610dfaa432e3ffa2e8a3050ebf"
|
||||||
"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
|
"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
|
||||||
"checksum duct 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "166298c17c5b4fe5997b962c2f22e887c7c5adc44308eb9103ce5b66af45a423"
|
"checksum duct 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "166298c17c5b4fe5997b962c2f22e887c7c5adc44308eb9103ce5b66af45a423"
|
||||||
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
|
|
||||||
"checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3"
|
"checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3"
|
||||||
"checksum flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fac2277e84e5e858483756647a9d0aa8d9a2b7cba517fd84325a0aaa69a0909"
|
"checksum flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fac2277e84e5e858483756647a9d0aa8d9a2b7cba517fd84325a0aaa69a0909"
|
||||||
"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
|
"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
|
||||||
|
@ -13,7 +13,6 @@ version = "0.3.0"
|
|||||||
atty = "0.2.2"
|
atty = "0.2.2"
|
||||||
ansi_term = "0.10"
|
ansi_term = "0.10"
|
||||||
console = "0.6"
|
console = "0.6"
|
||||||
either = "1.4"
|
|
||||||
error-chain = "0.11"
|
error-chain = "0.11"
|
||||||
directories = "0.10"
|
directories = "0.10"
|
||||||
lazy_static = "1.0"
|
lazy_static = "1.0"
|
||||||
|
119
src/main.rs
119
src/main.rs
@ -14,7 +14,6 @@ extern crate ansi_term;
|
|||||||
extern crate atty;
|
extern crate atty;
|
||||||
extern crate console;
|
extern crate console;
|
||||||
extern crate directories;
|
extern crate directories;
|
||||||
extern crate either;
|
|
||||||
extern crate git2;
|
extern crate git2;
|
||||||
extern crate syntect;
|
extern crate syntect;
|
||||||
|
|
||||||
@ -37,7 +36,6 @@ use ansi_term::Style;
|
|||||||
use atty::Stream;
|
use atty::Stream;
|
||||||
use clap::{App, AppSettings, Arg, ArgGroup, SubCommand};
|
use clap::{App, AppSettings, Arg, ArgGroup, SubCommand};
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
use either::Either;
|
|
||||||
use git2::{DiffOptions, IntoCString, Repository};
|
use git2::{DiffOptions, IntoCString, Repository};
|
||||||
|
|
||||||
use syntect::dumps::{dump_to_file, from_binary, from_reader};
|
use syntect::dumps::{dump_to_file, from_binary, from_reader};
|
||||||
@ -54,6 +52,7 @@ lazy_static! {
|
|||||||
mod errors {
|
mod errors {
|
||||||
error_chain! {
|
error_chain! {
|
||||||
foreign_links {
|
foreign_links {
|
||||||
|
Clap(::clap::Error);
|
||||||
Io(::std::io::Error);
|
Io(::std::io::Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,12 +71,32 @@ mod errors {
|
|||||||
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
||||||
pub enum OutputComponent {
|
pub enum OutputComponent {
|
||||||
Changes,
|
Changes,
|
||||||
Grid,
|
Grid,
|
||||||
Header,
|
Header,
|
||||||
Numbers,
|
Numbers,
|
||||||
|
Full,
|
||||||
|
Plain,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OutputComponent {
|
||||||
|
fn components(&self) -> &'static [OutputComponent] {
|
||||||
|
match *self {
|
||||||
|
OutputComponent::Changes => &[OutputComponent::Changes],
|
||||||
|
OutputComponent::Grid => &[OutputComponent::Grid],
|
||||||
|
OutputComponent::Header => &[OutputComponent::Header],
|
||||||
|
OutputComponent::Numbers => &[OutputComponent::Numbers],
|
||||||
|
OutputComponent::Full => &[
|
||||||
|
OutputComponent::Changes,
|
||||||
|
OutputComponent::Grid,
|
||||||
|
OutputComponent::Header,
|
||||||
|
OutputComponent::Numbers,
|
||||||
|
],
|
||||||
|
OutputComponent::Plain => &[],
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for OutputComponent {
|
impl FromStr for OutputComponent {
|
||||||
@ -89,38 +108,8 @@ impl FromStr for OutputComponent {
|
|||||||
"grid" => Ok(OutputComponent::Grid),
|
"grid" => Ok(OutputComponent::Grid),
|
||||||
"header" => Ok(OutputComponent::Header),
|
"header" => Ok(OutputComponent::Header),
|
||||||
"numbers" => Ok(OutputComponent::Numbers),
|
"numbers" => Ok(OutputComponent::Numbers),
|
||||||
_ => Err(ErrorKind::UnknownStyleName(s.to_owned()).into()),
|
"full" => Ok(OutputComponent::Full),
|
||||||
}
|
"plain" => Ok(OutputComponent::Plain),
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
|
||||||
enum PredefinedStyle {
|
|
||||||
Full,
|
|
||||||
Plain,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PredefinedStyle {
|
|
||||||
fn components(&self) -> &'static [OutputComponent] {
|
|
||||||
match *self {
|
|
||||||
PredefinedStyle::Full => &[
|
|
||||||
OutputComponent::Changes,
|
|
||||||
OutputComponent::Grid,
|
|
||||||
OutputComponent::Header,
|
|
||||||
OutputComponent::Numbers,
|
|
||||||
],
|
|
||||||
PredefinedStyle::Plain => &[],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for PredefinedStyle {
|
|
||||||
type Err = Error;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self> {
|
|
||||||
match s {
|
|
||||||
"full" => Ok(PredefinedStyle::Full),
|
|
||||||
"plain" => Ok(PredefinedStyle::Plain),
|
|
||||||
_ => Err(ErrorKind::UnknownStyleName(s.to_owned()).into()),
|
_ => Err(ErrorKind::UnknownStyleName(s.to_owned()).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -609,63 +598,17 @@ fn run() -> Result<()> {
|
|||||||
})
|
})
|
||||||
.unwrap_or_else(|| vec![None]); // read from stdin (None) if no args are given
|
.unwrap_or_else(|| vec![None]); // read from stdin (None) if no args are given
|
||||||
|
|
||||||
// Split the user-supplied parameter on commas (','), then go over each part and map it
|
let output_components = values_t!(app_matches.values_of("style"), OutputComponent)?
|
||||||
// to either a predefined style or a simple output style. Once this mapping is done,
|
|
||||||
// split it up into two lists: one for the matched predefined styles and one for the
|
|
||||||
// simple output styles.
|
|
||||||
let (_predefined_styles, _output_components): (Vec<_>, Vec<_>) = app_matches
|
|
||||||
.values_of("style")
|
|
||||||
.unwrap()
|
|
||||||
.map(|s| {
|
|
||||||
let predefined_style = PredefinedStyle::from_str(s);
|
|
||||||
if let Ok(style) = predefined_style {
|
|
||||||
Some(Either::Left(style))
|
|
||||||
} else {
|
|
||||||
match OutputComponent::from_str(s) {
|
|
||||||
Ok(style) => Some(Either::Right(style)),
|
|
||||||
Err(error) => {
|
|
||||||
eprintln!("{}: {}", Red.paint("[bat error]"), error);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.filter_map(|s| s)
|
|
||||||
.partition(|style| match *style {
|
|
||||||
Either::Left(_) => true,
|
|
||||||
Either::Right(_) => false,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Once we have the two lists of predefined styles and simple output styles, we take the
|
|
||||||
// first matched predefined style and use that as our style, discarding every other
|
|
||||||
// predefined or simple output style. If we have no predefined styles though, we take
|
|
||||||
// all matched simple output styles and pass those to the `Options` struct.
|
|
||||||
let output_components: Vec<OutputComponent>;
|
|
||||||
let output_components = if let Some(predefined_style) = _predefined_styles
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|e| e.left().unwrap())
|
.map(|style| style.components())
|
||||||
.next()
|
.fold(vec![], |mut acc, x| {
|
||||||
{
|
acc.extend_from_slice(x);
|
||||||
predefined_style.components()
|
acc
|
||||||
} else {
|
});
|
||||||
output_components = _output_components
|
|
||||||
.into_iter()
|
|
||||||
.map(|e| e.right().unwrap())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
// If we are in this else-block, `output_components` is empty and the requested style
|
|
||||||
// wasn't the empty string, the user supplied only wrong style arguments. In this
|
|
||||||
// case we inform them about this.
|
|
||||||
if output_components.is_empty() && app_matches.value_of("style").unwrap() != "" {
|
|
||||||
return Err(ErrorKind::NoCorrectStylesSpecified.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
&output_components
|
|
||||||
};
|
|
||||||
|
|
||||||
let options = Options {
|
let options = Options {
|
||||||
true_color: is_truecolor_terminal(),
|
true_color: is_truecolor_terminal(),
|
||||||
output_components,
|
output_components: &output_components,
|
||||||
language: app_matches.value_of("language"),
|
language: app_matches.value_of("language"),
|
||||||
colored_output: match app_matches.value_of("color") {
|
colored_output: match app_matches.value_of("color") {
|
||||||
Some("always") => true,
|
Some("always") => true,
|
||||||
|
Loading…
Reference in New Issue
Block a user