[PATCH v3 2/2] blame: use a helper to get suitable blame_date_width

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When show date in relative date format for git-blame, the max display
width of datetime is set as the length of the string "Thu Oct 19
16:00:04 2006 -0700" (30 characters long).  But actually the max width
for C locale is only 22 (the length of string "x years, xx months ago").
And for other locale, it maybe smaller.  E.g. For Chinese locale, only
needs a half (16-character width).

Add a helper function date_relative_maxwidth() to date.c, which returns
the suitable display width for the relative date field in different
locale.

Suggested-by: Junio C Hamano <gitster@xxxxxxxxx>
Signed-off-by: Jiang Xin <worldhello.net@xxxxxxxxx>
---
 builtin/blame.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

diff --git a/builtin/blame.c b/builtin/blame.c
index 35e95db..478f739 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -2229,6 +2229,67 @@ static int blame_move_callback(const struct option *option, const char *arg, int
 	return 0;
 }
 
+static int date_relative_maxwidth(void)
+{
+	struct strbuf buf = STRBUF_INIT, years_sb = STRBUF_INIT;
+	static int maxwidth = 0;
+	int width;
+
+	if (maxwidth)
+		return maxwidth;
+
+	strbuf_addf(&buf, _("in the future"));
+	maxwidth = utf8_strwidth(buf.buf);
+
+	strbuf_reset(&buf);
+	strbuf_addf(&buf, Q_("%lu second ago", "%lu seconds ago", 89L), 89L);
+	width = utf8_strwidth(buf.buf);
+	maxwidth = (maxwidth < width) ? width : maxwidth;
+
+	strbuf_reset(&buf);
+	strbuf_addf(&buf, Q_("%lu minute ago", "%lu minutes ago", 89L), 89L);
+	width = utf8_strwidth(buf.buf);
+	maxwidth = (maxwidth < width) ? width : maxwidth;
+
+	strbuf_reset(&buf);
+	strbuf_addf(&buf, Q_("%lu hour ago", "%lu hours ago", 35L), 35L);
+	width = utf8_strwidth(buf.buf);
+	maxwidth = (maxwidth < width) ? width : maxwidth;
+
+	strbuf_reset(&buf);
+	strbuf_addf(&buf, Q_("%lu day ago", "%lu days ago", 13L), 13L);
+	width = utf8_strwidth(buf.buf);
+	maxwidth = (maxwidth < width) ? width : maxwidth;
+
+	strbuf_reset(&buf);
+	strbuf_addf(&buf, Q_("%lu week ago", "%lu weeks ago", 10L), 10L);
+	width = utf8_strwidth(buf.buf);
+	maxwidth = (maxwidth < width) ? width : maxwidth;
+
+	strbuf_reset(&buf);
+	strbuf_addf(&buf, Q_("%lu month ago", "%lu months ago", 12L), 12L);
+	width = utf8_strwidth(buf.buf);
+	maxwidth = (maxwidth < width) ? width : maxwidth;
+
+	strbuf_addf(&years_sb, Q_("%lu year", "%lu years", 4L), 4L);
+	strbuf_reset(&buf);
+	strbuf_addf(&buf,
+		Q_("%s, %lu month ago", "%s, %lu months ago", 11L),
+		years_sb.buf, 11L);
+	width = utf8_strwidth(buf.buf);
+	maxwidth = (maxwidth < width) ? width : maxwidth;
+
+	strbuf_reset(&buf);
+	strbuf_addf(&buf, Q_("%lu year ago", "%lu years ago", 9999L), 9999L);
+	width = utf8_strwidth(buf.buf);
+	maxwidth = (maxwidth < width) ? width : maxwidth;
+
+	strbuf_release(&years_sb);
+	strbuf_release(&buf);
+
+	return maxwidth;
+}
+
 int cmd_blame(int argc, const char **argv, const char *prefix)
 {
 	struct rev_info revs;
@@ -2338,7 +2399,8 @@ parse_done:
 		blame_date_width = sizeof("2006-10-19");
 		break;
 	case DATE_RELATIVE:
-		/* "normal" is used as the fallback for "relative" */
+		blame_date_width = date_relative_maxwidth() + 1; /* add the null */
+		break;
 	case DATE_LOCAL:
 	case DATE_NORMAL:
 		blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
-- 
1.9.2.476.ga74def0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]