diff options
author | Nguyễn Thái Ngọc Duy <pclouds@gmail.com> | 2013-04-19 09:08:51 +1000 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2013-04-18 16:28:29 -0700 |
commit | a7f01c6b4ddf9f2f6bdcb23fc7afb56695807cba (patch) | |
tree | bbd2ecfc44a0e3349d2e419f379e653a008dc00f /utf8.c | |
parent | a57523428b4c08795e793356e12afc3de812f84a (diff) | |
download | git-a7f01c6b4ddf9f2f6bdcb23fc7afb56695807cba.tar.gz |
pretty: support truncating in %>, %< and %><
%>(N,trunc) truncates the right 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ễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'utf8.c')
-rw-r--r-- | utf8.c | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -421,6 +421,52 @@ void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len, free(tmp); } +void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width, + const char *subst) +{ + struct strbuf sb_dst = STRBUF_INIT; + char *src = sb_src->buf; + char *end = src + sb_src->len; + char *dst; + int w = 0, subst_len = 0; + + if (subst) + subst_len = strlen(subst); + strbuf_grow(&sb_dst, sb_src->len + subst_len); + dst = sb_dst.buf; + + while (src < end) { + char *old; + size_t n; + + while ((n = display_mode_esc_sequence_len(src))) { + memcpy(dst, src, n); + src += n; + dst += n; + } + + old = src; + n = utf8_width((const char**)&src, NULL); + if (!src) /* broken utf-8, do nothing */ + return; + if (n && w >= pos && w < pos + width) { + if (subst) { + memcpy(dst, subst, subst_len); + dst += subst_len; + subst = NULL; + } + w += n; + continue; + } + memcpy(dst, old, src - old); + dst += src - old; + w += 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) |