bczsalba / pytermgui

Python TUI framework with mouse support, modular widget system, customizable and rapid terminal markup language and more!
https://ptg.bczsalba.com
MIT License
2.23k stars 54 forks source link

[BUG] documentation example hangs on exit, leaves terminal in dirty state. #13

Closed anthonyjmartinez closed 2 years ago

anthonyjmartinez commented 2 years ago

Describe the bug When running the following documentation example, pressing the Exit button exits the window manager but not the application.

import sys

import pytermgui as ptg

with ptg.WindowManager() as manager:
    window = ptg.Window(
        "[wm-title]My first window!",
        "",
        ["Exit", lambda *_: sys.exit()],
    )

    manager.add(window)
    manager.run()

To Reproduce Steps to reproduce the behavior:

  1. Run the documentation's second examplt
  2. Click on 'Exit'
  3. Note that the application is still running
  4. SIGINT
  5. Try, and fail, to highly text as mouse events are still sent straight to stdin
  6. SIGINT
  7. Type 'reset' and hit enter.

Expected behavior Press 'Exit', see application cleanly exit with a clean terminal state.

Screenshots Screenshot

System information

pytermgui version shown

(venv) user@host:~$ pip show pytermgui
Name: pytermgui
Version: 1.0.1
Summary: A simple and robust terminal UI library, written in Python.
Home-page: https://github.com/bczsalba/pytermgui
Author: BcZsalba
Author-email: bczsalba@gmail.com
License: MIT
Location: /home/user/venv/lib/python3.9/site-packages
Requires: 
Required-by:

As requested

(venv) user@host:~$ ptg --version
usage: ptg [-h] [--app {Getch, MarkApp}] [-g] [-m] [-s] [-f FILE] [--print-only]
ptg: error: unrecognized arguments: --version

Additional note: ptg itself does the same thing on exit (re: leaving the terminal dirty)

Reproduces on my PureOS 10 (Debian based), Fedora 35, and Fedora Silverblue Rawhide systems using GNOME Terminal.

The hang behavior persists with XTerm, though the dirty state does not appear to.

Possible cause There appears to be a lock at play in the traceback. Maybe it isn't released?

Possible solution N/A

bczsalba commented 2 years ago

Thank you! After a bit of tinkering, I think the issue is that sys.exit(), when triggered by the mouse, doesn't stop the input being captured for some reason. If you call manager.stop() and then call sys.exit() it seems to work alright. Luckily there's already a method that does exactly that, so I'll update all the docs to reference manager.exit instead of sys.exit.

Could you elaborate on what happens in ptg? I can't seem to reproduce this behaviour.

P.S. The version info is not in the current PIP package, but if you install from GitHub it's present. Thank you for the pip show in its place!

anthonyjmartinez commented 2 years ago

@bczsalba - glad you were able to get to the bottom of it.

As for the ptg behavior, when I exit the application my mouse clicks are still captured by the terminal and I have to ctrl-c and then type reset to make it stop. This appears to not happen in XTerm but does happen in GNOME Terminal. Perhaps I'll install a dozen or so terminals (kitty, alacrity, qterminal, ...) on one of my machines and see what happens.

Generally it pains me to take screenshots of text, but perhaps I can use asciinema to capture it in some way that's less abhorrent and can be copy-pasted from after the fact. I'll get something for you in the coming days for the sake of completeness.

Looking forward to getting to know this library better.

One question: how does one engage the exit button without the mouse?

bczsalba commented 2 years ago

Hm, that's very interesting! I've tested it on Kitty (my main terminal) and Alacritty some months ago, and I never had this specific issue.

What is supposed to happen (in a clean exit) is that all the mouse capturing is unset, the output thread is stopped and the program should not leave any traces behind. Out of curiosity, how do you exit the program? I'm pretty sure something more "forced" than CTRL_C would cause that exact behaviour, as the program is forced to exit without having time to clean up. You're probably not using CTRL_\, but it's worth a guess.

Once you do have some more information on this problem, could you please open a new issue? I think the purposes of this one have been exhausted.

How does one engage the exit button without the mouse?

This might be a little understated in all of the examples, but there is a pretty major system for keyboard selection. You should be able to scroll down using the arrow keys, CTRL_N (up) & CTRL_P (down), or j and k (in non-InputFields).

Thank you for your interest, and for helping me improve!

anthonyjmartinez commented 2 years ago

18 created as requested as the ptg behavior is still present in GNOME Terminal.

Indeed I'm just using a good ol' CTRL-C - no bigger hammers in use here.