diff options
author | Karthik Nayak <karthik.188@gmail.com> | 2015-09-10 17:48:19 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2015-09-17 19:02:48 +0200 |
commit | 110dcda50d5ddaf3557666eea3b012a6ccc74dce (patch) | |
tree | f7691945290bc4f51be7ca7dd073fbd8ebf8535e /utf8.c | |
parent | ref-filter: introduce ref_formatting_state and ref_formatting_stack (diff) | |
download | git-110dcda50d5ddaf3557666eea3b012a6ccc74dce.tar.xz git-110dcda50d5ddaf3557666eea3b012a6ccc74dce.zip |
utf8: add function to align a string into given strbuf
Add strbuf_utf8_align() which will align a given string into a strbuf
as per given align_type and width. If the width is greater than the
string length then no alignment is performed.
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'utf8.c')
-rw-r--r-- | utf8.c | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len) *text += strlen(utf8_bom); return 1; } + +void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width, + const char *s) +{ + int slen = strlen(s); + int display_len = utf8_strnwidth(s, slen, 0); + int utf8_compensation = slen - display_len; + + if (display_len >= width) { + strbuf_addstr(buf, s); + return; + } + + if (position == ALIGN_LEFT) + strbuf_addf(buf, "%-*s", width + utf8_compensation, s); + else if (position == ALIGN_MIDDLE) { + int left = (width - display_len) / 2; + strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s); + } else if (position == ALIGN_RIGHT) + strbuf_addf(buf, "%*s", width + utf8_compensation, s); +} |