[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 17:57:30 +1100
On 29 October 2017 at 17:22, Sean Conner <email@example.com> wrote:
> It's close to what I want, but not fully there. The function u8_width()
> is close, but the documentation states, "[t]his function ignores control
> characters in the string," which to me, says it will treat this:
> Стоял он, дум\27[31;41m великих полн
> Стоял он, дум[31;41m великих полн
> and not
> Стоял он, дум великих полн
> which is what I'm trying to do. I'm also having to treat HT (the tab
> character) specially because it can be anywhere from 0 to 8 terminal
> positions, depending upon where it occurs in the string (I don't want it
> ignored). I'm pretty sure I can work around control codes and escape
> sequences using u8_width().
> -spc (So I'm still interested in non-LPeg solutions to this)
Generally you're going to want to pre-process (or post-process)
control characters yourself. e.g. escape them.
Otherwise a user's string may contain the escape code for "go to start
of line", and nothing ends up how you expected. (or worse: clear
A notable behaviour is that bash will count any chars between \1 and
\2 as zero width for the purpose of a PS1 prompt.
I take advantage of that here:
(sorry this is an old version, I haven't uploaded a more recent copy anywhere)
Tabs are a bit of an interesting case, as they generally have the
semantics of "go to next 8-aligned column".
However in a richer terminal ui, a text box may be offset in the x
coordinate, or you may want the facility to provide custom tab-stops.
So generally I end up handling tabs and such explicitly.