blame-separator and blame-separator-style options

This commit is contained in:
Dan Davison 2021-12-26 22:01:24 -05:00
parent 76905c8e87
commit 62ca45953f
5 changed files with 49 additions and 4 deletions

View File

@ -491,10 +491,19 @@ pub struct Opt {
/// "{timestamp}", "{author}", and "{commit}". /// "{timestamp}", "{author}", and "{commit}".
#[structopt( #[structopt(
long = "blame-format", long = "blame-format",
default_value = "{timestamp:<15} {author:<15.14} {commit:<8}" default_value = "{timestamp:<15} {author:<15.14} {commit:<8}"
)] )]
pub blame_format: String, pub blame_format: String,
/// Separator between the commit metadata and code sections of a line of git blame output.
#[structopt(long = "blame-separator", default_value = "")]
pub blame_separator: String,
#[structopt(long = "blame-separator-style")]
/// Style (foreground, background, attributes) for the separator between the commit metadata and
/// code sections of a line of `git blame` output.
pub blame_separator_style: Option<String>,
#[structopt(long = "blame-code-style")] #[structopt(long = "blame-code-style")]
/// Style (foreground, background, attributes) for the code section of a line of `git blame` /// Style (foreground, background, attributes) for the code section of a line of `git blame`
/// output. By default the code will be syntax-highlighted with the same background color as the /// output. By default the code will be syntax-highlighted with the same background color as the

View File

@ -63,6 +63,8 @@ pub struct Config {
pub blame_code_style: Option<Style>, pub blame_code_style: Option<Style>,
pub blame_format: String, pub blame_format: String,
pub blame_palette: Vec<String>, pub blame_palette: Vec<String>,
pub blame_separator: String,
pub blame_separator_style: Option<Style>,
pub blame_timestamp_format: String, pub blame_timestamp_format: String,
pub color_only: bool, pub color_only: bool,
pub commit_regex: Regex, pub commit_regex: Regex,
@ -166,7 +168,7 @@ impl Config {
impl From<cli::Opt> for Config { impl From<cli::Opt> for Config {
fn from(opt: cli::Opt) -> Self { fn from(opt: cli::Opt) -> Self {
let styles = parse_styles::parse_styles(&opt); let mut styles = parse_styles::parse_styles(&opt);
let styles_map = parse_styles::parse_styles_map(&opt); let styles_map = parse_styles::parse_styles_map(&opt);
let max_line_distance_for_naively_paired_lines = let max_line_distance_for_naively_paired_lines =
@ -242,8 +244,10 @@ impl From<cli::Opt> for Config {
.computed .computed
.background_color_extends_to_terminal_width, .background_color_extends_to_terminal_width,
blame_format: opt.blame_format, blame_format: opt.blame_format,
blame_code_style: styles.get("blame-code-style").copied(), blame_code_style: styles.remove("blame-code-style"),
blame_palette, blame_palette,
blame_separator: opt.blame_separator,
blame_separator_style: styles.remove("blame-separator-style"),
blame_timestamp_format: opt.blame_timestamp_format, blame_timestamp_format: opt.blame_timestamp_format,
commit_style: styles["commit-style"], commit_style: styles["commit-style"],
color_only: opt.color_only, color_only: opt.color_only,

View File

@ -47,11 +47,13 @@ impl<'a> StateMachine<'a> {
let metadata_style = let metadata_style =
self.blame_metadata_style(&key, previous_key.as_deref(), is_repeat); self.blame_metadata_style(&key, previous_key.as_deref(), is_repeat);
let code_style = self.config.blame_code_style.unwrap_or(metadata_style); let code_style = self.config.blame_code_style.unwrap_or(metadata_style);
let separator_style = self.config.blame_separator_style.unwrap_or(code_style);
write!( write!(
self.painter.writer, self.painter.writer,
"{}", "{}{}",
metadata_style.paint(&formatted_blame_metadata), metadata_style.paint(&formatted_blame_metadata),
separator_style.paint(&self.config.blame_separator)
)?; )?;
// Emit syntax-highlighted code // Emit syntax-highlighted code

View File

@ -128,6 +128,8 @@ pub fn set_options(
blame_code_style, blame_code_style,
blame_format, blame_format,
blame_palette, blame_palette,
blame_separator,
blame_separator_style,
blame_timestamp_format, blame_timestamp_format,
color_only, color_only,
commit_decoration_style, commit_decoration_style,

View File

@ -22,6 +22,7 @@ pub fn parse_styles(opt: &cli::Opt) -> HashMap<String, Style> {
make_hunk_styles(opt, &mut styles); make_hunk_styles(opt, &mut styles);
make_commit_file_hunk_header_styles(opt, &mut styles); make_commit_file_hunk_header_styles(opt, &mut styles);
make_line_number_styles(opt, &mut styles); make_line_number_styles(opt, &mut styles);
make_blame_styles(opt, &mut styles);
make_grep_styles(opt, &mut styles); make_grep_styles(opt, &mut styles);
make_merge_conflict_styles(opt, &mut styles); make_merge_conflict_styles(opt, &mut styles);
make_misc_styles(opt, &mut styles); make_misc_styles(opt, &mut styles);
@ -360,6 +361,33 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt, styles: &mut HashMap<&str
]); ]);
} }
fn make_blame_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) {
if let Some(style_string) = &opt.blame_code_style {
styles.insert(
"blame-code-style",
style_from_str(
style_string,
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
),
);
};
if let Some(style_string) = &opt.blame_separator_style {
styles.insert(
"blame-separator-style",
style_from_str(
style_string,
None,
None,
opt.computed.true_color,
opt.git_config.as_ref(),
),
);
};
}
fn make_grep_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) { fn make_grep_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) {
styles.extend([ styles.extend([
( (