Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom colors to be defined in gitconfig #788

Merged
merged 1 commit into from
Nov 22, 2021
Merged
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
18 changes: 14 additions & 4 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use lazy_static::lazy_static;
use syntect::highlighting::Color as SyntectColor;

use crate::fatal;
use crate::git_config::GitConfig;
use crate::utils;

pub fn parse_color(s: &str, true_color: bool) -> Option<Color> {
pub fn parse_color(s: &str, true_color: bool, git_config: Option<&GitConfig>) -> Option<Color> {
if s == "normal" {
return None;
}
Expand All @@ -18,12 +19,21 @@ pub fn parse_color(s: &str, true_color: bool) -> Option<Color> {
let syntect_color = if s.starts_with('#') {
SyntectColor::from_str(s).unwrap_or_else(|_| die())
} else {
s.parse::<u8>()
let syntect_color = s
.parse::<u8>()
.ok()
.and_then(utils::syntect::syntect_color_from_ansi_number)
.or_else(|| utils::syntect::syntect_color_from_ansi_name(s))
.or_else(|| utils::syntect::syntect_color_from_name(s))
.unwrap_or_else(die)
.or_else(|| utils::syntect::syntect_color_from_name(s));
if syntect_color.is_none() {
if let Some(git_config) = git_config {
if let Some(val) = git_config.get::<String>(&format!("delta.{}", s)) {
return parse_color(&val, true_color, None);
}
}
die();
}
syntect_color.unwrap()
};
utils::bat::terminal::to_ansi_color(syntect_color, true_color)
}
Expand Down
5 changes: 4 additions & 1 deletion src/handlers/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ impl<'a> StateMachine<'a> {
{
let is_repeat = previous_commit == Some(blame.commit);
let color = self.get_color(blame.commit, previous_commit, is_repeat);
let mut style = Style::from_colors(None, color::parse_color(&color, true));
let mut style = Style::from_colors(
None,
color::parse_color(&color, true, self.config.git_config.as_ref()),
);
// TODO: This will often be pointlessly updating a key with the
// value it already has. It might be nicer to do this (and
// compute the style) in get_color(), but as things stand the
Expand Down
74 changes: 47 additions & 27 deletions src/parse_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bitflags::bitflags;
use crate::color;
use crate::config::delta_unreachable;
use crate::fatal;
use crate::git_config::GitConfig;
use crate::style::{DecorationStyle, Style};

impl Style {
Expand All @@ -15,11 +16,15 @@ impl Style {
default: Option<Self>,
decoration_style_string: Option<&str>,
true_color: bool,
git_config: Option<&GitConfig>,
) -> Self {
let (ansi_term_style, is_omitted, is_raw, is_syntax_highlighted) =
parse_ansi_term_style(style_string, default, true_color);
let decoration_style =
DecorationStyle::from_str(decoration_style_string.unwrap_or(""), true_color);
parse_ansi_term_style(style_string, default, true_color, git_config);
let decoration_style = DecorationStyle::from_str(
decoration_style_string.unwrap_or(""),
true_color,
git_config,
);
Self {
ansi_term_style,
is_emph: false,
Expand All @@ -31,7 +36,7 @@ impl Style {
}

pub fn from_git_str(git_style_string: &str) -> Self {
Self::from_str(git_style_string, None, None, true)
Self::from_str(git_style_string, None, None, true, None)
}

/// Construct Style but interpreting 'ul', 'box', etc as applying to the decoration style.
Expand All @@ -40,11 +45,17 @@ impl Style {
default: Option<Self>,
decoration_style_string: Option<&str>,
true_color: bool,
git_config: Option<&GitConfig>,
) -> Self {
let (special_attributes_from_style_string, style_string) =
extract_special_decoration_attributes_from_non_decoration_style_string(style_string);
let mut style =
Style::from_str(&style_string, default, decoration_style_string, true_color);
let mut style = Style::from_str(
&style_string,
default,
decoration_style_string,
true_color,
git_config,
);
// TODO: box in this context resulted in box-with-underline for commit and file
style.decoration_style = DecorationStyle::apply_special_decoration_attributes(
&mut style,
Expand All @@ -61,18 +72,22 @@ impl Style {
decoration_style_string: Option<&str>,
deprecated_foreground_color_arg: Option<&str>,
true_color: bool,
git_config: Option<&GitConfig>,
) -> Self {
let mut style = Self::from_str_with_handling_of_special_decoration_attributes(
style_string,
default,
decoration_style_string,
true_color,
git_config,
);
if let Some(s) = deprecated_foreground_color_arg {
// The deprecated --{commit,file,hunk}-color args functioned to set the decoration
// foreground color. In the case of file, it set the text foreground color also.
let foreground_from_deprecated_arg =
parse_ansi_term_style(s, None, true_color).0.foreground;
parse_ansi_term_style(s, None, true_color, git_config)
.0
.foreground;
style.ansi_term_style.foreground = foreground_from_deprecated_arg;
style.decoration_style = match style.decoration_style {
DecorationStyle::Box(mut ansi_term_style) => {
Expand Down Expand Up @@ -120,11 +135,11 @@ bitflags! {
}

impl DecorationStyle {
pub fn from_str(style_string: &str, true_color: bool) -> Self {
pub fn from_str(style_string: &str, true_color: bool, git_config: Option<&GitConfig>) -> Self {
let (special_attributes, style_string) =
extract_special_decoration_attributes(style_string);
let (style, is_omitted, is_raw, is_syntax_highlighted) =
parse_ansi_term_style(&style_string, None, true_color);
parse_ansi_term_style(&style_string, None, true_color, git_config);
if is_raw {
fatal("'raw' may not be used in a decoration style.");
};
Expand Down Expand Up @@ -191,6 +206,7 @@ fn parse_ansi_term_style(
s: &str,
default: Option<Style>,
true_color: bool,
git_config: Option<&GitConfig>,
) -> (ansi_term::Style, bool, bool, bool) {
let mut style = ansi_term::Style::new();
let mut seen_foreground = false;
Expand Down Expand Up @@ -239,7 +255,7 @@ fn parse_ansi_term_style(
style.foreground = default.and_then(|s| s.ansi_term_style.foreground);
is_syntax_highlighted = default.map(|s| s.is_syntax_highlighted).unwrap_or(false);
} else {
style.foreground = color::parse_color(word, true_color);
style.foreground = color::parse_color(word, true_color, git_config);
}
seen_foreground = true;
} else if !seen_background {
Expand All @@ -253,7 +269,7 @@ fn parse_ansi_term_style(
background_is_auto = true;
style.background = default.and_then(|s| s.ansi_term_style.background);
} else {
style.background = color::parse_color(word, true_color);
style.background = color::parse_color(word, true_color, git_config);
}
seen_background = true;
} else {
Expand Down Expand Up @@ -321,11 +337,11 @@ mod tests {
#[test]
fn test_parse_ansi_term_style() {
assert_eq!(
parse_ansi_term_style("", None, false),
parse_ansi_term_style("", None, false, None),
(ansi_term::Style::new(), false, false, false)
);
assert_eq!(
parse_ansi_term_style("red", None, false),
parse_ansi_term_style("red", None, false, None),
(
ansi_term::Style {
foreground: Some(ansi_term::Color::Red),
Expand All @@ -337,7 +353,7 @@ mod tests {
)
);
assert_eq!(
parse_ansi_term_style("red green", None, false),
parse_ansi_term_style("red green", None, false, None),
(
ansi_term::Style {
foreground: Some(ansi_term::Color::Red),
Expand All @@ -350,7 +366,7 @@ mod tests {
)
);
assert_eq!(
parse_ansi_term_style("bold red underline green blink", None, false),
parse_ansi_term_style("bold red underline green blink", None, false, None),
(
ansi_term::Style {
foreground: Some(ansi_term::Color::Red),
Expand All @@ -370,11 +386,11 @@ mod tests {
#[test]
fn test_parse_ansi_term_style_with_special_syntax_color() {
assert_eq!(
parse_ansi_term_style("syntax", None, false),
parse_ansi_term_style("syntax", None, false, None),
(ansi_term::Style::new(), false, false, true)
);
assert_eq!(
parse_ansi_term_style("syntax italic white hidden", None, false),
parse_ansi_term_style("syntax italic white hidden", None, false, None),
(
ansi_term::Style {
background: Some(ansi_term::Color::White),
Expand All @@ -388,7 +404,7 @@ mod tests {
)
);
assert_eq!(
parse_ansi_term_style("bold syntax italic white hidden", None, false),
parse_ansi_term_style("bold syntax italic white hidden", None, false, None),
(
ansi_term::Style {
background: Some(ansi_term::Color::White),
Expand All @@ -407,12 +423,12 @@ mod tests {
#[test]
fn test_parse_ansi_term_style_with_special_omit_attribute() {
assert_eq!(
parse_ansi_term_style("omit", None, false),
parse_ansi_term_style("omit", None, false, None),
(ansi_term::Style::new(), true, false, false)
);
// It doesn't make sense for omit to be combined with anything else, but it is not an error.
assert_eq!(
parse_ansi_term_style("omit syntax italic white hidden", None, false),
parse_ansi_term_style("omit syntax italic white hidden", None, false, None),
(
ansi_term::Style {
background: Some(ansi_term::Color::White),
Expand All @@ -430,12 +446,12 @@ mod tests {
#[test]
fn test_parse_ansi_term_style_with_special_raw_attribute() {
assert_eq!(
parse_ansi_term_style("raw", None, false),
parse_ansi_term_style("raw", None, false, None),
(ansi_term::Style::new(), false, true, false)
);
// It doesn't make sense for raw to be combined with anything else, but it is not an error.
assert_eq!(
parse_ansi_term_style("raw syntax italic white hidden", None, false),
parse_ansi_term_style("raw syntax italic white hidden", None, false, None),
(
ansi_term::Style {
background: Some(ansi_term::Color::White),
Expand Down Expand Up @@ -496,15 +512,15 @@ mod tests {
#[test]
fn test_decoration_style_from_str_empty_string() {
assert_eq!(
DecorationStyle::from_str("", true),
DecorationStyle::from_str("", true, None),
DecorationStyle::NoDecoration,
)
}

#[test]
fn test_decoration_style_from_str() {
assert_eq!(
DecorationStyle::from_str("ol red box bold green ul", true),
DecorationStyle::from_str("ol red box bold green ul", true, None),
DecorationStyle::BoxWithUnderOverline(ansi_term::Style {
foreground: Some(ansi_term::Color::Red),
background: Some(ansi_term::Color::Green),
Expand All @@ -521,6 +537,7 @@ mod tests {
None,
Some("ol red box bold green ul"),
true,
None,
);
let red_green_bold = ansi_term::Style {
foreground: Some(ansi_term::Color::Red),
Expand All @@ -540,7 +557,7 @@ mod tests {

#[test]
fn test_style_from_str_raw_with_box() {
let actual_style = Style::from_str("raw", None, Some("box"), true);
let actual_style = Style::from_str("raw", None, Some("box"), true, None);
let empty_ansi_term_style = ansi_term::Style::new();
assert_eq!(
actual_style,
Expand All @@ -555,7 +572,7 @@ mod tests {

#[test]
fn test_style_from_str_decoration_style_only() {
let actual_style = Style::from_str("", None, Some("ol red box bold green ul"), true);
let actual_style = Style::from_str("", None, Some("ol red box bold green ul"), true, None);
let red_green_bold = ansi_term::Style {
foreground: Some(ansi_term::Color::Red),
background: Some(ansi_term::Color::Green),
Expand All @@ -578,6 +595,7 @@ mod tests {
None,
Some("ol red box bold green ul"),
true,
None,
);
let expected_decoration_style = DecorationStyle::BoxWithUnderOverline(ansi_term::Style {
foreground: Some(ansi_term::Color::Red),
Expand All @@ -601,6 +619,7 @@ mod tests {
None,
Some("box"),
true,
None,
);
let empty_ansi_term_style = ansi_term::Style::new();
assert_eq!(
Expand All @@ -624,7 +643,7 @@ mod tests {
..ansi_term::Style::new()
});
let actual_style = Style::from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg(
"", None, Some("ol red box bold green ul"), None, true
"", None, Some("ol red box bold green ul"), None, true, None
);
assert_eq!(
actual_style,
Expand All @@ -644,6 +663,7 @@ mod tests {
Some("box"),
None,
true,
None,
);
let empty_ansi_term_style = ansi_term::Style::new();
assert_eq!(
Expand Down
Loading