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.25k stars 56 forks source link

Windows compatibility and example apps #129

Open ouroborus opened 1 year ago

ouroborus commented 1 year ago

I'm trying PyTermGUI for the first time by using the example at examples\simple_app.py on Windows.

bczsalba commented 1 year ago

Could you give some more details on your configuration? Mouse support should work on the latest versions, so it's either an outdated terminal or outdated PTG install.

Thanks for trying it out!

ouroborus commented 1 year ago

I should mention that this is Windows 10 using its default PowerShell console. PTG version whatever was pulled in via pip install three weeks ago.

FrosT2k5 commented 8 months ago

This issue isn't limited to example code, it's the issue with library itself. I've made another code and it works completely fine as expected in Linux, but the input boxes and buttons are unclickable in windows. I've installed cygwin for windows and it works in that, I think it should work in WSL as well. But I think native windows consoles (CMD Prompt, Powershell) support needs to be fixed.

I've not changed anything with configuration of powershell/cmd prompt, they're stock ones that comes with windows by default

Tejus69 commented 8 months ago

This issue isn't limited to example code, it's the issue with library itself. I've made another code and it works completely fine as expected in Linux, but the input boxes and buttons are unclickable in windows. I've installed cygwin for windows and it works in that, I think it should work in WSL as well. But I think native windows consoles (CMD Prompt, Powershell) support needs to be fixed.

I've not changed anything with configuration of powershell/cmd prompt, they're stock ones that comes with windows by default

yes i Yes, I also had the same problem.

tritium21 commented 7 months ago

A breaking change happened between 7.4 and 7.5. Interestingly, there is an entirely different mouse error when the terminal is conhost.exe (windows console host - the legacy NT console) than with wt.exe (window terminal app).

import pytermgui as ptg

ptg.inline(ptg.Button("Quit", lambda *_: print("Pressed")))

windows terminal:

PS C:\devel\tui-play> .\env\Scripts\python .\tui.py
  Quit
Traceback (most recent call last):
  File "C:\devel\tui-play\env\Lib\site-packages\pytermgui\context_managers.py", line 142, in mouse_handler
    report_mouse(event, method=method)
  File "C:\devel\tui-play\env\Lib\site-packages\pytermgui\ansi_interface.py", line 522, in report_mouse
    raise NotImplementedError(f"Mouse report event {event!r} is not supported!")
NotImplementedError: Mouse report event 'hover' is not supported!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\devel\tui-play\tui.py", line 3, in <module>
    ptg.inline(ptg.Button("Quit", lambda *_: print("Pressed")))
  File "C:\devel\tui-play\env\Lib\site-packages\pytermgui\widgets\inline.py", line 90, in inline
    with mouse_handler(["press_hold", "hover"], "decimal_xterm") as translate:
  File "C:\Program Files\Python312\Lib\contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "C:\devel\tui-play\env\Lib\site-packages\pytermgui\context_managers.py", line 148, in mouse_handler
    report_mouse(event, method=method, stop=True)
  File "C:\devel\tui-play\env\Lib\site-packages\pytermgui\ansi_interface.py", line 522, in report_mouse
    raise NotImplementedError(f"Mouse report event {event!r} is not supported!")
NotImplementedError: Mouse report event 'hover' is not supported!
PS C:\devel\tui-play>

conhost

