Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# unreleased

## Features
- Use theme styles for git signs, see PR #3407 (@flashios09)
- Add `--gitsigns` option, see PR #3407 (@flashios09)
- Add build for windows/ARM64 platform. #3190 (@alcroito)

- Add paging to `--list-themes`, see PR #3239 (@einfachIrgendwer0815)
Expand Down
10 changes: 10 additions & 0 deletions doc/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ Options:
--diff-context <N>
Include N lines of context around added/removed/modified lines when using '--diff'.

--gitsigns <gitsigns>
Set git `added`, `modified`, `removed-above`, `removed-below` signs. (default: classic)
To set a default gitsigns, add the '--gitsigns="..."' option to the configuration file
or export the BAT_GITSIGNS environment variable (e.g.: export BAT_GITSIGNS="...").

Possible values:
* classic: preset `~,+,‾,_`
* modern: preset `▎,▎,‾,_`
* 4 comma-separated signs, e.g. `~,+,-,-`

--tabs <T>
Set the tab width to T spaces. Use a width of 0 to pass tabs through directly

Expand Down
2 changes: 2 additions & 0 deletions doc/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Options:
Specify the name to display for a file.
-d, --diff
Only show lines that have been added/removed/modified.
--gitsigns <gitsigns>
Set git `added`, `modified`, `removed-above`, `removed-below` signs. (default: classic)
--tabs <T>
Set the tab width to T spaces.
--wrap <mode>
Expand Down
17 changes: 17 additions & 0 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ use bat::{
MappingTarget, NonprintableNotation, PagingMode, SyntaxMapping, WrappingMode,
};

#[cfg(feature = "git")]
use bat::gitsigns::Gitsigns;

fn is_truecolor_terminal() -> bool {
env::var("COLORTERM")
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
Expand Down Expand Up @@ -335,6 +338,20 @@ impl App {
} else {
None
},
#[cfg(feature = "git")]
gitsigns: match self
.matches
.get_one::<String>("gitsigns")
.map(|s| s.as_str())
{
Some("classic") => Gitsigns::classic(),
Some("modern") => Gitsigns::modern(),
Some(s) => Gitsigns::parse(s).unwrap_or_else(|e| {
eprintln!("Error parsing `--gitsigns={s}` \n{e}");
std::process::exit(1);
}),
None => Gitsigns::default(),
},
})
}

Expand Down
15 changes: 15 additions & 0 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ pub fn build_app(interactive_output: bool) -> Command {
"Include N lines of context around added/removed/modified lines when using '--diff'.",
),
)
.arg(
Arg::new("gitsigns")
.long("gitsigns")
.overrides_with("gitsigns")
.help("Set git `added`, `modified`, `removed-above`, `removed-below` signs. (default: classic)")
.long_help(
"Set git `added`, `modified`, `removed-above`, `removed-below` signs. (default: classic)\n\
To set a default gitsigns, add the '--gitsigns=\"...\"' option to the configuration file\n\
or export the BAT_GITSIGNS environment variable (e.g.: export BAT_GITSIGNS=\"...\").\n\n\
Possible values:\n \
* classic: preset `~,+,‾,_`\n \
* modern: preset `▎,▎,‾,_`\n \
* 4 comma-separated signs, e.g. `~,+,-,-`",
),
)
}

app = app.arg(
Expand Down
26 changes: 16 additions & 10 deletions src/bin/bat/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,29 @@ fn get_args_from_str(content: &str) -> Result<Vec<OsString>, shell_words::ParseE
}

pub fn get_args_from_env_vars() -> Vec<OsString> {
[
let mut args = vec![
("--tabs", "BAT_TABS"),
("--theme", bat::theme::env::BAT_THEME),
("--theme-dark", bat::theme::env::BAT_THEME_DARK),
("--theme-light", bat::theme::env::BAT_THEME_LIGHT),
("--pager", "BAT_PAGER"),
("--paging", "BAT_PAGING"),
("--style", "BAT_STYLE"),
]
.iter()
.filter_map(|(flag, key)| {
env::var(key)
.ok()
.map(|var| [flag.to_string(), var].join("="))
})
.map(|a| a.into())
.collect()
];

#[cfg(feature = "git")]
{
args.push(("--gitsigns", bat::gitsigns::env::BAT_GITSIGNS));
}

args.iter()
.filter_map(|(flag, key)| {
env::var(key)
.ok()
.map(|var| [flag.to_string(), var].join("="))
})
.map(|a| a.into())
.collect()
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ fn invoke_bugreport(app: &App, cache_dir: &Path) {
"BAT_STYLE",
"BAT_TABS",
"BAT_THEME",
"BAT_GITSIGNS",
"COLORTERM",
"LANG",
"LC_ALL",
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "git")]
use crate::gitsigns::Gitsigns;
use crate::line_range::{HighlightedLineRanges, LineRanges};
use crate::nonprintable_notation::{BinaryBehavior, NonprintableNotation};
#[cfg(feature = "paging")]
Expand Down Expand Up @@ -107,6 +109,10 @@ pub struct Config<'a> {

