From 26cc290157915189803cf3c392400a1857a6c5c9 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Mon, 30 Jan 2023 20:41:50 -0800 Subject: [PATCH] Templater: Create `shortest_styled_prefix` function for ids --- src/template_parser.rs | 15 ++++++++++++--- src/templater.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/template_parser.rs b/src/template_parser.rs index 7fb830664..41a71112a 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -27,9 +27,9 @@ use crate::templater::{ CommitOrChangeIdShort, CommitOrChangeIdShortestPrefixAndBrackets, CommitterProperty, ConditionalTemplate, ConflictProperty, DescriptionProperty, DivergentProperty, DynamicLabelTemplate, EmptyProperty, FormattablePropertyTemplate, GitHeadProperty, - GitRefsProperty, IsWorkingCopyProperty, LabelTemplate, ListTemplate, Literal, - SignatureTimestamp, TagProperty, Template, TemplateFunction, TemplateProperty, - WorkingCopiesProperty, + GitRefsProperty, HighlightPrefix, IdWithHighlightedPrefix, IsWorkingCopyProperty, + LabelTemplate, ListTemplate, Literal, SignatureTimestamp, TagProperty, Template, + TemplateFunction, TemplateProperty, WorkingCopiesProperty, }; use crate::time_util; @@ -101,6 +101,7 @@ enum Property<'a, I> { String(Box + 'a>), Boolean(Box + 'a>), CommitOrChangeId(Box> + 'a>), + IdWithHighlightedPrefix(Box + 'a>), Signature(Box + 'a>), Timestamp(Box + 'a>), } @@ -121,6 +122,9 @@ impl<'a, I: 'a> Property<'a, I> { Property::CommitOrChangeId(property) => { Property::CommitOrChangeId(chain(first, property)) } + Property::IdWithHighlightedPrefix(property) => { + Property::IdWithHighlightedPrefix(chain(first, property)) + } Property::Signature(property) => Property::Signature(chain(first, property)), Property::Timestamp(property) => Property::Timestamp(chain(first, property)), } @@ -136,6 +140,7 @@ impl<'a, I: 'a> Property<'a, I> { Property::String(property) => wrap(property), Property::Boolean(property) => wrap(property), Property::CommitOrChangeId(property) => wrap(property), + Property::IdWithHighlightedPrefix(property) => wrap(property), Property::Signature(property) => wrap(property), Property::Timestamp(property) => wrap(property), } @@ -165,6 +170,9 @@ fn parse_method_chain<'a, I: 'a>( Property::CommitOrChangeId(property) => { parse_commit_or_change_id_method(name, args).after(property) } + Property::IdWithHighlightedPrefix(_property) => { + panic!("Commit or change ids with styled prefix don't have any methods") + } Property::Signature(property) => parse_signature_method(name, args).after(property), Property::Timestamp(property) => parse_timestamp_method(name, args).after(property), }; @@ -195,6 +203,7 @@ fn parse_commit_or_change_id_method<'a>( "shortest_prefix_and_brackets" => { Property::String(Box::new(CommitOrChangeIdShortestPrefixAndBrackets)) } + "shortest_styled_prefix" => Property::IdWithHighlightedPrefix(Box::new(HighlightPrefix)), name => panic!("no such commit ID method: {name}"), } } diff --git a/src/templater.rs b/src/templater.rs index 3c784ed69..6a08a7ea6 100644 --- a/src/templater.rs +++ b/src/templater.rs @@ -561,6 +561,38 @@ mod tests { } } +pub struct IdWithHighlightedPrefix { + prefix: String, + rest: String, +} + +impl Template<()> for IdWithHighlightedPrefix { + fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { + formatter.with_label("prefix", |fmt| fmt.write_str(&self.prefix))?; + formatter.with_label("rest", |fmt| fmt.write_str(&self.rest)) + } +} + +pub struct HighlightPrefix; +impl TemplateProperty> for HighlightPrefix { + type Output = IdWithHighlightedPrefix; + + fn extract(&self, context: &CommitOrChangeId) -> Self::Output { + let hex = context.hex(); + let (prefix, rest) = extract_entire_prefix_and_trimmed_tail( + &hex, + context + .repo + .shortest_unique_id_prefix_len(context.as_bytes()), + 12, + ); + IdWithHighlightedPrefix { + prefix: prefix.to_string(), + rest: rest.to_string(), + } + } +} + pub struct CommitOrChangeIdShort; impl TemplateProperty> for CommitOrChangeIdShort {