pytermgui icon indicating copy to clipboard operation
pytermgui copied to clipboard

[BUG] Non-interpreted ANSI sequence printed in the terminal

Open pawamoy opened this issue 3 years ago • 9 comments
trafficstars

Describe the bug For each invocation of ptg (on the CLI or programmatically), some ANSI sequence is printed on the console:

CLI:

% pdm run ptg --version
^[[4;1890;1072tPyTermGUI version 6.2.1

System details:
    Python version: 3.8.13
    $TERM:          xterm-256color
    $COLORTERM:     truecolor
    Color support:  ColorSystem.TRUE
    OS Platform:    Linux-5.17.9-arch1-1-x86_64-with-glibc2.35

Programmatically:

% make docs-serve
INFO     -  Building documentation...
INFO     -  Cleaning site directory
^[[4;1890;1072tINFO     -  Documentation built in 5.20 seconds
INFO     -  [13:37:05] Watching paths for changes: 'docs', 'mkdocs.yml', 'README.md', 'CONTRIBUTING.md', 'CHANGELOG.md',
            'src/markdown_exec'
INFO     -  [13:37:05] Serving on http://127.0.0.1:8000/markdown-exec/
INFO     -  [13:37:06] Browser connected: http://localhost:8000/markdown-exec/gallery/
^CINFO     -  Shutting down...

Expected behavior No ANSI sequence printed.

Possible cause Maybe PDM plays a role. I'll try without it.

pawamoy avatar May 26 '22 11:05 pawamoy

The sequence actually comes from your terminal! Basically, PTG writes a pixel dimension request, and the terminal has some time (I think 0.1 seconds) to respond. The timing is there to solve an issue where PTG would try to catch sequences that are never written, so it would just get stuck on an endless getch call.

The sensible thing to do here would be the only query the dimensions on demand, so it's not done when not needed. The timeout could also be a bit larger.

bczsalba avatar May 26 '22 11:05 bczsalba

I see, thanks for the explanation :slightly_smiling_face: I'd be totally fine with being able to configure the timeout through an env var if this is something you'd consider.

pawamoy avatar May 26 '22 12:05 pawamoy

Could you check on the latest commit? This should be fixed now.

bczsalba avatar May 29 '22 10:05 bczsalba

Just checked (pip install using master branch, i.e. commit e523f7091fddd1b9bf7aed781a39dcb72cf60bf7): seems to be fixed indeed! Thanks!

pawamoy avatar May 29 '22 11:05 pawamoy

Hello, I'm having a similar issue again. When I run the following snippet in a subprocess, it prints an ANSI sequence and then gets stuck:

from io import StringIO
import pytermgui as ptg

terminal = ptg.Terminal(stream=StringIO(), size=(80, 16))
ptg.set_global_terminal(terminal)
with terminal.record() as recorder:
    recorder.write(ptg.tim.parse(ptg.highlight_python(code)))
print("before svg")
svg = recorder.export_svg(inline_styles=True)
print("after svg")

Output when running it:

before svg
^[]10;rgb:e4e4/d9d9/c6c6^G

Note that this happens in a mkdocs subprocess, and then in an exec call (I'm working on markdown-exec :wink:). Not sure if it really matters.

Do you know to what corresponds this ANSI sequence ^[]10;rgb:e4e4/d9d9/c6c6^G? (and why it's printed on screen?)

pawamoy avatar Sep 01 '22 16:09 pawamoy

Aw heck. The sequence is the response to use querying the terminal's foreground color, which we need to create an accurate screenshot. Gonna try to look into why the response doesn't get captured.

bczsalba avatar Sep 01 '22 19:09 bczsalba

Hey, is this still an issue? I'm not sure if anything related has changed since then, but in case things are still broken I can look into it.

bczsalba avatar Jan 31 '23 12:01 bczsalba

Let me check, I had disabled pytermgui in the meantime :)

pawamoy avatar Jan 31 '23 21:01 pawamoy

Still happens, but only when I run the mkdocs command through my task runner (duty) it seems (didn't test extensively), so don't bother too much. I'll try to isolate the real cause when I get some time :slightly_smiling_face:

pawamoy avatar Jan 31 '23 21:01 pawamoy

Ever figure it out? :)

bczsalba avatar Feb 24 '24 13:02 bczsalba

Nope, sorry :confused:

pawamoy avatar Feb 24 '24 14:02 pawamoy

That's alright! Let me know if you ever run into it again.

bczsalba avatar Feb 24 '24 17:02 bczsalba

So it's still happening. The crux of the issue is probably that I'm capturing the output, preventing PyTermGUI to read it it itself when querying the terminal for the foreground color, so it blocks. The sequence I'm seeing: ^[]11;rgb:1212/1919/0d0d^[\. I'm capturing output at the file descriptor level: https://github.com/pawamoy/failprint/blob/3d1317dc9f663719bab381c1f39fe54cda308d3f/src/failprint/capture.py#L78.

Ideally, PyTermGUI wouldn't block thanks to a read timeout or something :slightly_smiling_face: I'm trying to think of something for disabling it when capturing the output (when I'm checking if the docs build correctly), but I don't see yet how I could disable just a paragraph in one my Markdown pages :thinking:

pawamoy avatar Jun 13 '24 12:06 pawamoy

OK found a workaround, I'm adding this at the top of the PyTermGUI snippet:

import os  # markdown-exec: hide
for envvar in ("PYTHON_VERSIONS", "CI", "MULTIRUN"):  # markdown-exec: hide
    if envvar in os.environ:  # markdown-exec: hide
        print(  # markdown-exec: hide
            "PyTermGUI blocks when querying the terminal foreground color with an ANSI sequence "  # markdown-exec: hide
            "because we capture the output ourselves through `failprint`, "  # markdown-exec: hide
            "so this gallery example is disabled in CI.",  # markdown-exec: hide
        )  # markdown-exec: hide
        raise SystemExit(0)  # markdown-exec: hide

pawamoy avatar Jun 13 '24 13:06 pawamoy