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.21k stars 54 forks source link

[BUG] io.UnsupportedOperation: truncate #47

Closed SkiHatDuckie closed 2 years ago

SkiHatDuckie commented 2 years ago

Describe the bug I was trying out one of the examples in the documentation, but it kept throwing an exception complaining about there being no method called truncate. I started to follow the traceback and found out that it was calling sys.stdout.truncate(0)... which doesn't exist (at least on windows it doesn't).

To Reproduce Steps to reproduce the behavior:

  1. Paste and run the following into an IDE
    
    import pytermgui as ptg

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

    manager.add(window)
    manager.run()
2. See error (pasted the one I'm getting below if you don't get one)

Exception in thread WM_DisplayLoop: Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64qbz5n2kfra8p0\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64qbz5n2kfra8p0\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\window_manager.py", line 447, in _loop animator.step() File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\animations.py", line 120, in step if animation.step(): File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\animations.py", line 69, in step step_callback(self.target) File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\window_manager.py", line 998, in _on_step ret_val = step_callback(_target) File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\window_manager.py", line 646, in _on_step self.nullify_cache(window) File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\window_manager.py", line 478, in nullify_cache self.redraw() File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\window_manager.py", line 977, in redraw terminal.write("\x1b[2J") File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\animations.py", line 69, in step step_callback(self.target) File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\window_manager.py", line 998, in _on_step ret_val = step_callback(_target) File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\window_manager.py", line 646, in _on_step self.nullify_cache(window) File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\window_manager.py", line 478, in nullify_cache self.redraw() File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\window_manager.py", line 977, in redraw terminal.write("\x1b[2J") File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\terminal.py", line 390, in write self.clear_stream() File "c:\Users\Conno\pyvenv\lib\site-packages\pytermgui\terminal.py", line 412, in clear_stream self._stream.truncate(0) io.UnsupportedOperation: truncate


**Expected behavior**
I was hoping for some kind of window to show up in the terminal. I don't think an exception counts as a window. :[

**Screenshots**
I don't think any screenshots are necessary.

**System information**

fatal: not a git repository (or any of the parent directories): .git PyTermGUI v4.3.1

Python: 3.8.10
Platform: Windows-10-10.0.19043-SP0



**Possible cause**
The program is calling the `truncate` method in `sys.stdout`, which does not exist.

**Possible solution**
Looking at the traceback, it seems like this occurs whenever the program tries to call `self.clear_stream()` in the Terminal class. I could try creating another way of clearing the screen in `sys.stdout`.
bczsalba commented 2 years ago

Can you provide the output of os.platform? The recent release should have fixed this, but I guess the detection isn't perfect :(.

SkiHatDuckie commented 2 years ago

Output: Windows-10-10.0.19043-SP0

SkiHatDuckie commented 2 years ago

I'll try and mess around with the Terminal class to see if I can do anything. It definitely is catching the error, but it's not doing anything except for raising an exception.

bczsalba commented 2 years ago

Sorry, just realized I mean os.name instead of platform. Been a long day.

I suspect it isn't getting nt, which is what it expects. It was implemented by a collaborator on Windows, and it worked for both them and me when I tried to run it locally, so not sure why your machine is different.

But hey, that's just the beauty of Windows compatibility! You never know what you get.

SkiHatDuckie commented 2 years ago

Just updated pytermgui to 4.3.2, and window now successfully renders! I'll report any other issues I find as I use the library. pyterm-first-window

bczsalba commented 2 years ago

Happy to hear that!