pytermgui: [BUG] SIGINT exits leave terminal in dirty state

Describe the bug When sending SIGINT (ctrl-c) to exit ptg the terminal continues to capture mouse input until the reset command is issued.

To Reproduce

  1. Install pytermgui
  2. Run ptg
  3. Exit with ctrl-c
  4. Push mouse buttons

Expected behavior Mouse events are not captured.

Screenshots ASCII cast here

System information

$ ptg --version
PyTermGUI v1.1.0
Python: 3.9.2
Platform: Linux-5.10.0-10-amd64-x86_64-with-glibc2.31
Git commit: 47bed90

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 36 (19 by maintainers)

Commits related to this issue

Most upvoted comments

Just now! Sorry about the delay, I’m currently on vacation.

P.S. Make sure you do pip install -U pytermgui to make sure it updates!

@bczsalba I noticed that, in the report_mouse function, the hover event corresponds to control code 1003. According to the XTerm documentation, this enables both hover and button press tracking, while 1002 (press_hold) enables just button press tracking.

However, by default, WindowManager.run enables both press_hold and hover events. Enabling/disabling both of these at the same time seems to cause the dirty terminal state in my case, as when I call run myself only specifying ["hover"] for the mouse_events argument, the issue doesn’t appear anymore.

This issue was happening independently of the one @stewartad described-- even when removing the blocking input(event), button presses were still being printed as seen in OP’s case, requiring the above to fix.

All of this was in GNOME Terminal 3.44.0.

I was also encountering this bug on the latest release (7.4.0). I noticed that it only occurs when I install the library from PyPI, and not when I install the library from Github. I diffed the files to see if there was anything obvious and found this in context_managers.py

From PyPI:

@contextmanager
def mouse_handler(
    events: list[str], method: str = "decimal_xterm"
) -> Generator[MouseTranslator, None, None]:

    event = None
    try:
        for event in events:
            report_mouse(event, method=method)

        yield lambda code: translate_mouse(code, method=method)

    finally:
        input(event)
        if event is not None:
            report_mouse(event, method=method, stop=True)

From Github:

@contextmanager
def mouse_handler(
    events: list[str], method: str = "decimal_xterm"
) -> Generator[MouseTranslator, None, None]:

    event = None
    try:
        for event in events:
            report_mouse(event, method=method)

        yield lambda code: translate_mouse(code, method=method)

    finally:
        if event is not None:
            report_mouse(event, method=method, stop=True)

The code is blocking on the input(event) call, and if you Ctrl+C here, then the cleanup code below will not run, leaving the terminal dirty, but any other keypress will exit normally.

I am experiencing similar issue, but terminal prints symbols on mouse movement

Well I’m certainly glad to hear that! I have no idea what the issue was, what fixed it or when it was fixed but I’m glad it was!

Thank you for the report!