lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 29 October 2017 at 15:38, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Dirk Laurie once stated:
>>
>> Put another way: the issue is not what can you do with LPeg, it is
>> what can you do that you can't do with Lua patterns, and whether
>> that extra is sufficiently common to justify adding LPeg to Lua.
>
>   I think I've come across something else that LPeg can easily do (for
> various values of "easily") that would be difficult to do with Lua patterns.
> The background for the project is to output text to fit the width of a
> terminal, and said output contains a mixture of UTF-8 and terminal escape
> codes, for example:
>
>         Стоял он, дум\27[31;41m великих полн"
>
> That string is 56 bytes long, and contains 26 printable glyphs.  If I wanted
> to print out only 20 glyphs (because that's the width of our terminal, or
> all that's left on the current line of our terminal), how do I calculate how
> many bytes to write?

Not all glyphs take up a single terminal cell. Some take no columns,
some take multiple columns.
To solve this, you need a library with knowledge of these widths.
One such library that is available on most computers is libunistring.
See https://www.gnu.org/software/libunistring/manual/libunistring.html#uniwidth_002eh
One level higher is the unistring function u8_width_linebreaks that
tells you where to insert line breaks for a given piece of text to fit
in a terminal.
https://www.gnu.org/software/libunistring/manual/libunistring.html#unilbrk_002eh

It's one of the reasons I wrote https://github.com/daurnimator/lua-unistring