pyte icon indicating copy to clipboard operation
pyte copied to clipboard

Feature request: support for SGR subparameters

Open jfly opened this issue 1 year ago • 0 comments

pyte current supports setting foreground/background color, but it only supports ; delimited parameters. My understanding is that this is a legacy of xterm mis-interpreting some ancient specification:

  • https://invisible-island.net/xterm/xterm.faq.html#color_by_number

    We used semicolon (like other SGR parameters) for separating the R/G/B values in the escape sequence, since a copy of ITU T.416 (ISO-8613-6) which presumably clarified the use of colon for this feature was costly.

  • Additional context:
    • https://news.ycombinator.com/item?id=35138390
    • https://wezfurlong.org/wezterm/escape-sequences.html#graphic-rendition-sgr

In other words, there are 2 ways set the foreground color of text (this applies to other SGR parameters, I'm just picking foreground color to be concrete):

  1. echo -e "\x1b[38:5:5mfoo\x1b[mbar": Uses colons to delimit context/subparameters
  2. echo -e "\x1b[38;5;5mfoo\x1b[mbar": Uses semicolons. Creates ambiguity between parameters and context/subparameters. Discouraged.

Let's see how pyte handles these things. Here's my test script:

test.py
import sys
import pyte

def show_chars(screen: pyte.Screen, properties: list[str]):
    for line in range(screen.lines):
        for column in range(screen.columns):
            char = screen.buffer[line][column]
            if char.data != " ":
                props = { prop: getattr(char, prop) for prop in properties }
                pretty_props = ", ".join(f"{k}={v}" for k, v in props.items())
                print(f"{char.data}\t{pretty_props}")

screen = pyte.Screen(columns=30, lines=5)
stream = pyte.Stream(screen)

stream.feed(sys.argv[1])
show_chars(screen, properties=['fg'])

pyte 0.8.2

semicolon delimited

This works as expected:

$ python test.py $'\x1b[38;5;5mfoo\x1b[mbar'
f	fg=cd00cd
o	fg=cd00cd
o	fg=cd00cd
b	fg=default
a	fg=default
r	fg=default

colon delimited

pyte does not "understand" the colors, and even worse, there's unnecessary spew printed. The spew will be addressed by my upcoming changes for https://github.com/selectel/pyte/issues/178.

$ python test.py $'\x1b[38:5:5mfoo\x1b[mbar'
5	fg=default
:	fg=default
5	fg=default
m	fg=default
f	fg=default
o	fg=default
o	fg=default
b	fg=default
a	fg=default
r	fg=default

jfly avatar Oct 08 '24 17:10 jfly