Merge pull request #997 from eth-p/misc-fixes-1

Allow 'bat' application to built without git feature, remove PrettyPrint git support when feature not enabled.
This commit is contained in:
Ethan P 2020-05-15 16:41:13 -07:00 committed by GitHub
commit 9c371ed8a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 60 deletions

View File

@ -13,6 +13,9 @@
## New syntaxes ## New syntaxes
## New themes ## New themes
## `bat` as a library ## `bat` as a library
- `PrettyPrinter::vcs_modification_markers` is no longer available without the `git` feature, see #997 (@eth-p)
## Packaging ## Packaging
# v0.15.1 # v0.15.1

View File

@ -198,22 +198,23 @@ impl App {
} }
}) })
.unwrap_or_else(|| String::from(HighlightingAssets::default_theme())), .unwrap_or_else(|| String::from(HighlightingAssets::default_theme())),
visible_lines: if self.matches.is_present("diff") { visible_lines: match self.matches.is_present("diff") {
VisibleLines::DiffContext( #[cfg(feature = "git")]
true => VisibleLines::DiffContext(
self.matches self.matches
.value_of("diff-context") .value_of("diff-context")
.and_then(|t| t.parse().ok()) .and_then(|t| t.parse().ok())
.unwrap_or(2), .unwrap_or(2),
) ),
} else {
VisibleLines::Ranges( _ => VisibleLines::Ranges(
self.matches self.matches
.values_of("line-range") .values_of("line-range")
.map(|vs| vs.map(LineRange::from).collect()) .map(|vs| vs.map(LineRange::from).collect())
.transpose()? .transpose()?
.map(LineRanges::from) .map(LineRanges::from)
.unwrap_or_default(), .unwrap_or_default(),
) ),
}, },
style_components, style_components,
syntax_mapping, syntax_mapping,

View File

