diff options
author | mliszcz <liszcz.michal@gmail.com> | 2022-08-12 01:07:34 +0200 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2022-08-16 18:17:10 -0400 |
commit | 6d96b78502d0fd41bd0a30dd2826723aaed18e96 (patch) | |
tree | cb2801e868778cd4df08f340354070379bceba03 | |
parent | f8bb6e47118783c64d37dcab56e226e2d581d2bf (diff) |
Support configurable timestamps in git blame output (#1157)
New CLI/config option is introduced: blame-timestamp-output-format.
Fixes #1157.
-rw-r--r-- | manual/src/full---help-output.md | 7 | ||||
-rw-r--r-- | src/cli.rs | 10 | ||||
-rw-r--r-- | src/config.rs | 2 | ||||
-rw-r--r-- | src/handlers/blame.rs | 47 | ||||
-rw-r--r-- | src/options/set.rs | 1 |
5 files changed, 64 insertions, 3 deletions
diff --git a/manual/src/full---help-output.md b/manual/src/full---help-output.md index 428d81cd..d7451562 100644 --- a/manual/src/full---help-output.md +++ b/manual/src/full---help-output.md @@ -49,6 +49,13 @@ OPTIONS: [default: "%Y-%m-%d %H:%M:%S %z"] + --blame-timestamp-output-format <FMT> + Format string for git blame timestamp output. + + This string is used for formatting the timestamps in git blame output. It must follow the `strftime` format syntax specification. If it is not present, the timestamps will be formatted in a human-friendly but possibly less accurate form. + + See: (https://docs.rs/chrono/latest/chrono/format/strftime/index.html) + --color-only Do not alter the input structurally in any way. @@ -260,6 +260,16 @@ pub struct Opt { /// Format of `git blame` timestamp in raw git output received by delta. pub blame_timestamp_format: String, + #[clap(long = "blame-timestamp-output-format", value_name = "FMT")] + /// Format string for git blame timestamp output. + /// + /// This string is used for formatting the timestamps in git blame output. It must follow + /// the `strftime` format syntax specification. If it is not present, the timestamps will + /// be formatted in a human-friendly but possibly less accurate form. + /// + /// See: (https://docs.rs/chrono/latest/chrono/format/strftime/index.html) + pub blame_timestamp_output_format: Option<String>, + #[clap(long = "color-only")] /// Do not alter the input structurally in any way. /// diff --git a/src/config.rs b/src/config.rs index c6d7ef51..4da616a7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -40,6 +40,7 @@ pub struct Config { pub blame_palette: Vec<String>, pub blame_separator_style: Option<Style>, pub blame_timestamp_format: String, + pub blame_timestamp_output_format: Option<String>, pub color_only: bool, pub commit_regex: Regex, pub commit_style: Style, @@ -240,6 +241,7 @@ impl From<cli::Opt> for Config { blame_separator_format: parse_blame_line_numbers(&opt.blame_separator_format), blame_separator_style: styles.remove("blame-separator-style"), blame_timestamp_format: opt.blame_timestamp_format, + blame_timestamp_output_format: opt.blame_timestamp_output_format, commit_style: styles["commit-style"], color_only: opt.color_only, commit_regex, diff --git a/src/handlers/blame.rs b/src/handlers/blame.rs index 5ec7e9ff..0e21f6bc 100644 --- a/src/handlers/blame.rs +++ b/src/handlers/blame.rs @@ -269,9 +269,12 @@ pub fn format_blame_metadata( let width = placeholder.width.unwrap_or(15); let field = match placeholder.placeholder { - Some(Placeholder::Str("timestamp")) => Some(Cow::from( - chrono_humanize::HumanTime::from(blame.time).to_string(), - )), + Some(Placeholder::Str("timestamp")) => { + Some(Cow::from(match &config.blame_timestamp_output_format { + Some(time_format) => blame.time.format(time_format).to_string(), + None => chrono_humanize::HumanTime::from(blame.time).to_string(), + })) + } Some(Placeholder::Str("author")) => Some(Cow::from(blame.author)), Some(Placeholder::Str("commit")) => Some(delta::format_raw_line(blame.commit, config)), None => None, @@ -409,6 +412,33 @@ mod tests { } #[test] + fn test_format_blame_metadata_with_default_timestamp_output_format() { + let format_data = format::FormatStringPlaceholderData { + placeholder: Some(Placeholder::Str("timestamp")), + ..Default::default() + }; + let blame = make_blame_line_with_time("1996-12-19T16:39:57-08:00"); + let config = integration_test_utils::make_config_from_args(&[]); + let regex = Regex::new(r"^\d+ years ago$").unwrap(); + let result = format_blame_metadata(&[format_data], &blame, &config); + assert!(regex.is_match(result.trim())); + } + + #[test] + fn test_format_blame_metadata_with_custom_timestamp_output_format() { + let format_data = format::FormatStringPlaceholderData { + placeholder: Some(Placeholder::Str("timestamp")), + ..Default::default() + }; + let blame = make_blame_line_with_time("1996-12-19T16:39:57-08:00"); + let config = integration_test_utils::make_config_from_args(&[ + "--blame-timestamp-output-format=%Y-%m-%d %H:%M", + ]); + let result = format_blame_metadata(&[format_data], &blame, &config); + assert_eq!(result.trim(), "1996-12-19 16:39"); + } + + #[test] fn test_color_assignment() { let mut writer = Cursor::new(vec![0; 512]); let config = integration_test_utils::make_config_from_args(&[ @@ -497,4 +527,15 @@ mod tests { .map(|(k, v)| (k.as_str(), v.as_str())) .collect() } + + fn make_blame_line_with_time(timestamp: &str) -> BlameLine { + let time = chrono::DateTime::parse_from_rfc3339(×tamp).unwrap(); + return BlameLine { + commit: "", + author: "", + time: time, + line_number: 0, + code: "", + }; + } } diff --git a/src/options/set.rs b/src/options/set.rs index 41776516..7cb8b0a7 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -132,6 +132,7 @@ pub fn set_options( blame_palette, blame_separator_style, blame_timestamp_format, + blame_timestamp_output_format, color_only, commit_decoration_style, commit_regex, |