[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: LPeg can do what Lua patterns can't (was Re: lpeg as a part of lua)
- From: Daurnimator <quae@...>
- Date: Sun, 29 Oct 2017 15:45:39 +1100
On 29 October 2017 at 15:38, Sean Conner <email@example.com> 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.
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.
It's one of the reasons I wrote https://github.com/daurnimator/lua-unistring