1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-09-02 19:32:25 +01:00

Move git changes support behind a feature

This commit is contained in:
David Tolnay
2020-03-30 10:37:29 -07:00
committed by David Peter
parent 570805bc98
commit 4e11abdf9b
6 changed files with 31 additions and 14 deletions

View File

@@ -23,10 +23,12 @@ application = [
"atty", "atty",
"clap", "clap",
"dirs", "dirs",
"git",
"lazy_static", "lazy_static",
"liquid", "liquid",
"wild", "wild",
] ]
git = ["git2"] # Support indicating git modifications
[dependencies] [dependencies]
atty = { version = "0.2.14", optional = true } atty = { version = "0.2.14", optional = true }
@@ -44,6 +46,7 @@ globset = "0.4"
[dependencies.git2] [dependencies.git2]
version = "0.13" version = "0.13"
optional = true
default-features = false default-features = false
[dependencies.syntect] [dependencies.syntect]

1
ci/script.bash vendored
View File

@@ -15,3 +15,4 @@ fi
# Check bat-as-a-library, which has a smaller set of dependencies # Check bat-as-a-library, which has a smaller set of dependencies
cargo check --target "$TARGET" --verbose --lib --no-default-features cargo check --target "$TARGET" --verbose --lib --no-default-features
cargo check --target "$TARGET" --verbose --lib --no-default-features --features git

View File

@@ -1,3 +1,4 @@
#[cfg(feature = "git")]
use crate::diff::LineChange; use crate::diff::LineChange;
use crate::printer::{Colors, InteractivePrinter}; use crate::printer::{Colors, InteractivePrinter};
use ansi_term::Style; use ansi_term::Style;
@@ -68,6 +69,7 @@ impl Decoration for LineNumberDecoration {
} }
} }
#[cfg(feature = "git")]
pub struct LineChangesDecoration { pub struct LineChangesDecoration {
cached_none: DecorationText, cached_none: DecorationText,
cached_added: DecorationText, cached_added: DecorationText,
@@ -76,6 +78,7 @@ pub struct LineChangesDecoration {
cached_modified: DecorationText, cached_modified: DecorationText,
} }
#[cfg(feature = "git")]
impl LineChangesDecoration { impl LineChangesDecoration {
#[inline] #[inline]
fn generate_cached(style: Style, text: &str) -> DecorationText { fn generate_cached(style: Style, text: &str) -> DecorationText {
@@ -96,6 +99,7 @@ impl LineChangesDecoration {
} }
} }
#[cfg(feature = "git")]
impl Decoration for LineChangesDecoration { impl Decoration for LineChangesDecoration {
fn generate( fn generate(
&self, &self,

View File

@@ -1,3 +1,5 @@
#![cfg(feature = "git")]
use std::collections::HashMap; use std::collections::HashMap;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs; use std::fs;

View File

@@ -21,11 +21,11 @@ use unicode_width::UnicodeWidthChar;
use crate::assets::HighlightingAssets; use crate::assets::HighlightingAssets;
use crate::config::Config; use crate::config::Config;
use crate::decorations::{ use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration};
Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration, #[cfg(feature = "git")]
}; use crate::decorations::LineChangesDecoration;
use crate::diff::get_git_diff; #[cfg(feature = "git")]
use crate::diff::LineChanges; use crate::diff::{get_git_diff, LineChanges};
use crate::errors::*; use crate::errors::*;
use crate::inputfile::{InputFile, InputFileReader}; use crate::inputfile::{InputFile, InputFileReader};
use crate::line_range::RangeCheckResult; use crate::line_range::RangeCheckResult;
@@ -100,6 +100,7 @@ pub struct InteractivePrinter<'a> {
panel_width: usize, panel_width: usize,
ansi_prefix_sgr: String, ansi_prefix_sgr: String,
content_type: Option<ContentType>, content_type: Option<ContentType>,
#[cfg(feature = "git")]
pub line_changes: Option<LineChanges>, pub line_changes: Option<LineChanges>,
highlighter: Option<HighlightLines<'a>>, highlighter: Option<HighlightLines<'a>>,
syntax_set: &'a SyntaxSet, syntax_set: &'a SyntaxSet,
@@ -130,8 +131,11 @@ impl<'a> InteractivePrinter<'a> {
decorations.push(Box::new(LineNumberDecoration::new(&colors))); decorations.push(Box::new(LineNumberDecoration::new(&colors)));
} }
if config.style_components.changes() { #[cfg(feature = "git")]
decorations.push(Box::new(LineChangesDecoration::new(&colors))); {
if config.style_components.changes() {
decorations.push(Box::new(LineChangesDecoration::new(&colors)));
}
} }
let mut panel_width: usize = let mut panel_width: usize =
@@ -153,6 +157,7 @@ impl<'a> InteractivePrinter<'a> {
panel_width = 0; panel_width = 0;
} }
#[cfg(feature = "git")]
let mut line_changes = None; let mut line_changes = None;
let highlighter = if reader let highlighter = if reader
@@ -162,14 +167,14 @@ impl<'a> InteractivePrinter<'a> {
None None
} else { } else {
// Get the Git modifications // Get the Git modifications
line_changes = if config.style_components.changes() { #[cfg(feature = "git")]
match file { {
InputFile::Ordinary(filename) => get_git_diff(filename), if config.style_components.changes() {
_ => None, if let InputFile::Ordinary(filename) = file {
line_changes = get_git_diff(filename);
}
} }
} else { }
None
};
// Determine the type of syntax for highlighting // Determine the type of syntax for highlighting
let syntax = assets.get_syntax(config.language, file, reader, &config.syntax_mapping); let syntax = assets.get_syntax(config.language, file, reader, &config.syntax_mapping);
@@ -183,6 +188,7 @@ impl<'a> InteractivePrinter<'a> {
decorations, decorations,
content_type: reader.content_type, content_type: reader.content_type,
ansi_prefix_sgr: String::new(), ansi_prefix_sgr: String::new(),
#[cfg(feature = "git")]
line_changes, line_changes,
highlighter, highlighter,
syntax_set: &assets.syntax_set, syntax_set: &assets.syntax_set,

View File

@@ -68,6 +68,7 @@ impl StyleComponents {
StyleComponents(components.iter().cloned().collect()) StyleComponents(components.iter().cloned().collect())
} }
#[cfg(feature = "git")]
pub fn changes(&self) -> bool { pub fn changes(&self) -> bool {
self.0.contains(&StyleComponent::Changes) self.0.contains(&StyleComponent::Changes)
} }