Skip to content

Feature request: support for SGR subparameters #179

@jfly

Description

@jfly

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:

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
#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions