| 818 | } |
| 819 | |
| 820 | void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width, |
| 821 | const char *s) |
| 822 | { |
| 823 | size_t slen = strlen(s); |
| 824 | int display_len = utf8_strnwidth(s, slen, 0); |
| 825 | int utf8_compensation = slen - display_len; |
| 826 | |
| 827 | if (display_len >= width) { |
| 828 | strbuf_addstr(buf, s); |
| 829 | return; |
| 830 | } |
| 831 | |
| 832 | if (position == ALIGN_LEFT) |
| 833 | strbuf_addf(buf, "%-*s", width + utf8_compensation, s); |
| 834 | else if (position == ALIGN_MIDDLE) { |
| 835 | int left = (width - display_len) / 2; |
| 836 | strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s); |
| 837 | } else if (position == ALIGN_RIGHT) |
| 838 | strbuf_addf(buf, "%*s", width + utf8_compensation, s); |
| 839 | } |