Re: [PATCH 01/11] graph: automatically track visible width of `strbuf`
- From
Denton Liu <liu.denton@gmail.com>
- Date
- Oct 11, 2019, 05:01 UTC
- Message-ID
- <20191011050111.GA94866@generichostname>
- In-Reply-To
- <xmqq7e5cjbwj.fsf@gitster-ct.c.googlers.com>
On Fri, Oct 11, 2019 at 10:42:20AM +0900, Junio C Hamano wrote:
Show 23 quoted lines
> Denton Liu <liu.denton@gmail.com> writes:
>
> > static int calculate_width(const struct strbuf *row)
> > {
> > int in_termcode = 0;
> > int width = 0;
> > int i;
> >
> > for (i = 0; i < row.len; i++) {
> > if (row.buf[i] == '\033')
> > in_termcode = 1;
> >
> > if (!in_termcode)
> > width++;
> > else if (row.buf[i] == 'm')
> > in_termcode = 0;
> > }
> > }
>
> Not every byte that is outside the escape sequence contributes to
> one display columns. You would want to take a look at utf8_width()
> for inspiration.
> Heh, I guess you're right. Looking right below the definition of utf8_width, I realised we have the utf8_strnwidth function. We should be able to just call
utf8_strnwidth(row.buf, row.len, 1);