C:\devel\tui-play>env\Scripts\python tui.py
←[?25l←[6nTraceback (most recent call last):
  File "C:\devel\tui-play\tui.py", line 3, in <module>
    ptg.inline(ptg.Button("Quit", lambda *_: print("Pressed")))
  File "C:\devel\tui-play\env\Lib\site-packages\pytermgui\widgets\inline.py", line 61, in inline
    cursor = report_cursor()
             ^^^^^^^^^^^^^^^
  File "C:\devel\tui-play\env\Lib\site-packages\pytermgui\ansi_interface.py", line 170, in report_cursor
    posy, posx = chars[2:-1].split(";")
    ^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)

C:\devel\tui-play>

This is run with the same code, in the same venv - python 3.12.0, PyTermGUI==7.7.0

bczsalba commented 7 months ago

Unfortunately I do not have a Windows machine to trial things on so I can't really know what's going on. @Tired-Fox implemented mouse support for Windows some time ago, and I just now fixed a bug with inline (mentioned in @tritium21's comment). I'm not super aware of how this implementation is in terms of compatibility, but I'd say the issue lies there.

@tritium21's example shows that Windows Shell seems to properly support ANSI mouse reporting, while conhost does not, even with our driver. Unfortunately that is as far as I can diagnose in my current state :)

tritium21 commented 7 months ago

@bczsalba FWIW anyone who wants a TUI on windows probably is running windows shell and not conhost anyways.

bczsalba commented 7 months ago

FWIW anyone who wants a TUI on windows probably is running windows shell and not conhost anyways.

That's been my assumption too, but you never know :)


To answer the original issue, @ouroborus:

  1. Mouse doesn't seem to work at all. (Not sure if this is just because that app doesn't support it.)

Should be up to the terminal emulator at this point, and Windows Terminal should work.

  1. Navigation seems limited to h (left), j (down), k (up), j (right), and Enter (trigger). (Figured this out via trial and error.)

Arrow keys should also work. In case they don't, could you show me the output of pressing the arrow keys while running ptg -g?

  1. When navigating, and the first panel ("Some sliders") is closed, the cursor can still navigate into that panel but the panel doesn't open so you can get "lost".

Going to try to fix this now :) (✔️)

  1. Navigation does not seem to include the "Quit" button at the bottom. (Is there some other key to get there? It's not Tab.)

Since the quit button is in a separate window (the footer), focus doesn't transfer to it with the normal methods. You can bind some form of focus manipulation through a function:

manager.bind(<some key>, lambda *_: manager.focus_next())
manager.bind(<some other key>, lambda *_: manager.focus_next(-1))

...but a native solution isn't impossible either. I'm not a huge fan of adding something like this though, as focus should be tied to a window, not something to freely flow between different ones. I agree from a UX perspective that button should be focusable some other way though!

FrosT2k5 commented 7 months ago

Should be up to the terminal emulator at this point, and Windows Terminal should work.

It doesn't work, I've tried with windows terminal(if this means command prompt) & powershell as well. Mouse doesn't work at all.

Arrow keys should also work. In case they don't, could you show me the output of pressing the arrow keys while running ptg -g?

The output of ptg -g is different from what I get by running it in linux. In windows this didn't even ask me for input and printed some random text. Here's the paste of what came out - https://katb.in/helukukerad

Let me know what can I do to further help you debugging this.

tritium21 commented 7 months ago

It doesn't work, I've tried with windows terminal(if this means command prompt) & powershell as well. Mouse doesn't work at all.

Windows Terminal is the console application that has a tab bar across the top.

the other option is conhost.exe - this is the legacy console

If you are running powershell or cmd.exe inside of these doesn't matter to the terminal - its the terminal, not the shell, that makes rendering decisions.

Windows Terminal (the first image, available from the microsoft store or through winget) is what pytermgui primarily targets on windows.

FrosT2k5 commented 7 months ago

Windows Terminal is the console application that has a tab bar across the top.

I see, that solves it. I installed Windows Terminal and ran my code on it. Mouse works fine but the arrow keys doesn't work, hjkl works but I won't consider them proper replacement since if an inputbox is active, the hjkl keys type letters instead of selecting other elements. Also there is lot of flickering around, which isn't there in Linux, but I don't necessary think this is the library's issue. Maybe it's my potato machine.

For arrow keys, ptg -g doesn't work the same way it does in linux, like mentioned in #133, and backspace in input field doesn't work as well. Update: In input field arrow keys doesn't work too, so I can't move the cursor in the input field

bczsalba commented 7 months ago

For arrow keys, ptg -g doesn't work the same way it does in linux, like mentioned in https://github.com/bczsalba/pytermgui/issues/133, and backspace in input field doesn't work as well.

Could you try the changes I suggested?

FrosT2k5 commented 7 months ago

Could you try the changes I suggested?

ptg -g fixes after that change, output of all arrow keys on ptg -g -> https://katb.in/isudunevaru

update: That seems to have somewhat fixed arrow keys and has completely fixed backspace. But when in input field, the up and down keys dont select next element, but instead inputs H and P resp., left and right arrow keys work fine in input boxes

Also the change seems to break mouse, the buttons and input boxes are unclickable, they start working again if I revert the change