From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Subject: [PATCH 11/12] pretty: support truncating in %>, %< and %>< Date: Sat, 16 Mar 2013 09:24:42 +0700 Message-ID: <1363400683-14813-12-git-send-email-pclouds@gmail.com> References: <1363400683-14813-1-git-send-email-pclouds@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Junio C Hamano , =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Sat Mar 16 03:26:30 2013 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UGgpG-00039h-Ge for gcvg-git-2@plane.gmane.org; Sat, 16 Mar 2013 03:26:26 +0100 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932300Ab3CPC0A convert rfc822-to-quoted-printable (ORCPT ); Fri, 15 Mar 2013 22:26:00 -0400 Received: from mail-pb0-f46.google.com ([209.85.160.46]:34350 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932223Ab3CPCZ7 (ORCPT ); Fri, 15 Mar 2013 22:25:59 -0400 Received: by mail-pb0-f46.google.com with SMTP id uo15so4573991pbc.19 for ; Fri, 15 Mar 2013 19:25:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=G5QVTjPhrZxd1JoLa38wsN9HmoYT04mDEiQlowG4Bs0=; b=VtOQ+I3tqTLDob6TP+QL7QLjeQ4eUuehKL2Xv3JqJFAeZgAzxWTO0Q1/n16kAB9Xu5 1Xh4ZAUocmKTKYqfIv5XrFLkHH/ks6kLNIvqScRL1TrOWUhHS8FDV1UO++WvvHfgifZj AvnJmlupRXOpmZoDD1w/418T5Z5W4YSOeQDMUxyF73Nqw9+Ko/GqdP4L+yvRVEQa0DdD hAYYQF2G3ga3HbFu39aJ1pKboazyfNl3A1e6ucgGNVvZexP0bUl3ey0iv59BG9Vq1f2t a1227+spdpfdGSHABUEij1pj2vWVo4jLtkIB2krXtkdWQeQySmbj3UJpkwiexvbNEQKX g5dQ== X-Received: by 10.68.196.168 with SMTP id in8mr21104302pbc.61.1363400759042; Fri, 15 Mar 2013 19:25:59 -0700 (PDT) Received: from lanh ([115.74.58.84]) by mx.google.com with ESMTPS id i10sm11524390pbd.1.2013.03.15.19.25.55 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 15 Mar 2013 19:25:58 -0700 (PDT) Received: by lanh (sSMTP sendmail emulation); Sat, 16 Mar 2013 09:25:53 +0700 X-Mailer: git-send-email 1.8.2.83.gc99314b In-Reply-To: <1363400683-14813-1-git-send-email-pclouds@gmail.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: %>(N,trunc) truncates the righ part after N columns and replace the last two letters with "..". ltrunc does the same on the left. mtrunc cuts the middle out. Signed-off-by: Nguy=E1=BB=85n Th=C3=A1i Ng=E1=BB=8Dc Duy --- Documentation/pretty-formats.txt | 6 +++-- pretty.c | 51 ++++++++++++++++++++++++++++++++= +++++--- utf8.c | 46 ++++++++++++++++++++++++++++++++= ++++ utf8.h | 2 ++ 4 files changed, 100 insertions(+), 5 deletions(-) diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-fo= rmats.txt index 87ca2c4..17f82f4 100644 --- a/Documentation/pretty-formats.txt +++ b/Documentation/pretty-formats.txt @@ -162,8 +162,10 @@ The placeholders are: - '%x00': print a byte from a hex code - '%w([[,[,]]])': switch line wrapping, like the -w option = of linkgit:git-shortlog[1]. -- '%<()': make the next placeholder take at least N columns, - padding spaces on the right if necessary +- '%<([,trunc|ltrunc|mtrunc])': make the next placeholder take at + least N columns, padding spaces on the right if necessary. + Optionally truncate at the beginning (ltrunc), the middle (mtrunc) + or the end (trunc) if the output is longer than N columns. - '%<|()': make the next placeholder take at least until Nth columns, padding spaces on the right if necessary - '%>()', '%>|()': similar to '%<(padding =3D to_column ? -width : width; c->flush_type =3D flush_type; + + if (*end =3D=3D ',') { + start =3D end + 1; + end =3D strchr(start, ')'); + if (!end || end =3D=3D start) + return 0; + if (!prefixcmp(start, "trunc)")) + c->truncate =3D trunc_right; + else if (!prefixcmp(start, "ltrunc)")) + c->truncate =3D trunc_left; + else if (!prefixcmp(start, "mtrunc)")) + c->truncate =3D trunc_middle; + else + return 0; + } else + c->truncate =3D trunc_none; + return end - placeholder + 1; } return 0; @@ -1335,9 +1360,29 @@ static size_t format_and_pad_commit(struct strbu= f *sb, /* in UTF-8 */ total_consumed++; } len =3D utf8_strnwidth(local_sb.buf, -1, 1); - if (len > padding) + if (len > padding) { + switch (c->truncate) { + case trunc_left: + strbuf_utf8_replace(&local_sb, + 0, len - (padding - 2), + ".."); + break; + case trunc_middle: + strbuf_utf8_replace(&local_sb, + padding / 2 - 1, + len - (padding - 2), + ".."); + break; + case trunc_right: + strbuf_utf8_replace(&local_sb, + padding - 2, len - (padding - 2), + ".."); + break; + case trunc_none: + break; + } strbuf_addstr(sb, local_sb.buf); - else { + } else { int sb_len =3D sb->len, offset =3D 0; if (c->flush_type =3D=3D flush_left) offset =3D padding - len; diff --git a/utf8.c b/utf8.c index 9d98043..766df80 100644 --- a/utf8.c +++ b/utf8.c @@ -421,6 +421,52 @@ void strbuf_add_wrapped_bytes(struct strbuf *buf, = const char *data, int len, free(tmp); } =20 +void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width, + const char *subst) +{ + struct strbuf sb_dst =3D STRBUF_INIT; + char *src =3D sb_src->buf; + char *end =3D src + sb_src->len; + char *dst; + int w =3D 0, subst_len =3D 0; + + if (subst) + subst_len =3D strlen(subst); + strbuf_grow(&sb_dst, sb_src->len + subst_len); + dst =3D sb_dst.buf; + + while (src < end) { + char *old; + size_t n; + + while ((n =3D display_mode_esc_sequence_len(src))) { + memcpy(dst, src, n); + src +=3D n; + dst +=3D n; + } + + old =3D src; + n =3D utf8_width((const char**)&src, NULL); + if (!src) /* broken utf-8, do nothing */ + return; + if (n && w >=3D pos && w < pos + width) { + if (subst) { + memcpy(dst, subst, subst_len); + dst +=3D subst_len; + subst =3D NULL; + } + w +=3D n; + continue; + } + memcpy(dst, old, src - old); + dst +=3D src - old; + w +=3D n; + } + strbuf_setlen(&sb_dst, dst - sb_dst.buf); + strbuf_attach(sb_src, strbuf_detach(&sb_dst, NULL), + sb_dst.len, sb_dst.alloc); +} + int is_encoding_utf8(const char *name) { if (!name) diff --git a/utf8.h b/utf8.h index 99db3e0..faf2f91 100644 --- a/utf8.h +++ b/utf8.h @@ -15,6 +15,8 @@ void strbuf_add_wrapped_text(struct strbuf *buf, const char *text, int indent, int indent2, int width); void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, in= t len, int indent, int indent2, int width); +void strbuf_utf8_replace(struct strbuf *sb, int pos, int width, + const char *subst); =20 #ifndef NO_ICONV char *reencode_string_iconv(const char *in, size_t insz, --=20 1.8.2.83.gc99314b