// Weather or not to set terminal title when using a pager
pub strip_ansi: StripAnsiMode,

/// Set git `added`, `modified`, `removed-above`, `removed-below` signs
#[cfg(feature = "git")]
pub gitsigns: Gitsigns,
}

#[cfg(all(feature = "minimal-application", feature = "paging"))]
Expand Down
20 changes: 13 additions & 7 deletions src/decorations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "git")]
use crate::diff::LineChange;
use crate::printer::{Colors, InteractivePrinter};
#[cfg(feature = "git")]
use crate::{diff::LineChange, gitsigns::Gitsigns};
use nu_ansi_term::Style;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -88,13 +88,19 @@ impl LineChangesDecoration {
}
}

pub(crate) fn new(colors: &Colors) -> Self {
pub(crate) fn new(colors: &Colors, gitsigns: &Gitsigns) -> Self {
LineChangesDecoration {
cached_none: Self::generate_cached(Style::default(), " "),
cached_added: Self::generate_cached(colors.git_added, "+"),
cached_removed_above: Self::generate_cached(colors.git_removed, "‾"),
cached_removed_below: Self::generate_cached(colors.git_removed, "_"),
cached_modified: Self::generate_cached(colors.git_modified, "~"),
cached_added: Self::generate_cached(colors.git_added, &gitsigns.added),
cached_removed_above: Self::generate_cached(
colors.git_removed,
&gitsigns.removed_above,
),
cached_removed_below: Self::generate_cached(
colors.git_removed,
&gitsigns.removed_below,
),
cached_modified: Self::generate_cached(colors.git_modified, &gitsigns.modified),
}
}
}
Expand Down
75 changes: 75 additions & 0 deletions src/gitsigns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
pub mod env {
pub const BAT_GITSIGNS: &str = "BAT_GITSIGNS";
}

#[cfg(feature = "git")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Gitsigns {
pub modified: String,
pub added: String,
pub removed_above: String,
pub removed_below: String,
}

#[cfg(feature = "git")]
impl Default for Gitsigns {
fn default() -> Self {
Gitsigns::classic()
}
}

#[cfg(feature = "git")]
impl Gitsigns {
pub fn classic() -> Self {
Self {
modified: "~".into(),
added: "+".into(),
removed_above: "‾".into(),
removed_below: "_".into(),
}
}

pub fn modern() -> Self {
Self {
modified: "▎".to_string(),
added: "▎".to_string(),
removed_above: "‾".to_string(),
removed_below: "_".to_string(),
}
}

pub fn parse(s: &str) -> Result<Self, String> {
let parts: Vec<&str> = s
.split(',')
.map(|p| {
// allow single space char as gitsign
if p == " " {
return p;
}

p.trim()
})
.collect();

if parts.len() != 4 {
return Err("Expected 4 comma-separated signs: `modified,added,removed-above,removed-below`, e.g. `~,+,‾,_`".into());
}

for (i, part) in parts.iter().enumerate() {
if part.chars().count() != 1 {
return Err(format!(
"Invalid sign at position {}: `{}`. Each sign must be a single character.",
i + 1,
part
));
}
}

Ok(Self {
modified: parts[0].to_string(),
added: parts[1].to_string(),
removed_above: parts[2].to_string(),
removed_below: parts[3].to_string(),
})
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub mod controller;
mod decorations;
mod diff;
pub mod error;
#[cfg(feature = "git")]
pub mod gitsigns;
pub mod input;
mod less;
#[cfg(feature = "lessopen")]
Expand Down
Loading