@ -8,7 +8,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
AppSettings::ColorNever AppSettings::ColorNever
}; };
let app = ClapApp::new(crate_name!()) let mut app = ClapApp::new(crate_name!())
.version(crate_version!()) .version(crate_version!())
.global_setting(clap_color_setting) .global_setting(clap_color_setting)
.global_setting(AppSettings::DeriveDisplayOrder) .global_setting(AppSettings::DeriveDisplayOrder)
@ -44,7 +44,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.long_help( .long_help(
"Show non-printable characters like space, tab or newline. \ "Show non-printable characters like space, tab or newline. \
This option can also be used to print binary files. \ This option can also be used to print binary files. \
Use '--tabs' to control the width of the tab-placeholders." Use '--tabs' to control the width of the tab-placeholders.",
), ),
) )
.arg( .arg(
@ -90,7 +90,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
'--highlight-line 40' highlights line 40\n \ '--highlight-line 40' highlights line 40\n \
'--highlight-line 30:40' highlights lines 30 to 40\n \ '--highlight-line 30:40' highlights lines 30 to 40\n \
'--highlight-line :40' highlights lines 1 to 40\n \ '--highlight-line :40' highlights lines 1 to 40\n \
'--highlight-line 40:' highlights lines 40 to the end of the file" '--highlight-line 40:' highlights lines 40 to the end of the file",
), ),
) )
.arg( .arg(
@ -101,59 +101,67 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.multiple(true) .multiple(true)
.value_name("name") .value_name("name")
.help("Specify the name to display for a file.") .help("Specify the name to display for a file.")
.long_help("Specify the name to display for a file. Useful when piping \ .long_help(
"Specify the name to display for a file. Useful when piping \
data to bat from STDIN when bat does not otherwise know \ data to bat from STDIN when bat does not otherwise know \
the filename."), the filename.",
) ),
.arg( );
Arg::with_name("diff")
.long("diff") #[cfg(feature = "git")]
.short("d") {
.help("Only show lines that have been added/removed/modified.") app = app
.long_help( .arg(
"Only show lines that have been added/removed/modified with respect \ Arg::with_name("diff")
.long("diff")
.short("d")
.help("Only show lines that have been added/removed/modified.")
.long_help(
"Only show lines that have been added/removed/modified with respect \
to the Git index. Use --diff-context=N to control how much context you want to see.", to the Git index. Use --diff-context=N to control how much context you want to see.",
), ),
)
.arg(
Arg::with_name("diff-context")
.long("diff-context")
.overrides_with("diff-context")
.takes_value(true)
.value_name("N")
.validator(
|n| {
n.parse::<usize>()
.map_err(|_| "must be a number")
.map(|_| ()) // Convert to Result<(), &str>
.map_err(|e| e.to_string())
}, // Convert to Result<(), String>
) )
.hidden_short_help(true) .arg(
.long_help( Arg::with_name("diff-context")
"Include N lines of context around added/removed/modified lines when using '--diff'.", .long("diff-context")
), .overrides_with("diff-context")
) .takes_value(true)
.arg( .value_name("N")
Arg::with_name("tabs") .validator(
.long("tabs") |n| {
.overrides_with("tabs") n.parse::<usize>()
.takes_value(true) .map_err(|_| "must be a number")
.value_name("T") .map(|_| ()) // Convert to Result<(), &str>
.validator( .map_err(|e| e.to_string())
|t| { }, // Convert to Result<(), String>
t.parse::<u32>() )
.map_err(|_t| "must be a number") .hidden_short_help(true)
.map(|_t| ()) // Convert to Result<(), &str> .long_help(
.map_err(|e| e.to_string()) "Include N lines of context around added/removed/modified lines when using '--diff'.",
}, // Convert to Result<(), String> ),
) )
.help("Set the tab width to T spaces.") }
.long_help(
"Set the tab width to T spaces. Use a width of 0 to pass tabs through \ app = app.arg(
Arg::with_name("tabs")
.long("tabs")
.overrides_with("tabs")
.takes_value(true)
.value_name("T")
.validator(
|t| {
t.parse::<u32>()
.map_err(|_t| "must be a number")
.map(|_t| ()) // Convert to Result<(), &str>
.map_err(|e| e.to_string())
}, // Convert to Result<(), String>
)
.help("Set the tab width to T spaces.")
.long_help(
"Set the tab width to T spaces. Use a width of 0 to pass tabs through \
directly", directly",
), ),
) )
.arg( .arg(
Arg::with_name("wrap") Arg::with_name("wrap")
.long("wrap") .long("wrap")
@ -334,7 +342,9 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.validator(|val| { .validator(|val| {
let mut invalid_vals = val.split(',').filter(|style| { let mut invalid_vals = val.split(',').filter(|style| {
!&[ !&[
"auto", "full", "plain", "changes", "header", "grid", "numbers", "snip" "auto", "full", "plain", "header", "grid", "numbers", "snip",
#[cfg(feature = "git")]
"changes",
] ]
.contains(style) .contains(style)
}); });

View File

@ -166,6 +166,7 @@ impl<'a> PrettyPrinter<'a> {
} }
/// Whether to show modification markers for VCS changes /// Whether to show modification markers for VCS changes
#[cfg(feature = "git")]
pub fn vcs_modification_markers(&mut self, yes: bool) -> &mut Self { pub fn vcs_modification_markers(&mut self, yes: bool) -> &mut Self {
self.active_style_components.vcs_modification_markers = yes; self.active_style_components.vcs_modification_markers = yes;
self self
@ -269,6 +270,7 @@ impl<'a> PrettyPrinter<'a> {
style_components.push(StyleComponent::Snip); style_components.push(StyleComponent::Snip);
} }
if self.active_style_components.vcs_modification_markers { if self.active_style_components.vcs_modification_markers {
#[cfg(feature = "git")]
style_components.push(StyleComponent::Changes); style_components.push(StyleComponent::Changes);
} }
self.config.style_components = StyleComponents::new(&style_components); self.config.style_components = StyleComponents::new(&style_components);

View File

@ -111,8 +111,7 @@ impl<'a> InteractivePrinter<'a> {
config: &'a Config, config: &'a Config,
assets: &'a HighlightingAssets, assets: &'a HighlightingAssets,
input: &mut OpenedInput, input: &mut OpenedInput,
#[cfg(feature = "git")] #[cfg(feature = "git")] line_changes: &'a Option<LineChanges>,
line_changes: &'a Option<LineChanges>,
) -> Self { ) -> Self {
let theme = assets.get_theme(&config.theme); let theme = assets.get_theme(&config.theme);

View File

@ -6,6 +6,7 @@ use crate::error::*;
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] #[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
pub enum StyleComponent { pub enum StyleComponent {
Auto, Auto,
#[cfg(feature = "git")]
Changes, Changes,
Grid, Grid,
Header, Header,
@ -25,12 +26,14 @@ impl StyleComponent {
StyleComponent::Plain.components(interactive_terminal) StyleComponent::Plain.components(interactive_terminal)
} }
} }
#[cfg(feature = "git")]
StyleComponent::Changes => &[StyleComponent::Changes], StyleComponent::Changes => &[StyleComponent::Changes],
StyleComponent::Grid => &[StyleComponent::Grid], StyleComponent::Grid => &[StyleComponent::Grid],
StyleComponent::Header => &[StyleComponent::Header], StyleComponent::Header => &[StyleComponent::Header],
StyleComponent::LineNumbers => &[StyleComponent::LineNumbers], StyleComponent::LineNumbers => &[StyleComponent::LineNumbers],
StyleComponent::Snip => &[StyleComponent::Snip], StyleComponent::Snip => &[StyleComponent::Snip],
StyleComponent::Full => &[ StyleComponent::Full => &[
#[cfg(feature = "git")]
StyleComponent::Changes, StyleComponent::Changes,
StyleComponent::Grid, StyleComponent::Grid,
StyleComponent::Header, StyleComponent::Header,
@ -48,6 +51,7 @@ impl FromStr for StyleComponent {
fn from_str(s: &str) -> Result<Self> { fn from_str(s: &str) -> Result<Self> {
match s { match s {
"auto" => Ok(StyleComponent::Auto), "auto" => Ok(StyleComponent::Auto),
#[cfg(feature = "git")]
"changes" => Ok(StyleComponent::Changes), "changes" => Ok(StyleComponent::Changes),
"grid" => Ok(StyleComponent::Grid), "grid" => Ok(StyleComponent::Grid),
"header" => Ok(StyleComponent::Header), "header" => Ok(StyleComponent::Header),