[PATCH v2 01/10] ref-filter: add %(refname:shortalign=X) option
- From
Karthik Nayak <karthik.188@gmail.com>
- Date
- Jul 9, 2015, 10:27 UTC
- Message-ID
- <1436437671-25600-1-git-send-email-karthik.188@gmail.com>
- In-Reply-To
- <CAOLa=ZQyHwza6L9r6iFX1GkVrC+F-XNwegO=bGyxafjY3JoYpw@mail.gmail.com>
Add support for %(refname:shortalign=X) where X is a number. This will print a shortened refname aligned to the left followed by spaces for a total length of X characters. If X is less than the shortened refname size, the entire shortened refname is printed.
Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> --- ref-filter.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/ref-filter.c b/ref-filter.c index dd0709d..3098497 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -10,6 +10,7 @@ #include "quote.h" #include "ref-filter.h" #include "revision.h" +#include "utf8.h" typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type; @@ -695,7 +696,23 @@ static void populate_value(struct ref_array_item *ref) int num_ours, num_theirs; formatp++; - if (!strcmp(formatp, "short")) + if (starts_with(formatp, "shortalign=")) { + const char *valp, *short_refname = NULL; + int val, len; + + skip_prefix(formatp, "shortalign=", &valp); + val = atoi(valp); + refname = short_refname = shorten_unambiguous_ref(refname, + warn_ambiguous_refs); + len = utf8_strwidth(refname); + if (val > len) { + struct strbuf buf = STRBUF_INIT; + strbuf_addstr(&buf, refname); + strbuf_addchars(&buf, ' ', val - len); + free((char *)short_refname); + refname = strbuf_detach(&buf, NULL); + } + } else if (!strcmp(formatp, "short")) refname = shorten_unambiguous_ref(refname, warn_ambiguous_refs); else if (!strcmp(formatp, "track") &&
-- 2.4.5