All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v2] branch -v: align even when branch names are in UTF-8
Date: Sun, 26 Aug 2012 01:17:12 +0700	[thread overview]
Message-ID: <1345918632-17756-1-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <7vvcg8yzgx.fsf@alter.siamese.dyndns.org>

Branch names are usually in ASCII so they are not the problem. The
problem most likely comes from "(no branch)" translation, which is in
UTF-8 and makes length calculation just wrong.

Update document to mention the fact that we may want ref names in
UTF-8. Encodings that produce invalid UTF-8 are safe as utf8_strwidth()
falls back to strlen(). The ones that incidentally produce valid UTF-8
sequences will cause misalignment.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 On Sat, Aug 25, 2012 at 12:25 AM, Junio C Hamano <gitster@pobox.com> wrote:
 > I agree with all of the above, but shouldn't you be computing the
 > "maxwidth" based on the strwidth in the first place?  The use of
 > maxwidth in strbuf_addf() here clearly wants "we know N columns is
 > sufficient to show all output items, so pad the string to N columns"
 > here.  Looking for assignment "item.len = xxx" in the same file
 > shows these are computed as byte length, so you are offsetting off
 > of an incorrectly computed value.
 >
 > Giving fewer padding bytes when showing a string that will occupy
 > fewer columns than it has bytes is independently necessary, once we
 > have the correct maxwidth that is computed in terms of the strwidth,
 > so this patch is not wrong per-se, but it is incomplete without a
 > correct maxwidth, no?

 Yes. This fixes that and also mentions about ref names in utf-8.

 Documentation/revisions.txt |  2 ++
 builtin/branch.c            | 12 +++++++-----
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/Documentation/revisions.txt b/Documentation/revisions.txt
index dc0070b..175d397 100644
--- a/Documentation/revisions.txt
+++ b/Documentation/revisions.txt
@@ -55,6 +55,8 @@ when you run `git cherry-pick`.
 +
 Note that any of the 'refs/*' cases above may come either from
 the '$GIT_DIR/refs' directory or from the '$GIT_DIR/packed-refs' file.
+While the ref name encoding is unspecified, UTF-8 is prefered as
+some output processing may assume ref names in UTF-8.
 
 '<refname>@\{<date>\}', e.g. 'master@\{yesterday\}', 'HEAD@\{5 minutes ago\}'::
   A ref followed by the suffix '@' with a date specification
diff --git a/builtin/branch.c b/builtin/branch.c
index 0e060f2..73ff7e7 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -17,6 +17,7 @@
 #include "revision.h"
 #include "string-list.h"
 #include "column.h"
+#include "utf8.h"
 
 static const char * const builtin_branch_usage[] = {
 	"git branch [options] [-r | -a] [--merged | --no-merged]",
@@ -354,7 +355,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	newitem->name = xstrdup(refname);
 	newitem->kind = kind;
 	newitem->commit = commit;
-	newitem->len = strlen(refname);
+	newitem->len = utf8_strwidth(refname);
 	newitem->dest = resolve_symref(orig_refname, prefix);
 	/* adjust for "remotes/" */
 	if (newitem->kind == REF_REMOTE_BRANCH &&
@@ -490,11 +491,12 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 	}
 
 	strbuf_addf(&name, "%s%s", prefix, item->name);
-	if (verbose)
+	if (verbose) {
+		int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf);
 		strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
-			    maxwidth, name.buf,
+			    maxwidth + utf8_compensation, name.buf,
 			    branch_get_color(BRANCH_COLOR_RESET));
-	else
+	} else
 		strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color),
 			    name.buf, branch_get_color(BRANCH_COLOR_RESET));
 
@@ -533,7 +535,7 @@ static void show_detached(struct ref_list *ref_list)
 	if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
 		struct ref_item item;
 		item.name = xstrdup(_("(no branch)"));
-		item.len = strlen(item.name);
+		item.len = utf8_strwidth(item.name);
 		item.kind = REF_LOCAL_BRANCH;
 		item.dest = NULL;
 		item.commit = head_commit;
-- 
1.7.12.rc2.18.g61b472e

  reply	other threads:[~2012-08-25 18:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-24 14:17 [PATCH] branch -v: align even when the first column is in UTF-8 Nguyễn Thái Ngọc Duy
2012-08-24 17:25 ` Junio C Hamano
2012-08-25 18:17   ` Nguyễn Thái Ngọc Duy [this message]
2012-08-26 18:04     ` [PATCH v2] branch -v: align even when branch names are " Junio C Hamano
2012-08-25 10:48 ` [PATCH] branch -v: align even when the first column is " Erik Faye-Lund
2012-08-25 11:19   ` Nguyen Thai Ngoc Duy
2012-08-26 18:28   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1345918632-17756-1-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.