Skip to content

Commit

Permalink
blame-separator and blame-separator-style options
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Dec 28, 2021
1 parent 76905c8 commit 62ca459
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,19 @@ pub struct Opt {
/// "{timestamp}", "{author}", and "{commit}".
#[structopt(
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,

/// 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")]
/// 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
Expand Down
8 changes: 6 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub struct Config {
pub blame_code_style: Option<Style>,
pub blame_format: String,
pub blame_palette: Vec<String>,
pub blame_separator: String,
pub blame_separator_style: Option<Style>,
pub blame_timestamp_format: String,
pub color_only: bool,
pub commit_regex: Regex,
Expand Down Expand Up @@ -166,7 +168,7 @@ impl Config {

impl From<cli::Opt> for Config {
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 max_line_distance_for_naively_paired_lines =
Expand Down Expand Up @@ -242,8 +244,10 @@ impl From<cli::Opt> for Config {
.computed
.background_color_extends_to_terminal_width,
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_separator: opt.blame_separator,
blame_separator_style: styles.remove("blame-separator-style"),
blame_timestamp_format: opt.blame_timestamp_format,
commit_style: styles["commit-style"],
color_only: opt.color_only,
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ impl<'a> StateMachine<'a> {
let metadata_style =
self.blame_metadata_style(&key, previous_key.as_deref(), is_repeat);
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!(
self.painter.writer,
"{}",
"{}{}",
metadata_style.paint(&formatted_blame_metadata),
separator_style.paint(&self.config.blame_separator)
)?;

// Emit syntax-highlighted code
Expand Down
2 changes: 2 additions & 0 deletions src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub fn set_options(
blame_code_style,
blame_format,
blame_palette,
blame_separator,
blame_separator_style,
blame_timestamp_format,
color_only,
commit_decoration_style,
Expand Down
28 changes: 28 additions & 0 deletions src/parse_styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn parse_styles(opt: &cli::Opt) -> HashMap<String, Style> {
make_hunk_styles(opt, &mut styles);
make_commit_file_hunk_header_styles(opt, &mut styles);
make_line_number_styles(opt, &mut styles);
make_blame_styles(opt, &mut styles);
make_grep_styles(opt, &mut styles);
make_merge_conflict_styles(opt, &mut styles);
make_misc_styles(opt, &mut styles);
Expand Down Expand Up @@ -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>) {
styles.extend([
(
Expand Down

0 comments on commit 62ca459

Please sign in to comment.