reflow icon indicating copy to clipboard operation
reflow copied to clipboard

OSC sequences are mis-counted in `PrintableRuneWidth`

Open rwe opened this issue 2 years ago • 0 comments

OSC sequences are sequences beginning with OSC (ESC ]) and ending in ST (ESC ], or BEL which is 0x07). Note that that's a right bracket, not a left one like CSI sequences.

Those sequences should be ignored, but currently they entirely misinterpreted.

Although PrintableRuneWidth ignores characters after ESC (0x1B), isTerminator does not correctly recognize either variant of ST: (ESC \\, or BEL).

In the former, it will mis-count the slash after the ESC; and the latter fails because BEL is not matched as a sequence terminator.

Specifically, this breaks text using OSC8 for describing URL links. OSC8 is a mechanism for describing URI/URL links in terminals, described very digestibly here: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#the-escape-sequence

It's widely supported by many modern terminals (https://github.com/Alhadis/OSC8-Adoption/), and those that don't support it fall back to omitting the URL part. (Same text, just not clickable).

On a whim, I wanted to make the GitHub cli (https://github.com/cli/cli) support those links, but hit a stumbling block: its tableprinter measures column text width ultimately through muesli/reflow.

It could be fixed in their intermediate go-gh package, but it seems like this is the "right" place to fix it, or at least to have the issue logged.

rwe avatar Oct 02 '23 23:10 rwe