Skip to content

Commit

Permalink
Add methods for getting GitConfig as reference (dandavison#1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickelc committed Mar 9, 2023
1 parent b614b1c commit 6a37445
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 82 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,10 @@ impl Opt {
})
.collect()
}

pub fn git_config(&self) -> Option<&GitConfig> {
self.git_config.as_ref()
}
}

// Option names to exclude when listing options to process for various purposes. These are all
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ impl Config {
_ => delta_unreachable("Unreachable code reached in get_style."),
}
}

pub fn git_config(&self) -> Option<&GitConfig> {
self.git_config.as_ref()
}
}

impl From<cli::Opt> for Config {
Expand Down
6 changes: 1 addition & 5 deletions src/features/hyperlinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ pub fn format_commit_line_with_osc8_commit_hyperlink<'a>(
format_osc8_hyperlink(&commit_link_format.replace("{commit}", commit), commit);
format!("{prefix}{formatted_commit}{suffix}")
})
} else if let Some(repo) = config
.git_config
.as_ref()
.and_then(GitConfig::get_remote_url)
{
} else if let Some(repo) = config.git_config().and_then(GitConfig::get_remote_url) {
COMMIT_LINE_REGEX.replace(line, |captures: &Captures| {
format_commit_line_captures_with_osc8_commit_hyperlink(captures, &repo)
})
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a> StateMachine<'a> {
// borrow checker won't permit that.
let style = Style::from_colors(
None,
color::parse_color(&color, true, self.config.git_config.as_ref()),
color::parse_color(&color, true, self.config.git_config()),
);
self.blame_key_colors.insert(key.to_owned(), color);
style
Expand Down
106 changes: 34 additions & 72 deletions src/parse_styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ fn resolve_style_references(
}

fn parse_as_style_or_reference_to_git_config(style_string: &str, opt: &cli::Opt) -> Style {
match style_from_str(style_string, None, None, true, opt.git_config.as_ref()) {
match style_from_str(style_string, None, None, true, opt.git_config()) {
StyleReference::Reference(style_ref) => parse_as_reference_to_git_config(&style_ref, opt),
StyleReference::Style(style) => style,
}
}

fn parse_as_reference_to_git_config(style_string: &str, opt: &cli::Opt) -> Style {
if let Some(git_config) = &opt.git_config {
if let Some(git_config) = opt.git_config() {
let git_config_key = format!("delta.{style_string}");
match git_config.get::<String>(&git_config_key) {
Some(s) => Style::from_git_str(&s),
Expand Down Expand Up @@ -128,7 +128,7 @@ fn make_hunk_styles<'a>(opt: &'a cli::Opt, styles: &'a mut HashMap<&str, StyleRe
)),
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let minus_emph_style = style_from_str(
Expand All @@ -142,15 +142,15 @@ fn make_hunk_styles<'a>(opt: &'a cli::Opt, styles: &'a mut HashMap<&str, StyleRe
)),
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let minus_non_emph_style = style_from_str(
&opt.minus_non_emph_style,
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

// The style used to highlight a removed empty line when otherwise it would be invisible due to
Expand All @@ -166,16 +166,10 @@ fn make_hunk_styles<'a>(opt: &'a cli::Opt, styles: &'a mut HashMap<&str, StyleRe
)),
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let zero_style = style_from_str(
&opt.zero_style,
None,
None,
true_color,
opt.git_config.as_ref(),
);
let zero_style = style_from_str(&opt.zero_style, None, None, true_color, opt.git_config());

let plus_style = style_from_str(
&opt.plus_style,
Expand All @@ -188,7 +182,7 @@ fn make_hunk_styles<'a>(opt: &'a cli::Opt, styles: &'a mut HashMap<&str, StyleRe
)),
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let plus_emph_style = style_from_str(
Expand All @@ -202,15 +196,15 @@ fn make_hunk_styles<'a>(opt: &'a cli::Opt, styles: &'a mut HashMap<&str, StyleRe
)),
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let plus_non_emph_style = style_from_str(
&opt.plus_non_emph_style,
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

// The style used to highlight an added empty line when otherwise it would be invisible due to
Expand All @@ -226,15 +220,15 @@ fn make_hunk_styles<'a>(opt: &'a cli::Opt, styles: &'a mut HashMap<&str, StyleRe
)),
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let whitespace_error_style = style_from_str(
&opt.whitespace_error_style,
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

styles.extend([
Expand All @@ -261,39 +255,39 @@ fn make_line_number_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleRefer
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let line_numbers_minus_style = style_from_str(
&opt.line_numbers_minus_style,
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let line_numbers_zero_style = style_from_str(
&opt.line_numbers_zero_style,
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let line_numbers_plus_style = style_from_str(
&opt.line_numbers_plus_style,
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

let line_numbers_right_style = style_from_str(
&opt.line_numbers_right_style,
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
);

styles.extend([
Expand All @@ -315,7 +309,7 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt, styles: &mut HashMap<&str
None,
Some(&opt.commit_decoration_style),
true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
),
(
Expand All @@ -325,7 +319,7 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt, styles: &mut HashMap<&str
None,
Some(&opt.file_decoration_style),
true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
),
(
Expand All @@ -335,7 +329,7 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt, styles: &mut HashMap<&str
None,
Some(&opt.hunk_header_decoration_style),
true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
),
(
Expand All @@ -345,7 +339,7 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt, styles: &mut HashMap<&str
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
),
(
Expand All @@ -355,7 +349,7 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt, styles: &mut HashMap<&str
None,
None,
true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
),
]);
Expand All @@ -370,7 +364,7 @@ fn make_blame_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>)
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
);
};
Expand All @@ -382,7 +376,7 @@ fn make_blame_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>)
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
);
};
Expand All @@ -393,69 +387,39 @@ fn make_grep_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>)
(
"grep-match-line-style",
if let Some(s) = &opt.grep_match_line_style {
style_from_str(
s,
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
)
style_from_str(s, None, None, opt.computed.true_color, opt.git_config())
} else {
StyleReference::Reference("zero-style".to_owned())
},
),
(
"grep-match-word-style",
if let Some(s) = &opt.grep_match_word_style {
style_from_str(
s,
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
)
style_from_str(s, None, None, opt.computed.true_color, opt.git_config())
} else {
StyleReference::Reference("plus-emph-style".to_owned())
},
),
(
"grep-context-line-style",
if let Some(s) = &opt.grep_context_line_style {
style_from_str(
s,
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
)
style_from_str(s, None, None, opt.computed.true_color, opt.git_config())
} else {
StyleReference::Reference("zero-style".to_owned())
},
),
(
"grep-file-style",
if let Some(s) = &opt.grep_file_style {
style_from_str(
s,
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
)
style_from_str(s, None, None, opt.computed.true_color, opt.git_config())
} else {
StyleReference::Reference("hunk-header-file-style".to_owned())
},
),
(
"grep-line-number-style",
if let Some(s) = &opt.grep_line_number_style {
style_from_str(
s,
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
)
style_from_str(s, None, None, opt.computed.true_color, opt.git_config())
} else {
StyleReference::Reference("hunk-header-line-number-style".to_owned())
},
Expand All @@ -471,7 +435,7 @@ fn make_merge_conflict_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleRe
None,
Some(&opt.merge_conflict_ours_diff_header_decoration_style),
opt.computed.true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
);
styles.insert(
Expand All @@ -481,7 +445,7 @@ fn make_merge_conflict_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleRe
None,
Some(&opt.merge_conflict_theirs_diff_header_decoration_style),
opt.computed.true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
);
}
Expand All @@ -494,15 +458,14 @@ fn make_misc_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>)
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
opt.git_config(),
),
);
styles.insert(
"git-minus-style",
StyleReference::Style(
match opt
.git_config
.as_ref()
.git_config()
.and_then(|cfg| cfg.get::<String>("color.diff.old"))
{
Some(s) => Style::from_git_str(&s),
Expand All @@ -514,8 +477,7 @@ fn make_misc_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>)
"git-plus-style",
StyleReference::Style(
match opt
.git_config
.as_ref()
.git_config()
.and_then(|cfg| cfg.get::<String>("color.diff.new"))
{
Some(s) => Style::from_git_str(&s),
Expand Down
3 changes: 1 addition & 2 deletions src/subcommands/show_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ pub fn show_colors() -> std::io::Result<()> {
}
// Two syntax-highlighted lines with background color
let color =
color::parse_color(color_name, config.true_color, config.git_config.as_ref())
.unwrap();
color::parse_color(color_name, config.true_color, config.git_config()).unwrap();
style.ansi_term_style.background = Some(color);
for line in [
&format!(r#"export function color(): string {{ return "{color_name}" }}"#),
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/show_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::
blame_palette = config
.blame_palette
.iter()
.map(|s| style::paint_color_string(s, config.true_color, config.git_config.as_ref()))
.map(|s| style::paint_color_string(s, config.true_color, config.git_config()))
.join(" "),
commit_style = config.commit_style.to_painted_string(),
file_style = config.file_style.to_painted_string(),
Expand Down
Loading

0 comments on commit 6a37445

Please sign in to comment.